Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2529333
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-13 10:52:21

  输入三个整数,按照由小到大的顺序输出。
  由于需要使用指针,使用指针就牵扯到。对两个数进行交换。需要用两个指针对值进行修改。三个数如果使用排序算法,则需要对进行三次比较才能将三个数进行交换。代码如下:
 

#include <stdio.h>

void swap(int *, int *);
void sort2(int *, int *);
void sort3(int *, int *, int *);
int main(int argc, char *argv[])
{
    int a,b,c;
    int *p_1,*p_2,*p_3;
    printf("please input a,b,c 3 three number:");
    scanf("%d,%d,%d",&a,&b,&c);
    p_1 = &a,p_2= &b,p_3 = &c;
    
    sort3(p_1,p_2,p_3);
    printf("sort: %d < %d < %d\n",*p_1,*p_2,*p_3);
    system("pause");
    return 0;
}

void swap(int *p1, int *p2)
{
     int temp;
     temp = *p1;
     *p1 = *p2;
     *p2 = temp;
}

void sort2(int *p1, int *p2)
{
     if (*p1 > *p2)
     {
        swap(p1,p2);
     }
}

void sort3(int *p1, int *p2, int *p3)
{
     sort2(p1,p2);
     sort2(p1,p3);
     sort2(p2,p3);
}


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