|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 DarkSE 于 2015-12-26 09:26 编辑
第一个从文件录入信息出错,我自己弄了一下好像指针pr没有问题,那应该是文件fp出现问题吧,fp的内容是一开始用wb的形式录入,成功了再用ab形式的。麻烦各位帮我看一下,谢谢。
问题在第142和143行
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- struct link *input(struct link *head);
- struct link *course_enroll();//课程录入//完成
- struct link *student_enroll();//学生录入//未完成
- void course_skim();//课程信息浏览//完成
- int check();//学分查询
- void event();//课程选修情况//完成
- void check_number();//学号查询//完成
- void deletememory();//清除内存
- struct student
- {
- int stu_number;
- char stu_name[10];
- int select_number;
- struct student *stu_next;
- }student1;
- struct link
- {
- int number;//课程编号
- char name[10];
- char nature[10];//课程性质
- int time;//总学时
- int time_class;//授课时间
- int score;
- int time_start;//开课时间
- struct student *stu;
- struct link *next;//节点
- };
- main()
- {
- int n,a=0;
- struct link *head=NULL;
- struct student *stu_head=NULL;
- for(;;)
- {
- do
- {
- system("cls");
- printf("\n\n\t\t ----------欢迎进入学生选课系统----------\n\n");
- printf("\t\t 1->课程信息录入系统 \n\t\t 2->学生选课信息录入系统 \n");
- printf("\t\t 3->课程信息浏览 \n\t\t 4->学分查询\n");
- printf("\t\t 5->课程选修情况 \n\t\t 6->学号查询功能\n\t\t 7->退出选课系统 \n");
- printf("\n\t\t ----------------------------------------");
- printf("\t\t 请输入:");
- scanf("%d",&n);
- if(n<0 || n>=8)
- {
- printf(" 输入错误,请重新输入:");
- }
- Sleep (500);
- }while(n<0 || n>=8);
- switch(n)
- {
- case 0:
- {
- system("cls");
- head=input(head);
- break;
- }
- case 1:
- {
- system("cls");
- head=(course_enroll(head));
- break;
- }
- case 2:
- {
- system("cls");
- stu_head =student_enroll(head,stu_head);
- break;
- }
- case 3:
- {
- system("cls");
- course_skim(head);
- break;
- }
- case 4:
- {
- system("cls");
- check(head,stu_head);
- break;
- }
- case 5:
- {
- system("cls");
- event(head,stu_head);
- break;
- }
- case 6:
- {
- system("cls");
- check_number(head,stu_head);
- break;
- }
- case 7:
- {
- deletememory(head,stu_head);
- exit(0);
- }
- }
- }
- }
- //文件导入信息
- struct link *input(struct link *head)
- {
- struct link *p=NULL;//定义指针p
- struct link *pr=head;//定义指针pr指向head
- FILE *fp;//文件
- if(!(fp = ("随意修改","rb")))
- {
- printf("不能打开文件!\n");
- return;
- }
- // rewind(fp);
- while(!feof(fp))
- {
- p = (struct link *)malloc(sizeof(struct link));//动态申请内存
- if(p == NULL)
- {
- printf("内存不足!\n");
- exit(1);
- }
- if(head == NULL)//head为空
- {
- head = p;
- }
- else
- {
- while(pr->next != NULL)
- {
- pr = pr->next;
- }
- pr->next = p;
- }
- pr = p;
- // printf("%d",pr);指针地址存在,pr不存在问题
- // fread(pr,sizeof(struct link),1,fp);
- fscanf(fp,"%d%s%s%d%d%d%d",&pr->number,&pr->name,&pr->nature,&pr->time,&pr->time_class,&pr->score,&pr->time_start);
- /* if(fread(pr,sizeof(struct link),1,fp))
- {
- printf("输出错误!\n");
- fclose(fp);
- }*/
- pr->next = NULL;
- }
- fclose(fp);
- return head;
- }
- struct link *course_enroll(struct link *head)//课程录入
- {
- int j=1,flag=1,i;
- FILE *fp;
- struct link *p=NULL;
- struct link *pr=head;//指针指向head
- if(!(fp = fopen("随意修改","ab")))
- {
- printf("不能打开文件!\n");
- return;
- }
- printf("\n\n\t\t----------欢迎进入课程信息录入系统:----------\n");
- while(flag == 1)
- {
- printf("\n\n\t\t 请输入录入的课程的门数:");
- scanf("%d",&i);
- j=1;
- system("cls");//清屏
- printf("请按照以下格式输入课程信息:\n");
- printf("---编号--名称--性质--学时--授课时间--学分--开课学期---\n");
- for(j;j<=i;j++)//输入的课程的数目
- {
- p=(struct link *)malloc(sizeof(struct link));//为struct link申请内存
- if(p==NULL)//检查申请内存是否为空
- {
- printf("系统错误!!");
- exit(0);
- }
- if(head==NULL)//检查head是否为空
- {
- head=p;//head为空,把第一个当做第一个链表
- }
- else//head 不为空
- {
- while (pr->next != NULL)//指向下一个,直到链尾为止
- {
- pr=pr->next;
- }
- pr->next=p;//把申请的内存接在链尾后面
- }
- pr=p;//把空链表指向head
- scanf("%d",&pr->number);
- scanf("%s",&pr->name);
- scanf("%s",&pr->nature);
- scanf("%d",&pr->time);
- scanf("%d",&pr->time_class);
- scanf("%d",&pr->score);
- scanf("%d",&pr->time_start);
- fwrite(pr,sizeof(struct link),1,fp);
- pr->next=NULL;//最后一个的指针域定义为NULL
- }
- printf("-----------------------------------------------------------");
- printf("\n\n是否退出课程信息录入系统?退出输入0,不退出输入1!\n请输入:");
- scanf("%d",&flag);
- do
- {
- if(flag<0 || flag>1)
- {
- printf("输入错误!! 请重新输入:");
- scanf("%d",&flag);
- }
- }while(flag <0 || flag > 1);
- p=head;
- }
- fclose(fp);
- return head;
- }
- struct link *student_enroll(struct link *head,struct student *stu_head)//学生录入//学生选择课程的编号进而录入
- {
- int number,flag=1;
- FILE *fp1;
- struct student *pstu=NULL;
- struct student *pstur=stu_head;
- struct link *p=head;
- if(!(fp1 = fopen("学生信息","ab")))
- {
- printf("不能打开文件!\n");
- return;
- }
- printf("\n\n\t\t----------欢迎进入学生选课信息录入系统:----------\n");
- Sleep(1000);
- while(flag == 1)
- {
- system("cls");//清屏
- if(p==NULL)//head是否为空
- {
- printf("系统未录入任何信息 !!\n");//没有数据可供选课
- }
- else
- {
- printf("请输入选择的课程编号:");
- scanf("%d",&number);
- while(p!=NULL)//
- {
- if( number == p->number )//寻找存在的课程编号
- {
- printf("请输入学生学号及姓名:");
- pstu = (struct student *)malloc(sizeof(struct student));
- if(pstu == NULL)//申请内存为空
- {
- printf("系统错误!!");
- exit(0);
- }
- if(stu_head==NULL)//stu_head为空
- {
- stu_head=pstu;
- }
- else
- {
- while(pstur->stu_next != NULL)//stu_head不为空,寻找到链尾为止
- {
- pstur=pstur->stu_next ;
- }
- pstur->stu_next = pstu;//申请的内存加入链尾
- }
- pstur=pstu;
- scanf("%d",&pstu->stu_number);
- scanf("%s",&pstu->stu_name);
- pstu->select_number = number;
- pstu->stu_next=NULL;//新节点指针域定义为NULL
- // p->stu = stu_head;
- fwrite(pstu,sizeof(struct link),1,fp1);
- break;
- }
- p=p->next ;//当寻找不到符合的课程编号,则指向下一个继续寻找,直到链尾
- }
- }
- printf("-----------------------------------------------------------");
- printf("\n\n是否退出学生选课信息录入系统?退出输入0,不退出输入1!\n请输入:");
- scanf("%d",&flag);
- do
- {
- if(flag<0 || flag>1)
- {
- printf("输入错误!! 请重新输入:");
- scanf("%d",&flag);
- }
- }while(flag <0 || flag > 1);
- p=head;
- }
- fclose(fp1);
- return stu_head;
- }
- void course_skim(struct link *head)//课程信息浏览
- {
- struct link *p=head;
- int flag=1;
- printf("\n\n\t\t----------欢迎进入课程信息浏览系统:----------\n");
- Sleep(1000);
- while(flag == 1)
- {
- system("cls");
- if(p==NULL)
- {
- printf("系统未录入任何信息 !!\n");
- }
- else
- {
- printf("---编号--名称--性质--学时--授课时间--学分--开课学期---\n");
- while(p != NULL)
- {
- printf("\n%d ",p->number);
- printf("%s ",p->name);
- printf("%s ",p->nature);
- printf("%d ",p->time);
- printf("%d ",p->time_class);
- printf("%d ",p->score);
- printf("%d \n",p->time_start);
- p = p->next;
- }
- }
- printf("-----------------------------------------------------------");
- printf("\n\n是否退出课程信息浏览系统?退出输入0,不退出输入1!\n请输入:");
- scanf("%d",&flag);
- do
- {
- if(flag<0 || flag>1)
- {
- printf("输入错误!! 请重新输入:");
- scanf("%d",&flag);
- }
- }while(flag <0 || flag > 1);
- p=head;
- }
- }
- int check(struct link *head,struct student *stu_head)//学分查询
- {
- int number,flag=1;
- struct link *p=head;
- struct student *pr=stu_head;
- printf("\n\n\t\t----------欢迎进入学分查询系统:----------\n");
- Sleep(1000);
- while(flag == 1)
- {
- system("cls");
- if(p==NULL)
- {
- printf("系统未录入任何信息 !!\n");
- }
- else
- {
- printf("请输入查询的课程学分:");
- scanf("%d",&number);//输入查询的学分
- printf("学分为%d的课程编号、课程名称为:\n",number);
- while( p != NULL)
- {
- if(number == (p->score))//查询的学分与课程学分相同
- {
- printf("\n%d---",p->number);
- printf("%s\n",p->name);
- }
- p = p->next;//学生信息下移到一个指针
- }//学生信息查询到结束
- }
- printf("-----------------------------------------------------------");
- printf("\n\n是否退出学分查询入系统?退出输入0,不退出输入1!\n请输入:");
- scanf("%d",&flag);
- do
- {
- if(flag<0 || flag>1)
- {
- printf("输入错误!! 请重新输入:");
- scanf("%d",&flag);
- }
- }while(flag <0 || flag > 1);
- p=head;
- }
- }
- void event(struct link *head,struct student *stu_head)//课程选修情况
- {
- struct link *p=head;
- struct student *pr=stu_head;
- struct link *pp=head;
- int flag=1;
- printf("\n\n\t\t----------欢迎进入课程选修查询系统:----------\n");
- Sleep(1000);
- while(flag == 1)
- {
- system("cls");
- if(p==NULL)
- {
- printf("系统未录入任何信息 !!\n");
- }
- else if(pr == NULL)
- {
- printf("学生未进行选课\n");
- }
- else
- {
- //输出课程信息
- printf("---编号--名称--性质--学时--授课时间--学分--开课学期--学生学号--学生姓名---\n");
- while(p != NULL )
- {
- pr=stu_head;
- printf("\n%d----",p->number);
- printf("%s----",p->name);
- printf("%s----",p->nature);
- printf("%d----",p->time);
- printf("%d----",p->time_class);
- printf("%d----",p->score);
- printf("%d----",p->time_start);
- //输出课程里面的学生信息
- while(pr != NULL)
- {
- if(pr->select_number == p->number)
- {
- printf("%d----",pr->stu_number);
- printf("%s \n",pr->stu_name);
- }
- pr = pr->stu_next;
- }
- p = p->next;
- }
- }
- printf("\n-----------------------------------------------------------");
- printf("\n\n是否退出课程选修查询系统?退出输入0,不退出输入1!\n请输入:");
- scanf("%d",&flag);
- do
- {
- if(flag<0 || flag>1)
- {
- printf("输入错误!! 请重新输入:");
- scanf("%d",&flag);
- }
- }while(flag <0 || flag > 1);
- p=head;
- pr=stu_head;
- }
- }
- //学生学号查询学生选课信息
- void check_number(struct link *head,struct student *stu_head)//5
- {
- int number,flag=1,i=0;
- struct link *p=head;
- struct student *pr=stu_head;
- printf("\n\n\t\t----------欢迎进入学号查询系统:----------\n\n");
- Sleep(1000);
- while(flag == 1)
- {
- system("cls");
- if(p==NULL)
- {
- printf("系统未录入任何信息 !!\n");
- }
- else if(pr == NULL)
- {
- printf("未有选课记录!!\n");
- }
- else
- {
- printf("请输入查询的学生学号:");
- scanf("%d",&number);//输入查询的学生编号
- i=0;
- while( pr != NULL)
- {
- if(number == (pr->stu_number))//查询的学号与学生学号相同
- {
- p = head;
- //输出选择的课程编号和名字
- while(p != NULL)//学生信息里的选课编号等于课程编号
- {
- if(pr->select_number == p->number)
- {
- printf("学生编号:%d\t学生名字:%s\t",pr->stu_number,pr->stu_name);//学生编号和名字
- printf("课程编号:%d\t",p->number);//课程编号
- printf("课程名字:%s \n",p->name);//课程名字
- i += p->score;
- }
- p = p->next;
- }
- }
- pr = pr->stu_next;//学生信息下移到一个指针
- }//学生信息查询到结束
- printf("-----------------------------------------------------------");
- if(i<15)
- {
- printf("\n学生编号为:%d,总学分为:%d\n\n警告警告!!总学分低于15分\n",number,i);
- }
- else
- {
- printf("\n学生编号为:%d,总学分为:%d\n\n",number,i);
- }
- }
- printf("\n\n是否退出学号查询系统?退出输入0,不退出输入1!\n请输入:");
- scanf("%d",&flag);
- do
- {
- if(flag<0 || flag>1)
- {
- printf("输入错误!! 请重新输入:");
- scanf("%d",&flag);
- }
- }while(flag <0 || flag > 1);
- pr=stu_head;
- p=head;
- }
- }
- //清除分配的内存
- void deletememory(struct link *head,struct student *stu_head)
- {
- struct link *p=head;
- struct student *pstu=stu_head;
- struct link *pr=NULL;
- system("cls");
- printf("\n\n\t\t\t ********************\n");
- printf("\t\t\t * 成功退出系统 *\n");
- printf("\t\t\t ********************\n\n");
- while(p != NULL)
- {
- pr=p;
- p=p->next ;
- free(pr);
- }
- while(pstu!=NULL)
- {
- pr=pstu;
- pstu=pstu->stu_next ;
- free(pr);
- }
- Sleep(500);
- }
复制代码 |
|