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\n", a);
    printf("sorted b: %d\n", b);
    printf("sorted c: %d\n", c);
    
    return 0;
}

 

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...