利用指针方法编写程序:1.输入3个整数,按由大到小的顺序输出。2.输入3字符串,按由大到小的顺序输出。
时间: 2024-02-13 11:02:59 浏览: 60
1. 输入3个整数,按由大到小的顺序输出:
```c++
#include <iostream>
using namespace std;
void Sort(int *a, int *b, int *c) {
int tmp;
if (*a < *b) {
tmp = *a;
*a = *b;
*b = tmp;
}
if (*a < *c) {
tmp = *a;
*a = *c;
*c = tmp;
}
if (*b < *c) {
tmp = *b;
*b = *c;
*c = tmp;
}
}
int main() {
int a, b, c;
cout << "请输入3个整数:";
cin >> a >> b >> c;
Sort(&a, &b, &c);
cout << "由大到小的顺序输出为:" << c << " " << b << " " << a << endl;
return 0;
}
```
2. 输入3个字符串,按由大到小的顺序输出:
```c++
#include <iostream>
#include <string>
using namespace std;
void Sort(string *a, string *b, string *c) {
string tmp;
if (*a < *b) {
tmp = *a;
*a = *b;
*b = tmp;
}
if (*a < *c) {
tmp = *a;
*a = *c;
*c = tmp;
}
if (*b < *c) {
tmp = *b;
*b = *c;
*c = tmp;
}
}
int main() {
string a, b, c;
cout << "请输入3个字符串:";
cin >> a >> b >> c;
Sort(&a, &b, &c);
cout << "由大到小的顺序输出为:" << c << " " << b << " " << a << endl;
return 0;
}
```
阅读全文