c语言一个成绩管理系统(有问题求修改)
#include "stdio.h"
struct Date /* 定义日期结构体类型*/
{
int year;
int month;
int day;
};
struct Student /*定义学生信息的结构体类型*/
{
int id;
char name[10];
char sex;
struct Date birthday;
char grade[10];
char class[5];
float scoretotal;
};
char stuinfocol[][12]={"ID","Name","Sex","Birthday","Grade","Class","ScoreTotal"}; /*定义学生信息的列标题*/
int stuinfoi=1;
void title(); /*完成若干个自定义函数的声明*/
int dupchkstuinfo(FILE *fp,int id);
int stcmp(char *str1,char *str2);
int found(char *str);
void inputstu();
void editstu();
void delstu();
void showstu();
void query();
void help();
void title() /*定义显示学生管理系统初始界面的函数*/
{
printf("\t\t\tStudent Management System\n\n");
printf(" Press 1 Press 2 Press 3 Press 4 press 5 Press h Press #\n");
printf(" InputStu EditStu DelStu ShowStu Query Help Exit\n\n\n");
}
int dupchkstuinfo(FILE *fp,int id) /*定义查重函数,当插入新记录前,检查与文件已有记录是否重复,以id号为关键字*/
{
FILE *fp2=fp;
struct Student data,*p=&data;
rewind(fp); /*将文件指针置于文件的头部*/
while(fread(p,sizeof(struct Student),1,fp),!feof(fp))
if((*p).id==id) break;
fp=fp2;
if((*p).id==id) return 1;
else return 0;
}
int stcmp(char *str1,char *str2) /*定义两个字符串匹配函数,比较两个字符串是否相同*/
{
while(*str1!='\0'&&*str2!='\0')
{
if(*str1!=*str2) break;
str1++;
str2++;
}
if(*str1=='\0'&&*str2=='\0') return 1;
else return 0;
}
int found(char *str) /*定义查找函数,当修改记录时,输入要修改的列的标题,在列标题数组中查找*/
{
int i;
for(i=0;i<7;i++)
if(stcmp(stuinfocol[i],str)) break;
if(stcmp(stuinfocol[i],str)) return i;
else return 7;
}
void inputstu() /*定义录入学生信息记录的函数*/
{
FILE *fp;
struct Student stu1;
char command[5]="add";
fp=fopen("stuinfo.dat","ab+"); /*以追加的读写二进制文件的方式打开二进制文件stuinfo.dat*/
while(!stcmp(command,"exit")) /*录入字符串exit的时候,退出插入记录的状态*/
{
printf("Input num %d ID:",stuinfoi);
scanf("%d",&stu1.id);
if(dupchkstuinfo(fp,stu1.id)) { printf("Your input ID is duplicate,input it again!!!\n"); continue; }
printf("Input num %d NAME:",stuinfoi);
scanf("%s",&stu1.name);
printf("Input num %d SEX:",stuinfoi);
scanf("\n%c",&stu1.sex);
printf("Input num %d BIRTH YEAR:",stuinfoi);
scanf("%d",&stu1.birthday.year);
printf("Input num %d BIRTH MONTH:",stuinfoi);
scanf("%d",&stu1.birthday.month);
printf("Input num %d BIRTH DAY:",stuinfoi);
scanf("%d",&stu1.birthday.day);
printf("Input num %d GRADE:",stuinfoi);
scanf("%s",stu1.grade);
printf("Input num %d CLASS:",stuinfoi);
scanf("%s",stu1.class);
printf("Input num %d SCORETOTAL:",stuinfoi);
scanf("%f",&stu1.scoretotal);
fwrite(&stu1,sizeof(struct Student),1,fp);
stuinfoi++;
getchar();
gets(command);
}
fclose(fp);
}
void editstu() /*定义修改学生信息记录的函数*/
{
FILE *fp;
struct Student stu1;
int id,i=0;
char command[12]="add";
printf("Please input the student id you want to edit:");
scanf("%d",&id); /*获取要修改的学生信息记录的学生id*/
fp=fopen("stuinfo.dat","rb+"); /*以正常的读写二进制文件的方式打开二进制文件stuinfo.dat*/
while(fread(&stu1,sizeof(struct Student),1,fp),!feof(fp)&&stu1.id!=id) i++;
if(stu1.id!=id) printf("The record is not exist!!\n"); /*判断录入的id在学生信息文件中是否存在,不存在,显示提示信息!!*/
else
{
printf("\n%-8s%-10s%-10s%-16s%-12s%-10s%-s\n",stuinfocol[0],stuinfocol[1],stuinfocol[2],stuinfocol[3],stuinfocol[4],stuinfocol[5],stuinfocol[6]);
printf("%-8d",stu1.id);
printf("%-11s",stu1.name);
printf("%-9c",stu1.sex);
printf("%-4d-",stu1.birthday.year);
if(stu1.birthday.month<10) printf("0%-d-",stu1.birthday.month);
else printf("%-d-",stu1.birthday.month);
if(stu1.birthday.day<10) printf("0%-7d",stu1.birthday.day);
else printf("%-8d",stu1.birthday.day);
printf("%-12s",stu1.grade);
printf("%-12s",stu1.class);
printf("%-.1f\n",stu1.scoretotal); /*录入的id在学生信息文件中存在,则显示对应的记录信息*/
getchar();
gets(command);
while(!stcmp(command,"exit")) /*录入字符串exit的时候,退出修改记录的状态*/
{
switch(found(command)) /*录入其他字符串时候,判断要修改本记录的哪个列标题的信息*/
{
case 0: printf("Please input new id:"); scanf("%d",&stu1.id); break;
case 1: printf("Please input new name:"); scanf("%s",stu1.name); break;
case 2: printf("Please input new sex:"); scanf("%c",&stu1.sex); break;
case 3: printf("Please input new birthday:"); scanf("%d-%d-%d",&stu1.birthday.year,&stu1.birthday.month,&stu1.birthday.day); break;
case 4: printf("Please input new grade:"); scanf("%d",&stu1.grade); break;
case 5: printf("Please input new class:"); scanf("%d",&stu1.class); break;
case 6: printf("Please input new scoretotal:"); scanf("%d",&stu1.scoretotal); break;
default: printf("Your input is wrong!!\n");
}
getchar();
gets(command);
}
fseek(fp,i*sizeof(struct Student),SEEK_SET);
fwrite(&stu1,sizeof(struct Student),1,fp);
}
fclose(fp);
}
void delstu() /*定义删除学生信息记录的函数*/
{
FILE *fp,*fp2;
struct Student stu1;
char *new="stuinfo.dat",*old="temp.dat";
int id;
printf("Please input the student id you want to delete:");
scanf("%d",&id); /*获取要删除的学生信息记录的学生id*/
fp=fopen("stuinfo.dat","ab+"); /*以追加的读写二进制文件的方式打开二进制文件stuinfo.dat*/
while(fread(&stu1,sizeof(struct Student),1,fp),!feof(fp)&&stu1.id!=id) ;
if(stu1.id!=id) printf("The record is not exist!!\n"); /*判断录入的id在学生信息文件中是否存在,不存在,显示提示信息!!*/
else
{
rewind(fp);
fp2=fopen("temp.dat","ab+"); /*以追加的读写二进制文件的方式新建打开二进制文件temp.dat*/
while(fread(&stu1,sizeof(struct Student),1,fp),!feof(fp))
if(stu1.id!=id) fwrite(&stu1,sizeof(struct Student),1,fp2); /*将stuinfo.dat中除录入id对应记录外的其他记录输入至temp.dat*/
fclose(fp);
fclose(fp2);
remove(new); /*删除二进制文件stuinfo.dat*/
if(!rename(old,new)) printf("ok!!\n"); /*重命名temp.dat为stuinfo.dat*/
else printf("error!!\n");
}
}
void showstu() /*定义显示学生信息记录的函数*/
{
FILE *fp;
struct Student stu1;
printf("\n%-8s%-10s%-10s%-16s%-12s%-10s%-s\n",stuinfocol[0],stuinfocol[1],stuinfocol[2],stuinfocol[3],stuinfocol[4],stuinfocol[5],stuinfocol[6]);
fp=fopen("stuinfo.dat","ab+"); /*以追加的读写二进制文件的方式打开二进制文件stuinfo.dat*/
while(fread(&stu1,sizeof(struct Student),1,fp),!feof(fp)) /*通过循环,不断读取stuinfo.dat的记录至stu1结构体变量,再输出stu1*/
{
printf("%-8d",stu1.id);
printf("%-11s",stu1.name);
printf("%-9c",stu1.sex);
printf("%-4d-",stu1.birthday.year);
if(stu1.birthday.month<10) printf("0%-d-",stu1.birthday.month);
else printf("%-d-",stu1.birthday.month);
if(stu1.birthday.day<10) printf("0%-7d",stu1.birthday.day);
else printf("%-8d",stu1.birthday.day);
printf("%-12s",stu1.grade);
printf("%-12s",stu1.class);
printf("%-.1f\n",stu1.scoretotal);
}
fclose(fp);
}
void query() /*定义查询学生信息记录的函数,此函数未完成*/
{
printf("\n");
}
void help() /*定义显示软件帮助信息的函数*/
{
printf("\n\nOperate help\n\n");
printf("Press 1,input student informatin!!\n");
printf("Press 2,edit student informatin!!\n");
printf("Press 3,delete student informatin!!\n");
printf("Press 4,show student informatin!!\n");
printf("Press 5,query student informatin!!\n");
printf("Press #,Exit from System!!\n");
printf("Press h,Get Help!!\n");
printf("Press exit,Exit from input or edit!!!\n");
getchar();
}
main() /*定义主函数*/
{
char ch;
clrscr();
title();
while((ch=getchar())!='#') /*通过循环录入不同字符,调用不同函数,完成不同的功能*/
{
switch(ch)
{
case '1': inputstu(); break;
case '2': editstu(); break;
case '3': delstu(); break; /* 删除 */
case '4': showstu(); break;
case '5': query(); break; /* 查询函数 该部分需要手动完成 */
case 'h': help(); break;
default: printf("Input error!!!\n"); /*输入为非法字符,显示提示信息*/
}
scanf("\n"); /*屏蔽回车符的录入*/
}
}