← 返回列表

C 练习实例66

C 练习实例66 题目: 输入3个数a,b,c,按大小顺序输出。 程序分析: 利用指针方法。 实例 // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // # include<stdio.h> void swap(int *, int *); int main(void) { int a, b, c; int *p1, *p2, *p3; printf("输入 a, b ,c:\n"); 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; } 以上程序执行输出结果为: 输入 a, b ,c: 1 3 2 1 2 3

C 练习实例66

题目: 输入3个数a,b,c,按大小顺序输出。

程序分析: 利用指针方法。

实例

//  Created by www.codingdict.com on 15/11/9.
//  Copyright © 2013年 编程字典. All rights reserved.
//

# include<stdio.h>

void swap(int *, int *);
int main(void)
{
    int a, b, c;
    int *p1, *p2, *p3;
    printf("输入 a, b ,c:\n");
    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;
}

以上程序执行输出结果为:

输入 a, b ,c:
1 3 2
1 2 3

打开资源 ↗

交流与评论