kk258966

10/17 進階程式設計練習一

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