鱼C论坛

 找回密码
 立即注册
查看: 2205|回复: 3

关于C语言的一道题

[复制链接]
发表于 2014-5-11 10:37:41 | 显示全部楼层 |阅读模式
50鱼币
文本文件num1.txt和num2.txt中各有一组用空格分隔的整数,将num1.txt和num2.txt中的整数联合排序,将其排序结果保存在num3.txt中。

最佳答案

查看完整内容

给我留点分吧{:7_163:}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-11 10:37:42 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-11 11:16:37 | 显示全部楼层
/*
文本文件num1.txt和num2.txt中各有一组用空格分隔的整数,
将num1.txt和num2.txt中的整数联合排序,
将其排序结果保存在num3.txt中。
*/
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<string.h>

void Bubble( int *a, int n )
{
        for( int i = 1; i < n; i++ )
        {
                for( int j = 0; j < n - i; j++ )
                {
                        if( a[j] > a[j+1] )
                        {
                                a[j] ^= a[j+1] ^= a[j] ^= a[j+1];
                        }
                }
        }
}

int main( void )
{
        FILE *num1, *num2, *num3;
        int a[1000], i;

        // 当前目录
        if( ! ( num1 = fopen( "num1.txt", "r" ) ) || ! ( num2 = fopen( "num2.txt", "r" ) ) )
        {
                printf("文件不存在!!\n\n");
                system("pause");
                exit( 0 );
        }

        i = 0;
        while( ! feof( num1 ) )
        {
                fscanf( num1, "%d", &a[i++] );
        }
        while( ! feof( num2 ) )
        {
                fscanf( num2, "%d", &a[i++] );
        }
        fclose( num1 );
        fclose( num2 );

        Bubble( a, i );

        num3 = fopen( "num3.txt", "w+" );  // 建立新文本读写

        for( int j = 0; j < i; j++ )
        {
                fprintf( num3, "%d ", a[j] );
        }
        fclose( num3 );
        system( "start num3.txt" );

        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-11 13:10:43 | 显示全部楼层
  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. int main()
  4. {
  5.         FILE *fp1,*fp2,*fp3;
  6.         int i,j,k,t;
  7.         char ch1[100],ch2[100];
  8.         int num[100];

  9.         if(!(fp1=fopen("num1.txt","r")))
  10.         {
  11.                 printf("Cannot Open the file!\n");
  12.                 exit(0);
  13.         }
  14.         if(!(fp2=fopen("num2.txt","r")))
  15.         {
  16.                 printf("Cannot Open the file!\n");
  17.                 exit(0);
  18.         }
  19.         if(!(fp3=fopen("num3.txt","w+")))
  20.         {
  21.                 printf("Cannot Open the file!\n");
  22.                 exit(0);
  23.         }
  24.        
  25.         //读字符串(以空格为标识符)并转化为整数
  26.         i=0;
  27.         j=0;

  28.         while(!feof(fp1))
  29.         {
  30.                 ch1[i] = fgetc(fp1);
  31.                 i++;
  32.                 if(ch1[i-1]==32 || ch1[i-1]==-1)
  33.                 {
  34.                         ch1[i]='\0';
  35.                         num[j]=atoi(ch1);
  36.                         j++;
  37.                         i=0;
  38.                 }
  39.         }

  40.         i=0;
  41.         while(!feof(fp2))
  42.         {
  43.                 ch2[i] = fgetc(fp2);
  44.                 i++;
  45.                 if(ch2[i-1]==32 || ch2[i-1]==-1)
  46.                 {
  47.                         ch2[i]='\0';
  48.                         num[j]=atoi(ch2);
  49.                         j++;
  50.                         i=0;
  51.                 }
  52.         }




  53.         //从大到小排序       
  54.         for( i=0 ; i<j-1 ; i++ )
  55.         {
  56.                 for( k=i ; k<j-1 ; k++ )
  57.                 {
  58.                         if( num[k]>num[i] )
  59.                         {
  60.                                 t = num[i];
  61.                                 num[i] = num[k];
  62.                                 num[k] = t;
  63.                         }
  64.                 }
  65.         }

  66.        
  67.         for( k=0 ; k<j-1 ; k++ )
  68.         {
  69.                 itoa(num[k],ch1,10);
  70.                 fputs( ch1 , fp3 );
  71.                 putc(' ' , fp3 );
  72.         }
  73.        

  74.         fclose(fp1);
  75.         fclose(fp2);
  76.         fclose(fp3);


  77.         return 0 ;
  78. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 14:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表