三个数比大小,从小到大输出,用指针和函数实现
#include <stdio.h>
void exchange(int *q1,int *q2,int *q3);
void swap(int *pt1,int *pt2);
int main(int argc, const char * argv[]) {
int a,b,c;
int *p1,*p2,*p3;
scanf("%d,%d,%d",&a,&b,&c);
p1 = &a;//把变量a的地址赋值给变量p1
p2 = &b;//把变量b的地址赋值给变量p2
p3 = &c;//把变量c的地址赋值给变量p3
exchange(p1,p2,p3);
printf("\n%d,%d,%d\n",a,b,c);
return 0;
}
void exchange(int *q1,int *q2,int *q3){
if (*q1 < *q2) swap(q1,q2);
if (*q1 < *q3) swap(q1,q3);
if (*q2 <*q3) swap(q2,q3);
}
void swap(int *pt1,int *pt2){
int temp;
temp = *pt1;
*pt1 = *pt2;
*pt2 = temp;
}
编程思路