关于函数的一个问题,求大神解答
键盘输入不大于 30 人的百分制成绩和姓名,以输入负数或大于 100 为结束输入, 分别按照成绩和姓名排序后,输出成绩和姓名,每一行输出一人,要求用函数形式完成
不知道这样是否符合你的要求
程序代码:#include<stdio.h>
#define MAX 30
typedef struct{
char name[10];
float score;
}Student;
void showInfo(Student &stu){
printf("%s\t%.2lf\n",stu.name,stu.score);
}
bool getInfo(Student &stu){
printf("姓名:");
scanf("%s",&stu.name);
printf("成绩:");
scanf("%f",&stu.score);
showInfo(stu);
printf("------------\n");
if(stu.score>100.0||stu.score<0){
return false;
}
return true;
}
void sortByName(Student *stu,int n){
for (int i = 1; i < n; i++){
if (stu[i - 1].name > stu[i].name ){
Student temp = stu[i];
int j = i;
while (j > 0 && stu[j - 1].name > temp.name){
stu[j] = stu[j - 1];
j--;
}
stu[j] = temp;
}
}
}
void sortByScore(Student *stu,int n){
for (int i = 1; i < n; i++){
if (stu[i - 1].score > stu[i].score ){
Student temp = stu[i];
int j = i;
while (j > 0 && stu[j - 1].score > temp.score){
stu[j] = stu[j - 1];
j--;
}
stu[j] = temp;
}
}
}
void stactical(Student * stu, int n){
int A=0,B=0,C=0;
float sum = 0,ave=0;
for(int i=0;i<n;i++){
sum+=stu[i].score;
if(stu[i].score>80)
{
++A;
}
if(stu[i].score>60)
++B;
else ++C;
}
ave = sum*1.0/n;
printf("统计信息:\n");
printf("优秀: %d\t及格: %d\t不及格: %d\t平均成绩: %.2f\n",A,B,C,ave);
}
void showAllInfo(Student *stu, int n){
int i=0;
while(i<n){
showInfo(stu[i++]);
}
}
int main(){
Student stu[MAX];
int count = 0;//记录实际输入的人数
while(true){
if(!getInfo(stu[count])){
break;
}
++count;
}
sortByName(stu,count);
printf("按姓名排序:\n");
showAllInfo(stu,count);
sortByScore(stu,count);
printf("按成绩排序:\n");
showAllInfo(stu,count);
stactical(stu,count);
getchar();
getchar();
return 0;
}