用C++编写三个函数,分别求两个整数、两个浮点数、两个字符中较大的哪一个,并在主函数中调用测试
时间: 2023-12-06 22:44:53 浏览: 76
以下是用C++编写的三个函数,分别求两个整数、两个浮点数、两个字符中较大的哪一个:
```c++
#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 i1 = 10, i2 = 20;
float f1 = 3.14, f2 = 2.71;
char c1 = 'A', c2 = 'a';
cout << "Max of " << i1 << " and " << i2 << " is " << max(i1, i2) << endl;
cout << "Max of " << f1 << " and " << f2 << " is " << max(f1, f2) << endl;
cout << "Max of " << c1 << " and " << c2 << " is " << max(c1, c2) << endl;
return 0;
}
```
输出结果如下:
```
Max of 10 and 20 is 20
Max of 3.14 and 2.71 is 3.14
Max of A and a is a
```
阅读全文