Advertisement
kk258966

進階程式設計 期中參考

Nov 6th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4.  
  5. int main(void)//主程式
  6. {
  7.     int i,max=0,min=10;
  8.     int num, no[10];
  9.     int count=0;   
  10.     srand(time(NULL));
  11. //////////////陣列歸零///////////////////////////
  12.  
  13.    
  14.     for (i=1; i<=10; i++)
  15.         no[i]=0;
  16.        
  17.        
  18. //////////////隨機產生不重複的數字//////////////////////////
  19.    
  20.     while (count<5)
  21.     {
  22.         num=rand()%10;
  23.         if (no[num]==0)
  24.         {
  25.            no[num]=1;
  26.            count = count + 1;
  27.         }
  28.     }
  29.  
  30. //////////////////////若陣列中的值為1則寫出///////////////
  31.  
  32.     FILE *written;
  33.     written = fopen("random.txt","w");
  34.    
  35.     for (i=1; i<=10; i++)
  36.     {
  37.         if (no[i]==1)
  38.         {
  39.                fprintf(written,"%d ",i);
  40.         }
  41.     }
  42.     fclose(written);
  43.     printf("寫檔完成\n");
  44.     printf("=====================\n\n");
  45.    
  46. /////////////////////讀檔找出最大值與最小值/////////////////////////////////////
  47.     printf("**讀檔**\n");
  48.     FILE *read;
  49.     int b;
  50.     read = fopen("random.txt","r");
  51.     printf("開出的號碼有:\n");
  52.     while((fscanf(read,"%d",&b))!=EOF)
  53.     {
  54.  
  55.  
  56.                if (b>max)
  57.                {
  58.                   max = b;
  59.                }
  60.      
  61.      
  62.                if (b<min)
  63.                {
  64.                   min = b;
  65.                }
  66.                printf("%d\n",b);      
  67.       }
  68.  
  69.     printf("=====================\n\n");
  70.    
  71.     printf("最大的號碼是:%d\n", max);
  72.     printf("最小的號碼是:%d\n", min);
  73.    
  74.  
  75.  
  76.  
  77.     printf("=====================\n\n");
  78.    system("pause");
  79.    return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement