完成一个学生成绩管理信息系统。要求:(1)使用结构体数组存储学生的学号、姓名、性别、C语言成绩;(

2022-07-31 教育 70阅读
#include
#include
#include
#include
typedef struct
{
char num[10];
char name[20];
int score;
}Student;
Student stu[80];
int menu_select()
{
char c;
do{
system("cls");
printf("\t\t****Students' Grade Management System****\n");
printf("\t\t | 1. Input Records |\n");
printf("\t\t | 2. Display All Records |\n");
printf("\t\t | 3. Sort |\n");
printf("\t\t | 4. Insert a Record |\n");
printf("\t\t | 5. Delete a Record |\n");
printf("\t\t | 6. Query |\n");
printf("\t\t | 7. Statistic |\n");
printf("\t\t | 8. Add Records from a Text File|\n");
printf("\t\t | 9. Write to a Text file |\n");
printf("\t\t | 0. Quit |\n");
printf("\t\t*****************************************\n");
printf("\t\t\tGive your Choice(0-9):");
c=getchar();
}while(c<'0'||c>'9');
return(c-'0');
}
int Input(Student stud[],int n)
{int i=0; char sign,x[10]; while(sign!='n'&&sign!='N') { printf("\t\t\tstudent's num:"); scanf("\t\t\t%s",stud[n+i].num); printf("\t\t\tstudent's name:"); scanf("\t\t\t%s",stud[n+i].name); printf("\t\t\tstudent's score:"); scanf("\t\t\t%d",&stud[n+i].score); gets(x); printf("\t\t\tany more records?(Y/N)"); scanf("\t\t\t%c",&sign); i++; }
return(n+i);
}
void Display(Student stud[],int n)
{
int i;
printf("\t\t\t-----------------------------------\n");
printf("\t\t\tnumber name score\n");
printf("\t\t\t-----------------------------------\n");
for(i=1;i{
printf("\t\t\t%-16s%-15s%d\n",stud[i-1].num,stud[i-1].name,stud[i-1].score);
if(i>1&&i%10==0)
{printf("\t\t\t-----------------------------------\n"); printf("\t\t\t"); system("pause"); printf("\t\t\t-----------------------------------\n"); }
}
printf("\t\t\t");
system("pause");
}
void Sort_by_num(Student stud[],int n)
{ int i,j,*p,*q,s;
char t[10];
for(i=0;ifor(j=0;jif(strcmp(stud[j].num,stud[j+1].num)>0)
{strcpy(t,stud[j+1].num); strcpy(stud[j+1].num,stud[j].num); strcpy(stud[j].num,t); strcpy(t,stud[j+1].name); strcpy(stud[j+1].name,stud[j].name); strcpy(stud[j].name,t); p=&stud[j+1].score; q=&stud[j].score; s=*p; *p=*q; *q=s; }
}
int Insert_a_record(Student stud[],int n)
{char x[10]; printf("\t\t\tstudent's num:"); scanf("\t\t\t%s",stud[n].num); printf("\t\t\tstudent's name:"); scanf("\t\t\t%s",stud[n].name); printf("\t\t\tstudent's score:"); scanf("\t\t\t%d",&stud[n].score); gets(x); n++; Sort_by_num(stud,n); printf("\t\t\tInsert Successed!\n"); return(n); }
int Delete_a_record(Student stud[],int n)
{ char s[20];
int i=0,j;
printf("\t\t\ttell me his(her) name:");
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&iif(i==n)
{ printf("\t\t\tnot find!\n");
return(n);
}
for(j=i;j{
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].name,stud[j+1].name);
stud[j].score=stud[j+1].score;
}
printf("\t\t\tDelete Successed!\n");
return(n-1);
}
void Query_a_record(Student stud[],int n)
{ char s[20];
int i=0;
printf("\t\t\tinput his(her) name:");
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&iif(i==n)
{ printf("\t\t\tnot find!\n");
return;

}
printf("\t\t\this(her) number:%s\n",stud[i].num);
printf("\t\t\this(her) score:%d\n",stud[i].score);
}
void Statistic(Student stud[],int n)
{ int i,j=0,k=0,sum=0;
float aver;
for(i=0;i{
sum+=stud[i].score;
if(stud[j].score>stud[i].score) j=i;
if(stud[k].score}
aver=1.0*sum/n;
printf("\t\t\tthere are %d records.\n",n);
printf("\t\t\tthe hignest score:\n");
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[j].num,stud[j].name,stud[j].score);
printf("\t\t\tthe lowest score:\n");
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[k].num,stud[k].name,stud[k].score);
printf("\t\t\tthe average score is %5.2f\n",aver);
}
int AddfromText(Student stud[],int n)
{ int i=0,num;
FILE *fp;
char filename[20];
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"rb"))==NULL)
{ printf("\t\t\tcann't open the file\n");
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num);
while(i{
fscanf(fp,"%s%s%d",stud[n+i].num,stud[n+i].name,&stud[n+i].score);
i++;
}
n+=num;
fclose(fp);
printf("\t\t\tSuccessed!\n");
printf("\t\t\t");
system("pause");
return(n);
}
void WritetoText(Student stud[],int n)
{
int i=0;
FILE *fp;
char filename[20];
printf("\t\t\tWrite Records to a Text File\n");
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n);
while(i{
fprintf(fp,"%-16s%-15s%d\n",stud[i].num,stud[i].name,stud[i].score);
i++;
}
fclose(fp);
printf("Successed!\n");
}
void main()
{
int n=0;
for(;;)
{
switch(menu_select())
{
case 1:
printf("\t\t\tInput Records\n");
n=Input(stu,n);
break;
case 2:
printf("\t\t\tDisplay All Records\n");
Display(stu,n);
break;
case 3:
printf("\t\t\tSort\n");
Sort_by_num(stu,n);
printf("\t\t\tSort Suceessed!\n");
printf("\t\t\t");
system("pause");
break;
case 4:
printf("\t\t\tInsert a Record\n");
n=Insert_a_record(stu,n);
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\tDelete a Record\n");
n=Delete_a_record(stu,n);
printf("\t\t\t");
system("pause");
break;
case 6:
printf("\t\t\tQuery\n");
Query_a_record(stu,n);
printf("\t\t\t");
system("pause");
break;
case 7:
printf("\t\t\tStatistic\n");
Statistic(stu,n);
printf("\t\t\t");
system("pause");
break;
case 8:
printf("\t\t\tAdd Records from a Text File\n");
n=AddfromText(stu,n);
break;
case 9:
printf("\t\t\tWrite to a Text file\n");
WritetoText(stu,n);
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\tHave a Good Luck,Bye-bye!\n");
printf("\t\t\t");
system("pause");
exit(0);
}
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com