Chinaunix首页 | 论坛 | 博客
  • 博客访问: 340616
  • 博文数量: 88
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 885
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-21 14:50
文章分类

全部博文(88)

文章存档

2010年(88)

我的朋友

分类: C/C++

2010-07-10 13:15:13

/*
 * Description:
 *         文件中有一个数组,排序后输出到另一文件  的c代码实现
 * Author  :FinL
 * Language: C
 * Date    : 2010-08-29
 */

#include
#include

void bubble_sort(int *a,int n)
{
int tmp;
int i,j;
bool exchange=1;
for(i=0;i
{
exchange=0;
for(j=0;j
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
exchange=1;
}
}
}
}

int main()
{
int tmp;
int i = 0,j;
int num=0;
int* arry;

FILE *fp = fopen("int.txt","r");
if(NULL==fp)
{
printf("Open file int.txt failed.\n");
exit(0);
}

FILE *fp1 = fopen("int_sort.txt","w");
if(NULL==fp1)
{
printf("Open file int_sort.txt failed.\n");
exit(0);
}

while(fscanf(fp,"%d\n",&tmp)!=-1){
num++;
}

printf("\n");
fseek(fp,0,SEEK_SET);
printf("num : %d",num);

printf("\n");

arry = (int *)malloc(num);

while(fscanf(fp,"%d\n",&tmp)!=-1){
arry[i++]=tmp;
}

for(j=0;j
{
printf("%d ",arry[j]);
}
printf("\n");
fclose(fp);

// arry after sorting.
bubble_sort(arry,num);
for(j=0;j
printf("%d ",arry[j]);
}
printf("\n");

for(j=0;j
{
fprintf(fp1,"%d ",arry[j]);
}

fclose(fp1);
return 0;
}
阅读(995) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~