使用C++编程:键盘输入10个数(可能为整形、浮点型、双精度型及其字符类型),分别利用函数重载和函数模板,求出其中的最大值和最小值。
时间: 2023-05-27 20:05:47 浏览: 106
函数重载实现:
```
#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;
}
double max(double a, double b) {
return a > b ? a : b;
}
char max(char a, char b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}
float min(float a, float b) {
return a < b ? a : b;
}
double min(double a, double b) {
return a < b ? a : b;
}
char min(char a, char b) {
return a < b ? a : b;
}
int main() {
int intArr[10];
float floatArr[10];
double doubleArr[10];
char charArr[10];
cout << "请输入10个整数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> intArr[i];
}
cout << "请输入10个浮点数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> floatArr[i];
}
cout << "请输入10个双精度数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> doubleArr[i];
}
cout << "请输入10个字符:" << endl;
for (int i = 0; i < 10; i++) {
cin >> charArr[i];
}
int maxInt = intArr[0], minInt = intArr[0];
float maxFloat = floatArr[0], minFloat = floatArr[0];
double maxDouble = doubleArr[0], minDouble = doubleArr[0];
char maxChar = charArr[0], minChar = charArr[0];
//求最大值和最小值
for (int i = 1; i < 10; i++) {
maxInt = max(intArr[i], maxInt);
minInt = min(intArr[i], minInt);
maxFloat = max(floatArr[i], maxFloat);
minFloat = min(floatArr[i], minFloat);
maxDouble = max(doubleArr[i], maxDouble);
minDouble = min(doubleArr[i], minDouble);
maxChar = max(charArr[i], maxChar);
minChar = min(charArr[i], minChar);
}
cout << "整数的最大值为:" << maxInt << endl;
cout << "整数的最小值为:" << minInt << endl;
cout << "浮点数的最大值为:" << maxFloat << endl;
cout << "浮点数的最小值为:" << minFloat << endl;
cout << "双精度数的最大值为:" << maxDouble << endl;
cout << "双精度数的最小值为:" << minDouble << endl;
cout << "字符的最大值为:" << maxChar << endl;
cout << "字符的最小值为:" << minChar << endl;
return 0;
}
```
函数模板实现:
```
#include <iostream>
using namespace std;
template <typename T>
T max(T a, T b) {
return a > b ? a : b;
}
template <typename T>
T min(T a, T b) {
return a < b ? a : b;
}
int main() {
int intArr[10];
float floatArr[10];
double doubleArr[10];
char charArr[10];
cout << "请输入10个整数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> intArr[i];
}
cout << "请输入10个浮点数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> floatArr[i];
}
cout << "请输入10个双精度数:" << endl;
for (int i = 0; i < 10; i++) {
cin >> doubleArr[i];
}
cout << "请输入10个字符:" << endl;
for (int i = 0; i < 10; i++) {
cin >> charArr[i];
}
int maxInt = intArr[0], minInt = intArr[0];
float maxFloat = floatArr[0], minFloat = floatArr[0];
double maxDouble = doubleArr[0], minDouble = doubleArr[0];
char maxChar = charArr[0], minChar = charArr[0];
//求最大值和最小值
for (int i = 1; i < 10; i++) {
maxInt = max(intArr[i], maxInt);
minInt = min(intArr[i], minInt);
maxFloat = max(floatArr[i], maxFloat);
minFloat = min(floatArr[i], minFloat);
maxDouble = max(doubleArr[i], maxDouble);
minDouble = min(doubleArr[i], minDouble);
maxChar = max(charArr[i], maxChar);
minChar = min(charArr[i], minChar);
}
cout << "整数的最大值为:" << maxInt << endl;
cout << "整数的最小值为:" << minInt << endl;
cout << "浮点数的最大值为:" << maxFloat << endl;
cout << "浮点数的最小值为:" << minFloat << endl;
cout << "双精度数的最大值为:" << maxDouble << endl;
cout << "双精度数的最小值为:" << minDouble << endl;
cout << "字符的最大值为:" << maxChar << endl;
cout << "字符的最小值为:" << minChar << endl;
return 0;
}
```
阅读全文