c语言 10-3

1、

#include <stdio.h>

void swap2(int *x, int *y)
{
    int tmp;
    tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort2(int *n1, int *n2, int *n3)
{
    if(*n1 > *n2)   //将前两个中最小的排在最前面
        swap2(n1, n2);  //swap2函数接收的实参不用使用指针运算符,因为swap2的参数是指针,n1、n2、n3分别是指向a、b、c的指针。
    if(*n2 > *n3)   //将后两个中的较小的排在前面
        swap2(n2, n3);
    if(*n1 > *n2)   //将两个较小的中的最小的排在前面
        swap2(n1, n2);
}

int main(void)
{
    int a, b, c;
    puts("please input three integers.");
    printf("a = "); scanf("%d", &a);
    printf("b = "); scanf("%d", &b);
    printf("c = "); scanf("%d", &c);
    
    sort2(&a, &b, &c);
    printf("sorted a: %d
", a);
    printf("sorted b: %d
", b);
    printf("sorted c: %d
", c);
    
    return 0;
}

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14825589.html