飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2732|回复: 1

[C/C++] 十九课作业

[复制链接]

该用户从未签到

发表于 2011-1-13 07:34:08 | 显示全部楼层 |阅读模式
本帖最后由 ngm20 于 2011-1-13 07:35 编辑
  1. #include<stdlib.h>
  2. void initial(int * pl,int n);/*初始化数组*/
  3. void sortintlist(int * pl,int n,char * type);/*排序调用函数*/
  4. void sortstol(int * pl,int n );/*从小到大排序*/
  5. void sortltos(int * pl,int n );/*从大到小排序*/
  6. void sortlx(int * pl,int n );/*乱序*/
  7. void change(int * pl,int * ph);/*交换二个数的位置*/
  8. void sortshow(int * pl,int n );/*显示数组元素*/
  9. int mycmpstr(char * pl,char *ph);/*比较字符串*/
  10. main( )
  11. {
  12.         int n;
  13.       int * pl;
  14.       printf("how many integer?\n");
  15.       scanf("%d",&n);
  16.       printf("please input %d integer\n",n);
  17.         pl=(int *)malloc(n*sizeof(int));

  18.       initial(pl,n);
  19.         printf("the numlist is\n");
  20.         sortshow(pl,n);

  21.         sortintlist(pl,n,"stol");
  22.         printf("the new numlist stol is\n");
  23.         sortshow(pl,n);

  24.         sortintlist(pl,n,"ltos");
  25.         printf("the new numlist ltos is\n");
  26.         sortshow(pl,n);

  27.         sortintlist(pl,n,"lx");
  28.         printf("the new numlist lx is\n");
  29.         sortshow(pl,n);

  30.         free(pl);
  31. }
  32. void initial(int * pl,int n)
  33. {
  34.         int i;
  35.         for(i=0;i<n;i++)
  36.         {
  37.                 scanf("%d",pl+i);
  38.         }
  39. }
  40. void sortstol(int * pl,int n )
  41. {
  42.         int i,j;
  43.         for(i=0;i<n-1;i++)
  44.         {
  45.                 for(j=0;j<n-i-1;j++)
  46.                 {
  47.                         if(*(pl+j)>*(pl+j+1))
  48.                         {
  49.                                 change((pl+j),(pl+j+1));
  50.                         }
  51.                 }
  52.         }
  53. }
  54. void change(int * pl,int * ph)
  55. {
  56.         int temp;
  57.         temp=*pl;
  58.         *pl=*ph;
  59.         *ph=temp;
  60. }
  61. void sortshow(int * pl,int n )
  62. {
  63.         int i;
  64.         for(i=0;i<n;i++)
  65.         {
  66.                 printf("%d ",*(pl+i));
  67.         }
  68.         printf("\n");
  69. }
  70. void sortltos(int * pl,int n )
  71. {
  72.         int i,j;
  73.         for(i=0;i<n-1;i++)
  74.         {
  75.                 for(j=0;j<n-i-1;j++)
  76.                 {
  77.                         if(*(pl+j)<*(pl+j+1))
  78.                         {
  79.                                 change((pl+j),(pl+j+1));
  80.                         }
  81.                 }
  82.         }
  83. }
  84. void sortlx(int * pl,int n )
  85. {
  86.         int i,j,k=1;
  87.         for(i=n-1;i>0;i--)
  88.         {
  89.                 for(j=n-1;j>n-i-1;j--)
  90.                 {
  91.                         if(*(pl+j-1)>*(pl+j) && k%2!=0)
  92.                         {
  93.                                 change((pl+j),(pl+j-1));
  94.                         }
  95.                         if(*(pl+j-1)<*(pl+j) && k%2==0)
  96.                         {
  97.                                 change((pl+j),(pl+j-1));
  98.                         }
  99.                 }
  100.                 k++;
  101.         }
  102. }
  103. void sortintlist(int * pl,int n,char * type)
  104. {
  105.         char a[]="stol";
  106.         char b[]="ltos";
  107.         char c[]="lx";
  108.         if(mycmpstr(a,type))
  109.         {
  110.                 sortstol(pl,n);
  111.         }
  112.         if(mycmpstr(b,type))
  113.         {
  114.                 sortltos(pl,n);
  115.         }
  116.         if(mycmpstr(c,type))
  117.         {
  118.                 sortlx(pl,n);
  119.         }
  120. }
  121. int mycmpstr(char * pl,char *ph)
  122. {
  123.         int i=0,bl=1;
  124.         while(*(pl+i)!='\0')
  125.         {
  126.                 if(*(pl+i)==*(ph+i))
  127.                 {
  128.                         i++;
  129.                         continue;
  130.                 }
  131.                 bl=0;
  132.                 break;
  133.         }               
  134.     return bl;
  135. }
复制代码
未命.jpg
PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2011-1-14 19:31:04 | 显示全部楼层
    支持楼主,佩服!
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

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