谢谢了
//根据学生的学号,姓名和C语言考试成绩定义一个结构体.编写一个程序, //实现:(1)输入全班30个学生的学号,姓名和C语言考试的成绩 //(2)计算和输出平均成绩 //(3)按从高到低的次序输出前5名学生的姓名和成绩
#include <stdio.h> #include <string.h>
struct student_information { char stu_num[10]; char stu_name[8]; float stu_c_grade; }stu_infor[6];
void stu_input(); void stu_average(); void stu_sort();
void stu_input() { for(int i=0;i<6;i++) { printf("please enter th%d student's information:\n",i+1); printf("please enter the stu_num :\n"); gets(stu_infor[1].stu_num); fflush(stdin); printf("please enter the stu_name:\n"); gets(stu_infor[i].stu_name); fflush(stdin); printf("please enter the stu_c_grade:\n"); scanf("%f",&stu_infor[i].stu_c_grade); fflush(stdin); } }
void stu_average() { float total=0; float average; for(int j=0;j<6;j++) { total=total+stu_infor[j].stu_c_grade; } average=total/6; printf("the average c_language grade is :\n"); printf("%f\n",average); }
void stu_sort() { for(int k=0;k<6;k++) { int m=k; for(int n=k+1;n<6;n++) { if(stu_infor[m].stu_c_grade<stu_infor[n].stu_c_grade) m=n; } if(m!=k) { float temp; int length_nmu; char num[10]; char name[8]; strcpy(num,stu_infor[m].stu_num); strcpy(name,stu_infor[m].stu_name); strcpy(stu_infor[m].stu_num,stu_infor[k].stu_num); strcpy(stu_infor[k].stu_num,num); strcpy(stu_infor[m].stu_name,stu_infor[k].stu_name); strcpy(stu_infor[k].stu_name,name); temp=stu_infor[m].stu_c_grade; stu_infor[m].stu_c_grade=stu_infor[k].stu_c_grade; stu_infor[k].stu_c_grade=temp; } } for(int q=0;q<5;q++) { printf("the th%d stuent's name is : %s , the student's gragde is: %f\n",q+1,stu_infor[q].stu_name,stu_infor[q].stu_c_grade); } printf("\n"); }
void main() { stu_input(); stu_average(); stu_sort(); }