怎么用c写一个成绩单·
从键盘上输入N名学生的姓名和某科成绩,按分数从高到低打印出成绩单。对非法数据,要求重新输入程序代码:
#include <stdio.h> #include <stdlib.h> struct Score_report { char name[7]; float grade; }; #define N 3 void Getsort(struct Score_report score[]); int main(void) { struct Score_report temp={0}; struct Score_report score[N]; int i, j; for(i=0; i<N; i++) { printf("Please enter the %d student's name and grade:\n", i+1); printf("Please you input the Name:"); scanf("%s", &score[i].name); printf("Please you input the Grade:"); scanf("%f", &score[i].grade); if(score[i].grade < 0.0 ||score[i].grade > 100.0) { printf("The grade you entered is not reasonable!\n"); printf("Please you re-enter:"); scanf("%f", &score[i].grade); } } for(i = 0; i < N-1; i++) { for(j = 0; j < N - 1 - i; j++) { if(score[j].grade < score[j+1].grade) { temp = score[j]; score[j] = score[j+1]; score[j+1] = temp; } } } printf("\n\n"); printf("*************Score report***************\n"); printf("\tName\tScore\n"); for(i=0; i<N; i++) { printf("\t%s\t%.1f\n", score[i].name, score[i].grade); } system("pause"); return 0; }
代码输出:
Please enter the 1 student's name and grade:
Please you input the Name:聂小倩
Please you input the Grade:-9
The grade you entered is not reasonable!
Please you re-enter:78
Please enter the 2 student's name and grade:
Please you input the Name:宁采臣
Please you input the Grade:109
The grade you entered is not reasonable!
Please you re-enter:54
Please enter the 3 student's name and grade:
Please you input the Name:燕赤霞
Please you input the Grade:99
*************Score report***************
Name Score
燕赤霞 99.0
聂小倩 78.0
宁采臣 54.0
[此贴子已经被作者于2019-6-10 15:47编辑过]