利用指针对三个字符串进行排序
指针变量作为形参,接收实参的地址
#include<iostream>
#include<string.h> //头文件包含strcmp函数、strcpy函数
using namespace std;
void sort(char* a, char* b, char* c); //函数声明
void swap(char* a, char* b);
int main()
{
char x[255], y[255], z[255];
cin >> x >> y >> z;
sort(x, y, z); //调用排序函数
cout << x << endl;
cout << y << endl;
cout << z << endl;
}
void sort(char* a, char* b, char* c) //排序函数
{
//选择排序
//第1遍排序
if (strcmp(a,b)>0) swap(a, b); //调用交换函数
if (strcmp(a,c)>0) swap(a, c);
//第2遍排序
if (strcmp(b,c)>0) swap(b, c);
}
void swap(char* a, char* b) //交换函数
{
char t[255];
strcpy(t, a);
strcpy(a, b);
strcpy(b, t);
}