2024-04-24 13:29:05
弄过类似的
稍改如下:
参考:
#include<iostream>
using namespace std;
struct student
{
int number;
char name[10];
float score[2];
float total;
};
void Total(student s[],int n)
{
int i;
for( i=0;i<n;i++)
s[i].total=(s[i].score[0]+s[i].score[1]+s[i].score[2]);
}
void smax(student s[],int n)
{
int i;
float max0 = s[0].score[0];
float max1 = s[0].score[1];
for( i=1;i<n;i++)
{
if(s[i].score[0]>max0) max0 = s[i].score[0];
if(s[i].score[1]>max1) max1 = s[i].score[1];
}
cout<<"语文最高分为:\n";
for( i=0;i<n;i++)
if(s[i].score[0] == max0)
cout<<s[i].number<<" \t"<<s[i].name<<"\t"<<s[i].score[0]<<"\t"<<s[i].score[1]<<"\t"<<s[i].total<<endl;
cout<<"数学最高分为:\n";
for( i=0;i<n;i++)
if(s[i].score[1] == max1)
cout<<s[i].number<<" \t"<<s[i].name<<"\t"<<s[i].score[0]<<"\t"<<s[i].score[1]<<"\t"<<s[i].total<<endl;
}
void order(student s[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(s[i].total>s[j].total)
{
student temper;
temper=s[i];
s[i]=s[j];
s[j]=temper;
}
cout<<"总分最高的为:\n";
cout<<s[n-1].number<<" \t"<<s[n-1].name<<"\t"<<s[n-1].score[0]<<"\t"<<s[n-1].score[1]<<"\t"<<s[n-1].total<<endl;
cout<<"the order of the score is:"<<endl;
cout<<"学号"<<"\t"<<"姓名"<<"\t"<<"语文"<<"\t"<<"数学"<<"\t"<<"总成绩"<<endl;
for( i=0;i<n;i++)
cout<<s[i].number<<" \t"<<s[i].name<<"\t"<<s[i].score[0]<<"\t"<<s[i].score[1]<<"\t"<<s[i].total<<endl;
}
int main()
{
int i;
student s[100];
cout<<"enter the numer name and the score of course:"<<endl;
for( i=0;i<100;i++)
{
cin>>s[i].number>>s[i].name>>s[i].score[0]>>s[i].score[1];
if(s[i].number == 0)
break;
}
Total(s,i);//求总分
smax(s,i);//单科最高分
order(s,i);//排序并输出两科最高分
return 0;
}
2024-04-24 07:16:43