学生成绩管理系统c语言代码

  • 格式:doc
  • 大小:46.00 KB
  • 文档页数:13

#include<stdio.h>#include<dos.h>#define N sizeof(struct STH)#define GESHI "%-10ld%-10s%-5s%-4d/%-2d/%-4d%-12s%-12s%s\n"#define PRINTF "学号性别生日系别班级家庭住址\n"struct STH* readfile();/*声明*/int insert(struct STH**);int n=0;struct dt{int year; /*全局变量*/int month;int day;};struct STH{long num; /*学号不能以0开头*/char name[9];char sex[5];struct dt birth;char xi[20];char ban[20];char add[30];struct STH* next;}*head;/*输入模块*/input_message(){char ch;int flag=1;do{if(insert(&head)!=0)n++; /*计数*/elseprintf("\n有相同的的学号!");printf("\n是否继续?(y/n)");getchar();scanf("%c",&ch);if(ch=='y'||ch=='Y'){printf("\n继续!");getch();flag=1;/*标志程序不结束*/}elseflag=0;}while(flag==1);save_message();printf("按任意键继续...");getch();}/*insert()模块*/ /*被输入和插入模块所调用*/int insert(struct STH**h){struct STH *p,*p0;p=(struct STH*)malloc(N);printf("\n请输入学号:");scanf("%ld",&p->num);printf("\n请输入:");scanf("%s",p->name);printf("\n请输入性别:");getchar(); /*取走上一个留下来得‘\n’*/scanf("%s",p->sex);printf("\n请输入生日(年月日,用回车隔开):\n");scanf("%d%d%d",&p->birth.year,&p->birth.month,&p->birth.day);printf("\n请输入系别:");scanf("%s",p->xi);printf("\n请输入班级:");scanf("%s",p->ban);printf("\n请输入家庭住址:");scanf("%s",p->add);p->next=NULL;if(*h==NULL){*h=p;return 1;}p0=*h;if(p0->num>p->num){p->next=p0;*h=p;return 1;}while(p0->next!=NULL&&p0->next->num<p->num)p0=p0->next;if(p0->next==NULL){p0->next=p;return 1;}else if(p0->next->num==p->num){free(p); /*有相同数据输入失败*/return 0;}else{p->next=p0->next;p0->next=p;return 1;}}/*读文件模块*/ /*读文件模块中创建了一个新的链表,其返回值为新链表的头指针*/struct STH *readfile(){FILE *fp;struct STH *p,*p0;char ch;int i;if((fp=fopen("STH.txt","r"))==NULL){printf("\n不能读取文件!\n");return NULL;}/*注意要该*/head=p=p0=(struct STH *)malloc(N);fscanf(fp,"%d",&n);fscanf(fp,"%c",&ch); /*为了使学号和信息个数分开加入一个‘/’*/fscanf(fp,"%ld%s%s%d%d%d%s%s%s",&p->num,p->name,p->sex,&p->birth.year,&p->birth. month,&p->birth.day,p->xi,p->ban,p->add);p->next=NULL;i=1; /*如果只有一个数时跳出*/if(n!=1){do{p=(struct STH *)malloc(N);fscanf(fp,"%ld%s%s%d%d%d%s%s%s",&p->num,p->name,p->sex,&p->birth.year, &p->birth.month,&p->birth.day,p->xi,p->ban,p->add);p0->next=p;p0=p;i++;}while(i<n);p0->next=NULL;}fclose(fp);return head;}/*插入模块*/insert_message(){struct STH *p;int flag=1;char ch;head=readfile();do{if(insert(&head)!=0){n++;save_message();printf("");} /*计数*/else{printf("\n添加失败!\n有相同的的学号!");save_message();getch();}printf("\n是否继续?(y/n)");getchar();ch=getchar();if(ch=='y'||ch=='Y'){printf("\n继续!");flag=1;/*标志程序不结束*/}elseflag=0;}while(flag==1);printf("\n按任意键继续...");getch();}/*修改模块*/renew_message(){struct STH *p;int i,flag=1;long num;p=head=readfile();if(head==NULL){printf("读文件有误!");getch();}else{printf("\n请按输入要修改的学生的学号:");scanf("%ld",&num);for(i=0;i<n;i++){if(p->num==num){p->num=num;printf("\n已找到要%ld号学生的信息!\n",num);printf("原为%s 要修改为:\n",p->name);scanf("%s",p->name);printf("原性别为%s 要修改为:\n",p->sex);scanf("%s",p->sex);printf("原生日为%d/%d/%d 要修改为:\n",p->birth.year,p->birth.month,p->birth.day);scanf("%d%d%d",&p->birth.year,&p->birt h.month,&p->birth.day);printf("原系别为%s 要修改为:\n",p->xi);scanf("%s",p->xi);printf("原班级为%s 要修改为:\n",p->ban);scanf("%s",p->ban);printf("原家庭住址为%s 要修改为:\n",p->add);scanf("%s",p->add);printf("你所更改后的信息为:\n");printf(PRINTF);printf(GESHI,p->num,p->name,p->sex, p->birth.year,p->birth.month,p->birth.day,p->xi,p->ban,p->add);printf("\n按任意键退出!\n");getch();flag=1;break;}elsep=p->next;}if(flag!=1)printf("无此人信息!\n");elsesave_message();getch();}}/*删除模块*/delete_message(){int flag;long num;char ch;struct STH*p,*p0;p0=p=head=readfile();if(head==NULL){printf("文件读取出错!");getch();}else{printf("请输入要删除学生的学号:\n");scanf("%ld",&num);if(p0->num==num){head=p0->next;free(p0);flag=1;n--; /*学生数减一*/}p=p0->next;while(p!=NULL){if(p->num==num){p0->next=p->next;free(p);flag=1;n--;}else{p0=p;p=p->next;}}if(p->next==NULL)flag=0;if(flag==1){printf("\n永久删除%ld号学生的信息?(y/n)",num);getchar();ch=getchar();if(ch=='y'||ch=='Y')save_message();else{printf("\n撤消删除!");getch();}}elseprintf("\n删除失败!");getch();}}/*查询模块*/inquire_message(){int i,flag=0;long num;struct STH *p;p=head=readfile();if(head==NULL){printf("文件读取出错!");getch();}else{printf("请输入要查询学生的学号:\n");scanf("%ld",&num);for(i=0;i<n;i++){if(p->num==num){printf("\n信息已找到:\n");getch();clrscr();printf("****************************** ****FOUND*********************************\n");printf("------------------------------------------------------------------------\n\n");printf(PRINTF);printf(GESHI,p->num,p->name,p->sex, p->birth.year,p->birth.month,p->birth.day,p->xi,p->ban,p->add);printf("\n------------------------------------------------------------------------\n");printf("****************************** *****END**********************************\n");flag=1;break;}elsep=p->next;}if(flag==0)printf("\n无此人信息!");elseprintf("\n显示完毕!\按任意键返回主菜单......");getch();}}/*显示模块*/output_message(){int i=0;struct STH*p;p=head=readfile();if(head==NULL){printf("文件读取出错!");getch();}else{clrscr();printf("\n************************************STUDENT* ***********************************\n");printf("-------------------------------------------------------------------------------\n");printf(PRINTF);while(i<n){printf(GESHI,p->num,p->name,p->sex,p->birth .year,p->birth.month,p->birth.day,p->xi,p->ban,p->add);p=p->next;i++;}printf("-------------------------------------------------------------------------------\n");printf("**************************************END*** ***********************************\n");printf("\n显示完毕!\n共%d条信息.\n按任意键返回主菜单......",n);getch();}}/*存档模块*/save_message(){FILE *fp;int i;struct STH*p;p=head;if((fp=fopen("STH.txt","w"))==NULL){printf("读文件错误!!");exit(0);}fprintf(fp,"%d",n);fprintf(fp,"%c",'/');/*注意要该*/for(i=0;i<n;i++){fprintf(fp,"%ld %s %s %d %d %d %s %s %s ",p->num,p->name,p->sex,p->birth.year,p->birth.month,p->birth.day,p->xi,p->ban,p->add);p=p->next;}fclose(fp);printf("\n信息已经保存在STH.txt文件中!");getch();}/*主函数*/void main(void){int choice;char ch;FILE *fp;struct date d;getdate(&d);if((fp=fopen("STH.txt","r"))==NULL){fp=fopen("STH.txt","w");fprintf(fp,"%d",0);fclose(fp);}else{fscanf(fp,"%d",&n);fclose(fp);}clrscr();printf("制作人:通工0501 艺凡");printf("\n\n\n");printf(" ***************************** ********************* \n");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");printf("%d年%d月%d日",d.da_year,d.da_mon,d.da_day);printf(" \n 请选择(0-6):");scanf("%d",&choice);if(n==0) /*记录为空时不让其进行其他操作*/{while(choice<0||choice>1){printf("记录为空,无法操作此项!");printf("请重新输入:(0或1)\n");scanf("%d",&choice);}}else{while(choice<0||choice>6){printf("输入有误!\n 请重新输入:\n");scanf("%d",&choice);}}do{switch(choice){case 1:input_message();break;case 2:insert_message();break;case 3:inquire_message();break;case 4:delete_message();break;case 5:renew_message();break;case 6:output_message();break;case 0:break;}printf("\n按任意键返回主菜单!");getchar();ch='y';if(ch=='Y'||ch=='y'){clrscr();printf("制作人:通工0501 艺凡");printf("\n\n\n");printf(" ************* ************************************* \n");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");printf("%d年%d月%d日",d.da_year,d.da_mon,d.da_day);printf(" \n 请选择(0-6):");scanf("%d",&choice);}}while(choice!=0);printf("以上信息已经保存在STH.txt中!\n");}。