#include #include typedef struct student { long ID; float score; }STU; void Print(STU s[], int n); STU * Read(int n); float Sum(STU s[], int n); void SortScore(STU s[], int n); void SortID(STU s[], int n); void Search(STU s[], int n); void Analyse(STU s[], int n); int main() { int n; //学生数量 STU *students; char key; //获取按键编号 float sum,ave; printf("Input student number(n<30):\n"); scanf("%d",&n); printf("Management for Students' scores\n1.Input record\n2.Caculate total and average score of course\n3.Sort in descending order by score\n4.Sort in ascending order by number\n5.Search by number\n6.Statistic analysis\n7.List record\n0.Exit\nPlease Input your choice:\n"); scanf(" %c",&key); while(1) { switch(key) { case '1': students = Read(n); break; case '2': sum = Sum(students, n); ave = (float)sum / n; printf("sum=%.0f,aver=%.2f\n",sum,ave); break; case '3': SortScore(students, n); break; case '4': SortID(students, n); break; case '5': Search(students, n); break; case '6': Analyse(students, n); break; case '7': Print(students, n); break; case '0': printf("End of program!"); exit(0); default: printf("Input error!\n"); } printf("Management for Students' scores\n1.Input record\n2.Caculate total and average score of course\n3.Sort in descending order by score\n4.Sort in ascending order by number\n5.Search by number\n6.Statistic analysis\n7.List record\n0.Exit\nPlease Input your choice:\n"); scanf(" %c",&key); } free(students); return 0; } //打印输出数据 void Print(STU s[], int n) { int i; for(i = 0; i < n; i++) { printf("%ld\t%.0f\n",s[i].ID, s[i].score); } } //创建数组并赋值 STU * Read(int n) { STU* p; int i; p = (STU*)malloc(sizeof(STU)*n); if(p == NULL) { printf("Error"); exit(0); } printf("Input student's ID, name and score:\n"); for(i = 0; i < n; i++) { scanf("%ld%f",&(p[i].ID),&(p[i].score)); } return p; } //计算和 float Sum(STU s[], int n) { int i; int sum = 0; for(i = 0; i < n; i++) { sum += s[i].score; } return sum; } //按照成绩排序 void SortScore(STU s[], int n) { int i,j; STU temp; for(i = 0; i < n; i++) { for(j = 0; j < n-1-i; j++) { if(s[j].score < s[j+1].score) { temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } } printf("Sort in descending order by score:\n"); Print(s, n); } void SortID(STU s[], int n) { int i,j; STU temp; for(i = 0; i < n; i++) { for(j = 0; j < n-1-i; j++) { if(s[j].ID > s[j+1].ID) { temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } } printf("Sort in ascending order by number:\n"); Print(s, n); } void Search(STU s[], int n) { long id; int i; printf("Input the number you want to search:\n"); scanf("%ld",&id); for(i = 0; i < n; i++) { if(id == s[i].ID) { printf("%ld\t%.0f\n",s[i].ID, s[i].score); return; } } printf("Not found!\n"); } void Analyse(STU s[], int n) { int i, score; int count[6] = {0}; for(i = 0; i < n; i++) { if(s[i].score >= 0 && s[i].score <= 100) { score = s[i].score / 10; switch(score) { case 0: case 1: case 2: case 3: case 4: case 5: count[0]++; break; case 6: count[1]++; break; case 7: count[2]++; break; case 8: count[3]++; break; case 9: count[4]++; break; case 10: count[5]++; break; } } } printf("<60\t%d\t%.2f%%\n",count[0], ((float)count[0]/n)*100); printf("%d-%d\t%d\t%.2f%%\n", 60, 69,count[1], ((float)count[1]/n)*100); printf("%d-%d\t%d\t%.2f%%\n", 70, 79,count[2], ((float)count[2]/n)*100); printf("%d-%d\t%d\t%.2f%%\n", 80, 89,count[3], ((float)count[3]/n)*100); printf("%d-%d\t%d\t%.2f%%\n", 90, 99,count[4], ((float)count[4]/n)*100); printf("%d\t%d\t%.2f%%\n",100,count[5],((float)count[5]/n)*100); return; }