第1个回答 2011-06-22
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define STU_NUM 40 /*最多的学生人数*/
#define COURSE_NUM 10 /*最多的考试科目*/
struct student
{
int number; /*每个学生的学号*/
char name[10]; /*每个学生的姓名*/
int score[COURSE_NUM]; /*每个学生M门功课的成绩*/
int sum; /*每个学生的总成绩*/
float average; /*每个学生的平均成绩*/
};
typedef struct student STU;
/*----------增加学生成绩模块-----------*/
int AppendScore(STU *head,int n,int m)
{
int j;
STU *p;
char ch;
for (p=head+n ; p<head+STU_NUM ; p++)
{
printf("\nInput number:");
scanf("%d",&p->number);
printf("Input name:");
scanf("%s",p->name);
for (j=0 ; j<m ; j++)
{
printf("Input score%d:",j+1);
scanf("%d",p->score+j);
}
n++;
printf("Do you want to append a new node(Y/N)?");
scanf("%c",&ch);
if(ch == 'n' || ch == 'N')
return n;
}
}
/*----------------显示学生成绩模块------------------*/
void PrintScore(STU *head,int n,int m)
{
STU *p;
int i;
char str[100]={'\0'},temp[3];
strcat(str," Number Name");
for (i=1;i<m;i++)
{
strcat(str,"Score");
itoa(i,temp,10);
strcat(str,temp);
strcat(str," ");
}
strcat(str," sum average");
printf("%s",str); /*打印表头*/
for (p=head;p<head+n;p++)
{
printf("\nNo.%3d%8s",p->number,p->name);
for (i=0;i<m;i++)
{
printf("%7d",p->score[i]);
}
printf("%11d%9.2f\n",p->sum,p->average);
}
}
/*---------------计算总成绩和平均成绩模块---------------------*/
void TotalScore(STU *head,int n,int m)
{
STU *p;
int i;
for(p=head;p<head+n;p++)
{
p->sum=0;
for(i=0;i<m;i++)
{
p->sum = p->sum + p->score[i];
}
p->average = (float)p->sum/m;
}
}
/*-----------------排序模块---------------------*/
void SortScore(STU *head,int n)
{
int i,j,k;
STU temp;
for (i=0;i<n-1;i++)
{
k=i;
for (j=i;j<n;j++)
{
if ((head+j)->sum > (head+k)->sum)
{
k=j;
}
}
if (k!=i)
{
temp=*(head+k);
*(head+k)=*(head+i);
*(head+i)=temp;
}
}
}
/*---------------------查找学生的学号模块------------------------*/
int SearchNum(STU *head,int num,int n)
{
int i;
for (i=0;i<n;i++)
{
if((head+i)->number == num)
return i;
}
return -1;
}
/*------------------查找学生成绩并显示查找结果---------------------*/
void SearchScore(STU *head,int n,int m)
{
int number,findNo;
printf("Please Input the number you want to search:");
scanf("%d",&number);
findNo=SearchNum(head,number,n);
if(findNo == -1)
{
printf("\nNot found!\n");
}
else
{
PrintScore(head+findNo,1,m);
}
}
/*-----------------删除记录模块-------------------*/
int DeleteScore(STU *head,int n,int m)
{
int i,num;
STU *p;
printf("Please input the number to Delete:");
scanf("%d",&num);
i=SearchNum(head,num,n);
if(i==-1)
{
printf("Number not found\n");
return n;
}
for(p=head+i;p<=head+n;p++)
{
memcpy(p,p+1,sizeof(struct student));
memset(p+1,0,sizeof(struct student));
}
n--;
return n;
}
/*-------------------文件保存模块-------------------------*/
void SaveScoreFile(STU *head,const int n,const int m)
{
FILE *fp;
int i;
STU *p=head;
if((fp=fopen("record","wb"))==NULL)
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file\n");
fwrite(&n,sizeof(int),1,fp);
fwrite(&m,sizeof(int),1,fp);
for (i=0;i<n;i++)
{
fwrite(head+i,sizeof(struct student),1,fp);
}
fclose(fp);
return;
}
/*---------------------菜单模块--------------------*/
char Menu(void)
{
char ch;
printf("#================================================#\n");
printf("# >>>>>> 学生成绩管理系统 <<<<<< #\n");
printf("#================================================#\n");
printf("# 1.增加成绩 #\n");
printf("# 2.显示成绩 #\n");
printf("# 3.删除成绩 #\n");
printf("# 4.修改成绩 #\n");
printf("# 5.查询成绩 #\n");
printf("# 6.学生排序 #\n");
printf("# 7.保存文件 #\n");
printf("# 0.退出系统 #\n");
printf("#================================================#\n");
printf("请输入0-8选择菜单项:");
scanf(" %c",&ch);
return ch;
}
/*-------------------------修改模块-----------------------*/
void ModifyScore(STU *head,int n,int m)
{
int i,j,num;
STU *p;
printf("Please input the number to modify:\n");
scanf("%d",&num);
i=SearchNum(head,num,m);
if(i==-1)
{
printf("Number not found!\n");
return;
}
p=head+i;
printf("Number:%d\n",p->number);
printf("Input name:");
scanf("%s",p->name);
for(j=0;j<m;j++)
{
printf("Input score%d:",j+1);
scanf("%d",p->score+j);
}
TotalScore(head,n,m);
}
/*-------------------主函数模块-----------------------*/
void main()
{
char ch;
int m=3,n=0;
STU stu[STU_NUM];
while (1)
{
ch=Menu(); /*显示菜单,并读取用户信息*/
switch (ch)
{
case '1':n=AppendScore(stu,n,m); /*调用成绩添加模块*/
TotalScore(stu,n,m);
break;
case '2':PrintScore(stu,n,m); /*调用成绩显示模块*/
break;
case '3':n=DeleteScore(stu,n,m); /*调用成绩删除模块*/
PrintScore(stu,n,m);
break;
case '4':ModifyScore(stu,n,m); /*调用成绩修改模块*/
PrintScore(stu,n,m);
break;
case '5':SearchScore(stu,n,m); /*调用按学号查找模块*/
break;
case '6':SortScore(stu,n); /*调用成绩排序模块*/
printf("\nSorted result\n");
PrintScore(stu,n,m);
break;
case '7':SaveScoreFile(stu,n,m); /*保存文件*/
break;
case '0':exit(0);
printf("End of program!"); /*退出程序*/
break;
default:printf("Input error!");
break;
}
}
}