键盘输入10个数(可能为整形、浮点型、双精度型及其字符类型),分别利用函数重载和函数模板,求出其中的最大值和最小值。
时间: 2023-09-08 20:04:56 浏览: 114
### 回答1:
好的,以下是代码:
使用函数重载:
#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;
}
template <typename T>
void findMinMax(T arr[], int size, T& minValue, T& maxValue) {
minValue = maxValue = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] < minValue) {
minValue = arr[i];
}
if (arr[i] > maxValue) {
maxValue = arr[i];
}
}
}
int main() {
int arrInt[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
float arrFloat[] = {1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, 0.1};
double arrDouble[] = {1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.90, 9.01, 0.12};
char arrChar[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int minInt, maxInt;
float minFloat, maxFloat;
double minDouble, maxDouble;
char minChar, maxChar;
findMinMax(arrInt, 10, minInt, maxInt);
findMinMax(arrFloat, 10, minFloat, maxFloat);
findMinMax(arrDouble, 10, minDouble, maxDouble);
findMinMax(arrChar, 10, minChar, maxChar);
cout << "Int min: " << minInt << ", max: " << maxInt << endl;
cout << "Float min: " << minFloat << ", max: " << maxFloat << endl;
cout << "Double min: " << minDouble << ", max: " << maxDouble << endl;
cout << "Char min: " << minChar << ", max: " << maxChar << endl;
return 0;
}
使用函数模板:
#include <iostream>
using namespace std;
template <typename T>
T max(T x, T y) {
return x > y ? x : y;
}
template <typename T>
T min(T x, T y) {
return x < y ? x : y;
}
template <typename T>
void findMinMax(T arr[], int size, T& minValue, T& maxValue) {
minValue = maxValue = arr[0];
for (int i = 1; i < size; ++i) {
if (arr[i] < minValue) {
minValue = arr[i];
}
if (arr[i] > maxValue) {
maxValue = arr[i];
}
}
}
int main() {
int arrInt[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
float arrFloat[] = {1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9, 9.0, 0.1};
double arrDouble[] = {1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89, 8.90, 9.01, 0.12};
char arrChar[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
int minInt, maxInt;
float minFloat, maxFloat;
double minDouble, maxDouble;
char minChar, maxChar;
findMinMax(arrInt, 10, minInt, maxInt);
findMinMax(arrFloat, 10, minFloat, maxFloat);
findMinMax(arrDouble, 10, minDouble, maxDouble);
findMinMax(arrChar, 10, minChar, maxChar);
cout << "Int min: " << minInt << ", max: " << maxInt << endl;
cout << "Float min: " << minFloat << ", max: " << maxFloat << endl;
cout << "Double min: " << minDouble << ", max: " << maxDouble << endl;
cout << "Char min: " << minChar << ", max: " << maxChar << endl;
return 0;
}
### 回答2:
使用函数重载和函数模板可以实现对不同类型的数求最大值和最小值。
使用函数重载的方法,可以分别创建多个函数来处理不同类型的数据。具体实现如下:
```cpp
#include<iostream>
#include<algorithm>
using namespace std;
int findMax(int nums[], int n) {
int maxNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] > maxNum) {
maxNum = nums[i];
}
}
return maxNum;
}
float findMax(float nums[], int n) {
float maxNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] > maxNum) {
maxNum = nums[i];
}
}
return maxNum;
}
double findMax(double nums[], int n) {
double maxNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] > maxNum) {
maxNum = nums[i];
}
}
return maxNum;
}
char findMax(char chars[], int n) {
char maxChar = chars[0];
for(int i=1; i<n; i++) {
if(chars[i] > maxChar) {
maxChar = chars[i];
}
}
return maxChar;
}
int findMin(int nums[], int n) {
int minNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] < minNum) {
minNum = nums[i];
}
}
return minNum;
}
float findMin(float nums[], int n) {
float minNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] < minNum) {
minNum = nums[i];
}
}
return minNum;
}
double findMin(double nums[], int n) {
double minNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] < minNum) {
minNum = nums[i];
}
}
return minNum;
}
char findMin(char chars[], int n) {
char minChar = chars[0];
for(int i=1; i<n; i++) {
if(chars[i] < minChar) {
minChar = chars[i];
}
}
return minChar;
}
int main() {
int int_nums[] = {1, 2, 3, 4, 5};
float float_nums[] = {1.1, 2.2, 3.3, 4.4, 5.5};
double double_nums[] = {1.111, 2.222, 3.333, 4.444, 5.555};
char char_chars[] = {'a', 'b', 'c', 'd', 'e'};
int int_max = findMax(int_nums, 5);
int float_max = findMax(float_nums, 5);
int double_max = findMax(double_nums, 5);
int char_max = findMax(char_chars, 5);
int int_min = findMin(int_nums, 5);
int float_min = findMin(float_nums, 5);
int double_min = findMin(double_nums, 5);
int char_min = findMin(char_chars, 5);
cout << "int类型数据的最大值为:" << int_max << endl;
cout << "float类型数据的最大值为:" << float_max << endl;
cout << "double类型数据的最大值为:" << double_max << endl;
cout << "char类型数据的最大值为:" << int(char_max) << endl;
cout << "int类型数据的最小值为:" << int_min << endl;
cout << "float类型数据的最小值为:" << float_min << endl;
cout << "double类型数据的最小值为:" << double_min << endl;
cout << "char类型数据的最小值为:" << int(char_min) << endl;
return 0;
}
```
使用函数模板的方法,可以创建一个函数模板来处理不同类型的数据。具体实现如下:
```cpp
#include<iostream>
#include<algorithm>
using namespace std;
template<typename T>
T findMax(T nums[], int n) {
T maxNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] > maxNum) {
maxNum = nums[i];
}
}
return maxNum;
}
template<typename T>
T findMin(T nums[], int n) {
T minNum = nums[0];
for(int i=1; i<n; i++) {
if(nums[i] < minNum) {
minNum = nums[i];
}
}
return minNum;
}
int main() {
int int_nums[] = {1, 2, 3, 4, 5};
float float_nums[] = {1.1, 2.2, 3.3, 4.4, 5.5};
double double_nums[] = {1.111, 2.222, 3.333, 4.444, 5.555};
char char_chars[] = {'a', 'b', 'c', 'd', 'e'};
int int_max = findMax(int_nums, 5);
float float_max = findMax(float_nums, 5);
double double_max = findMax(double_nums, 5);
char char_max = findMax(char_chars, 5);
int int_min = findMin(int_nums, 5);
float float_min = findMin(float_nums, 5);
double double_min = findMin(double_nums, 5);
char char_min = findMin(char_chars, 5);
cout << "int类型数据的最大值为:" << int_max << endl;
cout << "float类型数据的最大值为:" << float_max << endl;
cout << "double类型数据的最大值为:" << double_max << endl;
cout << "char类型数据的最大值为:" << int(char_max) << endl;
cout << "int类型数据的最小值为:" << int_min << endl;
cout << "float类型数据的最小值为:" << float_min << endl;
cout << "double类型数据的最小值为:" << double_min << endl;
cout << "char类型数据的最小值为:" << int(char_min) << endl;
return 0;
}
```
两种方法都可以实现对不同类型数据的最大值和最小值求解,根据实际需要选择使用函数重载或函数模板。
### 回答3:
函数重载和函数模板分别可以用来实现根据输入数的类型求最大值和最小值。下面是使用函数重载和函数模板的代码示例:
函数重载版本:
```cpp
#include <iostream>
using namespace std;
int getMax(int arr[], int n) {
int maxVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
double getMax(double arr[], int n) {
double maxVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
char getMax(char arr[], int n) {
char maxVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
int getMin(int arr[], int n) {
int minVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
double getMin(double arr[], int n) {
double minVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
char getMin(char arr[], int n) {
char minVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
int main() {
int intArr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double doubleArr[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0 };
char charArr[10] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
int maxInt = getMax(intArr, 10);
double maxDouble = getMax(doubleArr, 10);
char maxChar = getMax(charArr, 10);
int minInt = getMin(intArr, 10);
double minDouble = getMin(doubleArr, 10);
char minChar = getMin(charArr, 10);
cout << "最大整数:" << maxInt << endl;
cout << "最大浮点数:" << maxDouble << endl;
cout << "最大字符:" << maxChar << endl;
cout << "最小整数:" << minInt << endl;
cout << "最小浮点数:" << minDouble << endl;
cout << "最小字符:" << minChar << endl;
return 0;
}
```
函数模板版本:
```cpp
#include <iostream>
using namespace std;
template<typename T>
T getMax(T arr[], int n) {
T maxVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
}
}
return maxVal;
}
template<typename T>
T getMin(T arr[], int n) {
T minVal = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < minVal) {
minVal = arr[i];
}
}
return minVal;
}
int main() {
int intArr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
double doubleArr[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0 };
char charArr[10] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
int maxInt = getMax(intArr, 10);
double maxDouble = getMax(doubleArr, 10);
char maxChar = getMax(charArr, 10);
int minInt = getMin(intArr, 10);
double minDouble = getMin(doubleArr, 10);
char minChar = getMin(charArr, 10);
cout << "最大整数:" << maxInt << endl;
cout << "最大浮点数:" << maxDouble << endl;
cout << "最大字符:" << maxChar << endl;
cout << "最小整数:" << minInt << endl;
cout << "最小浮点数:" << minDouble << endl;
cout << "最小字符:" << minChar << endl;
return 0;
}
```
无论是函数重载还是函数模板,我们通过输入一个数组和数组长度来求最大值和最小值。由于题目中提到输入的数可能是整形、浮点型、双精度型及其字符类型,所以在函数重载的方法中我们分别定义了不同类型的函数。而在函数模板的方法中,通过使用`typename T`来表示不同的类型。这样就可以实现对不同类型数组的最大值和最小值求解。最后输出最大值和最小值的结果。
阅读全文