|
50鱼币
这是一个学生课程管理系统,在进行课程查询时经常显示查询不到,而文本中有相关数据。我是哪里出问题了,求大神指点
#include <stdio.h>
#include <string.h> //本程序中有两个文本,
#include <stdlib.h> //adm_course.txt为管理员控制的文本,能够对所有课程进行改动,
#include <malloc.h> //std_course.txt为学生文本,只能对自己选修的课程进行改动。
void prin1();
void choose();
typedef struct subjects
{
int num; //课程编号
char name[20]; //课程名称
char kind[10]; //课程性质
int stime; //总学时
int ttime; //授课学时
int etime; //实验或上机学时
int score; //学分
int term; //开课学期
struct subjects *next;
}SUB;
SUB *head=NULL;
SUB *create_form() //创建链表
{
SUB *head,*tail,*p;
int num,stime,ttime;
int etime,score,term;
char name[20],kind[10];
int size=sizeof(SUB);
head=tail=NULL;
printf("输入选修课程信息:\n");
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
while(num!=0)
{
p=(SUB *)malloc(size);
p->num=num;
strcpy(p->name,name);
strcpy(p->kind,kind);
p->stime=stime;
p->ttime=ttime;
p->etime=etime;
p->score=score;
p->term=term;
if(head==NULL)
{
head=p;
}
else
{
tail->next=p;
}
tail=p;
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);
}
tail->next=NULL;
return head;
}
void savefile() //保存文件,管理员添加或删除或修改课程信息
{
SUB *p;
FILE *fp;
fp=fopen("adm_course.txt","w");
if(fp==NULL)
{
exit(0);
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
for(p=head;p;p=p->next)
{
fprintf(fp,"%5d%12s%9s%8d%9d%11d%10d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
}
fclose(fp);
printf("创建后的信息已放入'adm_course.txt'文件中\n\n");
system("pause");
}
void savefile1() //保存文件,学生删除或修改课程信息
{
SUB *p;
FILE *fp;
fp=fopen("std_course.txt","w");
if(fp==NULL)
{
exit(0);
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
for(p=head;p;p=p->next)
{
fprintf(fp,"%5d%12s%9s%8d%9d%11d%10d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",p->num,p->name,p->kind,p->stime,p->ttime,p->etime,p->score,p->term);
}
fclose(fp);
printf("创建后的信息已放入'std_course.txt'文件中\n\n");
system("pause");
}
void readfile() //阅读文件
{
void *myInsert(SUB*);
SUB *newSub; //新课程
int num,stime,ttime,etime;
int score,term;
char c,name[20],kind[10],fname[20];
FILE *fp;
fp=fopen("adm_course.txt","r");
while(!feof(fp))
{
newSub=(SUB*)malloc(sizeof(SUB));
fscanf(fp,"%d%s%s%d%d%d%d%d\n",&newSub->num,newSub->name,newSub->kind,&newSub->stime,&newSub->ttime,&newSub->etime,&newSub->score,&newSub->term);
myInsert(newSub);
}
fclose(fp);
}
void prin() //浏览所有课程
{
SUB *ptr;
head=NULL;
readfile();
if(head==NULL)
{
printf("\n\n\t*********NO RECORDS!************\n");
return;
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
for(ptr=head;ptr;ptr=ptr->next)
{
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
}
printf("\n");
system("pause");
}
void prin1() //浏览所有选修课程
{
SUB *ptr;
FILE *fp;
if((fp=fopen("std_course.txt","r"))==NULL)
{
printf("Cannot open file.\n");
choose();
}
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
while(!feof(fp))
{
ptr=(SUB*)malloc(sizeof(SUB));
fscanf(fp,"%d%s%s%d%d%d%d%d\n",&ptr->num,ptr->name,ptr->kind,&ptr->stime,&ptr->ttime,&ptr->etime,&ptr->score,&ptr->term);
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
}
printf("\n");
system("pause");
}
void *myInsert(SUB *subj) //链表插入操作
{
SUB *ptr,*ptr2;
ptr=subj;
if(head==NULL)
{
head=ptr;
head->next=NULL;
}
else
{
for(ptr2=head;ptr2;ptr2=ptr2->next)
if(ptr2->next==NULL)
{
ptr2->next=subj;
subj->next=NULL;
break;
}
}
return head;
}
void *insert() //插入课程信息
{
SUB *ptr,*subj;
int size=sizeof(SUB);
char ch,ch1;
while(ch!='0')
{
subj=(SUB *)malloc(size);
ptr=subj;
printf("输入要插入的课程信息:\n");
printf("\n\t\t请输入课程编号:");scanf("%d",&subj->num);
printf("\n\t\t请输入课程名称:");scanf("%s",&subj->name);
printf("\n\t\t请输入课程性质:");scanf("%s",&subj->kind);
printf("\n\t\t请输入总学时:");scanf("%d",&subj->stime);
printf("\n\t\t请输入授课学时:");scanf("%d",&subj->ttime);
printf("\n\t\t请输入实践或上机学时:");scanf("%d",&subj->etime);
printf("\n\t\t请输输入学分:");scanf("%d",&subj->score);
printf("\n\t\t请输入开课学期:");scanf("%d",&subj->term);
myInsert(subj);
printf("\n添加完毕,新信息存入文件中\n");
printf("\n继续插入请按回车\n");
printf("\n结束添加课程按 0: [ ]\b\b");
ch1=getchar(); //将回车键赋给CH1,否则subj->term输完后输入的回车键会赋给CH,因此用CH1填补。
ch=getchar();
}
system("pause");
}
void *del() //删除课程
{
SUB *p1,*p2;
char ch,ch1;
int num;
while(ch!='0')
{
printf("输入想要删除的课程编号:[ ]\b\b");
scanf("%d",&num);
if(head->num==num)
{
p2=head; head=head->next; free(p2);
}
if(head==NULL)
{
return NULL;
}
p1=head;
p2=head->next;
while(p2)
{
if(p2->num==num)
{
p1->next=p2->next;
free(p2);
}
else
{
p1=p2;
}
p2=p1->next;
}
printf("\n继续删除请按回车\n");
printf("\n结束删除课程按 0: [ ]\b\b");
ch1=getchar(); //将回车键赋给CH1,否则num输完后再输入的回车键会赋给CH,因此用CH1填补。
ch=getchar();
printf("\n删除完毕,新信息存入文件中\n");
system("pause");
}
system("pause");
}
void choose() //选新课程--学生选课函数
{
SUB *p,*q;
int a[5];
int num,total=0,i=0,j;
printf("输入要选修的课程的编号,编号之间以空格分开\n");
scanf("%d",&num);
printf("如果确认输入完要选修的课程的编号,请输入0: [ ]\b\b");
while(num!=0)
{
for(p=head;p;p=p->next)
{
if(p->num==num)
{
total=total+p->score;
a[i]=num;
i++;
}
}
scanf("%d",&num);
}
if(total<60)
{
printf("选修总学分为%d,未达到60,选修失败!\n",total);
system("pause");
}
else
{
FILE *fp;
fp=fopen("std_course.txt","w");
for(j=0;j<i;j++)
{
for(p=head;q;q=q->next)
{
if(q->num==a[j])
{
fprintf(fp,"%5d%12s%9s%8d%9d%11d%10d%7d\n",q->num,q->name,q->kind,q->stime,q->ttime,q->etime,q->score,q->term);
}
}
}
fclose(fp);
printf("\t\t\n*****选修成功!****\n");
printf("\n您选修的课程总学分为%d,课程分别为:\n",total);
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
for(j=0;j<i;j++)
{
for(q=head;q;q=q->next)
{
if(q->num==a[j])
{
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",q->num,q->name,q->kind,q->stime,q->ttime,q->etime,q->score,q->term);
}
}
}
printf("\n以上信息全部保存在'std_course.txt'中\n");
}
printf("\n");
system("pause");
}
void search() //课程信息查询
{
int a,num;
int t=1;
char type[10],min[10];
SUB *ptr;
L1: system("cls");
printf("\n\n\t**********请选择查询方式*************\n");
printf("\n\t\t1---按课程性质查找\n");
printf("\n\t\t2---按学分查找\n");
printf("\n\t\t3---退出查找\n");
printf("\n\n\t**************************************\n");
printf("\n\nChiose your number(1-4):[ ]\b\b");
scanf("%d",&a);
switch(a)
{
case 1:printf("请输入要查找的课程的性质:");
scanf("%s",type);
for(ptr=head;ptr;ptr=ptr->next)
{
if(strcmp(type,ptr->kind)==0)
{
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
}
if(t)
{
printf("\n\t未找到!\n\n");
}
t=1;
system("pause");
goto L1;
case 2:printf("输入要查找的课程的学分:");
scanf("%d",&num);
for(ptr=head;ptr;ptr=ptr->next)
{
{
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机 学分 开课学期\n");
printf(" 学时学分\n");
printf("%5d%12s%9s%8d%9d%11d%10d%7d\n",ptr->num,ptr->name,ptr->kind,ptr->stime,ptr->ttime,ptr->etime,ptr->score,ptr->term);
t=0;
}
}
if(t)
{
printf("\n\t未找到!\n\n");
}
t=1;
system("pause");
goto L1;
case 3:break;
}
}
void mod() //修改课程信息
{
int a,num;
char ch,ch1;
SUB *ptr;
while(ch!='0')
{
C1: printf("\n请输入要修改的课程的编号:");
scanf("%d",&num);
for(ptr=head;ptr;ptr=ptr->next)
{
if(ptr->num==num)
{
system("cls");
printf("\n\n\t**********修改项*********************\n");
printf("\n\t\t1---课程编号\n");
printf("\n\t\t2---课程名称\n");
printf("\n\t\t3---课程性质\n");
printf("\n\t\t4---总学时\n");
printf("\n\t\t5---授课学时\n");
printf("\n\t\t6---实践或上机学时\n");
printf("\n\t\t7---学分\n");
printf("\n\t\t8---开课学期\n");
printf("\n\t\t9---重新输入课程编号\n");
printf("\n\t\t10---退出\n");
printf("\n\n\t**************************************\n");
printf("\n\nChiose your number(1-4):[ ]\b\b");
scanf("%d",&a);
switch(a)
{
case 1:scanf("%d",&ptr->num);break;
case 2:scanf("%s",&ptr->name);break;
case 3:scanf("%s",&ptr->kind); break;
case 4:scanf("%d",&ptr->stime);break;
case 5:scanf("%d",&ptr->ttime);break;
case 6:scanf("%d",&ptr->etime);break;
case 7:scanf("%d",&ptr->score);break;
case 8:scanf("%d",&ptr->term);break;
case 9:goto C1;
case 10:return;
}
printf("\n修改完毕,新信息存入文件中\n");
printf("\n继续修改请按回车\n");
printf("\n结束添加课程按 0: [ ]\b\b");
ch1=getchar(); //将回车键赋给CH1,否则subj->term输完后输入的回车键会赋给CH,因此用CH1填补。
ch=getchar();
}
else
{
printf("\n\t未找到!\n\n");
}
}
system("pause");
}
system("pause");
}
void Mangers() //管理员登录
{
int n,w=1,flag=0,i=3;
char s[8];
char password[7]="1";
do
{
printf("\n\nEnter password:");
scanf("%s",s);
if(!strcmp(s,password)) //进行密码匹配验证
{
flag=1;
break;
}
else
{
printf("\n\nError! You only have %d times! Enter again:\n",i-1);
i--;
}
}
while(i>0);
{
if(!flag)
{
printf("you have Enter 3 times!"); //输入密码超过了3次!!
exit(0); //自动退出
}
}
do
{
system("cls");
puts("\n\n\t*********************管理员菜单***********************\n\n");
puts("\t\t\t1---浏览课程\n");
puts("\t\t\t2---查询课程\n");
puts("\t\t\t3---添加课程\n");
puts("\t\t\t4---删除课程\n");
puts("\t\t\t5---修改课程信息\n");
puts("\t\t\t6---返回主菜单");
puts("\n\n\t******************************************************\n");
printf("Chiose your number(1-5):[ ]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:prin();break;
case 2:search();break;
case 3:insert();savefile();break;
case 4:del();savefile();break;
case 5:prin();mod();savefile1();break;
case 6:return;
default:;
}
}
while(w==1);
}
void Students() //学生登录菜单
{
int n,w=1;
do
{
system("cls");
puts("\n\n\t*********************学生菜单***********************\n\n");
puts("\t\t\t1---浏览所有课程\n");
puts("\t\t\t2---查询课程信息\n");
puts("\t\t\t3---选择选修课程\n");
puts("\t\t\t4---浏览我选修的课程\n");
puts("\t\t\t5---删除错选课程\n");
puts("\t\t\t6---修改课程信息\n");
puts("\t\t\t7---返回主菜单");
puts("\n\n\t****************************************************\n");
printf("Chiose your number(1-6):[ ]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:prin();break;
case 2:search();break;
case 3:choose();break;
case 4:prin1();break;
case 5:del();savefile1();break;
case 6:prin1();mod();savefile1();break;
case 7:return;
default:;
}
}
while(w==1);
}
int main()
{
int n,w=1;
do
{
system("cls");
puts("\n\n\t*********************MENU***********************\n\n");
puts("\t\t\t1.以管理员身份登录(password 1)\n");
puts("\t\t\t2.以学生身份登录\n");
puts("\t\t\t3.退出");
puts("\n\n\t************************************************\n");
printf("Chiose your number(1-3):[ ]\b\b");
scanf("%d",&n);
switch(n)
{
case 1:Mangers() ;break;
case 2:Students();break;
case 3:w=0;break;
default:;
}
}
while(w==1);
return 0;
}
|
|