题目描述
请提交如下内容,注意只允许提交一次
一、文件清单:
例如:
1. add.c
2. sub.c
3. score.c
4. score.h
二、各文件代码:
1. add.c 文件代码
*********
********
********
2. sub.c 文件代码
********
********
********
3. score.c 文件代码
********
********
********
4. score.h 文件代码
********
********
********
输入
暂无
输出
暂无
样例输入
暂无
样例输出
暂无
参考代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int num=0;
struct Student
{
char number[20];
char name[20];
int grade1;
int grade2;
int grade3;
int average;
}
stu[100];
void stu_read()//读å
¥æ件
{
FILE*fp=fopen("xyx.txt","r");
int j=0;
while(fscanf(fp,"%s%s%d%d%d%d",stu[j].number,stu[j].name,&stu[j].grade1,&stu[j].grade2,&stu[j].grade3,&stu[j].average)==6)
j++;
num=j;
fclose(fp);
}
void stu_put()//è¾åºæ件
{
FILE*fp=fopen("xyx.txt","w");
int j=0;
for (j=0;j<num;j++)
{
fprintf(fp,"%s %s %d %d %d %dn",stu[j].number,stu[j].name,stu[j].grade1,stu[j].grade2,stu[j].grade3,stu[j].average);
}
fclose(fp);
}
float average(struct Student stu)//å¹³åå¼
{
float grade;
grade=(stu.grade1+stu.grade2+stu.grade3)/3;
return grade;
}
int stu_number(char id[])//éè¿å¦å·è¿åä¸æ
{
int i;
for(i=0;i<num;i++)
{
if(strcmp(stu[i].number,id)==0)
{
return i;
}
}
return -1;
}
int stu_name(char name1[])//éè¿ååè¿åä¸æ
{
int i;
for(i=0;i<num;i++)
{
if(strcmp(stu[i].name,name1)==0)
{
return i;
}
}
return -1;
}
void stu_record(int i)//å¦çä¿¡æ¯
{
printf("å¦å·:%sn",stu[i].number);
printf("å§å:%sn",stu[i].name);
printf("è¯æ:%dn",stu[i].grade1);
printf("æ°å¦:%dn",stu[i].grade2);
printf("å¤è¯:%dn",stu[i].grade3);
printf("å¹³åå:%dn",stu[i].average);
}
void stu_insert()//æå
¥å¦çä¿¡æ¯
{
char a;
while(1)
{
printf("è¾å
¥å¦å·:");scanf("%s",stu[num].number);
printf("è¾å
¥å§å:");scanf("%s",stu[num].name);
printf("è¾å
¥è¯ææ绩:");scanf("%d",&stu[num].grade1);
printf("è¾å
¥æ°å¦æ绩:");scanf("%d",&stu[num].grade2);
printf("è¾å
¥å¤è¯æ绩:");scanf("%d",&stu[num].grade3);
stu[num].average=average(stu[num]);
num++;
printf("æ¯å¦ç»§ç»:y/nn");
a=getchar();
while(a!='n'&&a!='y')a=getchar();
if(a=='n')
{
break;
}
}
}
void stu_amend()//ä¿®æ¹å¦çä¿¡æ¯
{
char a;
while(1)
{
char id[20];
int i;
printf("请è¾å
¥è¦ä¿®æ¹çå¦å·:");
scanf("%s",&id);
i=stu_number(id);
if(i==-1)
printf("ä¸åå¨è¯¥å¦å·çå¦çn");
else
{
printf("è¾å
¥å¦å·:");scanf("%s",stu[i].number);printf("n");
printf("è¾å
¥å§å:");scanf("%s",stu[i].name);printf("n");
printf("è¾å
¥è¯ææ绩:");scanf("%d",&stu[i].grade1);printf("n");
printf("è¾å
¥æ°å¦æ绩:");scanf("%d",&stu[i].grade2);printf("n");
printf("è¾å
¥å¤è¯æ绩:");scanf("%d",&stu[i].grade3);printf("n");
stu[i].average=average(stu[i]);
}
printf("æ¯å¦ç»§ç»:y/nn");
a=getchar();
while(a!='n'&&a!='y')a=getchar();
if(a=='n')
{
break;
}
}
}
void stu_delete()//å é¤å¦çä¿¡æ¯
{
int i;
char a;
while(1)
{
char id[20];
int j,o;
printf("请è¾å
¥è¦å é¤çå¦çå¦å·:");
scanf("%s",&id);
j=stu_number(id);
if(j==-1)
printf("ä¸åå¨è¯¥å¦å·çå¦ç");
else
{
printf("æ¯å¦å é¤:y/nn");
o=getchar();
while(o!='n'&&o!='y')o=getchar();
if(o=='y')
{
for(i=j;i<num-1;i++)
stu[i]=stu[i+1];
num--;
}
}
printf("æ¯å¦ç»§ç»:y/nn");
a=getchar();
while(a!='n'&&a!='y')a=getchar();
if(a=='n')
{
break;
}
}
}
void stu_select()//æå§åæ¥æ¾
{
char a;
while(1)
{
char name[20];
int i;
printf("请æ¶å¾è¦æ¥è¯¢å¦ççå§å:");
scanf("%s",&name);
i=stu_name(name);
if(i==-1)
printf("ä¸åå¨å«%sçå¦ç",name);
else
stu_record(i);
printf("æ¯å¦ç»§ç»:y/nn");
a=getchar();
while(a!='n'&&a!='y')a=getchar();
if(a=='n')
{
break;
}
}
}
void stu_ave()//æå¹³ååæåº
{
int i;
int j;
struct Student t;
for(i=0;i<num;i++)
{
for(j=0;j<num-1;j++)
{
if(stu[j].average<stu[j+1].average)
{
t=stu[j];
stu[j]=stu[j+1];
stu[j+1]=t;
}
}
}
for(i=0;i<num;i++)
{
printf("%d->%st%st%dt%dt%dt%dn",i,stu[i].number,stu[i].name,stu[i].grade1,stu[i].grade2,stu[i].grade3,stu[i].average);
}
}
void main()
{
int choice;
stu_read();
while(1)
{
printf("n------ å¦çæ绩管çç³»ç»------n");
printf("1. å¢å å¦çè®°å½n");
printf("2. ä¿®æ¹å¦çè®°å½n");
printf("3. å é¤å¦çè®°å½n");
printf("4. æå§åæ¥è¯¢å¦çè®°å½n");
printf("5. æå¹³åæ绩æåºn");
printf("6. å½åºå»ä¸n");
printf("请éæ©(1-6):");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
stu_insert();
break;
}
case 2:
{
stu_amend();
break;
}
case 3:
{
stu_delete();
break;
}
case 4:
{
stu_select();
break;
}
case 5:
{
stu_ave();
break;
}
case 6:
{
return;
break;
}
}
stu_put();
}
}
解析
暂无