练习指针变量作为函数参数
//输入三个整数:a,b,c要求按由大到小的顺序将他们输出
输出样例:
Input three integer numbers:
34 76 12
The original number a=34,b=76,c=12
The sorted numbers:76,34,12
--------------------------------
Process exited after 5.684 seconds with return value 0
请按任意键继续. . .
代码一(有瑕疵)
#include<stdio.h>
int main()
{
void sort(int *p1,int* p2,int* p3);
int a,b,c;
printf("Input three integer numbers:\n");
scanf("%d%d%d",&a,&b,&c);
printf("The original number a=%d,b=%d,c=%d\n",a,b,c);
int *pointer_a=&a,*pointer_b=&b,*pointer_c=&c;
sort(pointer_a,pointer_b,pointer_c);
printf("The sorted numbers:%d,%d,%d\n",a,b,c);
return 0;
}
void sort(int *p1,int *p2,int *p3)
{
int temp;
if(*p1<*p2){
temp=*p2;
*p2=*p1;
*p1=temp;
}
if(*p1<*p3){
temp=*p3;
*p3=*p1;
*p1=temp;
}
if(*p2<*p3){
temp=*p3;
*p3=*p2;
*p2=temp;
}
}
代码二(excellent)
#include<stdio.h>
int main()
{
void sort(int *p1,int *p2,int *p3);
int a,b,c;
printf("input three integer numbers:\n");
scanf("%d%d%d",&a,&b,&c);
printf("a = %d,b = %d, c = %d\n",a,b,c);
int *pointer_a=&a,*pointer_b=&b,*pointer_c=&c;
sort(pointer_a,pointer_b,pointer_c);
printf("The sorted numbers:%d,%d,%d\n",a,b,c);
return 0;
}
void sort(int *p1,int *p2,int *p3)
{
void swap(int *x,int *y);
if(*p1>*p2) swap(p1,p2);
if(*p1>*p3) swap(p1,p3);
if(*p2>*p3) swap(p2,p3);
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}