1.整数
代码:
#include<iostream>
using namespace std;
void swap(int *p1,int *p2)
{
int t;
t=*p1;
*p1=*p2;
*p2=t;
}
void sort(int *a,int *b,int *c)
{
if(*a>*b) swap(a,b);
if(*a>*c) swap(a,c);
if(*b>*c) swap(b,c);
}
int main()
{
int a,b,c;
int *p1,*p2,*p3;
while(~scanf("%d%d%d",&a,&b,&c))
{
p1=&a;
p2=&b;
p3=&c;
sort(p1,p2,p3);
printf("%d %d %d\n",*p1,*p2,*p3);
}
return 0;
}
结果:
2.字符串
原理:数组即指针
代码:
#include<iostream>
#include<string.h>
using namespace std;
void swap(char *p1,char *p2)
{
char t[1005];
strcpy(t,p1);
strcpy(p1,p2);
strcpy(p2,t);
}
int main()
{
char s1[1005],s2[1005],s3[1005];
while(gets(s1)!=NULL)
{
gets(s2);
gets(s3);
if(strcmp(s1,s2)>0) swap(s1,s2);
if(strcmp(s2,s3)>0) swap(s2,s3);
if(strcmp(s1,s3)>0) swap(s1,s3);
printf("\n%s\n%s\n%s\n",s1,s2,s3);
}
return 0;
}
结果: