学生管理系统
程序代码:#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
int age;
int grade;
char name[20];
char major[20];
char sex[8];
};
//void init_(FILE *fp);//初始化
void init_table1(void);
void init_table2(void);
void check_(FILE *fp,int n);//查询
void check(FILE *fp);//查询
void add_(FILE *fp);//增加成员
void delete_(FILE *fp);//注销、删除成员 用学号来注销
void modif_(FILE *fp);//修改成员信息
void content(FILE *fp);//查看目录
int main()
{
FILE *fp;
int n;
errno_t err;
err=fopen_s(&fp,"data","rb+");
if(err!=0)
{
printf("error\n");
exit(1);
}
else
printf(" 欢迎来到学生管理系统\n\n made by 卡桑\n");
// init_(fp);
init_table1();
while(1)
{
printf("请选择选项:");
if(scanf("%d",&n)!=1)
printf("非法输入,请按要求输入\n\n");
else
switch (n)
{
case 0: fclose(fp); system("pause");return 0;//退出
case 1: add_(fp);break;//录入
case 2: content(fp);break;//浏览
case 3: check(fp);break;//查询
case 4: delete_(fp);break;//删除
case 5: modif_(fp);break;//修改
default : printf("非法输入,请按要求输入\n\n");break;
}
init_table1();
}
fclose(fp);
return 0;
}
/*
void init_(FILE *fp)//初始化
{
errno_t err;
err=fopen_s(&fp,"data","ab+");
if(err==0)
{
printf("error\n");
exit(1);
}
else
printf("欢迎来到学生管理系统\n\n made by 卡桑\n\n\n");
}
*/
void init_table1(void)
{
printf("\n--------------------------------\n");
printf(" 主菜单 ");
printf("\n--------------------------------\n");
printf(" [1]----录入学生信息 \n");
printf(" [2]----浏览学生信息 \n");
printf(" [3]----查询学生信息 \n");
printf(" [4]----删除学生信息 \n");
printf(" [5]----修改学生信息 \n");
printf(" [0]----退出系统 \n");
printf("--------------------------------\n\n");
}
void init_table2(void)
{
printf("\n--------------------------------\n");
printf(" 1----修改学生姓名 \n");
printf(" 2----修改学生学号 \n");
printf(" 3----修改学生性别 \n");
printf(" 4----修改学生年龄 \n");
printf(" 5----修改学生年级 \n");
printf(" 6----修改学生专业 \n");
printf(" 0----退 出 \n");
printf("--------------------------------\n\n");
}
void check(FILE *fp)
{
int temp;
printf("\n\n");
printf("请输入要查询的学号:");
scanf("%d",&temp);
printf("\n");
check_(fp,temp);
printf("\n\n");
}
void check_(FILE *fp,int n)//检查 用学号 c++可以写重载函数,用学号和姓名 有该人流返回该人的位置,否则回到最前面
{
struct student *temp;
temp=(struct student *)malloc(sizeof(student));
fseek(fp,0L,SEEK_SET);
while(fread(temp,sizeof(student),1,fp)==1);//读一个人,若还有的话,没有则跳出
{
if(temp->num==n)
{
printf("姓名:%s\n学号:%d\n性别:%s\n年龄:%d\n年级:%d\n专业:%s\n",temp->name,temp->num,temp->sex,temp->age,temp->grade,temp->major);
free(temp);
fseek(fp,-sizeof(student),SEEK_CUR);
return ;
}
}
printf("你所查找的人不在档案");
fseek(fp,0L,SEEK_SET);
free(temp);
// return 0;
}
void add_(FILE *fp)//增加成员
{
struct student *add_new;
add_new=(struct student *)malloc(sizeof(student));
printf("\n\n");
printf("请输入姓名:");
scanf("%s",add_new->name);
printf("请输入学号:");
scanf("%d",&(add_new->num));
printf("请输入性别:");
scanf("%s",add_new->sex);
printf("请输入年龄:");
scanf("%d",&(add_new->age));
printf("请输入年级:");
scanf("%d",&(add_new->grade));
printf("请输入专业:");
scanf("%s",add_new->major);
//以后改成有检错重新输入的
fseek(fp,0L,SEEK_END);
fwrite(add_new,sizeof(student),1,fp);
fseek(fp,0L,SEEK_SET);
free(add_new);
printf("\n");
}
void delete_(FILE *fp)//注销、删除成员 用学号来注销
{
//暂时没想到好方法,目前知道的就是把后面的都先存起来,往前面盖上去
printf("\n sorry,暂无此功能!\n\n");
}
void modif_(FILE *fp)//修改成员信息
{
struct student *temp;
int n,m,l=0;
temp=(struct student *)malloc(sizeof(student));
fread(temp,sizeof(student),1,fp);
fseek(fp,-sizeof(student),SEEK_CUR);
printf("\n修改对象基本信息:\n");
printf("姓名:%s\n学号:%d\n性别:%s\n年龄:%d\n年级:%d\n专业:%s\n",temp->name,temp->num,temp->sex,temp->age,temp->grade,temp->major);
init_table2();
while(1)
{
m=scanf("%d",&n);
if(m!=1)
break;
switch(n)
{
case 1:l=0;printf("请输入新的姓名:");scanf("%s",temp->name);break; //姓名
case 2:l=0;printf("请输入新的学号:");scanf("%d",&(temp->num));break; //学号
case 3:l=0;printf("请输入新的性别:");scanf("%s",temp->sex);break; //性别
case 4:l=0;printf("请输入新的年龄:");scanf("%d",&(temp->age));break; //年龄
case 5:l=0;printf("请输入新的年级:");scanf("%d",&(temp->grade));break; //年级
case 6:l=0;printf("请输入新的专业:");scanf("%s",temp->major);break; //专业
// case 0:l=0;break;
default:l=1;break;
}
if(l)
break;
init_table2();
}
printf("姓名:%s\n学号:%d\n性别:%s\n年龄:%d\n年级:%d\n专业:%s\n",temp->name,temp->num,temp->sex,temp->age,temp->grade,temp->major);
fwrite(temp,sizeof(student),1,fp);
fseek(fp,0L,SEEK_SET);
free(temp);
}
void content(FILE *fp)
{
struct student *temp;
int i=0;
temp=(struct student *)malloc(sizeof(student));
fseek(fp,0L,SEEK_SET);
printf("\n");
while(fread(temp,sizeof(student),1,fp)==1)//如果有数据的话
printf("%d 姓名:%s 学号:%d\n",++i,temp->name,temp->num);
if(i==0)
printf("学生档案为空!!!\n");
free(temp);
fseek(fp,0L,SEEK_SET);
printf("\n");
}
想请教一下,这个完成以后,下一个应该写什么东西







