Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************/
- /** 20141028 Exam 06 **/
- /** Maker : Yan **/
- /** E-Mail : [email protected] **/
- /** Date : 2014/10/28 pm.12:00 **/
- /************************************/
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char const *argv[])
- {
- FILE *fptr = fopen("input.txt", "r");
- float averages[1000] = {0}; //建立平均值陣列(稍後排序)
- int count[1000] = {0}; //建立每條目之顯示行陣列
- int temp_i = 0; //暫存總共有幾項條目
- for(int i = 0; ; i++)
- {
- char name[5] = {NULL};
- float score1 = 0, score2 = 0;
- fscanf(fptr, "%s %f %f", name, &score1, &score2);
- if((name[0] == NULL) || (score1 == 0) || (score2 == 0))
- {
- temp_i = i;
- break;
- }
- //建立平均值陣列
- float average = (score1 + score2) / 2;
- averages[i] = average;
- }
- //暫時關閉檔案資料流
- fclose(fptr);
- //設定每條目之顯示行陣列(count)
- int counter = 0;
- for(int i = 100; i > 0; i--)
- {
- for(int j = 0; j < temp_i; j++)
- {
- if((i > averages[j]) && (averages[j] >= (i - 1)))
- {
- count[j] = counter;
- counter++;
- continue;
- }
- }
- }
- //以二分排序法排序averages(平均值)陣列
- for(int i = 0; i < temp_i; i++)
- {
- for(int j = i; j < temp_i; j++)
- {
- if(averages[j] > averages[i])
- {
- float temp_average = averages[j];
- averages[j] = averages[i];
- averages[i] = temp_average;
- }
- }
- }
- /*data = 二維字串(長度50)陣列,格式如下
- [Name] [Chinese score] [English score]
- [Name] [Chinese score] [English score]
- [Name] [Chinese score] [English score]
- [Name] [Chinese score] [English score]*/
- char data[1000][3][50];
- fptr = fopen("input.txt", "r"); //再次開啟檔案資料
- //再次進行寫入data 以做後續輸出動作
- for(int i = 0; i < temp_i; i++)
- fscanf(fptr, "%s %s %s", data[count[i]][0], data[count[i]][1], data[count[i]][2]);
- //輸出資料
- for(int i = 0; i < temp_i; i++)
- printf("Name: %s.\n Chinese score: %s, English score: %s. Score average is: %.1f\n", \
- data[i][0], data[i][1], data[i][2] , averages[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment