Advertisement
kk258966

5/28 C++ 練習一

May 28th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.     int i;
  6.     int num,no[20];
  7.     int count,line;
  8.  
  9. int main(void)
  10. {
  11.     line=1;
  12.     FILE *written;
  13.     written=fopen("random.txt","w");
  14.     srand(time(NULL));
  15.    
  16.     ///////////////////陣列歸零///////////////////////
  17.     for(i=1;i<=9;i++)
  18.     no[i]=0;
  19.    
  20.     //////////////////隨機產生重複的數字//////////////
  21.    
  22.     while (line<6)
  23.     {
  24.           for (i=1;i<=10;i++)
  25.           {
  26.               num=rand()%100;
  27.               fprintf(written,"%d ",num);
  28.           }
  29.           fprintf(written,"\n");
  30.          
  31.           line++;
  32.     }  
  33.     fclose(written);
  34.    
  35.     printf("\n***寫檔完成***\n");
  36.    
  37.     ///////////////////讀檔///////////////////
  38.      
  39.     printf("\n****讀檔****\n");
  40.     FILE *read;
  41.     read=fopen("random.txt","r");
  42.     int string[20];
  43.    
  44.    
  45.    
  46.     while(! feof(read))
  47.     {
  48.                       count=0;
  49.                       for(i=1;i<=10;i++)
  50.                       {
  51.                                         fscanf(read,"%d",&string[i]);
  52.                                         printf("%d ",string[i]);
  53.                                         count=count+string[i];
  54.                       }
  55.                       printf("平均=%d",count/10);
  56.                       printf("\n");
  57.     }
  58.     fclose(read);                    
  59.     system("pause");
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement