一、实验内容:
用C语言编写代码实现以下内容要求:
(1) 输入10个学生记录,其中软件技术专业5人,人工智能专业5人,并存入文件student.dat中;
(2) 输入3门课程(数据库、数据结构、程序设计)信息记录,并存入文件course.dat中;
(3) ) 输入上述10位同学分别选修上述三门课程的考试成绩到文件courseGrade.dat中;
(4) 从文件student.dat中读出学生信息,生成按照学号升序排列的单向链表,并在屏幕上显示输出;
(5) ) 从文件course.dat中读出课程信息,生成按照课程号升序排列的单向链表,并在屏幕上显示输出;
(6) 从文件courseGrade.dat中读出成绩信息,生成按照学号和课程号升序排列的单链表,并在屏幕上显示输出;
(7) 查询所有学生所有课程的考试成绩,生成该课程的成绩单链表,要求包括学号、学生姓名、专业、课程名、考试成绩等信息,按照学号升序,同一考生成绩降序排列,并将学生的该成绩信息输出到文件studentGrade.dat中,同时在屏幕上显示输出;
(8) 在(7)的链表中,查询指定课程号的所有学生的考试成绩,生成该课程的成绩单链表,要求包括学号、学生姓名、专业、课程名、考试成绩等信息,按照考试成绩降序排列输出到屏幕上显示;
(9) 在(7)的链表中,查询指定课程号的考试成绩小于60分的学生成绩信息,生成该课程的成绩链表,要求包括学号、学生姓名、专业、课程名、考试成绩等信息,并按照考试成绩降序排列在屏幕上显示输出;
(10) 使用栈实现将(4)的单链表中的学生信息逆序生存新的链表;
二、代码:
#include
#include
#include
#define MaxFileNames 20
//定义学生单链表结点类型
typedef struct student //定义数据结点类型
{ char sno[12]; //学号
char sname[10]; //姓名
char sex[4]; //性别
char major[20]; //专业
struct student *next; //指向后继数据结点
} StudentList;
typedef struct stable //定义学生表
{ int rows; //记录学生表行数
int cols; //记录学生表列数
StudentList *snode; //学生信息结点
} Stable;
//定义课程单链表中数据结点类型
typedef struct course //定义数据结点类型
{ char cno[10]; //课程号
char cname[20]; //课程名称
int classHours; //课时数
struct course *next; //指向后继结点
} CourseList;
typedef struct ctable //定义课程表
{ int rows;
int cols;
CourseList *cnode;
} Ctable;
//成绩单中数据结点类型定义
typedef struct grade //定义数据结点类型
{ char sno[12]; //学号
char cno[10]; //课号
int score; //分数
struct grade *next; //指向后继数据结点
} GradeList;
typedef struct gtable //定义成绩表
{ int rows;
int cols;
GradeList *gnode; //成绩信息结点
}Gtable;
typedef struct completdat
{ char sno[12]; //学号
char sname[10]; //姓名
char sex[4]; //性别
char major[20]; //专业
char cname1[20]; //课程名称
int score1; //分数
char cname2[20]; //课程名称
int score2; //分数
char cname3[20]; //课程名称
int score3; //分数
struct completdat *next;
}Completdat;
//定义链栈
typedef struct linknode
{ char sno[12]; //学号
char sname[10]; //姓名
char sex[4]; //性别
char major[20]; //专业
struct linknode *next;
}LinkStNode;
Stable *Shead;
Ctable *Chead;
Gtable *Ghead;
Completdat *Cplhead;
StudentList *SortedStudentList;
char filename[MaxFileNames];
void CreateStudentList() //创建学生链表,先将信息存在链表中
{ int i=0;
StudentList *p,*r;
Shead=(Stable *)malloc(sizeof(Stable));
Shead->snode=(StudentList *)malloc(sizeof(StudentList));
r=Shead->snode ; r->next =NULL;
Shead->cols = 0;
for(i;i<10;i++)
{ int cols=0;
p=(StudentList *)malloc(sizeof(StudentList));
printf("请输入第%d个学生的学号:",i+1);
scanf("%s",&p->sno); cols++;
printf("请输入第%d个学生的姓名:",i+1);
scanf("%s",&p->sname ); cols++;
printf("请输入第%d个学生的性别:",i+1);
scanf("%s",&p->sex ); cols++;
printf("请输入第%d个学生的专业:",i+1);
scanf("%s",&p->major ); cols++;
if(Shead->cols cols =cols;
Shead->rows =i+1;
r->next =p;
r =p;
}
r->next = NULL;
printf("\n");
}
void CreateAndStoreCourseList() //输入三门课程信息,存入course.dat
{ int i=0;
CourseList *p,*r;
Chead=(Ctable *)malloc(sizeof(Ctable));
Chead->cnode=(CourseList *)malloc(sizeof(CourseList));
r=Chead->cnode ; r->next =NULL;
Chead->cols = 0;
for(i;i<3;i++)
{ int cols=0;
p=(CourseList *)malloc(sizeof(CourseList));
printf("\n\n");
printf("请输入第%d门课的课程号:",i+1);
scanf("%s",&p->cno ); cols++;
printf("请输入第%d门课的名称:",i+1);
scanf("%s",&p->cname ); cols++;
printf("请输入第%d门课的课时数:",i+1);
scanf("%d",&p->classHours ); cols++;
if(Chead->cols cols =cols;
Chead->rows =i+1;
r->next =p;
r =p;
}
r->next = NULL;
printf("\n");
//将以Chead->cnode为首指针的链表读入到文件course.dat中
FILE *fp;
int j=0;
char filename[MaxFileNames];
strcpy(filename,"course.dat");
fp=fopen(filename,"w");
p=Chead->cnode->next ;
for(j;j<3;j++)
{ fprintf(fp,"%s %s %d ",p->cno ,p->cname ,p->classHours );
p=p->next;
}
fclose(fp); //关闭文件
fp=NULL;
}
void PrintStudentList()
{ int i=0;
StudentList *p;
p=Shead->snode->next ;
printf("学号 |姓名 |性别 |专业 ");
while(p!=NULL)
{ printf("\n%-16s %-10s %-8s %-10s",p->sno ,p->sname ,p->sex ,p->major );
p=p->next;
}
printf("\n************************************************************************");
}
void CreateFileStudentInfor() //创建student.dat文件,并将链表信息写入该文件
{ FILE *fp;
int i=0;
strcpy(filename,"student.dat");
fp=fopen(filename,"w");
StudentList *p;
p=Shead->snode->next ;
for(i;irows ;i++)
{ fprintf(fp,"%s %s %s %s ",p->sno ,p->sname ,p->sex ,p->major );
p=p->next;
}
fclose(fp); //关闭文件
fp=NULL;
}
void ReadFileStudentInforAndPrint() //从student.dat中读取信息,并输出排好序的链表
{
FILE *fp;
strcpy(filename,"student.dat");
fp = fopen(filename,"r");
if (fp==NULL)
printf("文件为空,读取失败,请先创建文件或重新尝试");
int i=0;
StudentList *L,*s,*r;
L=(StudentList *)malloc(sizeof(StudentList));r=L;
for (i;irows;i++)
{ s=(StudentList *)malloc(sizeof(StudentList));
fscanf(fp,"%s%s%s%s",&s->sno ,&s->sname ,&s->sex ,&s->major );
r->next =s;
r=s;
}
r->next = NULL;fclose(fp);
//StudentList型链表的快速排序号(按照学号排序)
StudentList *L2,*r2,*s2,*t2,*q2,*r3,*s3;
SortedStudentList=(StudentList *)malloc(sizeof(StudentList));
q2=SortedStudentList;q2->next =NULL;
int j=0;char MinSnode[12];
for(j;jrows ;j++)
{ s2=L;r2=L->next;
strcpy(MinSnode,r2->sno );
while(r2 !=NULL)
{ if(strcmp(MinSnode,r2->sno )>=0)
{ strcpy(MinSnode,r2->sno );
r3=r2; s3=s2;
r2=r2->next ;s2=s2->next ;
}
else
{ r2=r2->next ;s2=s2->next ;
}
}
t2=(StudentList *)malloc(sizeof(StudentList));
strcpy(t2->sno ,MinSnode);strcpy(t2->sname ,r3->sname );
strcpy(t2->sex ,r3->sex ); strcpy(t2->major ,r3->major );
q2->next =t2;q2=q2->next ;q2->next =NULL;
s3->next =r3->next ;
free(r3);
}
//输出按学号排好序的链表
L2=SortedStudentList;
printf("\n\n学号 |姓名 |性别 |专业 ");
while(L2->next !=NULL)
{ printf("\n%-16s %-10s %-8s %-10s",L2->next->sno ,L2->next->sname ,L2->next->sex ,L2->next->major );
L2=L2->next;
}
printf("\n************************************************************************");
}
void ReadFileCourseInforAndPrint() //从course.dat中读取信息,并输出排好序的链表
{
FILE *fp;char filename[MaxFileNames];
strcpy(filename,"course.dat");
fp = fopen(filename,"r");
if (fp==NULL)
printf("文件为空,读取失败,请先创建文件或重新尝试");
int i=0;
CourseList *L,*s,*r;
L=(CourseList *)malloc(sizeof(CourseList));r=L;
for (i;irows ;i++)
{ s=(CourseList *)malloc(sizeof(CourseList));
fscanf(fp,"%s%s%d",&s->cno ,&s->cname ,&s->classHours );
r->next =s;
r=s;
}
r->next = NULL;fclose(fp);
//CourseList型链表的快速排序号(按照学号排序)
CourseList *L2,*r2,*s2,*t2,*q2,*r3,*s3;
L2=(CourseList *)malloc(sizeof(CourseList));
q2=L2;q2->next =NULL;int MinClasshours;
for(int j=0;jrows;j++)
{ s2=L;r2=L->next;
MinClasshours=r2->classHours;
while(r2 !=NULL)
{ if(MinClasshours>=r2->classHours )
{ MinClasshours=r2->classHours;
r3=r2; s3=s2;
r2=r2->next ;s2=s2->next ;
}
else
{ r2=r2->next ;s2=s2->next ;
}
}
t2=(CourseList *)malloc(sizeof(CourseList));
t2->classHours=MinClasshours;strcpy(t2->cname ,r3->cname );
strcpy(t2->cno ,r3->cno );
q2->next =t2;q2=q2->next ;q2->next =NULL;
s3->next =r3->next ;
free(r3);
}
//输出按学号排好序的链表
printf("\n\n课程号 |名称 |课时数 ");
while(L2->next !=NULL)
{ printf("\n%-16s %-16s %-9d",L2->next->cno ,L2->next->cname ,L2->next->classHours );
L2=L2->next;
}
printf("\n************************************************************************");
}
//问题三 输入上述十位同学的三门选修成绩到courseGrade.dat
void CreatcourseGradedat()
{ Ghead=(Gtable *)malloc(sizeof(Gtable));
Ghead->gnode =(GradeList *)malloc(sizeof(GradeList));
StudentList *p; CourseList *q; GradeList *g,*s;
p=Shead->snode; q=Chead->cnode; g=Ghead->gnode;
if(p->next == NULL)
{ printf("操作错误:不存在学生信息");
return ;
}
if(q->next == NULL )
{ printf("操作错误:不存在课程信息");
return ;
}
Ghead->rows =0;Ghead->cols =3;
for(int i=0;irows ;i++)
{ q=Chead->cnode;int score;
printf("\n请输入第%d位学生:%s的各项选修课成绩",i+1,p->next->sname);
printf("\n%s成绩:",q->next->cname);//录第一门选修课成绩
scanf("%d",&score);
s=(GradeList *)malloc(sizeof(GradeList));
strcpy(s->sno ,p->next->sno );strcpy(s->cno ,q->next->cno );s->score = score;q=q->next;
g->next =s;g=s;Ghead->rows ++;
printf("\n%s成绩:",q->next->cname);//录第二门选修课成绩
scanf("%d",&score);
s=(GradeList *)malloc(sizeof(GradeList));
strcpy(s->sno ,p->next->sno );strcpy(s->cno ,q->next->cno );s->score = score;q=q->next;
g->next =s;g=s;Ghead->rows ++;
printf("\n%s成绩:",q->next->cname);//录第三门选修课成绩
scanf("%d",&score);
s=(GradeList *)malloc(sizeof(GradeList));
strcpy(s->sno ,p->next->sno );strcpy(s->cno ,q->next->cno );s->score = score;q=q->next;
g->next =s;g=s;Ghead->rows ++;
p=p->next ;
} g->next =NULL;
//创建文件courseGrade.dat,并写入上述信息
g=Ghead->gnode->next;
FILE *fp;
int j=0;
strcpy(filename,"courseGrade.dat");
fp=fopen(filename,"w");
for(j;jrows;j++)
{ fprintf(fp,"%s %s %d ",g->sno ,g->cno ,g->score );
g=g->next;
}
fclose(fp); //关闭文件
fp=NULL;
}
void ReadFIlecourseGradedatAndPrint() //读取courseGrade.dat并排序输出
{ GradeList *p,*r,*s,*p2,*r2,*s2,*r3,*s3,*t2,*L;
p=(GradeList *)malloc(sizeof(GradeList));
r=p;
FILE *fp;
strcpy(filename,"courseGrade.dat");
fp=fopen(filename,"r");
for(int j=0;jrows;j++)
{ s=(GradeList *)malloc(sizeof(GradeList));
fscanf(fp,"%s%s%d ",&s->sno ,&s->cno ,&s->score );
r->next=s;r=s;
}r->next =NULL;
fclose(fp); //关闭文件
L=(GradeList *)malloc(sizeof(GradeList));p2=L;
for(int k=0;krows ;k++)
{ s2=p;r2=p->next;char Minsno[12],Mincno[12];
strcpy(Mincno,r2->cno);strcpy(Minsno,r2->sno);
while(r2!=NULL)
{ if(strcmp(Mincno,r2->cno )>0)
{ strcpy(Mincno,r2->cno );strcpy(Minsno,r2->sno );
r3=r2; s3=s2;
r2=r2->next ;s2=s2->next ;
}
else if(strcmp(Mincno,r2->cno )==0)
{ if(strcmp(Minsno,r2->sno )>=0)
{ strcpy(Mincno,r2->cno );strcpy(Minsno,r2->sno );
r3=r2; s3=s2;
r2=r2->next ;s2=s2->next ;
}
else
{
r2=r2->next ;s2=s2->next ;
}
}
else
{ r2=r2->next ;s2=s2->next ;
}
}
t2=(GradeList *)malloc(sizeof(GradeList));
strcpy(t2->sno ,r3->sno );strcpy(t2->cno ,r3->cno);t2->score=r3->score ;
p2->next =t2;p2=t2 ;
s3->next =r3->next ;
free(r3);
}p2->next =NULL;
//输出排好序的列表GradeList
p2=L->next ;
printf("\n\n**************************************");
printf("\n学号 |课程号 |分数 ");
while(p2!=NULL)
{ printf("\n%-12s %-12s %-12d",p2->sno ,p2->cno ,p2->score ); p2=p2->next;
}
printf("\n**************************************");
}
void CreatstudentGradedat() //第七个问题
{ Cplhead = (Completdat*)malloc(sizeof(Completdat));
if(Shead->snode->next==NULL||Chead->cnode->next==NULL||Ghead->gnode->next ==NULL)
{printf("系统错误,请先录入学生信息!");
return ;}
StudentList *s; Completdat *cpl,*t; CourseList *c ; GradeList *g;
s=Shead->snode->next; cpl=Cplhead; c=Chead->cnode->next ; g=Ghead->gnode->next;
while(s!=NULL)
{
t=(Completdat*)malloc(sizeof(Completdat));
strcpy(t->sno,s->sno);strcpy(t->sname ,s->sname );
strcpy(t->sex,s->sex);strcpy(t->major ,s->major);
while(strcmp(s->sno,g->sno)!=0)
{ g=g->next; }
while(strcmp(g->cno,c->cno)!=0)
{ c=c->next; }
strcpy(t->cname1,c->cname); t->score1=g->score; c=Chead->cnode->next ;
while(strcmp(g->next->cno,c->cno)!=0)
{ c=c->next;}
strcpy(t->cname2,c->cname); t->score2=g->next->score; c=Chead->cnode->next ;
while(strcmp(g->next->next->cno,c->cno)!=0)
{ c=c->next;}
strcpy(t->cname3,c->cname); t->score3=g->next->next->score; c=Chead->cnode->next ;
g=Ghead->gnode->next;
cpl->next=t;cpl=t;s=s->next;
}
cpl->next=NULL;
//将头节点为Cplhead的 Completdat链表排序生成新的以Ncplhead为头节点的Completdat链表
Completdat *Ncplhead,*Ncpl,*NNcplhead,*NNcpl,*p1,*p2,*p3,*p4;
Ncplhead=(Completdat*)malloc(sizeof(Completdat));
NNcplhead=(Completdat*)malloc(sizeof(Completdat));
Ncpl=Ncplhead; cpl=Cplhead->next; NNcpl=NNcplhead;
//将成绩按降序排列
for(cpl;cpl!=NULL;cpl=cpl->next)
{
t=(Completdat*)malloc(sizeof(Completdat));
strcpy(t->sno,cpl->sno); strcpy(t->sname ,cpl->sname ); strcpy(t->sex,cpl->sex); strcpy(t->major,cpl->major);
if(cpl->score1>=cpl->score2&&cpl->score2>=cpl->score3)
{ strcpy(t->cname1,cpl->cname1); strcpy(t->cname2,cpl->cname2); strcpy(t->cname3,cpl->cname3);
t->score1 = cpl->score1 ; t->score2 = cpl->score2 ; t->score3 = cpl->score3 ;
}
else if(cpl->score1>=cpl->score3&&cpl->score3>=cpl->score2)
{ strcpy(t->cname1,cpl->cname1); strcpy(t->cname2,cpl->cname3); strcpy(t->cname3,cpl->cname2);
t->score1 = cpl->score1 ; t->score2 = cpl->score3; t->score3 = cpl->score2 ;
}
else if(cpl->score2>=cpl->score1&&cpl->score1>=cpl->score3)
{strcpy(t->cname1,cpl->cname2); strcpy(t->cname2,cpl->cname1); strcpy(t->cname3,cpl->cname3);
t->score1 = cpl->score2 ; t->score2 = cpl->score1 ; t->score3 = cpl->score3 ;
}
else if(cpl->score2>=cpl->score3&&cpl->score3>=cpl->score1)
{strcpy(t->cname1,cpl->cname2); strcpy(t->cname2,cpl->cname3); strcpy(t->cname3,cpl->cname1);
t->score1 = cpl->score2 ; t->score2 = cpl->score3 ; t->score3 = cpl->score1 ;
}
else if(cpl->score3>=cpl->score1&&cpl->score1>=cpl->score2)
{strcpy(t->cname1,cpl->cname3); strcpy(t->cname2,cpl->cname1); strcpy(t->cname3,cpl->cname2);
t->score1 = cpl->score3 ; t->score2 = cpl->score1 ; t->score3 = cpl->score2 ;
}
else if(cpl->score3>=cpl->score2&&cpl->score2>=cpl->score1)
{strcpy(t->cname1,cpl->cname3); strcpy(t->cname2,cpl->cname2); strcpy(t->cname3,cpl->cname1);
t->score1 = cpl->score3 ; t->score2 = cpl->score2 ; t->score3 = cpl->score1 ;
}
Ncpl->next=t;
Ncpl=t;
}
Ncpl->next =NULL;
cpl=Ncplhead->next; p1=Ncplhead;p2=Ncplhead->next;
while(cpl!=NULL)
{ t=(Completdat*)malloc(sizeof(Completdat));
char Minsno[12];
p1=Ncplhead;p2=Ncplhead->next;
strcpy(Minsno,p2->sno);
for(p2;p2!=NULL;p2=p2->next) //遍历全部链表找到最小学号所在结点
{ if(strcmp(Minsno,p2->sno)>=0)
{strcpy(Minsno,p2->sno);p4=p2;p3=p1;p1=p1->next;
}
else
{ p1=p1->next;
}
}
strcpy(t->sno,p4->sno); strcpy(t->sname ,p4->sname ); strcpy(t->sex,p4->sex); strcpy(t->major,p4->major);
strcpy(t->cname1,p4->cname1); strcpy(t->cname2,p4->cname2); strcpy(t->cname3,p4->cname3);
t->score1 = p4->score1 ; t->score2 = p4->score2 ; t->score3 = p4->score3 ;
NNcpl->next=t;
NNcpl=t;
p3->next = p4->next;
free(p4);
cpl=Ncplhead->next;
}
NNcpl->next =NULL;
//创建文件studentGrade.dat,并写入上述信息
NNcpl=NNcplhead->next;
FILE *fp;
strcpy(filename,"studentGrade.dat");
fp=fopen(filename,"w");
for(NNcpl;NNcpl!=NULL;NNcpl=NNcpl->next)
{ fprintf(fp,"%s %s %s %s %s %d %s %d %s %d ",NNcpl->sno,NNcpl->sname,NNcpl->sex
,NNcpl->major,NNcpl->cname1,NNcpl->score1,NNcpl->cname2,NNcpl->score2,NNcpl->cname3,NNcpl->score3);
}
fclose(fp); //关闭文件
fp=NULL;
//在屏幕上输出
NNcpl=NNcplhead->next;
printf("\n\n******************************************************************************************");
printf("\n学号 ||姓名 ||性别 ||专业 ||课程一 ||课程一分数 ||课程二 ||课程二分数 ||课程三 ||课程三分数 ");
for(NNcpl;NNcpl!=NULL;NNcpl=NNcpl->next)
{ printf("\n%12s||%12s||%12s||%12s||%-12s||%-12d||%-12s||%-12d||%-12s||%-12d",NNcpl->sno ,
NNcpl->sname,NNcpl->sex,NNcpl->major,NNcpl->cname1,NNcpl->score1,NNcpl->cname2,NNcpl->score2,NNcpl->cname3,NNcpl->score3);
}
return;
}
void CreatCourseGradeList() //第八题,查找一门课程的成绩,并按降序排列
{ char cno[12],cname[20];
int i=0;
printf("\n\n请输入所要查询课程成绩的课程号:");
scanf("%s",&cno);
CourseList *C;
C=Chead->cnode->next;
//判断是否已录入该课程号
for(C;C!=NULL;C=C->next)
{ if(strcmp(C->cno,cno)==0)
{ i++;
strcpy(cname,C->cname);
}
}
if(i==0)
{ printf("\n未找到该课程号,请先录入或重新尝试\n\n");
return ;
}
//将所要查找的课程转录到新的以L1为头指针的Completdat链表中,所查找的课程存在cname1中
Completdat *p,*L1,*t1,*s1;
p=Cplhead->next;
L1=(Completdat*)malloc(sizeof(Completdat));
t1=L1;
for(p;p!=NULL;p=p->next)
{ s1=(Completdat *)malloc(sizeof(Completdat));
strcpy(s1->sno,p->sno); strcpy(s1->sname,p->sname);
strcpy(s1->sex,p->sex); strcpy(s1->major,p->major);
if(strcmp(p->cname1,cname)==0)
{ strcpy(s1->cname1,p->cname1); s1->score1=p->score1;
strcpy(s1->cname2,p->cname2); s1->score2=p->score2;
strcpy(s1->cname3,p->cname3); s1->score3=p->score3;
}
else if(strcmp(p->cname2,cname)==0)
{ strcpy(s1->cname1,p->cname2); s1->score1=p->score2;
strcpy(s1->cname2,p->cname3); s1->score2=p->score3;
strcpy(s1->cname3,p->cname1); s1->score3=p->score1;
}
else if(strcmp(p->cname3,cname)==0)
{ strcpy(s1->cname1,p->cname3); s1->score1=p->score3;
strcpy(s1->cname2,p->cname2); s1->score2=p->score2;
strcpy(s1->cname3,p->cname1); s1->score3=p->score1;
}
else
{ printf("\n系统错误,请重试!\n\n");
}
t1->next=s1;
t1=s1;
} t1->next=NULL;
//将以L1为头指针的链表按照score1降序排列转存在以L2为头指针的Complet链表中,并销毁L1链表
Completdat *p1,*p2,*p3,*p4,*L2,*t2,*s2;
int Maxscore;
p=L1->next;
p1=L1;
p2=L1->next;
L2=(Completdat*)malloc(sizeof(Completdat));
t2=L2;
Maxscore=p2->score1;
while(p!=NULL)
{ p1=L1;
p2=L1->next;
Maxscore=p2->score1;
s2=(Completdat *)malloc(sizeof(Completdat));
for(p2;p2!=NULL;p2=p2->next)
{ if(p2->score1>=Maxscore)
{ Maxscore=p2->score1;
p4=p2; p3=p1;
p1=p1->next;
}
else
{ p1=p1->next;
}
}
strcpy(s2->sno,p4->sno); strcpy(s2->sname,p4->sname);
strcpy(s2->sex,p4->sex); strcpy(s2->major,p4->major);
strcpy(s2->cname1 ,p4->cname1); s2->score1 = p4->score1;
strcpy(s2->cname2 ,p4->cname2); s2->score2 = p4->score2;
strcpy(s2->cname3 ,p4->cname3); s2->score3 = p4->score3;
p3->next=p4->next;
free(p4);
t2->next=s2;
t2=s2;
p=L1->next;
}
t2->next=NULL;
printf("\n******************************************************************************************");
printf("\n学号 ||姓名 ||性别 ||专业 ||课程 ||课程分数 ");
p=L2->next;
for(p;p!=NULL;p=p->next)
{ printf("\n%12s||%12s||%12s||%12s||%-12s||%-12d",p->sno ,p->sname,p->sex,p->major,p->cname1,p->score1);
}
return;
}
void SearchScore() //查询指定课程号的考试成绩小于60分的学生成绩信息
{ char cno[12],cname[20];
int i=0;
printf("\n\n请输入所要查询课程成绩的课程号:");
scanf("%s",&cno);
CourseList *C;
C=Chead->cnode->next;
//判断是否已录入该课程号
for(C;C!=NULL;C=C->next)
{ if(strcmp(C->cno,cno)==0)
{ i++;
strcpy(cname,C->cname);
}
}
if(i==0)
{ printf("\n未找到该课程号,请先录入或重新尝试\n\n");
return ;
}
//将所要查找的课程转录到新的以L1为头指针的Completdat链表中,所查找的课程存在cname1中
Completdat *p,*L1,*t1,*s1;
p=Cplhead->next;
L1=(Completdat*)malloc(sizeof(Completdat));
t1=L1;
for(p;p!=NULL;p=p->next)
{ s1=(Completdat *)malloc(sizeof(Completdat));
if(strcmp(p->cname1,cname)==0&&p->score1<60)
{ strcpy(s1->sno,p->sno); strcpy(s1->sname,p->sname);
strcpy(s1->sex,p->sex); strcpy(s1->major,p->major);
strcpy(s1->cname1,p->cname1); s1->score1=p->score1;
strcpy(s1->cname2,p->cname2); s1->score2=p->score2;
strcpy(s1->cname3,p->cname3); s1->score3=p->score3;
t1->next=s1;
t1=s1;
}
else if(strcmp(p->cname2,cname)==0&&p->score2<60)
{ strcpy(s1->sno,p->sno); strcpy(s1->sname,p->sname);
strcpy(s1->sex,p->sex); strcpy(s1->major,p->major);
strcpy(s1->cname1,p->cname2); s1->score1=p->score2;
strcpy(s1->cname2,p->cname3); s1->score2=p->score3;
strcpy(s1->cname3,p->cname1); s1->score3=p->score1;
t1->next=s1;
t1=s1;
}
else if(strcmp(p->cname3,cname)==0&&p->score3<60)
{ strcpy(s1->sno,p->sno); strcpy(s1->sname,p->sname);
strcpy(s1->sex,p->sex); strcpy(s1->major,p->major);
strcpy(s1->cname1,p->cname3); s1->score1=p->score3;
strcpy(s1->cname2,p->cname2); s1->score2=p->score2;
strcpy(s1->cname3,p->cname1); s1->score3=p->score1;
t1->next=s1;
t1=s1;
}
} t1->next=NULL;
//将以L1为头指针的链表按照score1降序排列转存在以L2为头指针的Complet链表中,并销毁L1链表
Completdat *p1,*p2,*p3,*p4,*L2,*t2,*s2;
int Maxscore;
p=L1->next;
p1=L1;
p2=L1->next;
L2=(Completdat*)malloc(sizeof(Completdat));
t2=L2;
Maxscore=p2->score1;
while(p!=NULL)
{ p1=L1;
p2=L1->next;
Maxscore=p2->score1;
s2=(Completdat *)malloc(sizeof(Completdat));
for(p2;p2!=NULL;p2=p2->next)
{ if(p2->score1>=Maxscore)
{ Maxscore=p2->score1;
p4=p2; p3=p1;
p1=p1->next;
}
else
{ p1=p1->next;
}
}
strcpy(s2->sno,p4->sno); strcpy(s2->sname,p4->sname);
strcpy(s2->sex,p4->sex); strcpy(s2->major,p4->major);
strcpy(s2->cname1 ,p4->cname1); s2->score1 = p4->score1;
strcpy(s2->cname2 ,p4->cname2); s2->score2 = p4->score2;
strcpy(s2->cname3 ,p4->cname3); s2->score3 = p4->score3;
p3->next=p4->next;
free(p4);
t2->next=s2;
t2=s2;
p=L1->next;
}
t2->next=NULL;
printf("\n******************************************************************************************");
printf("\n学号 ||姓名 ||性别 ||专业 ||课程 ||课程分数 ");
p=L2->next;
for(p;p!=NULL;p=p->next)
{ printf("\n%12s||%12s||%12s||%12s||%-12s||%-12d",p->sno ,p->sname,p->sex,p->major,p->cname1,p->score1);
}
return;
}
//用栈实现(4)中学生信息表的逆序
void ReverseStudentListOrder()
{ LinkStNode *Stackhead,*s,*t;
Stackhead=(LinkStNode *)malloc(sizeof(LinkStNode));
Stackhead->next = NULL;
StudentList *L;
L=SortedStudentList->next;
for(L;L!=NULL;L=L->next)
{ t=(LinkStNode *)malloc(sizeof(LinkStNode));
strcpy(t->sno,L->sno); strcpy(t->sname,L->sname);
strcpy(t->sex,L->sex); strcpy(t->major,L->major);
t->next = Stackhead->next;
Stackhead->next=t;
}
//输出栈
s=Stackhead;
printf("\n\n学号 |姓名 |性别 |专业 ");
while(s->next !=NULL)
{ printf("\n%-16s %-10s %-8s %-10s",s->sno ,s->sname ,s->sex ,s->major );
s=s->next;
}
printf("\n************************************************************************");
}
int main()
{ Done:printf("\n*************************************************************************************************************************");
printf("\n* 1. 输入10个学生记录,其中软件技术专业5人,人工智能专业5人,并存入文件student.dat中 ; *");
printf("\n* 2. 输入3门课程(数据库、数据结构、程序设计)信息记录,并存入文件course.dat中; *");
printf("\n* 3. 输入上述10位同学分别选修上述三门课程的考试成绩到文件courseGrade.dat中 *");
printf("\n* 4. 从文件student.dat中读出学生信息,生成按照学号升序排列的单向链表,并在屏幕上显示输出 *");
printf("\n* 5. 从文件course.dat中读出课程信息,生成按照课程号升序排列的单向链表,并在屏幕上显示输出 *");
printf("\n* 6. 从文件courseGrade.dat中读出成绩信息,生成按照学号和课程号升序排列的单链表,并在屏幕上显示输出 *");
printf("\n* 7. 查询所有学生所有课程的考试成绩,生成该课程的成绩单链表,要求包括学号、学生姓名、专业、课程名、考试成绩等信息, *");
printf("\n* 按照学号升序,同一考生成绩降序排列,并将学生的该成绩信息输出到文件studentGrade.dat中,同时在屏幕上显示输出 *");
printf("\n* 8. 在(7)的链表中,查询指定课程号的所有学生的考试成绩,生成该课程的成绩单链表,要求包括学号、学生姓名、专业、课程 *");
printf("\n* 名、考试成绩等信息,按照考试成绩降序排列输出到屏幕上显示 *");
printf("\n* 9. 在(7)的链表中,查询指定课程号的考试成绩小于60分的学生成绩信息,生成该课程的成绩链表,要求包括学号、学生姓名、 *");
printf("\n*专业、课程名、考试成绩等信息,并按照考试成绩降序排列在屏幕上显示输出; *");
printf("\n* 10.使用栈实现将(4)的单链表中的学生信息逆序生存新的链表。 *");
printf("\n* 0. 退出程序 *");
printf("\n*************************************************************************************************************************");
printf("\n\n请输入操作序号:");
int Menu;
scanf("%d",&Menu);
switch(Menu)
{ case 1: CreateStudentList() ; PrintStudentList(); CreateFileStudentInfor(); goto Done; break;
case 2: CreateAndStoreCourseList(); goto Done; break;
case 3: CreatcourseGradedat(); goto Done; break;
case 4: ReadFileStudentInforAndPrint(); goto Done; break;
case 5: ReadFileCourseInforAndPrint(); goto Done; break;
case 6: ReadFIlecourseGradedatAndPrint(); goto Done; break;
case 7: CreatstudentGradedat(); goto Done; break;
case 8: CreatCourseGradeList(); goto Done; break;
case 9: SearchScore(); goto Done; break;
case 10: ReverseStudentListOrder(); goto Done; break;
case 0: return 0; goto Done; break;
default: printf("操作序号无效,请重新尝试。"); goto Done; break;
}
return 0;
}