C语言经典算法100例-066-练习使用指针
题目:
输入3 个数a,b,c,按大小顺序输出。
源码:
#include <stdio.h>
#include <stdlib.h>
//利用指针排序三个数 a b c
int main()
{
void swap(int *,int *);
int n1,n2,n3;
int *pointer1,*pointer2,*pointer3;
printf("Please input 3 numbers.\n");
scanf("%d,%d,%d",&n1,&n2,&n3);
pointer1=&n1;
pointer2=&n2;
pointer3=&n3;
if(n1>n2) swap(pointer1,pointer2);
if(n1>n3) swap(pointer1,pointer3);
if(n2>n3) swap(pointer2,pointer3);
printf("The sorted numbers are %4d %4d %4d",n1,n2,n3);
return 0;
}
void swap(int *p1,int *p2) //交换两个指针所指的值
{
int p;
p=*p1;
*p1=*p2;
*p2=p;
}