资源描述
综合设计题目:学籍管理及选课系统
课程名称: C语言高级程序及设计
学 院 :自动化
专业班级: 12电力4班
学 号:
姓 名:郑程鹏
联系方式:
任课老师:王星华
2014年12月3日
#include〈stdio.h〉
#include<stdlib.h〉
#include〈string。h>
intcourcenum,studentnum;
//课程的结构体
structcource
{
intnum,credit,maxnum,selectednum;
charcourceName[30];
structcource * next;
}cource;
//学生的结构体
struct student
{
charstudentname[20];
intselectednum,selectedcredit,num;
intcource[10];
struct student * next;
}student;
//指针链表的头
structcource * courcehead;
struct student * studenthead;
int j;
void main()
{
voidgetcource();
voidgetstudent();
inti;//作为获取用户的输入的变量
getcource();//从文件中读取已保存的课程数据
getstudent();//从文件中读取已保存的学生数据
system(”cls");
run:
for(j =0;j 〈 80;j++) printf(”*");
printf(”\n\n\n1。查看课程 ”);
printf(”2.查看管理学生 ");
printf(”3.查看选课 ");
printf("4.选课情况 ");
printf(”5。退出系统 \n\n\n”);
for(j =0;j 〈 80;j++) printf(”*");
printf("请输入您的选择:”);
scanf("%d”,&i);
if(i < 1 || i > 5)
{
printf(" 输入有误\n”);
goto run;
}
if(i == 1)
{
system(”cls”);//清屏函数,清楚屏幕所有的东西
viewcource();//查看课程的函数
goto run;
}
else if(i == 2)
{
system(”cls”);
manage();//管理学生的函数
goto run;
}
else if(i == 3)
{
system(”cls");
xuankecaidan();//选课的函数
goto run;
}
else if(i == 4)
{
system(”cls”);
sortstudent();//查看选课情况的函数
goto run;
}
else if(i == 5)
{
system(”cls”);
}
}
voidgetcource()
{
FILE * fp;
structcource *p1,*p2;
courcenum=0;
char filepath[] = ”cource。txt";//默认txt文件在当前目录下
if((fp=fopen(filepath,”r”))==NULL)//以只读的方式打开文件
{
//无法打开文件的时候
printf("error to find courcefile%s!\n",filepath);
exit(0);
}
//生成structcource大小的内存并让p1,p2指向
p1=p2=(structcource*)malloc(sizeof(structcource));
//从文件中读取
fscanf(fp,”%d%s%d%d%d”,&p1—〉num,p1—>courceName,&p1—〉credit,&p1-〉maxnum,&p1—>selectednum);
courcehead=NULL;
while(!feof(fp))//循环读取直到读到文件结尾
{
courcenum=courcenum+1;
//读取第一个课程的时候情况不同
if(courcenum==1) courcehead=p1;
else p2—>next=p1;
p2=p1;
p1=(structcource * )malloc(sizeof(structcource));
fscanf(fp,”%d%s%d%d%d”,&p1->num,p1—〉courceName,&p1—>credit,&p1—〉maxnum,&p1->selectednum);
}
p2—〉next=NULL;
}
voidgetstudent()
{
charbianhao[20];
char * fgzf = ";”;
char * cba;
inti;
FILE * fp;
struct student *p1,*p2;
courcenum=0;
charfilepath[] = ”student。txt”;
if((fp=fopen(filepath,"r"))==NULL)
{
printf(”error to find student file%s!\n",filepath);
exit(0);
}
p1=p2=(struct student*)malloc(sizeof(struct student));
fscanf(fp,"%d%s%d%s%d",&p1—>num,p1-〉studentname,&p1-〉selectednum,bianhao,&p1-〉selectedcredit);
cba=strtok(bianhao,fgzf);//char *strtok(char s[], const char *delim);分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
i=0;
while(cba)
{
p1—〉cource[i]=change(cba);
cba=strtok(NULL,fgzf);
i++;
}
studenthead=NULL;
while(!feof(fp))
{
studentnum=studentnum+1;
if(studentnum==1) studenthead=p1;
else p2->next=p1;
p2=p1;
p1=(struct student * )malloc(sizeof(struct student));
fscanf(fp,”%d%s%d%s%d”,&p1—〉num,p1-〉studentname,&p1—〉selectednum,bianhao,&p1->selectedcredit);
cba=strtok(bianhao,fgzf);
i=0;
while(cba)
{
p1—>cource[i]=change(cba);
cba=strtok(NULL,fgzf);
i++;
}}
p2—〉next=NULL;
}
int change(char *str)
{
int v=0;
do
{
v=10*v+*str—’0';
str++;
}
while((*str〉='0')&&(*str<='9'));
return v;
}
voidsetcource()
{
FILE * fp;
structcource * c;
charfilepath[] = ”cource。txt";
if((fp=fopen(filepath,"w”))==NULL)
{
printf(”\nfail”);
exit(0);
}
c=courcehead;
while(c!=NULL)
{
fprintf(fp,”%d\t%s\t%d\t%d\t%d\n",c—>num,c—〉courceName,c-〉credit,c—>maxnum,c->selectednum);
c=c-〉next;
}
fclose(fp);
}
voidsetstudent()
{
FILE * fp;
struct student * s;
charfilepath[] = ”student。txt";
if((fp=fopen(filepath,”w"))==NULL)
{printf("\nnot save”);
exit(0);
}
charfgzf[2]=”;”;
inti;
s=studenthead;
while(s!=NULL && s—〉num!=NULL)
{
charoutcource[15]=””;
for(i=0;i<s—〉selectednum;i++)
{
char temp[20];
if(i==0)
{
itoa(s—〉cource[i],temp,10);
strcat(outcource,temp);
}
else
{
strcat(outcource,fgzf);
itoa(s-〉cource[i],temp,10);
strcat(outcource,temp);
}}
if(s-〉selectednum==0)
strcat(outcource,”0");
fprintf(fp,"%d\t%s\t%d\t%s\t%d\n”,s—〉num,s-〉studentname,s—〉selectednum,outcource,s-〉selectedcredit);
s=s—〉next;
}
fclose(fp);
printf(”ok\n",filepath);
}
void save()
{
setcource();
setstudent();
}
voidviewstudent()
{
struct student * s;
charfgzf[2]=";”;
inti;
system(”cls”);
s=studenthead;
for(j =0;j 〈 80;j++) printf(”*”);
printf(”学号姓名已选课程数已选课程序号总学分\n”);
while(s!=NULL && s—〉num!=NULL)
{
charoutcource[15]=”";
for(i=0;i〈s-〉selectednum;i++)
{
char temp[20];
if(i==0)
{itoa(s-〉cource[i],temp,10);
strcat(outcource,temp);
}
else
{strcat(outcource,fgzf);
itoa(s-〉cource[i],temp,10);
strcat(outcource,temp);
}}
printf("%-16d%-16s%—16d%—16s%-10d\n”,s—>num,s-〉studentname,s-〉selectednum,outcource,s->selectedcredit);
s=s-〉next;
}
for(j =0;j 〈 80;j++) printf(”*");
}
voidviewcource()
{
system(”cls”);
structcource * c;
c=courcehead;
printf(”课程序号课程名称学分最大人数已选人数\n”);
while(c!=NULL)
{
printf("%—16d%-16s%—10d%—16d%—10d\n”,c-〉num,c—>courceName,c—〉credit,c—>maxnum,c->selectednum);
c=c—〉next;
}}
voidzengjia(struct student * newstudent)
{
struct student *p0,*p1,*p2;
p1=studenthead;
p0=newstudent;
if(studenthead==NULL)
{
studenthead=p0;
p0—〉next=NULL;
}
else
{while((p0-〉num > p1—>num) && (p1->next!=NULL))
{
p2=p1;
p1=p1—〉next;
}
if(p0-〉num == p1—〉num)
{
printf(”您所输入的学号重复\n");
goto end;
}
if(p0—〉num 〈 p1->num)
{
if(studenthead==p1)
studenthead=p0;
else p2->next=p0;
p0—>next=p1;
}
else
{
p1-〉next=p0;
p0—>next=NULL;
}}
studentnum=studentnum+1;
save();
end: ;
}
void manage()
{
struct student * newstudent;
inti,num;
run:
for(j =0;j 〈 80;j++) printf(”*”);
printf(”1。添加新学生\n");
printf("2.查看已存在的学生资料\n");
printf("3.返回\n");
for(j =0;j 〈 80;j++) printf(”*”);
scanf(”%d”,&i);
if(i == 1)
{
system("cls”);
newstudent=(struct student *)malloc(sizeof(struct student));
newstudent->selectednum=0;
newstudent-〉cource[0]=0;
printf(”学号姓名 \n”);
scanf("%d %s”,&newstudent—〉num,newstudent->studentname);
newstudent—〉selectedcredit=0;
zengjia(newstudent);
system(”cls”);
goto run;
}
else if(i == 2)
{
viewstudent();
goto run;
}
else if(i == 3)
{
system("cls”);
}
}
voidxuanke(struct student * s)
{
structcource * c;
intnum,i,j=0;
run:
for(j =0;j < 80;j++) printf(”*”);
printf(”请输入课程序号,返回请输入9.\n”);
scanf(”%d”,&num);
if(num==0)
{
goto end;
}
c=courcehead;
while(c-〉num!=num && c—〉next!=NULL)
{
c=c->next;
}
if(c—>num!=num)
{
printf("输入有误\n");
goto run;
}
if(c—>maxnum==c->selectednum)
{
printf("该课程人数已爆满\n");
goto run;
}
for(i=0;s—〉cource[i]>0;i++)
{
if(num==s—〉cource[i])
{
printf("不能重复选课\n”);
goto run;
}
}
if(s-〉selectednum〉=5)
{
printf("所选课程数已达5门\n");
goto end;
}
s-〉cource[i]=num;//刷新数据
(s—〉selectednum)++;
s->selectedcredit+=c-〉credit;
(c—>selectednum)++;
save();
printf(”选课成功\n”);
end:;
}
voidtuixuan(struct student * s)
{
structcource * c;
intnum,i,j=0;
run:printf("课程序号(返回请按9回车):\n”);
scanf("%d”,&num);
if(num==9)
{
goto end;
}
c=courcehead;
while(c—〉num!=num && c—>next!=NULL)
{
c=c-〉next;
}
if(c-〉num!=num)
{
printf(”输入有误\n”);
goto run;
}
for(i=0;s—〉cource[i]!=num;i++)
{
if(i〉=s-〉selectednum)
{
printf(”您还没选此课程\n”);
goto run;
}}
for(j=i;s->cource[j]〉0;j++)
s—>cource[j]=s->cource[j+1];
s—>cource[——j]=0;
s-〉selectednum——;
s—>selectedcredit—=c—〉credit;
(c—〉selectednum)-—;
save();
printf("已退选该课程\n”);
end:;
}
voidchaxunkexuan()
{
char e;
structcource * c;
struct student * s;for(j =0;j 〈 80;j++) printf(”*");
intnum,j=0;
system("cls");
printf(”请输入学号:\n”);
scanf(”%d”,&num);
system(”cls");
s=studenthead;
while(s-〉num!=num && s-〉next!=NULL) s=s-〉next;
if(s-〉num!=num)
{
printf(”没有此人!\n”);
goto end;
}
system(”cls");
c=courcehead;
for(j =0;j 〈 80;j++) printf("*");
printf(”可选课程:\n”);
printf(”序号课程学分人数上限已选人数\n”);
while(c!=NULL)
{if(c-〉maxnum!=c—〉selectednum)
{printf("%-16d%-16s%—10d%-16d%—10d\n”,c—〉num,c->courceName,c—>credit,c—>maxnum,c—〉selectednum);
j++;
}
c=c—>next;
}
if(j==0)
{
printf(”课程已被选完\n”);
goto end;
}
for(j =0;j 〈 80;j++) printf("*”);
printf(”\n是否选课y/n?:\n”);
getchar();
e=getchar();
while(e=='y’)
{
xuanke(s);
printf(”\n继续选课y/n?:\n”);
getchar();
e=getchar();
}
system(”cls”);
end:;
}
voidchaxunyixuan()
{
char e;
struct student * s;
intnum,i;
system("cls");
printf("请输入学号:\n”);
scanf("%d",&num);
s=studenthead;
while(s—>num!=num && s—〉next!=NULL) s=s—〉next;
if(s—>num!=num)
{
printf(”无此人\n");
goto end;
}
system(”cls”);
printf(”已选课程序号:\n”);
if(s—>cource==0)
{
printf(”没选课\n”);
goto end;
}
for(i=0;(s-〉cource[i]〉0)&&(i<5);i++)
{
printf(”%d\n”,s-〉cource[i]);
}
printf(”\n是否退课?y/n”);
getchar();
e=getchar();
while(e==’y’)
{
tuixuan(s);
printf("\n继续退课?y/n");
getchar();
e=getchar();
system(”cls”);
}
end:;
}
struct student *sort(struct student *head)
{
struct student *zhijiecharu;
struct student *t;
struct student *p;
struct student *q;
zhijiecharu = head-〉next;
head—>next = NULL;
while (zhijiecharu != NULL)
{
for (t = zhijiecharu, q = head; ((q!= NULL) && (q—>selectedcredit 〈 t—〉selectedcredit)); p = q, q = q—〉next);
zhijiecharu = zhijiecharu-〉next;
if (q == head)
{
head = t;
}
else
{
p—〉next = t;
}
t—>next = q;
}
return head;
}
voidsortstudent()
{
struct student *newstudenthead,*p,*temp=(struct student * )malloc(sizeof(struct student));
if(studenthead==NULL)
{
printf(”尚未进行选课\n”);
exit(0);
}
newstudenthead=studenthead;
p = sort(newstudenthead);
charfgzf[2]=”;”;
inti;
for(j =0;j < 80;j++) printf(”*”);
printf(”学生序号学生姓名已选课程数量已选序号已选总学分\n”);
while(p!=NULL && p—〉num!=NULL)
{
charoutcource[15]="”;
for(i=0;i<p—〉selectednum;i++)
{
char temp[20];
if(i==0)
{
itoa(p—〉cource[i],temp,10);
strcat(outcource,temp);
}
else
{
strcat(outcource,fgzf);
itoa(p—〉cource[i],temp,10);
strcat(outcource,temp);
}
}
printf(”%-16d%-16s%—16d%—16s%—10d\n",p-〉num,p-〉studentname,p->selectednum,outcource,p->selectedcredit);
p=p—〉next;
}
}
voidxuankecaidan()
{
inti;
for(j =0;j 〈 80;j++) printf("*”);
printf("1。可选课程\n”);
printf("2。已选课程\n");
printf(”3.返回\n");
for(j =0;j 〈 80;j++) printf(”*");
scanf(”%d",&i);
switch(i)
{
case(1):chaxunkexuan();break;
case(2):chaxunyixuan();break;
case(3):system("cls”);break;
}}
展开阅读全文