C. File Operations: Bubble Sorting

Source: Internet
Author: User

Sort by selection (nextArticle) And bubble sort to sort the following files.
The following call is supported: bubblesort.exe input.txt output.txt 0
Bubblesort.exe input.txt output.txt 1
(0: from small to large, 1: from large to small ).
Note Chinese processing (if the student ID cannot be sorted, it must be described in the document)

 1 # Include <stdio. h> 2 # Include <stdlib. h> 3 # Include < String . H>4   5   # Define N 200 6   Struct  Student {  7       Char Num [ 200  ];  8       Char Name [ 200  ];  9       Float  Ave; 10   } St [N], temp1;  11   12   Struct  Newstudent {  13       Char Num [ 200  ];  14       Char Name [ 200  ];  15       Char Row [200  ];  16       Float  Ave;  17   } St_new [N], temp2;  18   19   //  Void selectionsort ();  20   21   22   Void Main ( Int Argc, Char ** Argv ){  23 File * FP1 ,* Fp2;  24      Int  I, j, sum, Min, K;  25      Char STR [ 200  ];  26      Char * Input ,* Output, flag;  27      Char Tempone [200 ], Temptwo [ 200 ], Tempthree [ 200 ], Tempfour [ 200  ];  28      //  Char * STR [N];  29   30      If (Argc < 4  ){  31 Input = " Input.txt  "  ;  32 Output = "  Output.txt  "  ;  33 Flag = '  0  '  ;  34   }  35      Else {  36 Input = * (argv + 1  );  37 Output = * (argv + 2  );  38 Flag = ** (argv + 3  );  39   }  40   41      /*  Read files */  42      If (FP1 = fopen ( "  Input.txt  " , "  R  " ) = Null ){  43 Printf ( "  Can not open.  "  );  44 Exit (0  );  45   }  46 Sum = 0  ;  47 Printf ( "  The file'AlgorithmDesign point register 'is: \ n  "  );  48      49      For (I = 0 ; (Fgets (STR, 200 , FP1 ))! = NULL; I ++, sum ++ ){  50 Strcpy (ST [I]. Num, strtok (STR, "  ,  "  ));  51 Strcpy (ST [I]. Name, strtok (null, "  ,  "  ));  52          //  Printf ("% s", St [I]. Num ); 53          //  Printf ("% s", St [I]. Name );  54   }  55 Printf ( "  % D  "  , Flag );  56 Printf ( "  If you want to arrange data in ascending order, enter 0. If you want to arrange data in ascending order, enter 1: \ n.  "  );  57 Scanf ("  % D  " ,& Flag );  58     /*  Select sort  */  59     //  For (I = 0; I <193; I ++ ){  60           //  Min = I;  61           //  For (j = I + 1; j <194; j ++) 62           //  If (FLAG ){  63           //  If (strcmp (ST [J]. Num, St [Min]. Num)> 0)  64           //  Min = J;  65           //  }  66           //  Else {  67           // If (strcmp (ST [J]. Num, St [Min]. Num) <0)  68           //  Min = J;  69           //  }  70           //  Strcpy (tempone, St [I]. Num );  71           //  Strcpy (ST [I]. Num, St [Min]. Num );  72           //  Strcpy (ST [Min]. Num, tempone ); //  Swap Value  73           //  74           //  Strcpy (temptwo, St [I]. Name );  75           //  Strcpy (ST [I]. Name, St [Min]. Name );  76           //  Strcpy (ST [Min]. Name, temptwo );  //  Swap Value  77     // }  78      /*  Bubble Sorting  */  79      For (I = 0 ; I < 193 ; I ++ )  80   {  81           For (J = 0 ; J < 193 -I; j ++){  82               If  (FLAG ){  83                 If (Strcmp (ST [J]. Num, St [J + 1 ]. Num) < 0  ){  84   Strcpy (tempthree, St [J]. Name );  85 Strcpy (ST [J]. Name, St [J + 1  ]. Name );  86 Strcpy (ST [J + 1  ]. Name, tempthree );  87   88   Strcpy (tempfour, St [J]. Num );  89 Strcpy (ST [J]. Num, St [J + 1  ]. Num );  90 Strcpy (ST [J + 1  ]. Num, tempfour );  91   }  92  }  93               Else  {  94                   If (Strcmp (ST [J]. Num, St [J + 1 ]. Num)> 0  ){  95   Strcpy (tempthree, St [J]. Name );  96 Strcpy (ST [J]. Name, St [J + 1  ]. Name );  97 Strcpy (ST [J + 1  ]. Name, tempthree );  98   99   Strcpy (tempfour, St [J]. Num );  100 Strcpy (ST [J]. Num, St [J + 1  ]. Num );  101 Strcpy (ST [J + 1  ]. Num, tempfour );  102   }  103  }  104   }  105   }  106      /*  Write New File  */  107      If (Fp2 = fopen ( "  Output.txt  " , "  WB  " ) =Null ){  108 Printf ( "  Can not open.  "  );  109 Exit ( 0  );  110   }  111      For (J = 0 ; J <sum; j ++ ){  112 Strcpy (st_new [J]. Num, strcat (ST [J]. Num, "  ,  " )); //  Obtain the student ID and comma  113          //  Printf ("% s", st_new [J]. Num );  114 Strcpy (st_new [J]. Name, strcat (st_new [J]. Num, St [J]. Name )); //  Merge num and Student name  115 Printf ( "  % S " , St_new [J]. Name ); //  Output sorted roster  116   Fputs (st_new [J]. Name, fp2 );  117   }  118   119      /*  Close all files  */  120   Fclose (FP1 );  121   Fclose (fp2 ); 122      /*  Nouse  */  123 Scanf ( "  % D  "  , I );  124 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.