C语言——利用数组实现的成绩管理系统
这是上个星期老师置的作业,今天完成了,写上来给大家参考下,编译已通过.如果发现哪有不足的地方请指正.
#include <stdio.h>
#include <string.h>
#include <graphics.h>
#define NUM 100 /*定义最多学生人数*/
/*定义学生数据结构体变量*/
struct std
{ long num;
char name[20];
int en_score;
int math_score;
int c_score;
float aver;
int order;
}data[NUM]={{0,"",0,0,0,0,0}},*pt;
void border();
void item();
int SIZE=0;
/*输入函数:输入学生数据*/
void input()
{
pt=data;
gotoxy(0,0);
printf("Please input student number:");
scanf("%d",&SIZE);
for (;pt<data+SIZE;pt++)
{
scanf("%ld%s%d%d%d",&pt->num,pt->name,&pt->en_score,&pt->math_score,&pt->c_score);
}
pt=data;
}
/*输出学生数据*/
void output()
{
if (SIZE==0)
{
printf("No Data!\n");
input();
}
border();
item();
for (;pt<data+SIZE;pt++)
list();
border();
pt=data;
}
/*绘制菜单边框*/
void border()
{
int i,j;
printf(" %c",179);
for (i=0;i<6;i++)
putchar('-');
putchar(179);
for (i=0;i<20;i++)
putchar('-');
putchar(179);
for (i=0;i<5;i++)
{
for (j=0;j<8;j++)
putchar('-');
putchar(179);
}
printf(" ");
}
/*菜单选项输出*/
void item()
{
int i;
printf(" %c%6s%c%20s%c%8s%c%8s%c%8s%c%8s%c%8s%c ",179,"Number",179,"Name",179,"English",179,"Math",179,"Cprogram",179,"Average",179,"Order",179);
printf(" %c",179);
for (i=0;i<72;i++)
printf("%c",'-');
printf("%c ",179);
}
/*定位学生函数*/
void locate()
{
long n;
printf("please input the student's number");
scanf("%ld",&n);
for (;pt<data+SIZE;pt++)
{
if (n==pt->num)
{
border();
item();
list();
border();
}
}
pt=data;
}
/*输出列表函数*/
int list()
{
int i;
printf(" %c%6ld%c%20s%c%8d%c%8d%c%8d%c%8.1f%c%8d%c ",179,pt->num,179,pt->name,179,pt->en_score,179,pt->math_score,179,pt->c_score,179,pt->aver,179,pt->order,179);
printf(" %c",179);
for (i=0;i<72;i++)
printf("%c",'-');
printf("%c ",179);
return 0;
}
int list2(struct std *a)
{
int i;
printf(" %c%6ld%c%20s%c%8d%c%8d%c%8d%c%8.1f%c%8d%c ",179,a->num,179,a->name,179,a->en_score,179,a->math_score,179,a->c_score,179,a->aver,179,a->order,179);
printf(" %c",179);
for (i=0;i<72;i++)
printf("%c",'-');
printf("%c ",179);
return 0;
}
/*平均数函数*/
void average()
{
for (pt=data;pt<data+SIZE;pt++)
pt->aver=(float)(pt->en_score+pt->math_score+pt->c_score)/3;
printf("\n\n\n\n\n\t\t\tSUCCESS!\n");
pt=data;
}
/*排序函数*/
void sort()
{
int i;
border();
item();
for (i=1;i<=SIZE;i++)
for (pt=data;pt<data+SIZE;pt++)
if (pt->order==i)
{
list();
}
border();
}
void courseaver()
{
int i;
float aver=0;
border();
for (i=0;i<SIZE;i++)
{
aver+=(pt+i)->en_score;
aver/=SIZE;
}
printf(" | English Course Average : %8.1f |\n",aver);
aver=0;
border();
for (i=0;i<SIZE;i++)
{
aver+=(pt+i)->math_score;
aver/=SIZE;
}
printf(" | Maths Course Average : %8.1f |\n",aver);
aver=0;
border();
for (i=0;i<SIZE;i++)
{
aver+=(pt+i)->c_score;
aver/=SIZE;
}
printf(" | C Program Course Average : %8.1f |\n",aver);
aver=0;
border();
}
/*出入学生数据*/
void insert()
{
pt=data;
printf("Please add a student's data:\n");
scanf("%ld%s%d%d%d",&(pt+SIZE)->num,(pt+SIZE)->name,&(pt+SIZE)->en_score,&(pt+SIZE)->math_score,&(pt+SIZE)->c_score);
SIZE=SIZE+1;
}
/*排名*/
void order()
{
int i,j,k,max[10]={0};
pt=data;
for (i=0;i<SIZE;i++)
{
for (j=i+1;j<SIZE;j++)
{
for (k=0;k<10;k++)
if (j==max[k]) break;
if ( (pt+i)->aver <=(pt+j)->aver )
max=j;
else
max=i;
}
(pt+max)->order=i+1;
}
}
/*删除学生数据*/
void del()
{
long n,i;
printf("Please input the student's num to delete:\n");
scanf("%ld",&n);
for (;pt<data+SIZE;pt++)
{
if(pt->num==n)
{
for (i=pt-data;i<SIZE;i++)
{
(pt+i)->num=(pt+i+1)->num;
strcpy((pt+i)->name,(pt+i+1)->name);
(pt+i)->en_score=(pt+i+1)->en_score;
(pt+i)->math_score=(pt+i+1)->math_score;
(pt+i)->c_score=(pt+i+1)->c_score;
(pt+i)->aver=(pt+i+1)->aver;
(pt+i)->order=(pt+i+1)->order;
}
}
}
SIZE=SIZE-1;
pt=data;
}
/*保存数据*/
void save()
{
FILE *fp;
char fname[50];
int i;
printf("Please input a filename to save:\n");
scanf("%s",fname);
if ((fp=fopen(fname,"wb"))==NULL)
{
printf("File creat error!\n");
quit();
}
for (i=0;i<SIZE;i++)
fwrite(pt+i,sizeof(struct std),1,fp);
fclose(fp);
}
/*编辑学生数据*/
void edit()
{
long n;
printf("Please input a student's num to edit:");
scanf("%ld",&n);
for (pt=data;pt<data+SIZE;pt++)
{
if(pt->num==n)
{
scanf("%ld%s%d%d%d",&pt->num,pt->name,&pt->en_score,&pt->math_score,&pt->c_score);
break;
}
}
pt=data;
}
/*载入学生数据*/
void load()
{
FILE *fp;
char fname[50];
int i,j=0;
printf("Please input a filename to load:\n");
scanf("%s",fname);
if ((fp=fopen(fname,"rb"))==NULL)
{
printf("File load error!\n");
quit();
}
for (i=0;i<!feof(fp);i++)
j+=(fread(pt+i,sizeof(struct std),1,fp));
SIZE=j;
border();
item();
for (i=0;i<j;i++)
{
list(pt+i);
}
border();
fclose(fp);
}
/*绘制菜单*/
void menu()
{
int i;
printf("\n\n\n\n\n\t%c",201);
for (i=0;i<62;i++)
printf("%c",205);
printf("%c\t",187);
items();
printf("\t%c",200);
for (i=0;i<62;i++)
printf("%c",205);
printf("%c\t",188);
printf("\t SIZE=%d.\n",SIZE);
printf("\t Please input your choice:");
react();
}
/*退出函数*/
int quit()
{
char ch;
printf("\n\n\n\n\n\t\tAre you sure to quit?(Y/N)");
ch=getchar();
if (ch=='y' ||ch=='Y')
{
exit(0);
closegraph();
}
else if(ch=='n' ||ch=='N')
{
clrscr();
menu();
}
else
{
printf("\n\tINVALID INPUT! TYR AGAIN");
clrscr();
quit();
}
}
/*输出所有菜单选项*/
void choice(char *s)
{
int i,j;
printf("\t%c",186);
for (j=0;j<6;j++)
printf("%c",32);
printf("%s",s);
for (j=0;j<(80-18-6-strlen(s));j++)
printf("%c",32);
printf("%c\t",186);
}
int items()
{
int i;
char ch1[51],ch2[51];
for (i=0;i<50;i++)
{
ch2='*';
if (i>=23 && i<=26)
{ch1[23]='M';ch1[24]='E';ch1[25]='N';ch1[26]='U';}
else
ch1='*';
}
ch1[50]='\0';
ch2[50]='\0';
choice(ch1);
choice(" 0. INPUT data of students");
choice(" 1. OUTPUT all data of students");
choice(" 2. LOCATE data on NUMBER of students");
choice(" 3. Calculate AVERAGE of students");
choice(" 4. SORT on AVERAGE of students");
choice(" 5. Calculate COURSE AVERAGE of all");
choice(" 6. Output AVERAGE total of course");
choice(" 7. Fill order of all ");
choice(" 8. Insert a record in table");
choice(" 9. Delete a record in table");
choice(" 10. Update records in table");
choice(" 11. Save student table to a file");
choice(" 12. Load student table from a file");
choice(" 13. Edit a student's data");
choice(" 14. Quit");
choice(ch2);
return 0;
}
/*根据不同的选项做出不同的反应*/
int react()
{
int ch;
scanf("%d",&ch);
switch(ch)
{
case 0:cleardevice();input();system("pause");cleardevice();menu();break;
case 1:cleardevice();output();system("pause");cleardevice();menu();break;
case 2:cleardevice();locate();system("pause");cleardevice();menu();break;
case 3:cleardevice();average();system("pause");cleardevice();menu();break;
case 4:cleardevice();sort();system("pause");cleardevice();menu();break;
case 5:cleardevice();courseaver();system("pause");cleardevice();menu();break;
case 6:cleardevice();output();system("pause");cleardevice();menu();break;
case 7:cleardevice();order();system("pause");cleardevice();menu();break;
case 8:cleardevice();insert();system("pause");cleardevice();menu();break;
case 9:cleardevice();del();system("pause");cleardevice();menu();break;
case 10:cleardevice();printf("\n\n\n\n\n\tUpdate Success!\n");system("pause");cleardevice();menu();break;
case 11:cleardevice();save();system("pause");cleardevice();menu();break;
case 12:cleardevice();load();system("pause");cleardevice();menu();break;
case 13:cleardevice();edit();system("pause");cleardevice();menu();break;
case 14:cleardevice();quit();system("pause");cleardevice();menu();break;
default:printf("\t Invalid Choice!Try Again:");react();
}
}
/*主函数*/
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\\TURBOC2\\");
menu();
return 0;
}
本程序还要一些不完善的地方,有兴趣的朋友可以看看。