输入3个数a,b,c,按大小顺序输出_要求用到指针

来源:互联网 发布:网络客服工作指责 编辑:程序博客网 时间:2024/05/16 19:33

# include<stdio.h>
void swap(int *, int *);
int main(void)
{
int a, b, c;
int *p1, *p2, *p3;
printf("Input a, b ,c:");
scanf("%d %d %d", &a, &b, &c);
p1 = &a;
p2 = &b;
p3 = &c;
if(a>b)
swap(p1, p2);
if(a>c)
swap(p1, p3);
if(b>c)
swap(p2, p3);
printf("%d %d %d\n", a, b, c);
}
void swap(int *s1, int *s2)
{
int t;
t = *s1; *s1 = *s2; *s2 = t;
}

/*结果:

---------------------

Input a, b ,c:5 1 6
1 5 6

---------------------

*/

此题编制了一个函数swap(int *s1, int *s2),用以交换两个参数指针所指的数据。用主函数去调用子函数swap,将两个变量的值进行交换。
0 0