c语言 苏小红 第五版 第12章实验题答案
发布时间:2024-09-20
发布时间:2024-09-20
c语言 苏小红 第五版 第12章实验题答案
#include<stdio.h>
#include<string.h>
#define N 30
#define MAX_LEN 10
#define M 3
typedef struct student
{
long studentID;
char studentName[10];
int score[3];
}STUDENT;
void InputScore(STUDENT stu[],int n);
void SumforCorse(STUDENT stu[],int n,float sumcorse[],float avercorse[]);
void SumforStudent(STUDENT stu[],int n,float sumstudent[],float averstudent[]);
void DateScore(STUDENT stu[],int n,int (*compare)(int a,int b));
void DateNum(STUDENT stu[],int n);
void DateName(STUDENT stu[],int n);
int LinSearch(STUDENT stu[],long x,int n);
int LinSearchname(STUDENT stu[],char name[],int n);
void RateScore(STUDENT stu[],int n);
void PrintfScoreNum(STUDENT stu[],int n,float sumcorse[],float avercorse[]);
void Printf(STUDENT stu[],int n);
void Swapnum(long *a,long *b);
void Swapsum(int*a,int *b);
void Swapscore(int *a,int *b);
int Ascending(int a,int b);
int Descending(int a,int b);
main()
{int n,m,pos=0;
long x=0;
float avercorse[3]={0},averstudent[N]={0},sumcorse[3]={0},sumstudent[N]={0};
char name[MAX_LEN];
STUDENT stu[N];
printf("how many student?");
scanf("%d",&n);
InputScore(stu,n);
printf("计算每门课程的总分和平均分输入1\n");
printf("计算每个学生的总分和平均分输入2\n");
printf("按每个学生的总分由高到低输入3\n");
printf("按照每个学生的总分由低到高输入4\n");
printf("学号有小到大排出成绩表输入5\n");
printf("按姓名在字典中的排序输入6\n");
printf("按学号查询学生排名及各科考试成绩比输入7\n");
printf("按姓名查询学生排名及各科考试成绩比输入8\n");
printf("按成绩百分比输入9\n");
printf("输出每个学生的学号 姓名 各科考试成绩总分 平均分及每门课程的总分 平均分输入10\n");
scanf("%d",&m);
switch(m)
{
case 1:
SumforCorse(stu,n,sumcorse,avercorse);
break;
case 2:
SumforStudent(stu,n,sumstudent,averstudent);
break;
case 3:
DateScore(stu,n,Descending);
Printf(stu,n);
break;
case 4:
DateScore(stu,n,Ascending);
Printf(stu,n);
break;
case 5:
DateNum(stu,n);
Printf(stu,n);
break;
case 6:
DateName(stu,n);
Printf(stu,n);
break;
case 7:
{int j;
DateScore(stu,n,Descending);
printf("input the number you want to search:");
scanf("%ld",&x);
pos=LinSearch(stu,x,n);
if(pos!=-1)
printf("MT\t EN\t PH\t:\n");
for(j=0;j<3;j++)
{
printf("%d\t",stu[pos].score[j]);
}
printf("第%d名\n",pos+1);
break;
}
case 8:
{int j;
DateScore(stu,n,Descending);
printf("input the name you want to search:\n");
scanf("%s",name);
pos=LinSearchname(stu,name,n);
if(pos!=-1)
printf("MT\t EN\t PH\t:\n");
for(j=0;j<3;j++)
{printf("%d\t",stu[pos].score[j]);}
}
printf("第%d名\n",pos+1);
break;
case 9:
RateScore(stu,n);
break;
case 10:
PrintfScore
Num(stu,n,sumcorse,avercorse);
break;
}
}
void InputScore(STUDENT stu[],int n)
{
int i,j,m=3;
for(i=0;i<n;i++)
{
printf("inpu
c语言 苏小红 第五版 第12章实验题答案
t the %d student ID name score(MT EN PH):",i+1);
scanf("%ld",&stu[i].studentID);
scanf("%s",stu[i].studentName);
for(j=0;j<m;j++)
{
scanf("%d",&stu[i].score[j]);
}
}
}
void SumforCorse(STUDENT stu[],int n,float sumcorse[],float avercorse[])
{
int i,j;
for(j=0;j<3;j++)
{
for(i=0;i<n;i++)
{
sumcorse[j]=sumcorse[j]+stu[i].score[j];
}
avercorse[j]=sumcorse[j]/n;
switch(j)
{
case 0:
printf("the MT totle MT:%f\n",sumcorse[j]);
printf("the MT aver MT:%f\n",avercorse[j]);
break;
case 1:
printf("the EN totle EN:%f\n",sumcorse[j]);
printf("the EN aver EN:%f\n",avercorse[j]);
break;
case 2:
printf("the PH totle PH:%f\n",sumcorse[j]);
printf("the PH aver PH:%f\n",avercorse[j]);
break;
}
}
}
void SumforStudent(STUDENT stu[],int n,float sumstudent[],float averstudent[])
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
sumstudent[i]=sumstudent[i]+stu[i].score[j];
}
averstudent[i]=sumstudent[i]/3;
printf("第%d个the totle score and average is %f %f\n ",i+1,sumstudent[i],averstudent[i]);
}
}
void DateScore(STUDENT stu[],int n,int (*compare)(int a,int b))
{int i,j,k,m,t,sumstudent[N];
char temp[MAX_LEN];
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
sumstudent[i]=sumstudent[i]+stu[i].score[j];
}
}
for(t=0;t<n-1;t++)
{
k=t;
for(m=t+1;m<n;m++)
{
if((*compare)(sumstudent[k],sumstudent[m]))
{
k=m;
}
if(k!=t)
{
Swapsum(&sumstudent[t],&sumstudent[k]);
Swapnum(&stu[t].studentID,&stu[k].studentID);
Swapscore(stu[t].score,stu[k].score);
strcpy(temp,stu[t].studentName);
strcpy(stu[t].studentName,stu[k].studentName);
strcpy(stu[k].studentName,temp);
}
}
}
}
void Swapsum(int *a,int *b)
{int temp;
temp=*a;
*a=*b;
*b=temp;
}
void Swapnum(long *a,long *b)
{
long temp;
temp=*a;
*a=*b;
*b=temp;
}
void Swapscore(int *a,int *b)
{
int temp[M],i;
for(i=0;i<3;i++)
{temp[i]=a[i];
a[i]=b[i];
b[i]=temp[i];
}
}
int Ascending(int a,int b)
{
return a>b;
}
int Descending(int a,int b)
{
return a<b;
}
void DateNum(STUDENT stu[],int n)
{
int i,j,k;
char temp[MAX_LEN];
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(stu[j].studentID<stu[k].studentID)
{k=j;
}
if(k!=i)
{
Swapnum(&stu[i].studentID,&stu[k].studentID);
Swapscore(stu[k].score,stu[i].score);
strcpy(temp,stu[i].studentName);
strcpy(stu[i].studentName,stu[k].studentName);
strcpy(stu[k].studentName,temp);
}
}
}
}
void DateName(STUDENT stu[],int n)
{
int i,j,k;
char temp[MAX_LEN];
for(i=0;i<n;i++)
{
k=i;
for(j=i+1;j<n;j++)
{if(strcmp(stu[k].studentName,stu[j].studentName)>0)
{
Swapnum(&stu[i].studentID,&stu[k].studentID);
Swapscore(stu[i].score,stu[k].score);
strcpy(temp,stu[i].studentName);
strcpy(stu[i].studentName,stu[k].studentName);
strcpy
(stu[k].studentName,temp);
}
}
}
}
int LinSearch(STUDENT stu[],long x,int n)
{
int i;
for(i=0;i<n;i++)
{
if (stu[i].studentID==x)
c语言 苏小红 第五版 第12章实验题答案
return i;
}
return -1;
}
int LinSearchname(STUDENT stu[],char name[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(strcmp(name,stu[i].studentName)==0)
return i;
}
return -1;
}
void RateScore(STUDENT stu[],int n)
{
int i,a,b,c,d,e,j;
static float rate1,rate2,rate3,rate4,rate5;
for(j=0;j<3;j++)
{
a=0;b=0;c=0;d=0;e=0;
for(i=0;i<n;i++)
{if(stu[i].score[j]>=90&&stu[i].score[j]<=100)
{a++;
}
if(stu[i].score[j]>=80&&stu[i].score[j]<90)
{b++;
}
if(stu[i].score[j]>=70&&stu[i].score[j]<80)
{c++;
}
if(stu[i].score[j]>=60&&stu[i].score[j]<70)
{d++;
}
if(stu[i].score[j]>=0&&stu[i].score[j]<60)
{e++;
}
rate1=(float)a/n;
rate2=(float)b/n;
rate3=(float)c/n;
rate4=(float)d/n;
rate5=(float)e/n;
}
switch(j)
{
case 0:
printf("MT is:\n");
printf("优秀:%d所占百分比:%f\n",a,rate1);
printf("良好:%d所占百分比:%f\n",b,rate2);
printf("中等:%d所占百分比:%f\n",c,rate3);
printf("及格:%d所占百分比:%f\n",d,rate4);
printf("不及格:%d所占百分比:%f\n",e,rate5);
break;
case 1:
printf("EN is:\n");
printf("优秀:%d所占百分比:%f\n",a,rate1);
printf("良好:%d所占百分比:%f\n",b,rate2);
printf("中等:%d所占百分比:%f\n",c,rate3);
printf("及格:%d所占百分比:%f\n",d,rate4);
printf("不及格:%d所占百分比:%f\n",e,rate5);
break;
case 2:
printf("PH is:\n");
printf("优秀:%d所占百分比:%f\n",a,rate1);
printf("良好:%d所占百分比:%f\n",b,rate2);
printf("中等:%d所占百分比:%f\n",c,rate3);
printf("及格:%d所占百分比:%f\n",d,rate4);
printf("不及格:%d所占百分比:%f\n",e,rate5);
break;
}
}
}
void PrintfScoreNum(STUDENT stu[],int n,float sumcorse[],float avercorse[])
{
int i,j,k;
for(i=0;i<n;i++)
{
printf("%ld",stu[i].studentID);
puts(stu[i].studentName);
printf("the score MT\t EN\t PH\t\n:");
for(j=0;j<3;j++)
{
printf("%d\t",stu[i].score[j]);
}
printf("\n");
}
for(j=0;j<3;j++)
{
for(i=0;i<n;i++)
{
sumcorse[j]=sumcorse[j]+stu[i].score[j];
}
avercorse[j]=sumcorse[j]/n;
}
for(k=0;k<3;k++)
{
printf("the sum and aver is %f %f\n",sumcorse[k],avercorse[k]);
}
}
void Printf(STUDENT stu[],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("%ld\n",stu[i].studentID);
puts(stu[i].studentName);
printf(
"the score MT\t EN\t PH\t\n:");
for(j=0;j<3;j++)
{
printf("%d\t",stu[i].score[j]);
}
printf("\n");
}
}
下一篇:流化床秸秆燃烧技术研究与开发