使用C++ 编写三个函数,分别求两个整数、两个浮点数、两个 字符中较大的那一个,并在主函数中调用测试。
时间: 2023-12-06 19:44:52 浏览: 113
以下是使用 C++ 编写的三个函数,用于比较两个整数、两个浮点数和两个字符中较大的那一个:
```cpp
#include <iostream>
using namespace std;
// 比较两个整数中较大的那一个
int max(int a, int b) {
return (a > b) ? a : b;
}
// 比较两个浮点数中较大的那一个
float max(float a, float b) {
return (a > b) ? a : b;
}
// 比较两个字符中较大的那一个
char max(char a, char b) {
return (a > b) ? a : b;
}
int main() {
int a1 = 10, b1 = 20;
float a2 = 3.14, b2 = 2.71;
char a3 = 'A', b3 = 'B';
cout << "Max of " << a1 << " and " << b1 << " is " << max(a1, b1) << endl;
cout << "Max of " << a2 << " and " << b2 << " is " << max(a2, b2) << endl;
cout << "Max of " << a3 << " and " << b3 << " is " << max(a3, b3) << endl;
return 0;
}
```
输出结果为:
```
Max of 10 and 20 is 20
Max of 3.14 and 2.71 is 3.14
Max of A and B is B
```
阅读全文