Advertisement
kk258966

9/26 進階程式設計上課程式練習二

Sep 26th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int main(void)
  5. {
  6.    
  7.     int i,j;
  8.     int no[10]; //no[10]放數字的陣列
  9.     int count=1; //記錄共幾個點
  10.     int tmp; //位置對調時所需的暫存參數
  11.    
  12.    
  13.     ////////////////////讀檔///////////////////////
  14.     printf("**讀檔**\n");
  15.     FILE *read;
  16.     int b;
  17.     read=fopen("random.txt","r");
  18.    
  19.     printf("開出的號碼有:\n");
  20.     while((fscanf(read,"%d",&b))!=EOF)
  21.     {
  22.     no[count]=b;
  23.     printf("%d\n",no[count]);
  24.     count++;
  25.     }
  26.     printf("==========讀檔完成 =============\n\n");
  27.    
  28.     ////////////////排序////////////////////
  29.    
  30.      printf("==========排序 =============\n\n");
  31.     for(i=1;i<count;i++)
  32. {
  33.     for(j=i+1;j<count;j++)
  34.     {
  35.                           if(no[i]>no[j])
  36.                           {
  37.                                          tmp=no[j];
  38.                                          no[j]=no[i];
  39.                                          no[i]=tmp;
  40.                           }
  41.     }
  42.                          
  43. }
  44.  
  45.  printf("==========排序完成 =============\n\n");
  46.  
  47.  printf("由小至大:");
  48.  
  49.    for(j=1;j<count;j++)
  50.    {
  51.      printf("%d  ",no[j]);                  
  52.    }
  53.      printf("\n\n");
  54.      
  55.      
  56.     system("pause");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement