//hwk1213.c --- sample program to read studat.txt
//========== Originally by tsaiwn@csie.nctu.edu.tw
//====== Modified by student_ID student_Name  _______________
// You have to modify this program to do this Homework
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
char* fileName = "studat.txt";  // Data File
long test( );
void sort( ); // your sorting program according to the score
void sort2Print( ); // your another sorting program (照學號)
void print( ); // 用來印出資料, 但只印出一部份!
int main( ) {
   long long tStart, tStop;  long i, k, n;
   double tRun;
   double sum, sumSquare, average, variance, std;
   test( );  // 這列以上不准改!
   // 打開檔案讀資料到記憶體, 也可寫成 function 在此 call 它 !
   // 一邊讀, 一邊算出每個人的學期總成績; 或讀完再算也可以!
   // ..注意, 還要算出全部同學的平均 和 標準差
   printf("Before sort...\n");
   print( ); // 叫用 print( ) 印出部份資料
   // todo..  print average, standard deviation
   tStart = clock( );
   sort( ); // 叫用 你的 sort 程式, 照總成績由高到低
   tStop = clock( );
   tRun = 1.0*(tStop - tStart)/CLOCKS_PER_SEC;
   printf("...Sorting done in %.5f seconds.\n", tRun);
   printf("After sort...\n");
   print( ); // 叫用 print( ) 印出部份資料
   // Extra credit: 要有名次, 同分者比期末考, 再同比期中考, ..
   printf("Now, sorting according to Student_ID ...");
   sort2Print( ); // 在裡面做照學號由小到大sort, 注意要測時間
   fprintf(stderr, "\nHit ENTER key...");
   getchar( );
   return 0;
}
void print( ) {  // 要用 Global 或是用傳參數的方式自行決定
   // 印出前面 6 筆
   printf(" ..."); // 印出 " ... "
   // 印出最後 6 筆
}
void sort( ) { // 要用 Global 或是用傳參數的方式自行決定
   // insertion sort, selection sort, bubble sort, quick sort ... 
   int i, j, k, gg;  // 寫好後請把下列 comment 掉 //
   // todo ...
   for(i=1;i<=258;++i)for(k=1;k<= 999999;++k) gg= gg*time(0);
}
void sort2Print( ) { // 要照學號由小到大 !
   // Ascending order, 記得要量時間 
   int i, j, k, gg;  // 寫好後請把下列 comment 掉 
   // todo ...
   for(i=1;i<=123;++i)for(k=1;k<= 999999;++k) gg= gg*time(0);
   // todo ... sort 好之後把量測到的時間印出來
   printf("\n === Now already sorted in Student_ID"
         " at ascending order.\n");
   print( );  // call print( ) to print some data
}
/// do NOT modify the following code
long test( ) {     // 這函數不准改 !
    long id; char name[9]; int s1, s2, s3, s4, kk;
    char buf[99];
    FILE* fp;
    fp = fopen(fileName, "rt");   // read, text
    if(fp==0) {  // Fail to open it
       fprintf(stderr, "File %s NOT found!\n", fileName);
       getchar( ); exit(38);  // force to exit with an error
    }
    fgets(buf, sizeof(buf), fp);  // stdin
    if(feof(fp) ) {
        fprintf(stderr, "EOF encounted.\n");
        return;
    }
    fprintf(stderr, "Line-1: %s\n", buf);
    fgets(buf, sizeof(buf), fp);  // stdin
    fprintf(stderr, "Line-2: %s\n", buf);
    s1=s2=s3=s4=0;
    kk = sscanf(buf,"%ld %s %d %d %d %d", 
       &id, name, &s1, &s2, &s3,&s4);
    printf("kk=%d\n", kk);
    if( kk != 4) printf(" something Wrong with data!\n");
    printf("id=%ld  name = %s\n", id, name);
    printf("score= %d %d %d %d\n", s1, s2, s3, s4);
    fprintf(stderr, " counting..."); fflush(stderr);
    kk = 2;  // already read 2 records
    while(! feof(fp) ) {   // 只要檔案還沒結束
       buf[0] = 0;
       fgets(buf, sizeof(buf), fp);
       if( feof(fp) ) {  // EOF 也可能有讀到不完整的 Line
          if(buf[0] == 0) break;  // 沒有讀到啦
       }
       ++kk;  // got one record
    }
    fprintf(stderr, " .. done.\nTotal %d records in file %s\n",
       kk, fileName);   // 印到 stderr (螢幕)
    fflush(stderr);
    fclose(fp); // close the file
    return kk;   // total  kk records in the file
} // test(
