/* 作者:一公41號謝宜珊 程式功能:Structure 應用練習 - 全班學期成績計算與排序 日期:2012.03.14 */
#include<stdio.h> #include<stdlib.h>
double calc(double, double, double);
struct student{ /*定義結構*/ char num[20]; char name[20]; double midterm; double final; double general; double ave; }stu[15], t; /*宣告結構變數*/
int main(void) { int i, k, j, l; int N; double temp; scanf("%d", &N); /*輸入總人數*/ for(i=0; i<N; i++) /*輸入每人資料並計算總成績*/ { scanf("%s %s %lf %lf %lf",&stu[i].name,&stu[i].num,&stu[i].midterm,&stu[i].final,&stu[i].general); stu[i].ave=calc(stu[i].midterm, stu[i].final, stu[i].general); } for(j=1; j<N; j++) /*排序(Insertion sort)*/ { temp=stu[j].ave; t=stu[j]; l=j-1; while(temp>stu[l].ave && l>0) { stu[l+1]=stu[l]; l--; } stu[l+1]=t; } printf("\n"); for(k=0; k<N; k++) /*輸出結果*/ { printf("%d. %s %s %.2lf %.2lf %.2lf %.2lf\n", k+1, stu[k].name, stu[k].num, stu[k].midterm, stu[k].final, stu[k].general, stu[k].ave); } system("PAUSE"); return 0; } double calc(double a, double b, double c) /*計算總成績之函數*/ { return a*0.3+b*0.3+c*0.4; }
/* 10 Anny 10030101 98 96 95 Mary 10030102 87 65 92 Jack 10030103 99 87 100 Jemery 10030104 95 92 99 Sandy 10030105 66 52 82 Sophia 10030106 54 52 88 Anita 10030107 88 92 98 Kiki 10030108 98 92 90 Anthony 10030109 66 76 86 Rose 10030110 88 94 92
1. Anny 10030101 98.00 96.00 95.00 96.20 2. Jack 10030103 99.00 87.00 100.00 95.80 3. Jemery 10030104 95.00 92.00 99.00 95.70 4. Anita 10030107 88.00 92.00 98.00 93.20 5. Kiki 10030108 98.00 92.00 90.00 93.00 6. Rose 10030110 88.00 94.00 92.00 91.40 7. Mary 10030102 87.00 65.00 92.00 82.40 8. Anthony 10030109 66.00 76.00 86.00 77.00 9. Sandy 10030105 66.00 52.00 82.00 68.20 10. Sophia 10030106 54.00 52.00 88.00 67.00 請按任意鍵繼續 . . . */
|