c 实现对文件操作:冒泡排序

分别实现选择排序(在下下一篇文章)和冒泡排序对以下文件进行排序。
支持如下形式调用:BubbleSort.exe input.txt output.txt 0
                                    BubbleSort.exe input.txt output.txt 1
                                     (0:从小到大,1:从大到小)。
注意中文处理(若无法处理可以只对学号排序,需要在文档中说明)

  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    /*读文件*/
 42    if((fp1=fopen("input.txt","r"))==NULL){
 43        printf("can not open.");
 44        exit(0);
 45    }
 46    sum=0;
 47    printf("the File '算法设计点名册' 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("如果希望从小到大排列请输入0,如果希望从大到小排列请输入1:\n");
 57    scanf("%d",&flag);
 58   /*选择排序*/
 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);//互换值
 73         //
 74         //strcpy(tempTwo,st[i].name);
 75         //strcpy(st[i].name,st[min].name);
 76         //strcpy(st[min].name,tempTwo);//互换值
 77   // }
 78    /*冒泡排序*/
 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    /*写入新文件*/
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,","));//得到学号和逗号
113        //printf("%s",st_New[j].num);
114        strcpy(st_New[j].name,strcat(st_New[j].num,st[j].name));//合并num和学生姓名
115        printf("%s",st_New[j].name);//输出排序后的点名册
116        fputs(st_New[j].name,fp2);
117    }
118 
119    /*关闭所有文件*/
120    fclose(fp1);
121    fclose(fp2);
122    /*NOUSe*/
123    scanf("%d",i);
124 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值