《C语言学生成绩管理系统设计报告.doc》由会员分享,可在线阅读,更多相关《C语言学生成绩管理系统设计报告.doc(44页珍藏版)》请在装配图网上搜索。
______________________________________________________________________________________________________________
计算机科学与技术学院
课程设计报告
2015— 2016学年第二学期
课程名称 C语言程序设计
题 目 学生成绩管理系统
学生姓名 XXX
学 号 XXX
专业班级 XXX
指导教师 XXX
合 作 者 XXX
2016年 6月 30日
学生成绩管理系统
本程序可实现对学生语数外三门课程成绩的录入与保存。
1、 设计目的
本程序旨在训练读者的基本编程能力,了解管理信息系统开发流程,熟悉C语言的各种语法、编写流程、以及能够熟练运用各种算法、以及各种函数的使用。
2、 功能描述
本程序可实现的功能:
(1) 录入学生的成绩
(2) 输出学生的成绩
(3) 添加学生的成绩信息
(4) 删除指定学生的成绩信息
(5) 按照要求对学生成绩信息进行排序
(6) 根据学号查询指定学生的成绩
(7) 将学生的成绩信息以文件形式保存
3、 总体设计
3.1具体实现
main()函数:程序首先调用menu()函数,显示出系统主菜单,然后将menu()函数返回的从用户读取的选项k值赋予k,接着进入switch—case语句进入对应选项函数,若输入错误没有该选项则给出提示(default)以上过程为一个死循环,直到用户输入0为止。
menu()函数:在屏幕上打印选项名称,然后用一个int类型的变量接受从用户输入的选项,最后将其return至主函数。
score *creatlink()函数:创建链表主要实现流程如下:
print()函数:先令P=head,使p指向第一个节点,当head=!NULL时说明没有到链表尾端,那么就输出p所指向的结构数据,然后让p指向下一个节点,直到发现p==NULL为止。而当head==NULL说明链表中不存在数据,直接停止输出。
score *add()函数,添加新的学生信息,具体实现路程如流程图所示(图片制作时没有加Y/N判断,在判断图框中均为向左为真,向右为假):
score *search()函数:用来查询学生成绩,传递给函数指向链表的头指针,查询时,如果找到与输入相匹配的学号则打印此学生,反之则输出“没有任何学生资料!”:
score *sortdata()函数:该函数有学号、姓名、单科成绩排序(冒泡排序法),实行过程如图:
save()函数:用来保存数据,首先从用户输入取得要保存的文件名,然后定义一个指向文件的指针,以读写方式打开文件。将写生信息依次存入文件。
score *load()函数:用于读取数据,通过“r+”方式打开文件并判断是否打开成功。具体实现如下:
score *statistics()函数:它能实现程序的统计,通过switch-case语句选择统计方式,通过循环计算总分或者平均分并打印出来,流程都是通过循环,让指针逐个遍历整个链表,读取相应的数据并实现统计
3.2数据结构设计
数据结构:定义了一个包含学生成绩信息的结构体(struct scorenode{}),学生信息包括学号(number)、姓名(name[10])、语文、数学、英语成绩(chinese、mathmatic、english)、以及指向下一个结构体的链表指针(struct scorenode *next)。
3.3函数功能描述
main()函数:主函数功能主要是让程序选择将要进行的操作,通过menu()函数返回的选项进入其他函数执行。
int menu(int k)函数:此函数显示主菜单内容,需要一个int类型变量作为输入要执行的选项并返回给main()函数。
score *creatlink()函数:此函数用于创建链表,为了节省内存空间,我们采用malloc()函数为结构体分配动态内存空间。另外考虑到学号不可能是0,所以用输入0 的方式来判断是否结束输入,将最后的结构体中的指针指向NULL,并返回一个指向链表第一个结构的指针。
void print(score *head)函数:此函数返回值为空,知识为了在stdout流(屏幕)上打印出学生的成绩信息,需要一个指向链表头的指针来逐个向后打印。
score *add(score *head , score *stu)函数:为学生信息中添加新的学生资料,然后重新排序(按学号),并返回头指针。传入函数的head为链表头指针,stu指针指的是要添加的位置。
score *search(score *head)函数:按照学号查找学生信息,需要链表头指针并返回指向被搜索学生的指针。搜索原理就是从头向后面依次检索。
score *dele(score *head)函数:删除指定学生的资料。传入头指针,在函数中创建变量储存要删除学生的学号,然后从头向尾检索,直至找到该学生并将其删除,返回头指针。
score *sortdata(score *head)函数:用于按要求(学号、姓名、单科成绩)排序,最后返回头指针,排序运用老师上课时讲过的冒泡排序法。
int save(score *p1)函数:将链表内的数据以文件的形式储存,传入的p1指针一开始指向链表头,随着储存顺序一个一个地向后面指,直到NULL为止。函数内部定义一个指向文件的指针*fp,用于写入文件。
score *load(score *head)函数:读取文件数据,head为一个新建的链表头指针,读取文件数据之后令其保存至新建的链表之中,并返回头指针。
score *statistics(score *head)函数:统计成绩,可以统计总分、平均分、最高(低)分,返回操作后的链表首地址(头指针)。
4、 程序实现
4.1源代码
#include
#include
#include
#include
#define LEN sizeof(struct scorenode)
#define DEBUG
/*===========数据结构==========*/
struct scorenode
{
int number; //学生学号
char name[10]; //学生姓名
float chinese; //语文成绩
float mathmatic; //数学成绩
float english; //英语成绩
struct scorenode *next;
};
typedef struct scorenode score; //定义结构体变量
typedef struct scorenode *scoreptr; //定义结构体变量指针
int n,k;
/*n,k为全局变量,本程序中的函数均可以使用它,分别用于记数和标记*/
/*==========创建链表==========*/
/*返回一个指向链表头的指针*/
score *creatlink()
{
score*head;
score *p1,*p2,*p3,*max;
int i,j;
float fen;
char t[10];
n=0;
p1=p2=p3=(score *)malloc(LEN);head=p3; //开辟一个新单元
printf("请输入学生资料,输0退出!\n");
repeat1: printf("请输入学生学号(学号应大于0):"); //输入学号,学号应大于0
scanf("%d",&p1->number);
while(p1->number<0)
{
getchar();
printf("输入错误,请重新输入学生学号:");
scanf("%d",&p1->number);
} //输入学号为字符或小于0时,程序报错,提示重新输入学号
if(p1->number==0)
goto end; //当输入的学号为0时,转到末尾,结束创建链表
else
{
p3=head;
if(n>0)
{
for(i=0;inumber!=p3->number)
p3=p3->next;
else
{
printf("学号重复,请重输!\n");
goto repeat1;
/*当输入的学号已经存在,程序报错,返回前面重新输入*/
}
}
}
}
printf("请输入学生姓名:");
scanf("%s",&p1->name); /*输入学生姓名*/
printf("请输入语文成绩(0~100):"); /*输入语文成绩,成绩应在0-100*/
scanf("%f",&p1->chinese);
while(p1->chinese<0||p1->chinese>100)
{
getchar();
printf("输入错误,请重新输入语文成绩"); /*输入错误,重新输入语文成绩直到正确为止*/
scanf("%f",&p1->chinese);
}
printf("请输入数学成绩(0~100):"); /*输入数学成绩,成绩应在0-100*/
scanf("%f",&p1->mathmatic);
while(p1->mathmatic<0||p1->mathmatic>100)
{
getchar();
printf("输入错误,请重新输入数学成绩"); /*输入错误,重新输入数学成绩直到正确为止*/
scanf("%f",&p1->mathmatic);
}
printf("请输入英语成绩(0~100):"); /*输入英语成绩,成绩应在0-100*/
scanf("%f",&p1->english);
while(p1->english<0||p1->english>100)
{
getchar();
printf("输入错误,请重新输入英语成绩");
scanf("%f",&p1->english);
} /*输入错误,重新输入英语成绩直到正确为止*/
head=NULL;
while(p1->number!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(score *)malloc(LEN);
printf("请输入学生资料,输0退出!\n");
repeat2:printf("请输入学生学号(学号应大于0):");
scanf("%d",&p1->number); /*输入学号*/
while(p1->number<0)
{
getchar();
printf("输入错误,请重新输入学生学号:");
scanf("%d",&p1->number);
} /*输入学号为字符或小于0时,程序报错,提示重新输入学号*/
if(p1->number==0)
goto end; /*当输入的学号为0时,转到末尾,结束创建链表*/
else
{
p3=head;
if(n>0)
{
for(i=0;inumber!=p3->number)
p3=p3->next;
else
{
printf("学号重复,请重输!\n");
goto repeat2; /*当输入的学号已经存在,程序报错,返回前面重新输入*/
}
}
}
}
printf("请输入学生姓名:");
scanf("%s",&p1->name); /*输入学生姓名*/
printf("请输入语文成绩(0~100):");
scanf("%f",&p1->chinese); /*输入语文成绩,成绩应在0-100*/
while(p1->chinese<0||p1->chinese>100)
{
getchar();
printf("输入错误,请重新输入语文成绩");
scanf("%f",&p1->chinese);
} /*输入错误,重新输入语文成绩直到正确为止*/
printf("请输入数学成绩(0~100):");
scanf("%f",&p1->mathmatic); /*输入数学成绩,成绩应在0-100*/
while(p1->mathmatic<0||p1->mathmatic>100)
{
getchar();
printf("输入错误,请重新输入数学成绩");
scanf("%f",&p1->mathmatic);
} /*输入错误,重新输入数学成绩直到正确为止*/
printf("请输入英语成绩(0~100):");
scanf("%f",&p1->english); /*输入英语成绩,成绩应在0-100*/
while(p1->english<0||p1->english>100)
{
getchar();
printf("输入错误,请重新输入英语成绩");
scanf("%f",&p1->english);
} /*输入错误,重新输入英语成绩直到正确为止*/
}
end:
p1=head;
p3=p1;
for(i=1;inext;
if(max->number>p1->number)
{
k=max->number;
max->number=p1->number;
p1->number=k; /*交换前后结点中的学号值,使得学号大者移到后面的结点中*/
strcpy(t,max->name);
strcpy(max->name,p1->name);
strcpy(p1->name,t); /*交换前后结点中的姓名,使之与学号相匹配*/
fen=max->chinese;
max->chinese=p1->chinese;
p1->chinese=fen; /*交换前后结点中的语文成绩,使之与学号相匹配*/
fen=max->mathmatic;
max->mathmatic=p1->mathmatic;
p1->mathmatic=fen; /*交换前后结点中的数学成绩,使之与学号相匹配*/
fen=max->english;
max->english=p1->english;
p1->english=fen; /*交换前后结点中的英语成绩,使之与学号相匹配*/
}
}
max=head;p1=head; /*重新使max,p指向链表头*/
}
p2->next=NULL; /*链表结尾*/
printf("输入的学生数为:%d个!\n",n);
return(head);
}
/*==========显示数据==========*/
/*函数print,功能:显示学生成绩*/
void print(score *head)
{
score *p;
if(head==NULL)
{printf("\n没有任何学生资料!\n");}
else
{
printf("%d\n",n);
printf("-----------------------------------------\n");
printf("|学号\t|姓名\t|语文\t|数学\t|英语\t|\n");
printf("-----------------------------------------\n"); /*打印表格域*/
p=head;
do
{
printf("|%d\t|%s\t|%.1f\t|%.1f\t|%.1f\t|\n",p->number,p->name,p->chinese,p->mathmatic,p->english);
printf("-----------------------------------------\n"); /*打印表格域*/
p=p->next;
}while (p!=NULL);
}
}
/*==========添加学生数据==========*/
/*函数add,功能:追加学生资料,并且将所有学生资料按学号排序*/
score *add(score *head,score *stu)
{
score *p0,*p1,*p2,*p3,*max;
int i,j;
float fen;
char t[10];
p3=stu=(score *)malloc(LEN); /*开辟一个新单元*/
printf("\n输入要增加的学生的资料!");
repeat4: printf("请输入学生学号(学号应大于0):");
scanf("%d",&stu->number); /*输入学号,学号应大于0*/
while(stu->number<0)
{
getchar();
printf("输入错误,请重新输入学生学号:");
scanf("%d",&stu->number);
} /*输入错误,重新输入学号*/
if(stu->number==0)
goto end2; /*当输入的学号为0时,转到末尾,结束追加*/
else
{
p3=head;
if(n>0)
{ for(i=0;inumber!=p3->number)
p3=p3->next;
else
{
printf("学号重复,请重输!\n");
goto repeat4; /*当输入的学号已经存在,程序报错,返回前面重新输入*/
}
}
}
}
printf("输入学生姓名:");
scanf("%s",stu->name); /*输入学生姓名*/
printf("请输入语文成绩(0~100):");
scanf("%f",&stu->chinese); /*输入语文成绩,成绩应在0-100*/
while(stu->chinese<0||stu->chinese>100)
{ getchar();
printf("输入错误,请重新输入语文成绩");
scanf("%f",&stu->chinese);
} /*输入错误,重新输入语文成绩直到正确为止*/
printf("请输入数学成绩(0~100):");
scanf("%f",&stu->mathmatic); /*输入数学成绩,成绩应在0-100*/
while(stu->mathmatic<0||stu->mathmatic>100)
{
getchar();
printf("输入错误,请重新输入数学成绩");
scanf("%f",&stu->mathmatic);
} /*输入错误,重新输入数学成绩直到正确为止*/
printf("请输入英语成绩(0~100):");
scanf("%f",&stu->english); /*输入英语成绩,成绩应在0-100*/
while(stu->english<0||stu->english>100)
{
getchar();
printf("输入错误,请重新输入英语成绩");
scanf("%f",&stu->english);
} /*输入错误,重新输入英语成绩直到正确为止*/
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
} /*当原来链表为空时,从首结点开始存放资料*/
else /*链表不为空*/
{
if(p1->next==NULL) /*找到原来链表的末尾*/
{
p1->next=p0;
p0->next=NULL; /*将它与新开单元相连接*/
}
else
{
while(p1->next!=NULL) /*还没找到末尾,继续找*/
{
p2=p1;p1=p1->next;
}
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
p1=head;
p0=stu;
for(i=1;inext;
if(max->number>p1->number)
{
k=max->number;
max->number=p1->number;
p1->number=k; /*交换前后结点中的学号值,使得学号大者移到后面的结点中*/
strcpy(t,max->name);
strcpy(max->name,p1->name);
strcpy(p1->name,t); /*交换前后结点中的姓名,使之与学号相匹配*/
fen=max->chinese;
max->chinese=p1->chinese;
p1->chinese=fen; /*交换前后结点中的语文成绩,使之与学号相匹配*/
fen=max->mathmatic;
max->mathmatic=p1->mathmatic;
p1->mathmatic=fen; /*交换前后结点中的数学成绩,使之与学号相匹配*/
fen=max->english;
max->english=p1->english;
p1->english=fen; /*交换前后结点中的英语成绩,使之与学号相匹配*/
}
}
max=head;p1=head ; /*重新使max,p指向链表头*/
} end2:
printf("现在的学生数为:%d个!\n",n);
return(head);
}
/*==========查询数据==========*/
/*函数search,功能:查询学生成绩*/
score *search(score *head)
{
int number;
score *p1,*p2;
printf("输入要查询的学生的学号:");
scanf("%d",&number);
while(number!=0)
{
if(head==NULL)
{ printf("\n没有任何学生资料!\n");return(head);}
printf("-----------------------------------------\n");
printf("|学号\t|姓名\t|语文\t|数学\t|英语\t|\n");
printf("-----------------------------------------\n");
p1=head;
while(number!=p1->number&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(number==p1->number)
{ printf("|%d\t|%s\t|%.1f\t|%.1f\t|%.1f\t|\n",p1->number,p1->name,p1->chinese,p1->mathmatic,p1->english);
printf("-----------------------------------------\n");
}
else
printf("%d不存在此学生!\n",number);
printf("输入要查询的学生的学号,");
scanf("%d",&number);
}
printf("已经退出了!\n");
return(head);
}
/*==========删除数据==========*/
/*函数dele,功能:删除学生资料*/
score *dele(score *head)
{
score *p1,*p2;
int number;
printf("输入要删除的学生的学号(输入0时退出):");
scanf("%d",&number);
getchar();
while(number!=0) /*输入学号为0时退出*/
{
if(head==NULL)
{
printf("\n没有任何学生资料!\n");
return(head);
}
p1=head;
while(number!=p1->number&&p1->next!=NULL) /*p1指向的不是所要找的首结点,并且后面还有结点*/
{
p2=p1;p1=p1->next;
} /*p1后移一个结点*/
if(number==p1->number) /*如果找到了*/
{
if(p1==head)
head=p1->next; /*若p1指向的是首结点,把地二个结点地址赋予head*/
else
p2->next=p1->next; /*否则将下一个结点地址 赋给前一结点地址*/
printf("删除:%d\n",number);n=n-1;
}
else
printf("%d不存在此学生!\n",number); /*找不到该结点*/
printf("输入要删除的学生的学号:");
scanf("%d",&number);
getchar();
}
#ifdef DEBUG
printf("已经退出了!\n");
#endif
printf("现在的学生数为:%d个!\n",n);
return(head);
}
/*==========排序==========*/
/*定义排序函数。此函数带回一个指向链表头的指针*/
score *sortdata(score *head)
{
score *p,*max;
int i,j,x;
float fen;
char t[10];
if(head==NULL)
{
printf("\n没有任何学生资料,请先建立链表!\n");
return(head);
} /*链表为空*/
max=p=head;
for(i=0;i<80;i++)
printf("*");
printf("1按学生学号排序\t2按学生姓名排序\t3按语文成绩排序\n");
printf("4按数学成绩排序\t5按英语成绩排序\t\n");
for(i=0;i<80;i++)
printf("*");
printf("请选择操作:");
scanf("%d",&x); /*选择操作*/
getchar();
switch(x) /*用switch语句实现功能选择*/
{
case 1 :
for(i=1;inext;
if(max->number>p->number)
{
k=max->number;
max->number=p->number;
p->number=k; /*交换前后结点中的学号值,使得学号大者移到后面的结点中*/
strcpy(t,max->name);
strcpy(max->name,p->name);
strcpy(p->name,t); /*交换前后结点中的姓名,使之与学号相匹配*/
fen=max->chinese;
max->chinese=p->chinese;
p->chinese=fen; /*交换前后结点中的语文成绩,使之与学号相匹配*/
fen=max->mathmatic;
max->mathmatic=p->mathmatic;
p->mathmatic=fen; /*交换前后结点中的数学成绩,使之与学号相匹配*/
fen=max->english;
max->english=p->english;
p->english=fen; /*交换前后结点中的英语成绩,使之与学号相匹配*/
}
}
max=head;
p=head; /*重新使max,p指向链表头*/
}
print(head);
break; /*打印值排序后的链表内容*/
case 2 :
for(i=1;inext;
if(strcmp(max->name,p->name)>0) /*strcmp:字符串比较函数*/
{
strcpy(t,max->name); /*strcpy:字符串复制函数*/
strcpy(max->name,p->name);
strcpy(p->name,t); /*交换前后结点中的姓名,使得姓名字符串的值大者移到后面的结点中*/
k=max->number;
max->number=p->number;
p->number=k; /*交换前后结点中的学号值,使之与姓名相匹配*/
fen=max->chinese;
max->chinese=p->chinese;
p->chinese=fen; /*交换前后结点中的语文成绩,使之与姓名相匹配*/
fen=max->mathmatic;
max->mathmatic=p->mathmatic;
p->mathmatic=fen; /*交换前后结点中的数学成绩,使之与姓名相匹配*/
fen=max->english;
max->english=p->english;
p->english=fen; /*交换前后结点中的英语成绩,使之与姓名相匹配*/
}
}
p=head;
max=head;
}
print(head);
break;
case 3 :
for(i=1;inext;
if(max->chinese>p->chinese)
{
fen=max->chinese;
max->chinese=p->chinese;
p->chinese=fen; /*交换前后结点中的语文成绩,使得语文成绩高者移到后面的结点中*/
k=max->number;
max->number=p->number;
p->number=k; /*交换前后结点中的学号,使之与语文成绩相匹配*/
strcpy(t,max->name);
strcpy(max->name,p->name);
strcpy(p->name,t); /*交换前后结点中的姓名,使之与语文成绩相匹配*/
fen=max->mathmatic;
max->mathmatic=p->mathmatic;
p->mathmatic=fen; /*交换前后结点中的数学成绩,使之与语文成绩相匹配*/
fen=max->english;
max->english=p->english;
p->english=fen; /*交换前后结点中的英语成绩,使之与语文成绩相匹配*/
}
}
p=head;
max=head;
}
print(head);
break;
case 4 :
for(i=1;inext;
if(max->mathmatic>p->mathmatic)
{
fen=max->mathmatic;
max->mathmatic=p->mathmatic;
p->mathmatic=fen; /*交换前后结点中的数学成绩,使得数学成绩高者移到后面的结点中*/
k=max->number;
max->number=p->number;
p->number=k; /*交换前后结点中的学号,使之与数学成绩相匹配*/
strcpy(t,max->name);
strcpy(max->name,p->name);
strcpy(p->name,t); /*交换前后结点中的姓名,使之与数学成绩相匹配*/
fen=max->chinese;
max->chinese=p->chinese;
p->chinese=fen; /*交换前后结点中的语文成绩,使之与数学成绩相匹配*/
fen=max->english;
max->english=p->english;
p->english=fen; /*交换前后结点中的英语成绩,使之与数学成绩相匹配*/
}
}
p=head;
max=head;
}
print(head);
break;
case 5 :
for(i=1;inext;
if(max->english>p->english)
{
fen=max->english;
max->english=p->english;
p->english=fen; /*交换前后结点中的英语成绩,使得英语成绩高者移到后面的结点中*/
k=max->number;
max->number=p->number;
p->number=k; /*交换前后结点中的学号,使之与英语成绩相匹配*/
strcpy(t,max->name);
strcpy(max->name,p->name);
strcpy(p->name,t); /*交换前后结点中的姓名,使之与英语成绩相匹配*/
fen=max->chinese;
max->chinese=p->chinese;
p->chinese=fen; /*交换前后结点中的语文成绩,使之与英语成绩相匹配*/
fen=max->mathmatic;
max->mathmatic=p->mathmatic;
p->mathmatic=fen; /*交换前后结点中的数学成绩,使之与英语成绩相匹配*/
}
}
p=head;
max=head;
}
print(head);
break;
default :
printf("输入错误,请重试! \n");
}
return (0);
}
/*==========保存数据==========*/
/*函数save,功能:保存学生的资料*/
void save(score *p1)
{
FILE *fp;
char filepn[20];/*用来存放文件保存路径以及文件名*/
printf("请输入文件路径及文件名:");
scanf("%s",filepn);
if((fp=fopen(filepn,"w+"))==NULL)
{
printf("不能打开文件!\n");
exit(1);
}
fprintf(fp," 学生成绩管理系统 \n");
fprintf(fp,"\n");
fprintf(fp,"-----------------------------------------\n");
fprintf(fp,"|学号\t|姓名\t|语文\t|数学\t|英语\t|\n");
fprintf(fp,"-----------------------------------------\n"); /*打印表格域*/
while(p1!=NULL)
{
fprintf(fp,"%d\t%s\t%.1f\t%.1f\t%.1f\t\n",p1->number,p1->name,p1->chinese,p1->mathmatic,p1->english);
p1=p1->next; /*下移一个结点*/
}
fclose(fp);
printf("文件已经保存!\n");
return ;
}
/*==========调入文件==========*/
/*函数loadfile,功能:从文件读入学生记录*/
score *loadfile(score *head)
{
score *p1,*p2;
int m=0;
char filename[10];
FILE *fp;
printf("请输入文件路径及文件名:");
scanf("%s",filename);/*输入文件路径及名称*/
if((fp=fopen(filename,"r+"))==NULL)
{
printf("不能打开文件!\n");
return 0;
}
fscanf(fp," 学生成绩管理系统 \n");
fscanf(fp,"\n");
fscanf(fp,"---------------------------------------
链接地址:https://www.zhuangpeitu.com/p-1103271.html