C语言课程设计报告
一、实践的目的和要求
加深对《C语言程序设计》课程所学知识的理解,进一步巩固C语言讲法规则。学会编制结构清晰、风格良好、数据结构适当的C语言程序,从而具备解决综合性实际问题的能力
二、实践内容
在熟练掌握C语言的基本知识:数据类型(整形、实型、字符型、指针、数组、结构等);运算类型(算术运算、逻辑运算、自增自减运算、赋值运算等);程序结构(顺序结构、判断选择结构、循环结构);函数的使用,结构体和文件的操作等。
三、设计说明
学生成绩管理系统
建立一个学生成绩管理系统。
程序运行时显示一个简单的菜单。
例如:
1.信息输入(INPUT)
2.信息的删除与修改
3.显示当前学生成绩的记录信息,若无记录,则给出提示信息
4.学生或者各个班级各门课程总分、平均分、最低分、最高分等的统计
5.可以按照班级、姓名、性别、分数(可以有区间)等方式进行查询
6.具有良好的交互功能
7.能使用图形函数进行简单的界面设计
8.具有良好的纠错功能
9.退出等等
程序:
#include /*文件操作(读文件)*/
#include
#include /*其它说明*/
#include /*字符串函数*/
#include /*内存操作函数*/
#include /*屏幕操作函数*/
#include /*字符操作函数*/
#include /*动态地址分配函数*/
#include
#define LEN sizeof(STUDENT)
#define N 3
typedef struct stu /*定义结构体数组用于缓存数据*/
{char grade[6];
char name[5];
char sex[4] ;
int score[N];
int sum;
float average;
int order;
struct stu *next;
}STUDENT;
/*以下是函数原型*/
STUDENT *init(); /*初始化函数*/
STUDENT *create(); /*创建链表*/
STUDENT *delete(STUDENT *head); /*删除记录*/
STUDENT *sort(STUDENT *head); /*按平均分排序*/
void print(STUDENT *head); /* 显示所有记录*/
void name(STUDENT *head); /*姓名查找*/
void grade(STUDENT *head); /*班级查找*/
void sex(STUDENT *head); /*性别查找*/
void average(STUDENT *head); /*按平均分查找在那个分数里*/
STUDENT *statistciam(STUDENT *head);/*统计记录*/
void save(STUDENT *head); /*保存文件*/
STUDENT *load(); /*读文件*/
STUDENT *insert(STUDENT *head,STUDENT *new); /*插入记录*/
int menu_select(); /*菜单函数*/
int menu_search(); /*查询菜单*/
void code(); /*密码验证*/
/*主函数界面*/
main()
{
int i,j,n,m;
STUDENT *head,new; /*链表定义头指针*/
randomize();/*开启扬声器,发出声音*/
while(!bioskey(1))
{
j=rand()*5000;
sound(j);
delay(10);
}
nosound();/*关闭声音文件*/
code();/*密码输入*/
clrscr(); /*清屏*/
for(;;) /*无限循环*/
{
switch(menu_select()) /*调用主菜单函数,返回值整数作开关语句的条件*/
{ /*值不同,执行的函数不同,break 不能省略*/
case 0:head=init();break; /*执行初始化*/
case 1:head=create();break; /*创建链表*/
case 2:head=delete(head);break; /*删除记录*/
case 3:print(head);break; /*显示全部记录*/
case 4:
loop:
printf("sreach:\n 1.grade\n 2.name\n 3.sex\n 4.average\n 5.quit\n Enter you choice(0~5):");
scanf("%d",&n);
switch(n)
{
case 1:grade(head);
goto loop;
case 2:name(head);
goto loop;
case 3:sex(head);
goto loop;
case 4:average(head);
goto loop;
case 5: break;
}
break;
/*查找记录*/
case 5:sort(head);break; /*排序*/
case 6:save(head);break; /*保存文件*/
case 7:head=load(); break; /*读文件*/
case 8:head=insert(head,&new); break; /*插入记录*/
case 9:head=statistciam(head); break; /*统计记录*/
case 10:exit(0); /*如菜单返回值为10程序结束*/
}
}
}
/*密码输入*/
void code()
{
int i;
char number[20];
const w=10;
printf("\n\t\t\t--------------------------------------------------");
printf("\n\t\t\tBecause this system is busywork,Code is: 123456");
printf("\n\t\t\t----------------------------------------------- ");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n\t*********************************");
printf("\n\t Please Enter The Code : ");
printf("\n\t*********************************\n");
scanf("%s",number);
printf("\n\t\t\tExamining!");
for(i=0;i<=w;i++)
{
printf("*");
}
if(strcmp(number,"123456")==0)
{
printf("\n\t\t\tCode is right!press any key enter menu......");
clrscr();
}
else
{
printf("\n\t\t\tCode is wrong!Please enter again : ");
clrscr();
code();
}
}
/*初始化函数*/
STUDENT *init()
{
return NULL; /*返回空指针*/
}
/*菜单选择函数*/
menu_select()
{
char *menu[]={"********************************************",
" Welcome to ",
" The student score manage system!",
"*****************MENU***********************", /*定义菜单字符串数组*/
" 0. Init list", /*初始化*/
" 1. Input list", /*输入记录*/
" 2. Delete a record from list", /*从表中删除记录*/
" 3. Print list ", /*显示当前学生成绩的记录信息*/
" 4. Search record ", /*查询学生记录*/
" 5. Sort to make new a file ", /*排序*/
" 6. Save the file", /*将单链表中记录保存到文件中*/
" 7. Load the file", /*从文件中读入记录*/
" 8. insert record to list ", /*插入记录到表中*/
" 9. statistciam record to list ", /*统计记录到表中*/
" 10. Quit", /*退出*/
"********************************************",
" Producer:ZengWenyan ZhaoJingxiu ",
" Faculty Adviser:XiaoFengrui ",}; /*退出*/
char s[3]; /*以字符形式保存选择号*/
int c,i; /*定义整形变量*/
textbackground(MAGENTA); /*设置背景颜色为粉色*/
gotoxy(1,50); /*移动光标*/
printf("press any key enter menu......\n"); /*压任一键进入主菜单*/
getch(); /*输入任一键*/
clrscr(); /*清屏幕*/
gotoxy(1,1); /*移动光标*/
textcolor(YELLOW); /*设置文本显示颜色为黄色*/
textbackground(BLUE); /*设置背景颜色为蓝色*/
gotoxy(15,2); /*移动光标*/
putch(0xc9); /*输出左上角边框┏*/
for(i=1;i<44;i++)
putch(0xcd); /*输出上边框水平线*/
putch(0xbb); /*输出右上角边框 ┓*/
for(i=3;i<23;i++)
{
gotoxy(15,i);putch(0xba); /*输出左垂直线*/
gotoxy(59,i);putch(0xba);
} /*输出右垂直线*/
gotoxy(15,22);putch(0xc8); /*输出左上角边框┗*/
for(i=1;i<44;i++)
putch(0xcd); /*输出下边框水平线*/
putch(0xbc); /*输出右下角边框┛*/
window(16,3,58,21); /* 制作显示菜单的窗口,大小根据菜单条数设计*/
clrscr(); /*清屏*/
for(i=0;i<18;i++) /*输出主菜单数组*/
{
gotoxy(1,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK); /*设置背景颜色为黑色*/
window(1,1,80,25); /*恢复原窗口大小*/
gotoxy(10,23); /*移动光标*/
do{
printf("\n Enter you choice(0~10):"); /*在菜单窗口外显示提示信息*/
scanf("%s",s); /*输入选择项*/
c=atoi(s); /*将输入的字符串转化为整形数*/
}while(c<0||c>10); /*选择项不在0~10之间重输*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
/*输入函数*/
STUDENT *create()
{int i,s;
STUDENT *head=NULL,*p; /* 定义函数.此函数带回一个指向链表头的指针*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
for(;;)
{p=(STUDENT *)malloc(LEN); /*开辟一个新的单元*/
if(!p) /*如果指针p为空*/
{printf("\nOut of memory."); /*输出内存溢出*/
return (head); /*返回头指针,下同*/
}
printf("Enter the grade(0:list end):");
scanf("%s",p->grade);
if(p->grade[0]=='0') break; /*如果班级首字符为0则结束输入*/
printf("Enter the name:");
scanf("%s",p->name);
do{
printf("Enter the sex(M or W):");
scanf("%s",&p->sex[0]);
if(p->sex[0]!='M'&&p->sex[0]!='W')
printf("Data error,please enter again.\n");
}while(p->sex[0]!='M'&&p->sex[0]!='W');
printf("Please enter the %d scores\n",3); /*提示开始输入成绩*/
s=0; /*计算每个学生的总分,初值为0*/
for(i=0;i
{
do{
printf("score%d:",i+1);
scanf("%d",&p->score[i]);
if(p->score[i]<0 || p->score[i]>100) /*确保成绩在0~100之间*/
printf("Data error,please enter again.\n");
}while(p->score[i]<0 || p->score[i]>100);
s=s+p->score[i]; /*累加各门成绩*/
}
p->sum=s; /*将总分保存*/
p->average=(float)s/3; /*先用强制类型转换将s转换成float型,再求平均值*/
p->order=0; /*未排序前此值为0*/
p->next=head; /*将头结点做为新输入结点的后继结点*/
head=p; /*新输入结点为新的头结点*/
}
return(head);
}
/*删除记录函数*/
STUDENT *delete(STUDENT *head)
{int n;
STUDENT *p1,*p2; /*p1为查找到要删除的结点指针,p2为其前驱指针*/
char c,s[6]; /*s[6]用来存放班级,c用来输入字母*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
printf("Please enter the deleted grade: ");
scanf("%s",s);
p1=p2=head; /*给p1和p2赋初值头指针*/
while(strcmp(p1->grade,s) && p1 != NULL) /*当记录的班级不是要找的,或指针不为空时*/
{p2=p1; /*将p1指针值赋给p2作为p1的前驱指针*/
p1=p1->next; /*将p1指针指向下一条记录*/
}
if(strcmp(p1->grade,s)==0) /*班级找到了*/
{printf("**************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p1->grade,p1->name,p1->score[0],p1->score[1],p1->score[2],p1->sum,p1->average,p1->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
printf("Are you sure to delete the student Y/N ?"); /*提示是否要删除,输入Y删除,N则退出*/
for(;;)
{scanf("%c",&c);
if(c=='n'||c=='N') break; /*如果不删除,则跳出本循环*/
if(c=='y'||c=='Y')
{
if(p1==head) /*若p1==head,说明被删结点是首结点*/
head=p1->next; /*把第二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将一下结点地址赋给前一结点地址*/
n=n-1;
printf("\nNum %s student have been deleted.\n",s);
printf("Don't forget to save.\n");break; /*删除后就跳出循环*/
}
}
}
else
printf("\nThere is no num %s student on the list.\n",s); /*找不到该结点*/
return(head);
}
/* 显示全部记录函数*/
void print(STUDENT *head)
{int i=0; /* 统计记录条数*/
STUDENT *p; /*移动指针*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
p=head; /*初值为头指针*/
printf("\n************************************STUDENT************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Rec | Grade | Name | Sc1 | Sc2 | Sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf("| %3d | %4s | %-4s | %3d | %3d | %3d | %3d | %4.2f| %-5d |\n",
i, p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("-------------------------------------------------------------------------------\n");
printf("**************************************END**************************************\n");
}
/*姓名查找记录函数*/
void name(STUDENT *head)
{STUDENT *p; /* 移动指针*/
char s[5]; /*存放姓名用的字符数组*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
printf("Please enter name for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->name,s) && p != NULL) /*当记录的姓名不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d|\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no name %s student on the list.\n",s); /*显示没有该学生*/
}
/*班级查找记录函数*/
void grade(STUDENT *head)
{STUDENT *p; /* 移动指针*/
char s[6]; /*存放班级用的字符数组*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
printf("Please enter grade for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->grade,s) && p != NULL) /*当记录班级不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no grade %s student on the list.\n",s); /*显示没有该学生*/
}
/*性别查找记录函数*/
void sex(STUDENT *head)
{STUDENT *p; /* 移动指针*/
char s[4]; /*存放性别用的字符数组*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
printf("Please enter sex for searching.\n");
scanf("%s",s);
p=head; /*将头指针赋给p*/
while(strcmp(p->sex,s) && p != NULL) /*当记录的性别不是要找的,或指针不为空时*/
p=p->next; /*移动指针,指向下一结点*/
if(p!=NULL) /*如果指针不为空*/
{printf("\n*************************************FOUND************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is no sex %s student on the list.\n",s); /*显示没有该学生*/
}
/* 用平均分进行分数段查找 */
void average(STUDENT *head)
{
STUDENT *p; /* 移动指针*/
clrscr();
textbackground(MAGENTA);
textcolor(BLUE);
p=head; /*将头指针赋给p*/
if(p!=NULL&&p->average<60&&p->average>0) /*如果指针不为空且平均分在0-60之间*/
{
printf("\n**********************************not pass************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else if(p!=NULL&&p->average<70&&p->average>=60) /*如果指针不为空且平均分在60-70之间*/
{
printf("\n**********************************60-70************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else if(p!=NULL&&p->average<80&&p->average>=70) /*如果指针不为空且平均分在70-80之间*/
{
printf("\n**********************************70-80************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else if(p!=NULL&&p->average<90&&p->average>=80) /*如果指针不为空且平均分在80-90之间*/
{
printf("\n**********************************80-90************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else if(p!=NULL&&p->average<100&&p->average>=90) /*如果指针不为空且平均分在90-100之间*/
{
printf("\n**********************************90-100************************************\n");
printf("-------------------------------------------------------------------------------\n");
printf("| Grade | Name | sc1 | sc2 | sc3 | Sum | Ave | Order |\n");
printf("-------------------------------------------------------------------------------\n");
printf("| %4s | %4s | %3d | %3d | %3d | %3d | %4.2f | %-5d |\n",
p->grade,p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("-------------------------------------------------------------------------------\n");
printf("***************************************END**************************************\n");
}
else
printf("\nThere is not average student on the list.\n"); /*显示没有该学生*/
}
/*保存数据到文件函数*/
void save(STUDENT *head)
{FILE *fp; /*定义指向文件的指针*/
STUDENT *p; /* 定义移动指针*/
char outfile[10];
printf("Enter outfile name,for example c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,为只写方式*/
{
printf("Cannot open the file\n");
return; /*若打不开则返回菜单*/
}
printf("\nSaving the file......\n");
p=head; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{
fwrite(p,LEN,1,fp); /*写入一条记录*/
p=p->next; /*指针后移*/
}
fclose(fp); /*关闭文件*/
printf("Save the file successfully!\n");
}
/* 从文件读数据函数*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定义记录指针变量*/
FILE *fp; /* 定义指向文件的指针*/
char infile[10];
printf("Enter infile name,for example c:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为只读方式*/
{
printf("Can not open the file.\n");
return(head);
}
printf("\nLoading the file!\n");
p1=(STUDENT *)malloc(LEN); /*开辟一个新单元*/
if(!p1)
{
printf("Out of memory!\n");
return(head);
}
head=p1; /*申请到空间,将其作为头指针*/
while(!feof(fp)) /*循环读数据直到文件尾结束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果没读到数据,跳出循环*/
p1->next=(STUDENT *)malloc(LEN); /*为下一个结点开辟空间*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*指针后移,新读入数据链到当前表尾*/
}
p2->next=NULL; /*最后一个结点的后继指针为空*/
fclose(fp);
printf("You have success to read data from the file!\n");
return (head);
}
/*按平均分排序函数*/
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定义临时指针*/
temp=head->next; /*将原表的头指针所指的下一个结点作头指针*/
head->next=NULL; /*第一个结点为新表的头结点*/
while(temp!=NULL) /*当原表不为空时,进行排序*/
{
t=temp; /*取原表的头结点*/
temp=temp->next; /*原表头结点指针后移*/
p1=head; /*设定移动指针p1,从头指针开始*/
p2=head; /*设定移动指针p2做为p1的前驱,初值为头指针*/
while(t->averageaverage&&p1!=NULL) /*作成绩平均分比较*/
{
p2=p1; /*待排序点值小,则新表指针后移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,说明待排序点值大,应排在首位*/
{
t->next=p1; /*待排序点的后继为p*/
head=t; /*新头结点为待排序点*/
}
else /*待排序点应插入在中间某个位置p2和p1之间,如p为空则是尾部*/
{
t->next=p1; /*t的后继是p1*/
p2->next=t; /*p2的后继是t*/
}
}
p1=head; /*已排好序的头指针赋给p1,准备填写名次*/
while(p1!=NULL) /*当p1不为空时,进行下列操作*/
{
i++; /*结点序号*/
p1->order=i; /*将结点序号赋值给名次*/
p1=p1->next; /*指针后移*/
}
printf("Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入记录函数*/
STUDENT *insert(STUDENT *head,STUDENT *new)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一个结点*/
p0=new; /*p0指向要插入的结点*/
printf("\nPlease enter a new record.\n"); /*提示输入记录信息*/
printf("Enter the grade:");
scanf("%s",new->grade);
printf("Enter the name:");
scanf("%s",new->name);
do{
printf("Enter the sex(M or W):");
scanf("%s",&new->sex[0]);
if(new->sex[0]!='M'&&new->sex[0]!='W')
printf("Data error,please enter again.\n");
}while(new->sex[0]!='M'&&new->sex[0]!='W');
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新记录的总分,初值为0*/
for(i=0;i<3;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&new->score[i]);
if(new->score[i]>100||new->score[i]<0)
printf("Data error,please enter again.\n");
}while(new->score[i]>100||new->score[i]<0);
sum1=sum1+new->score[i]; /*累加各门成绩*/
}
new->sum=sum1; /*将总分存入新记录中*/
new->average=(float)sum1/3;
new->order=0;
if(head==NULL) /*原来的链表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的结点作为头结点*/
else
{while((p0->averageaverage)&&(p1->next!=NULL))
{p2=p1; /*使p2指向刚才p1指向的结点*/
p1=p1->next; /*p1后移一个结点*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原来第一个结点之前*/
else p2->next=p0; /*插到p2指向的结点之后*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最后的结点之后*/
}
n=n+1; /*结点数加1*/
head=sort(head); /*调用排序的函数,将学生成绩重新排序*/
printf("\nStudent %s have been inserted.\n",new->name);
printf("Don't forget to save the new file.\n");
return(head);
}
/*统计学生成绩函数*/
STUDENT *statistciam(STUDENT *head)
{
float sum1=0,sum2=0,sum3=0,ave1=0,ave2=0,ave3=0,max=0,min;
STUDENT *p;
int x,y=0,i=0;
p=head;
printf("1.sum and average\n2.single of average\n3.max of sum\n4.min of sum\n");
scanf("%d",&x);
getchar();
switch(x)
/*用switch语句实现功能选择*/
{
case 1: if(head==NULL)
{printf("\n There are not anyone's information !\n");return(head);}/*链表为空*/
else
{
printf("---------------------------------------------------------\n");
printf("|Grade\t|Name\t|Sc1\t|Sc2\t|Sc3\t|Sum\t|Ave\t|\n");
printf("---------------------------------------------------------\n");/*打印表格域*/
while(p!=NULL)
{
sum1=p->score[0]+p->score[1]+p->score[2]; /*计算个人总分*/
ave1=sum1/3;/*计算个人平均分*/
printf("|%s\t|%s\t|%.1d\t|%.1d\t|%.1d\t|%.1f\t|%.1f\t|\n",p->grade,p->name,p->score[0],p->score[1],p->score[2],sum1,ave1);
/*打印结果*/
printf("---------------------------------------------------------\n");/*打印表格域*/
p=p->next;}
}
return(head);
case 2: if(head==NULL)
{printf("\nThere are not anyone's information !\n");return(head);}/*链表为空*/
while(p!=NULL)
{
sum1=sum1+p->score[0];
sum2=sum2+p->score[1];
sum3=sum3+p->score[2];/*计算总分*/
y=y+1;
ave1=sum1/y;
ave2=sum2/y;
ave3=sum3/y;/*计算平均分*/
p=p->next;/*使p指向下一个结点*/
}
printf("score1 average is%.1f\n",ave1);
printf("score2 average is%.1f\n",ave2);
printf("score3 average is%.1f\n",ave3);/*打印结果*/
return(head);
case 3:
if(head==NULL)
{printf("\nThere are not anyone's information !\n");return(head);}/*链表为空*/
max=p->score[0]+p->score[1]+p->score[2];
while(i
{
i=i+1;
sum1=p->score[0]+p->score[1]+p->score[2]; /*计算个人总分*/
if(max
max=sum1;
p=p->next;
}
printf("max of sum:%.1f",max);
printf("\n");
return(head);
case 4: if(head==NULL)
{printf("\nThere are not anyone's information !\n");return(head);}/*链表为空*/
while(p!=NULL)
{
min=p->score[0]+p->score[1]+p->score[2];
while(p!=NULL)
{sum2=p->score[0]+p->score[1]+p->score[2];
if(min>sum2)
min=sum2;
p=p->next;
}
}
printf("min of sum:%.1f",min);
printf("\n");
return(head);
default :printf("Data error,please enter again!\n");
}
return(head);
}