赞
踩
该结构定义了学生结构体,包括学号、姓名、年龄、性别、专业、班级、电话号码和导员姓名等信息,以及定义了存储学生信息的数组。
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 1000 //宏定义最大学员数
typedef struct //定义学生信息结构体
{
char id[20]; //学号
char name[20]; //姓名
int age; //年龄
int gender; //性别
char major[20]; //专业
char clazz[20]; //班级
char phone[20]; //电话
char advisor[20]; //导员姓名
} Student;
int find_student(Student *students, int count, char *id); // 查找学员信息函数
void add_student(Student *students, int *count); // 添加学员信息函数
void show_students(Student *students, int count); // 查看学员信息函数
void update_student(Student *students, int count, int id); // 更新学员信息函数
void delete_student(Student *students, int *count, int id); // 删除学员信息函数
void save_students(Student *students, int count); // 保存学员信息函数
int load_students(Student *students); // 读取学员信息函数
根据用户的选择,调用相应的模块实现相应的功能。
int main()
{
Student students[MAX_STUDENT];
int count = load_students(students);
while (1)
{ //打印选择菜单,提供用户选择。
printf("\n\n");
printf("++++++++欢迎来到学员管理系统+++++++++\n");
printf("------------------------------\n"); //修饰菜单,使其更美观
printf("|0. 退 出|\n");
printf("|1. 添 加 学 生|\n");
printf("|2. 查 看 学 生|\n");
printf("|3. 修 改 学 生|\n");
printf("|4. 删 除 学 生|\n");
printf("|5. 保 存 学 生|\n");
printf("------------------------------\n");
printf("请选择操作:\n");
int choice; //定义选择的值
scanf("%d", &choice);
switch (choice) //通过switch函数来选择
{
case 0:
printf("谢谢使用!\n");
return 0;
case 1:
add_student(students, &count);//调用添加学员函数
break;
case 2:
show_students(students, count);//调用查看学员的函数
break;
case 3:
printf("请输入要修改的学生学号:");
char id[20];
scanf("%s", id);
update_student(students, count, id);//调用修改学员的函数
break;
case 4:
printf("请输入要删除的学生学号:");
char del_id[20];
scanf("%s", del_id);
delete_student(students, &count, del_id);//调用删除函数
break;
case 5:
save_students(students, count);//调用保存函数
break;
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
通过用户输入学生信息,创建一个新的学生结构体,将其添加到学生数组中。
void add_student(Student *students, int *count)
{
if (*count == MAX_STUDENT)//判断学生的数量,如果超出则提示满员
{
printf("学生信息已满,无法添加!\n");
return;
}
char id[20], name[20], major[20], clazz[20], phone[20], advisor[20];
int age, gender;
printf("请输入学号:");
scanf("%s", id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入性别(1表示男,0表示女):");
scanf("%d", &gender);
printf("请输入专业:");
scanf("%s", major);
printf("请输入班级:");
scanf("%s", clazz);
printf("请输入电话号码:");
scanf("%s", phone);
printf("请输入导员姓名:");
scanf("%s", advisor);
strcpy(students[*count].id, id);
strcpy(students[*count].name, name);
students[*count].age = age;
students[*count].gender = gender;
strcpy(students[*count].major, major);
strcpy(students[*count].clazz, clazz);
strcpy(students[*count].phone, phone);
strcpy(students[*count].advisor, advisor);
(*count)++;//记录添加学员的数量,每次执行加1
printf("添加成功!\n");
}
遍历学生数组,按照一定的格式输出每个学生的信息。
void show_students(Student *students, int count)
{
printf("学号\t\t姓名\t年龄\t性别\t专业\t\t班级\t电话\t\t导员\n"); //列出表格
for (int i = 0; i < count; i++)
{
printf("%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n", students[i].id, students[i].name, students[i].age, students[i].gender == 1 ? "男" : "女", students[i].major, students[i].clazz, students[i].phone, students[i].advisor); //打印各个信息
}
}
int find_student(Student *students, int count, char *id)
{
int left = 0, right = count - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (strcmp(students[mid].id, id) == 0)
{
return mid;
}
else if (strcmp(students[mid].id, id) < 0)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1; //如果没有查找到信息返回值-1
}
通过用户输入学生学号和要修改的信息,找到对应的学生结构体,修改相应的信息。
void update_student(Student *students, int count, char *id)
{
int index = find_student(students, count, id);
if (index == -1) //判断输入是否合法问题
{
printf("学生不存在!\n");
return;
}
char name[20], major[20], clazz[20], phone[20], advisor[20];
int age, gender;
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入性别(1表示男,0表示女):");
scanf("%d", &gender);
printf("请输入专业:");
scanf("%s", major);
printf("请输入班级:");
scanf("%s", clazz);
printf("请输入电话号码:");
scanf("%s", phone);
printf("请输入导员姓名:");
scanf("%s", advisor);
strcpy(students[index].name, name);
students[index].age = age;
students[index].gender = gender;
strcpy(students[index].major, major);
strcpy(students[index].clazz, clazz);
strcpy(students[index].phone, phone);
strcpy(students[index].advisor, advisor);
printf("修改成功!\n");
}
通过用户输入学生学号,找到对应的学生结构体,将其从学生数组中删除。
void delete_student(Student *students, int *count, char *id)
{
int index = find_student(students, *count, id);
if (index == -1)
{
printf("学生不存在!\n");
return;
}
for (int i = index; i < *count - 1; i++)
{
students[i] = students[i + 1];
}
(*count)--;
printf("删除成功!\n");
}
实现了将学生信息保存到文件和从文件中读取学生信息的功能。
void save_students(Student *students, int count)
{
FILE *fp = fopen("students.dat", "wb"); //文件操作函数
if (fp == NULL)
{
printf("打开文件失败!\n");
return;
}
fwrite(students, sizeof(Student), count, fp);
fclose(fp);
printf("保存成功!\n");
}
读取文件信息
int load_students(Student *students)
{
FILE *fp = fopen("students.txt", "r");
if (fp == NULL)
{
printf("打开文件失败!\n");
return 0;
}
int count = 0;
while (fread(&students[count], sizeof(Student), 1, fp) == 1)
{
count++;
}
fclose(fp);
printf("加载成功!\n");
return count;
}
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 100
typedef struct
{
char id[20];
char name[20];
int age;
int gender;
char major[20];
char clazz[20];
char phone[20];
char advisor[20];
} Student;
int find_student(Student *students, int count, char *id);
void add_student(Student *students, int *count);
void show_students(Student *students, int count);
void update_student(Student *students, int count, char *id);
void delete_student(Student *students, int *count, char *id);
void save_students(Student *students, int count);
int load_students(Student *students);
int find_student(Student *students, int count, char *id)
{
int left = 0, right = count - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (strcmp(students[mid].id, id) == 0)
{
return mid;
}
else if (strcmp(students[mid].id, id) < 0)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
return -1;
}
void add_student(Student *students, int *count)
{
if (*count == MAX_STUDENT)
{
printf("学生信息已满,无法添加!\n");
return;
}
char id[20], name[20], major[20], clazz[20], phone[20], advisor[20];
int age, gender;
printf("请输入学号:");
scanf("%s", id);
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入性别(1表示男,0表示女):");
scanf("%d", &gender);
printf("请输入专业:");
scanf("%s", major);
printf("请输入班级:");
scanf("%s", clazz);
printf("请输入电话号码:");
scanf("%s", phone);
printf("请输入导员姓名:");
scanf("%s", advisor);
strcpy(students[*count].id, id);
strcpy(students[*count].name, name);
students[*count].age = age;
students[*count].gender = gender;
strcpy(students[*count].major, major);
strcpy(students[*count].clazz, clazz);
strcpy(students[*count].phone, phone);
strcpy(students[*count].advisor, advisor);
(*count)++;
printf("添加成功!\n");
}
void show_students(Student *students, int count)
{
printf("学号\t姓名\t年龄\t性别\t专业\t班级\t电话\t导员\n");
for (int i = 0; i < count; i++)
{
printf("%s\t%s\t%d\t%s\t%s\t%s\t%s\t%s\n", students[i].id, students[i].name, students[i].age, students[i].gender == 1 ? "男" : "女", students[i].major, students[i].clazz, students[i].phone, students[i].advisor);
}
}
void update_student(Student *students, int count, char *id)
{
int index = find_student(students, count, id);
if (index == -1)
{
printf("学生不存在!\n");
return;
}
char name[20], major[20], clazz[20], phone[20], advisor[20];
int age, gender;
printf("请输入姓名:");
scanf("%s", name);
printf("请输入年龄:");
scanf("%d", &age);
printf("请输入性别(1表示男,0表示女):");
scanf("%d", &gender);
printf("请输入专业:");
scanf("%s", major);
printf("请输入班级:");
scanf("%s", clazz);
printf("请输入电话号码:");
scanf("%s", phone);
printf("请输入导员姓名:");
scanf("%s", advisor);
strcpy(students[index].name, name);
students[index].age = age;
students[index].gender = gender;
strcpy(students[index].major, major);
strcpy(students[index].clazz, clazz);
strcpy(students[index].phone, phone);
strcpy(students[index].advisor, advisor);
printf("修改成功!\n");
}
void delete_student(Student *students, int *count, char *id)
{
int index = find_student(students, *count, id);
if (index == -1)
{
printf("学生不存在!\n");
return;
}
for (int i = index; i < *count - 1; i++)
{
students[i] = students[i + 1];
}
(*count)--;
printf("删除成功!\n");
}
void save_students(Student *students, int count)
{
FILE *fp = fopen("students.dat", "wb");
if (fp == NULL)
{
printf("打开文件失败!\n");
return;
}
fwrite(students, sizeof(Student), count, fp);
fclose(fp);
printf("保存成功!\n");
}
int load_students(Student *students)
{
FILE *fp = fopen("students.dat", "rb");
if (fp == NULL)
{
printf("打开文件失败!\n");
return 0;
}
int count = 0;
while (fread(&students[count], sizeof(Student), 1, fp) == 1)
{
count++;
}
fclose(fp);
printf("加载成功!\n");
return count;
}
int main()
{
Student students[MAX_STUDENT];
int count = load_students(students);
while (1)
{
printf("请选择操作:\n");
printf("0. 退出\n");
printf("1. 添加学生\n");
printf("2. 查看学生\n");
printf("3. 修改学生\n");
printf("4. 删除学生\n");
printf("5. 保存学生\n");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 0:
printf("谢谢使用!\n");
return 0;
case 1:
add_student(students, &count);
break;
case 2:
show_students(students, count);
break;
case 3:
printf("请输入要修改的学生学号:");
char id[20];
scanf("%s", id);
update_student(students, count, id);
break;
case 4:
printf("请输入要删除的学生学号:");
char del_id[20];
scanf("%s", del_id);
delete_student(students, &count, del_id);
break;
case 5:
save_students(students, count);
break;
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
一个完整的C语言课程设计就写完了,有需要的自取,程序不是很严谨,还需要进一步完善!!!
最后给个支持吧!感谢大家。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。