Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418667
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: C/C++

2014-03-13 15:08:11


点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>

  4. int *read_arr1(int);
  5. int *read_arr2(int);
  6. int *order(int *,int);
  7. int *combine(int *,int *,int,int);

  8. int main()
  9. {
  10.     int num_arr1,num_arr2,*ptr_arr1,*ptr_arr2,*com_arr,i;

  11.     printf("Please input the size of arr1:");
  12.     scanf("%d",&num_arr1);
  13.     getchar();
  14.     printf("Please input the size of arr2:");
  15.     scanf("%d",&num_arr2);
  16.     getchar();

  17.     printf("\n\n");

  18.     ptr_arr1=read_arr1(num_arr1);
  19.     ptr_arr2=read_arr2(num_arr2);

  20.     ptr_arr1=order(ptr_arr1,num_arr1);
  21.     ptr_arr2=order(ptr_arr2,num_arr2);

  22.     printf("\n\n");

  23.     printf("The ascending order of arr1 is:");
  24.     for(i=0;i<num_arr1;i++)
  25.     {
  26.         printf("%d ",ptr_arr1[i]);
  27.     }
  28.     printf("\n");

  29.     printf("The ascending order of arr2 is:");
  30.     for(i=0;i<num_arr2;i++)
  31.     {
  32.         printf("%d ",ptr_arr2[i]);
  33.     }
  34.     printf("\n");

  35.     com_arr=combine(ptr_arr1,ptr_arr2,num_arr1,num_arr2);
  36.     com_arr=order(com_arr,num_arr1+num_arr2);

  37.     printf("The ascending order of combine of arr1 and arr2 is:");
  38.     for(i=0;i<num_arr1+num_arr2;i++)
  39.     {
  40.         printf("%d ",com_arr[i]);
  41.     }
  42.     printf("\n");

  43.     return 0;
  44. }

  45. int *read_arr1(int num_arr1)
  46. {
  47.     int *ptr_arr1,i;
  48.     ptr_arr1=malloc(num_arr1*sizeof(int));
  49.     
  50.     printf("Please iput the elements of arr1:");
  51.     for(i=0;i<num_arr1;i++)
  52.     {
  53.         scanf("%d",&ptr_arr1[i]);
  54.     }
  55.     getchar();
  56.     return ptr_arr1;
  57. }

  58. int *read_arr2(int num_arr2)
  59. {
  60.     int *ptr_arr2,i;
  61.     ptr_arr2=malloc(num_arr2*sizeof(int));
  62.     
  63.     printf("Please iput the elements of arr2:");
  64.     for(i=0;i<num_arr2;i++)
  65.     {
  66.         scanf("%d",&ptr_arr2[i]);
  67.     }
  68.     getchar();
  69.     return ptr_arr2;
  70. }

  71. int *order(int *arr,int num)
  72. {
  73.     int i,j,temp;
  74.     for(j=0;j<=num-2;j++)
  75.     {
  76.         for (i=0;i<num-j-1;i++)
  77.         {
  78.             if (arr[i]>arr[i+1])
  79.             {
  80.                 temp=arr[i];
  81.                 arr[i]=arr[i+1];
  82.                 arr[i+1]=temp;
  83.             }
  84.         }
  85.     }

  86.     return arr;
  87. }

  88. int *combine(int *ptr_arr1,int *ptr_arr2,int num_arr1,int num_arr2)
  89. {
  90.     int i,j,*com_arr;
  91.     com_arr=malloc(sizeof(int)*(num_arr1+num_arr2));

  92.     for(i=0;i<num_arr1;i++)
  93.     {
  94.         com_arr[i]=ptr_arr1[i];
  95.     }
  96.     for(j=0;j<num_arr2;i++,j++)
  97.     {
  98.         com_arr[i]=ptr_arr2[j];
  99.     }

  100.     return com_arr;
  101. }

阅读(1333) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~