---
title: "学生课程选修--图书管理系统（C++版）第二部分"
author: "ldb2741"
date: "2009-08-25T15:19:00Z"
tags: []
slug: "c"
draft: false
lastmod: "2009-08-25T15:19:00Z"
id: 580733
url: "https://ldb2741.iteye.com/blog/580733"
---

**//functionstu.h**

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 \*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-&gt;num=num;  
strcpy(p-&gt;name,name);  
strcpy(p-&gt;kind,kind);  
p-&gt;stime=stime;  
p-&gt;ttime=ttime;  
p-&gt;etime=etime;  
p-&gt;score=score;  
p-&gt;term=term;  
if(head==NULL)  
head=p;  
else  
tail-&gt;next=p;  
tail=p;  
scanf("%d%s%s%d%d%d%d%d",&num,name,kind,&stime,&ttime,&etime,&score,&term);  
}  
tail-&gt;next=NULL;  
return head;  
}

void savefile(SUB \*head)  
{  
SUB \*p;  
FILE \*fp;  
if((fp=fopen("e:\\\\subjects.txt","w"))==NULL){  
printf("open fail!\\n");  
exit(0);  
}  
fprintf(fp,"课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\\n");  
for(p=head;p;p=p-&gt;next)  
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\\n",p-&gt;num,p-&gt;name,p-&gt;kind,p-&gt;stime,p-&gt;ttime,p-&gt;etime,p-&gt;score,p-&gt;term);  
if(fclose(fp)){  
printf("close fail!\\n");  
exit(0);  
}  
printf("创建后的信息已放入E盘根目录的“subjects.txt”文件中\\n");  
}

void savefileadd(SUB \*head)  
{  
SUB \*p;  
FILE \*fp;  
if((fp=fopen("e:\\\\subjectsadd.txt","w"))==NULL){  
printf("open fail!\\n");  
exit(0);  
}  
fprintf(fp,"课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\\n");  
for(p=head;p;p=p-&gt;next)  
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\\n",p-&gt;num,p-&gt;name,p-&gt;kind,p-&gt;stime,p-&gt;ttime,p-&gt;etime,p-&gt;score,p-&gt;term);  
if(fclose(fp)){  
printf("close fail!\\n");  
exit(0);  
}  
printf("修改后的信息已放入E盘根目录的“subjectsadd.txt”文件中\\n");  
}

void savefiledel(SUB \*head)  
{  
SUB \*p;  
FILE \*fp;  
if((fp=fopen("e:\\\\subjectsdel.txt","w"))==NULL){  
printf("open fail\\n");  
exit(0);  
}  
fprintf(fp,"课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\\n");  
for(p=head;p;p=p-&gt;next)  
fprintf(fp,"%5d%12s%9s%9d%9d%11d%11d%7d\\n",p-&gt;num,p-&gt;name,p-&gt;kind,p-&gt;stime,p-&gt;ttime,p-&gt;etime,p-&gt;score,p-&gt;term);  
if(fclose(fp)){  
printf("close fail!\\n");  
exit(0);  
}  
printf("修改后的信息已放入E盘根目录的“subjectsdel.txt”文件中\\n");  
}

void prin(SUB \*head)  
{  
SUB \*ptr;  
if(head==NULL){  
printf("NO RECORDS!\\n");  
return;  
}  
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学期\\n");  
for(ptr=head;ptr;ptr=ptr-&gt;next)  
printf("%5d%12s%9s%9d%9d%11d%11d%7d\\n",ptr-&gt;num,ptr-&gt;name,ptr-&gt;kind,ptr-&gt;stime,ptr-&gt;ttime,ptr-&gt;etime,ptr-&gt;score,ptr-&gt;term);  
}

void search(SUB \*head)  
{  
int a,num;  
int t=1;  
char type\[10];  
char ch='a',ch1;  
SUB \*ptr;

while(ch!=' '){  
printf("若要按课程性质查找请输入1，若要按学分查找请输入2:\\n");  
scanf("%d",&a);  
switch(a){  
case 1:printf("请输入要查找的课程的性质:\\n");  
scanf("%s",type);  
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学期\\n");  
for(ptr=head;ptr;ptr=ptr-&gt;next)  
if(strcmp(type,ptr-&gt;kind)==0){  
printf("%5d%12s%9s%9d%9d%11d%11d%7d\\n",ptr-&gt;num,ptr-&gt;name,ptr-&gt;kind,ptr-&gt;stime,ptr-&gt;ttime,ptr-&gt;etime,ptr-&gt;score,ptr-&gt;term);  
t=0;  
}  
if(t) printf("未找到!\\n");  
t=1;  
break;  
case 2:printf("输入要查找的课程的学分\\n");  
scanf("%d",&num);  
printf("课程编号 课程名称 课程性质 总学时 授课学时 实践或上机学时 学分 开课学期\\n");  
for(ptr=head;ptr;ptr=ptr-&gt;next)  
if(ptr-&gt;score==num){  
printf("%5d%12s%9s%9d%9d%11d%11d%7d\\n",ptr-&gt;num,ptr-&gt;name,ptr-&gt;kind,ptr-&gt;stime,ptr-&gt;ttime,ptr-&gt;etime,ptr-&gt;score,ptr-&gt;term);  
t=0;  
}  
if(t) printf("未找到!\\n");  
t=1;

}  
printf("继续查找请按回车键，结束请按空格键:\\n");  
ch1=getchar(); //将回车键赋给CH1，否则CASE里面最后输入的回车键会赋给CH，因此用CH1填补。  
ch=getchar();  
}

}

SUB \*insert(SUB \*head)  
{  
SUB \*ptr,\*ptr2,\*subj;  
int size=sizeof(SUB);  
char ch='a',ch1;  
while(ch!=' '){  
subj=(SUB \*)malloc(size);  
ptr=subj;  
printf("输入要插入的课程信息:\\n");  
scanf("%d%s%s%d%d%d%d%d",&subj-&gt;num,subj-&gt;name,subj-&gt;kind,&subj-&gt;stime,&subj-&gt;ttime,&subj-&gt;etime,&subj-&gt;score,&subj-&gt;term);  
if(head==NULL){  
head=ptr;  
head-&gt;next=NULL;  
}  
else{  
for(ptr2=head;ptr2;ptr2=ptr2-&gt;next)  
if(ptr2-&gt;next==NULL){  
ptr2-&gt;next=subj;  
subj-&gt;next=NULL;  
break;  
}  
}  
printf("继续插入请按回车，结束请按空格:\\n");  
ch1=getchar(); //将回车键赋给CH1，否则subj-&gt;term输完后输入的回车键会赋给CH，因此用CH1填补。  
ch=getchar();  
}  
return head;  
}

SUB \*del(SUB \*head)  
{  
SUB \*p1,\*p2;  
char ch='a',ch1;  
int num;  
while(ch!=' '){  
printf("输入想要删除的课程编号:\\n");  
scanf("%d",&num);  
if(head-&gt;num==num){  
p2=head;  
head=head-&gt;next;  
free(p2);  
}  
if(head==NULL)  
return NULL;  
p1=head;  
p2=head-&gt;next;  
while(p2){  
if(p2-&gt;num==num){  
p1-&gt;next=p2-&gt;next;  
free(p2);  
}  
else p1=p2;  
p2=p1-&gt;next;  
}  
printf("继续删除请按回车，结束请按空格:\\n");  
ch1=getchar(); //将回车键赋给CH1，否则num输完后再输入的回车键会赋给CH，因此用CH1填补。  
ch=getchar();  
}

return head;  
}

void choose(SUB \*head)  
{  
SUB \*p,\*q;  
int a\[5];  
int num,total=0,i=0,j;  
printf("输入要选修的课程的编号，编号之间以空格分开，输完后以0结束\\n");  
scanf("%d",&num);  
while(num!=0){  
for(p=head;p;p=p-&gt;next)  
if(p-&gt;num==num){  
total=total+p-&gt;score;  
a\[i]=num;  
i++;  
}  
scanf("%d",&num);  
}  
if(total&lt;60) printf("选修总学分未达到60,选修失败!\\n");  
else {  
printf("选修成功!\\n");  
printf("您选修的课程为:\\n");  
for(j=0;j&lt;i;j++)  
for(q=head;q;q=q-&gt;next)  
if(q-&gt;num==a\[j])  
printf("%s ",q-&gt;name);  
printf("\\n");  
}  
}

void readfile() //将文件重新读出  
{  
int num,stime,ttime,etime;  
int score,term;  
char c,name\[20],kind\[10];  
FILE \*fp=fopen("e:\\\\subjects.txt","r");  
while(!feof(fp))  
{  
if((c=fgetc(fp))=='\\n')  
break;  
}  
printf("课程编号 课程名称 课程性质 总学时 授课学时 实验或上机学时 学分 开课学期\\n");  
while(!feof(fp))  
{  
fscanf(fp,"%d%s%s%d%d%d%d%d\\n",&num,name,kind,&stime,&ttime,&etime,&score,&term);  
printf("%5d%12s%9s%9d%9d%11d%11d%7d\\n",num,name,kind,stime,ttime,etime,score,term);  
}  
fclose(fp);  
}