|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
输入三个数,按大小顺序输出这三个数,自己写了一段程序,测试了一下,没问题
#include <stdio.h> void main()
{
//void swap(int *x, int *y);
int a, b, c, *p1, *p2, *p3;
int t;
printf("Input 3 No.s:\n"); scanf("%d %d %d", &a, &b, &c); p1 = &a;
p2 = &b;
p3 = &c; if (*p1 < *p2)
{
//swap(p1, p2);
t = p1;
p1 = p2;
p2 = t;
}
if (*p1 < *p3)
{
//swap(p1, p3);
t = p1;
p1 = p3;
p3 = t;
}
if (*p2 < *p3)
{
//swap(p2, p3);
t = p2;
p2 = p3;
p3 = t;
} printf("a = %d, b = %d, c = %d\n", a, b, c);
printf("*p1 = %d, *p2 = %d *p3 = %d\n", *p1, *p2, *p3);
}
/*
void swap(int *x, int *y)
{
int *t;
t = x;
x = y;
y = t;
}*/
a,b,c的值都没有改变。但我想把程序分成两个函数,却出了点问题。
#include <stdio.h> void main()
{
void swap(int *x, int *y);
int a, b, c, *p1, *p2, *p3;
int t;
printf("Input 3 No.s:\n"); scanf("%d %d %d", &a, &b, &c); p1 = &a;
p2 = &b;
p3 = &c; if (*p1 < *p2)
{
swap(p1, p2);
/* t = p1;
p1 = p2;
p2 = t;*/
}
if (*p1 < *p3)
{
swap(p1, p3);
/* t = p1;
p1 = p3;
p3 = t;*/
}
if (*p2 < *p3)
{
swap(p2, p3);
/* t = p2;
p2 = p3;
p3 = t;*/
} printf("a = %d, b = %d, c = %d\n", a, b, c);
printf("*p1 = %d, *p2 = %d *p3 = %d\n", *p1, *p2, *p3);
} void swap(int *x, int *y)
{
int *t;
t = x;
x = y;
y = t;
}
a,b,c的值都被改变了。
怎么把前面的程序改成多个函数,程序功能不变?
|
|