Advertisement
kk258966

進階程式設計 期中參考2

Nov 6th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6.  
  7.  
  8. int main(void)//主程式
  9. {
  10.  
  11.    
  12.     FILE *read;
  13.     read = fopen("score.txt","r");
  14.     int i,j;
  15.     int string[50];
  16.     int count, sum;
  17.     float avg, degree[20];//avg放平均分數,degree[變數]=1為不及格, degree[變數]=0為及格
  18.     count = 0;
  19. ///////////////////////陣列歸零//////////////////////////////////////////////////
  20.  
  21.     for (i=1; i<=20; i++)
  22.         degree[i]=0;
  23.  
  24.  
  25.    
  26. /////////////////////讀成績檔////////////////////////////////////////////    
  27.    
  28.     printf("\n**讀成績檔**\n\n");  
  29.     while(! feof(read) ){
  30.             fscanf(read, "%d %d %d %d %d %d %d %d %d %d\r\n", &string[1],&string[2],&string[3],&string[4],&string[5],&string[6],&string[7],&string[8],&string[9],&string[10]);
  31.             sum=0;
  32.             for (i=1; i<=10; i++)
  33.             {
  34.                 sum = sum + string[i];
  35.                 printf("%d ",string[i]);
  36.             }
  37.             count = count + 1;
  38.             avg = (float)sum/10;
  39.            
  40.             if (avg<60)//將不及格的挑出
  41.                degree[count]=1;
  42.            
  43.  
  44.            
  45.             printf("   平均:%.2f",(float)sum/10);
  46.             printf("\n===============================================================\n\n");
  47.     }
  48.     fclose(read);
  49.  
  50. ////////////////////讀檔,若degree[count]=1則印出來/////////////////////////////////////////
  51.    
  52.     printf("**讀檔**\n");
  53.     char str[100]; 
  54.     int b,num;
  55.     read = fopen("student.txt","r");
  56.     count=1;
  57.    
  58.     printf("不及格的有:\n");
  59.     while (fgets (str, 65535, read))
  60.     {
  61.           if (degree[count]==1)
  62.           {
  63.            
  64.             printf("%s",str);
  65.           }
  66.           count=count+1;
  67.     }
  68.     fclose(read);
  69. /////////////////////////////////////////////////////////////////////////////////////////    
  70.  
  71.    printf("\n=====================\n\n");
  72.    system("pause");
  73.    return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement