#include<stdio.h> #include<malloc.h> #define LEN sizeof(struct course) #include<stdlib.h> struct course {int num; char name[8]; char xingzhi[50]; int zhongxueshi; int shoukeshi; int shiyan; int shangji; int xuefen; char kaixuexueqi[20]; struct course *next; }; int n; struct course *creat() {struct course *head; struct course *p1,*p2; int a; n=0; printf("\n !!!输入功能信息!!!\n"); printf("|------------------------------------------------------------------|\n"); p1=p2=(struct course *)malloc(LEN); printf("输入选修编号:"); scanf("%d",&p1->num); a=p1->num; printf("选修课程:"); scanf("%s",p1->name); printf("课程性质:"); scanf("%s",p1->xingzhi); printf("课程总学时:"); scanf("%d",&p1->zhongxueshi); printf("课程授课时:"); scanf("%d",&p1->shoukeshi); printf("课程实验时:"); scanf("%d",&p1->shiyan); printf("课程上机时:"); scanf("%d",&p1->shangji); printf("课程学分:"); scanf("%d",&p1->xuefen); printf("课程开课学期:"); scanf("%s",p1->kaixuexueqi); printf("\n"); head=NULL; while(p1->num!=0) {n=n+1; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct course *)malloc(LEN); printf("输入选修编号:"); scanf("%d",&p1->num); if(p1->num==0) {p2->next=NULL; return (head);} if(p1->num==a){printf("请从新输入\n"); printf("输入选修课编号:"); scanf("%d",&p1->num);;} printf("选修课程:"); scanf("%s",p1->name); printf("课程性质:"); scanf("%s",p1->xingzhi); printf("课程总学时:"); scanf("%d",&p1->zhongxueshi); printf("课程授课时:"); scanf("%d",&p1->shoukeshi); printf("课程实验时:"); scanf("%d",&p1->shiyan); printf("课程上机时:"); scanf("%d",&p1->shangji); printf("课程学分:"); scanf("%d",&p1->xuefen); printf("课程开课学期:"); scanf("%s",p1->kaixuexueqi); printf("\n"); } p2->next=NULL; return (head); } int main() { struct course *creat(void); void soso(struct course *head); void print(struct course *head); void choice(struct course *head); void tianjia(struct course *head); void fprint(struct course *head); struct course *load(); struct course *head; int c,n=0; char k; do{if(n) system("cls"); n++; printf("\n !!!选修课管理系统!!!\n"); printf("|------------------------------------------------------------------|\n"); printf("|1.输入功能------------------- |\n"); printf("|2.浏览----------------------- |\n"); printf("|3.查阅----------------------- |\n"); printf("|4.添加----------------------- |\n"); printf("|5.选课----------------------- |\n"); printf("|0.退出系统------------------- |\n"); printf("|------------------------------------------------------------------|\n"); printf("选择服务内容---------"); scanf("%d",&c); system("cls"); switch(c) { case 1:head=creat(); printf("是否将输入的信息保存到文件以覆盖文件中已存在的信息?(y/n)\n"); getchar(); scanf("%c",&k); getchar(); switch(k){ case 'n': break; case 'y': fprint(head); printf("保存成功!\n"); getchar(); break; } break; case 2: head=load(); print(head); getchar(); break; case 3: head=load();soso(head);getchar();break; case 4:tianjia(head); printf("是否将输入的信息保存到文件以覆盖文件中已存在的信息?(y/n)\n"); getchar(); scanf("%c",&k); getchar(); switch(k){ case 'n': break; case 'y': fprint(head); printf("保存成功!\n"); getchar(); break; } break; case 5:choice(head);break; } }while(c!=0); if(c==0) return 0; } void fprint(struct course *head) { FILE *fp;//定义文件型指针变量 char ch='1'; struct course *p1; if((fp=fopen("f1.txt","w"))==NULL)//创建一个输出文件"f1.txt",并打开输出文件"f1.txt" { printf("File open error!\n");//如果打开出错,就输出“打开出错”信息 exit(0);//把信息传回主函数,并终止程序 } fputc(ch,fp);//向磁盘输出一个字符 for(p1=head;p1!=NULL;p1=p1->next) { fprintf(fp,"%8d %8s %4s %6d %6d %6d %6d %6d %8s\n", p1->num,p1->name,p1->xingzhi,p1->zhongxueshi,p1->shoukeshi,p1->shiyan,p1->shangji,p1->xuefen,p1->kaixuexueqi); } fclose(fp); } struct course *load() { FILE *fp; char ch; struct course *head,*p2,*p1; head=p2=NULL; if((fp=fopen("f1.txt","r"))==NULL) { printf("File open error!\n"); exit(0); } ch=fgetc(fp); if(ch=='1') { while(!feof(fp)) { p1=(struct course *)malloc(LEN); fscanf(fp,"%8d %8s %4s %6d %6d %6d %6d %6d %8s", p1->num,p1->name,p1->xingzhi,p1->zhongxueshi,p1->shoukeshi,p1->shiyan,p1->shangji,p1->xuefen,p1->kaixuexueqi); if(head==NULL) head=p1; else p2->next=p1; p2=p1; } p2->next=NULL; fclose(fp); return head; } else return NULL; } void soso(struct course *head) //搜索函数 {int c; struct course *p1; printf("|请输入要查的学分:"); //c存放要查找的学号,输入c; scanf("%d",&c); while("c!=0") //当c不为0时查找;为0时结束查找 { for(p1=head;p1!=NULL;p1=p1->next) {if(p1->num==c) printf("| 编号 |选修课程|性质|总学时|授课时|实验时|上机时| 学分 |开课学期|\n"); break;} for(p1=head;p1!=NULL;p1=p1->next) if(p1->xuefen==c) //当循环到学号等于c学号时输出该生信息; { printf("|%8d|%8s|%4s|%6d|%6d|%6d|%6d|%6d|%8s|\n", p1->num,p1->name,p1->xingzhi,p1->zhongxueshi,p1->shoukeshi,p1->shiyan,p1->shangji,p1->xuefen,p1->kaixuexueqi); } printf("|继续查询直接输入学号,结束输入0:"); //继续查询直接输入学号,结束输入0 scanf("%d",&c); if(c==0) break; } } void choice(struct course *head) //搜索函数 {int c; struct course *p1; printf("|请输入要选课的序号:"); //c存放要查找的学号,输入c; scanf("%d",&c); while("c!=0") //当c不为0时查找;为0时结束查找 { for(p1=head;p1!=NULL;p1=p1->next) {if(p1->num==c) printf("| 编号 |选修课程|性质|总学时|授课时|实验时|上机时| 学分 |开课学期|\n"); break;} for(p1=head;p1!=NULL;p1=p1->next) if(p1->num==c) //当循环到学号等于c学号时输出该生信息; { printf("|%8d|%8s|%4s|%6d|%6d|%6d|%6d|%6d|%8s|\n", p1->num,p1->name,p1->xingzhi,p1->zhongxueshi,p1->shoukeshi,p1->shiyan,p1->shangji,p1->xuefen,p1->kaixuexueqi); } printf("|继续查询直接输入学号,结束输入0:"); //继续查询直接输入学号,结束输入0 scanf("%d",&c); if(c==0) break; } } void print(struct course *head) { struct course *p1; if(head==NULL) { printf("\n没有信息!\n"); } printf("\n !!!选修课信息表信息!!!\n"); printf(" ==========================================================\n"); printf("| 编号 |选修课程|性质|总学时|授课时|实验时|上机时| 学分 |开课学期|\n"); for(p1=head;p1!=NULL;p1=p1->next) printf("|%8d|%8s|%4s|%6d|%6d|%6d|%6d|%6d|%8s|\n", p1->num,p1->name,p1->xingzhi,p1->zhongxueshi,p1->shoukeshi,p1->shiyan,p1->shangji,p1->xuefen,p1->kaixuexueqi); printf(" ==========================================================\n"); } void tianjia(struct course *head) { struct course *p1,*pt; pt=head; while(pt->next!=NULL) pt=pt->next; do{ p1=(struct course *)malloc(LEN); printf("输入选修课编号:"); scanf("%d",&p1->num); if(p1->num!=0) { pt->next=p1; printf("选修课程:"); scanf("%s",p1->name); printf("课程性质:"); scanf("%s",p1->xingzhi); printf("课程总学时:"); scanf("%d",&p1->zhongxueshi); printf("课程授课时:"); scanf("%d",&p1->shoukeshi); printf("课程实验时:"); scanf("%d",&p1->shiyan); printf("课程上机时:"); scanf("%d",&p1->shangji); printf("课程学分:"); scanf("%d",&p1->xuefen); printf("课程开课学期:"); scanf("%s",p1->kaixuexueqi); p1->next=NULL; pt=p1; printf("\n");} else break; pt=p1; }while(pt->num!=0); }
没有明白什么意思
第二个功能|2.浏览----------------------- |不能正确运行,我觉得可能时load()函数出了问题