#include#include #include #include typedef struct student { char stuCode[100]; char stuName[50][100]; int stuScore[3][100]; int sumScore[100]; } STUDENT; STUDENT stu[100]; //学生信息的结构体数组 void menu(void); //供给用户选择的图形页面菜单 void input(STUDENT stu[], int n); //输入数据:学号、姓名、各科分数 void output(STUDENT stu[], int n); //输出数据:学号、姓名、各科分数、总分、平均分 void scoreSortLow(STUDENT stu[], int n); //总分低到高排名 void scoreSortHigh(STUDENT stu[], int n); //总分高到低排名 void percentage(STUDENT stu[], int n); //输出各科成绩段百分比 void searchCode(STUDENT stu[], int n); //搜索学号 void searchName(STUDENT stu[], int n); //搜索姓名 void stuNameSort(STUDENT stu[], int n); //按字典顺序对学生姓名排序 void stuCodeSort(STUDENT stu[], int n); int inputChoice(void); //输入菜单中需要实现的功能选项 int legalInput(void); //检验合法性、输入学生人数 int inputChoice(void) //键入需要实现的功能选项 { int n, check; printf("Input your choice:n"); check = scanf("%d", &n); while (check != 1 || n < 1 || n > 11) { while (getchar() != 'n') ; printf("Input Error! Please enter again:n"); check = scanf("%d", &n); } return n; } void menu(void) { int n; printf("nn"); //与上次的输出结果保持间隔,便于美观 printf(" _____________________________________n"); //菜单输出 printf(" | MENU |n"); printf(" | 1.Input data |n"); printf(" | 2.Output data |n"); printf(" | 3.Search data |n"); printf(" | 4.Search name |n"); printf(" | 5.Sort high to low |n"); printf(" | 6.Sort low to high |n"); printf(" | 7.Sort by name |n"); printf(" | 8.Sort by code |n"); printf(" | 9.Percentage of courses |n"); printf(" | 10.Quit |n"); printf(" |___________________________________|n"); printf(" Please choose your function:(1-11):"); //功能选择 printf("n"); } void input(struct student stu[], int n) //读入学号姓名、各科成绩 { int i, j; for (i = 0; i < n; i++) { printf("Please input the number and name of student%dn", i + 1); printf("The number should include 6 figures.(Example:110110)n"); scanf("%s", stu[i].stuCode); while (strlen(stu[i].stuCode) != 6 || stu[i].stuCode[0] < '0' || stu[i].stuCode[0] > '9' || stu[i].stuCode[1] < '0' || stu[i].stuCode[1] > '9' || stu[i].stuCode[2] < '0' || stu[i].stuCode[2] > '9' || stu[i].stuCode[3] < '0' || stu[i].stuCode[3] > '9' || stu[i].stuCode[4] < '0' || stu[i].stuCode[4] > '9' || stu[i].stuCode[5] < '0' || stu[i].stuCode[5] > '9') { printf("Please input a valid number! n"); scanf("%s", stu[i].stuCode); } scanf("%s", stu[i].stuName); printf("Please input the scores of student%d by the order of Chinese、mathematics and Englishn", i); printf("Example:096 099 100(no more than 100)n"); scanf("%d%d%d", stu[i].stuScore[0], stu[i].stuScore[1], stu[i].stuScore[2]); printf("%s***%s***%d***%d***%dn", stu[i].stuCode, stu[i].stuName, *stu[i].stuScore[0], *stu[i].stuScore[1], *stu[i].stuScore[2]); } for (i = 0; i < n; i++) { *stu[i].sumScore = *stu[i].stuScore[0] + *stu[i].stuScore[1] + *stu[i].stuScore[2]; printf("%dn", *stu[i].sumScore); } } void output(STUDENT stu[], int n) //输出信息 { int i, j, k; printf("____________________________________________________________________________________________________________________n"); printf("| Report |n"); printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|n"); printf("| ID | name | Chinese grade | Math grade | English grade | Sum | Average |n"); printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|n"); for (i = 0; i < n; i++) { printf("|%11s%15s%13d%17d%18d%19.2f%19.2fn", stu[i].stuCode, stu[i].stuName, *stu[i].stuScore[0], *stu[i].stuScore[1], *stu[i].stuScore[2], (*stu[i].stuScore[0] + *stu[i].stuScore[1] + *stu[i].stuScore[2]) * 1.0, (*stu[i].stuScore[0] + *stu[i].stuScore[1] + *stu[i].stuScore[2]) / 3.0); } } int legalInput(void) //合法化输入,防御式编程 { int n, check; printf("Input the number of students:n"); check = scanf("%d", &n); while (check != 1 || n < 1 || n > 13) { while (getchar() != 'n') ; printf("Input Error! Please enter again:n"); check = scanf("%d", &n); } return n; } //已检验 void stuNameSort(STUDENT stu[], int n) //按照姓名字典顺序排序 { int i, j; struct student temp; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (strcmp(stu[i].stuName, stu[j].stuName) > 0) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } for (i = 0; i < n; i++) { printf("%s %sn", stu[i].stuCode, stu[i].stuName); } } void stuCodeSort(STUDENT stu[], int n) //按学号顺序排名 { int i, j; struct student temp; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (strcmp(stu[i].stuCode, stu[j].stuCode) > 0) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } for (i = 0; i < n; i++) { printf("%s %sn", stu[i].stuCode, stu[i].stuName); } } void searchCode(STUDENT stu[], int n) //按学号查找 { int i, j, flag = 0; char key[100]; printf("Please input the code of the student:"); scanf("%s", key); for (i = 0; i < n; i++) { if (strcmp(key, stu[i].stuCode) == 0) { printf("The student is %s,%s", stu[i].stuName, stu[i].stuCode); flag = 1; } } while (flag == 0) { printf("Not found!"); flag = 1; } } void searchName(STUDENT stu[], int n) //按姓名查找 { int i, j, flag = 0; char key[100]; printf("Please input the name of the student:"); scanf("%s", key); for (i = 0; i < n; i++) { if (strcmp(key, stu[i].stuName) == 0) { printf("The student is %s,%sn", stu[i].stuName, stu[i].stuCode); flag = 1; } } while (flag == 0) { printf("Not found!"); flag = 1; } } void percentage(STUDENT stu[], int n) //输出百分比 { float percent[3][5] = {}; int i, j, k; for (i = 0; i < 3; i++) { for (j = 0; j < n; j++) { if (*stu[j].stuScore[i] < 60) { percent[i][0]++; } if (*stu[j].stuScore[i] >= 60 && *stu[j].stuScore[i] < 70) { percent[i][1]++; } if (*stu[j].stuScore[i] >= 70 && *stu[j].stuScore[i] < 80) { percent[i][2]++; } if (*stu[j].stuScore[i] >= 80 && *stu[j].stuScore[i] < 90) { percent[i][3]++; } if (*stu[j].stuScore[i] >= 90 && *stu[j].stuScore[i] <= 100) { percent[i][4]++; } } } printf("语文———————数学——————英语n"); printf("___________n"); for (i = 0; i < 3; i++) { printf("不及格:%fn", percent[i][0] / n); printf("及格:%fn", (percent[i][1] / n)); printf("中等:%fn", (percent[i][2] / n)); printf("良好:%fn", (percent[i][3] / n)); printf("优秀:%fn", (percent[i][4] / n)); printf("___________n"); } } void scoreSortHigh(STUDENT stu[], int n) //总分高到低 { int i, j; STUDENT temp; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (*stu[j].sumScore > *stu[i].sumScore) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } printf("The rank from high to low is:n"); for (i = 0; i < n; i++) { printf("no.%d: %sn", i + 1, stu[i].stuName); } } void scoreSortLow(STUDENT stu[], int n) //总分低到高 { int i, j; STUDENT temp; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (*stu[j].sumScore < *stu[i].sumScore) { temp = stu[i]; stu[i] = stu[j]; stu[j] = temp; } } } printf("The rank from low to high is:n"); for (i = 0; i < n; i++) { printf("no.%d: %sn", i + 1, stu[i].stuName); } } int main(void) { int n, m; n = legalInput(); while (1) { menu(); m = inputChoice(); switch (m) { case 1: input(stu, n); system("pause"); system("cls"); break; case 2: output(stu, n); system("pause"); system("cls"); break; case 3: searchCode(stu, n); system("pause"); system("cls"); break; case 4: searchName(stu, n); system("pause"); system("cls"); break; case 5: scoreSortHigh(stu, n); system("pause"); system("cls"); break; case 6: scoreSortLow(stu, n); system("pause"); system("cls"); break; case 7: stuNameSort(stu, n); system("pause"); system("cls"); break; case 8: stuCodeSort(stu, n); system("pause"); system("cls"); break; case 9: percentage(stu, n); system("pause"); system("cls"); break; case 10: printf("Thanks for using!(感谢相遇捏!天天开心哦!) XD~"); system("pause"); return 0; default: return 0; } } return 0; }



