C++ 使用函数模板查找任何输入数据类型的中间值?
C++ Find the middle value of any inputted data type using function templates?
我 运行 遇到了一些问题,一开始看起来很简单,但现在我似乎无法解决,任何帮助将不胜感激。
所以问题是:用户输入了三个数据类型的值,int、float 或 char。然后程序必须 return 中间值。
这是我目前的情况:
练习要求我使用函数模板而不是数组。
Header 文件:
#ifndef MIDTEMP_H
#define MIDTEMP_H
template <class T>
T doMiddle(T param, T param2, T param3)
{
if ((param<param2 && param>param3) || (param<param3 && param>param2) || (param == param2 == param3))
{
return param;
}
else if ((param2<param && param2>param3) || (param2<param3 && param2>param) || (param2 == param == param3))
{
return param2;
}
else if ((param3<param2 && param3>param) || (param3<param && param3>param2) || (param3 == param2 == param))
{
return param3;
}
else if (param == param2 && param < param3)
{
return param;
}
}
#endif
现在是测试我的 header 函数的代码:
#include<iostream>
#include "Middle.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << "Please enter 3 integers. IF input is incorrect please input until correct." << endl;
cout << endl;
int a, b, c;
while (!(cin >> a >> b >> c))
{
cin.clear();
cin.ignore();
}
cout << "The Middle is: " << doMiddle(a, b, c) << endl;
cout << endl;
//=======================================================
cout << "Please enter 3 floating point values. IF input is incorrect please input until correct." << endl;
cout << endl;
float d, e, f;
while (!(cin >> d >> e >> f))
{
cin.clear();
cin.ignore();
}
cout << "The Middle is: " << doMiddle(d, e, f) << endl;
cout << endl;
//=======================================================
cout << "Please enter 3 characters. IF input is incorrect please input until correct." << endl;
cout << endl;
char g, h, j;
while (!(cin >> g >> h >> j))
{
cin.clear();
cin.ignore();
}
cout << "The Middle is: " << doMiddle(g, h, j) << endl;
cout<<endl;
return 0;
}
所以它工作得很好但假设我这样做(从 cmd):
请输入 3 个整数。如果输入不正确,请输入正确为止。
5
3.2
6个
2个
中间是:2
请输入 3 个浮点值。如果输入不正确请输入直到
9.0
9
9.0
中间是:-1.#IND
请输入3个字符。如果输入不正确,请输入正确为止。
r
r
r
中间是:r
为什么要 return 那个 -1.#IND 值,有没有其他方法可以做到这一点而不需要那些荒谬的 if 语句?
你的函数打印 -1.#IND
因为你的模板函数不 return 如果所有检查都失败(如果所有值都相同则发生)的值。
当输入的所有值都相同时,(param == param2 == param3)
将始终计算为 false
,因为 param == param2
计算为 bool 而 bool(例如 true)不等于 9.0 (从你的例子)。
然后模板方法在没有 returning 的情况下退出,打印时您从堆栈中取出垃圾。
你的平等检查真的应该是第一个这样的:
if ((param == param2) && (param2 == param3))
return param;
然后你继续检查中间值,不要忘记在最后 else
个案例中 return 一些东西。
说到检查,如果只有 2 个值相等,您的逻辑就有缺陷。您还应该谨慎地直接比较 float
值,因为一些接近的值可能表示为相同的浮点值。
template<class T>
T doMiddle(T t1, T t2, T t3){
T arr[]={t1,t2,t3};
using std::begin; using std::end;
std::nth_element( begin(arr), begin(arr)+1, end(arr) );
return arr[1];
}
就可以了。为什么要重新发明轮子?
我 运行 遇到了一些问题,一开始看起来很简单,但现在我似乎无法解决,任何帮助将不胜感激。
所以问题是:用户输入了三个数据类型的值,int、float 或 char。然后程序必须 return 中间值。
这是我目前的情况:
练习要求我使用函数模板而不是数组。
Header 文件:
#ifndef MIDTEMP_H
#define MIDTEMP_H
template <class T>
T doMiddle(T param, T param2, T param3)
{
if ((param<param2 && param>param3) || (param<param3 && param>param2) || (param == param2 == param3))
{
return param;
}
else if ((param2<param && param2>param3) || (param2<param3 && param2>param) || (param2 == param == param3))
{
return param2;
}
else if ((param3<param2 && param3>param) || (param3<param && param3>param2) || (param3 == param2 == param))
{
return param3;
}
else if (param == param2 && param < param3)
{
return param;
}
}
#endif
现在是测试我的 header 函数的代码:
#include<iostream>
#include "Middle.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << "Please enter 3 integers. IF input is incorrect please input until correct." << endl;
cout << endl;
int a, b, c;
while (!(cin >> a >> b >> c))
{
cin.clear();
cin.ignore();
}
cout << "The Middle is: " << doMiddle(a, b, c) << endl;
cout << endl;
//=======================================================
cout << "Please enter 3 floating point values. IF input is incorrect please input until correct." << endl;
cout << endl;
float d, e, f;
while (!(cin >> d >> e >> f))
{
cin.clear();
cin.ignore();
}
cout << "The Middle is: " << doMiddle(d, e, f) << endl;
cout << endl;
//=======================================================
cout << "Please enter 3 characters. IF input is incorrect please input until correct." << endl;
cout << endl;
char g, h, j;
while (!(cin >> g >> h >> j))
{
cin.clear();
cin.ignore();
}
cout << "The Middle is: " << doMiddle(g, h, j) << endl;
cout<<endl;
return 0;
}
所以它工作得很好但假设我这样做(从 cmd):
请输入 3 个整数。如果输入不正确,请输入正确为止。
5 3.2 6个 2个 中间是:2
请输入 3 个浮点值。如果输入不正确请输入直到
9.0 9 9.0 中间是:-1.#IND
请输入3个字符。如果输入不正确,请输入正确为止。
r r r 中间是:r
为什么要 return 那个 -1.#IND 值,有没有其他方法可以做到这一点而不需要那些荒谬的 if 语句?
你的函数打印 -1.#IND
因为你的模板函数不 return 如果所有检查都失败(如果所有值都相同则发生)的值。
当输入的所有值都相同时,(param == param2 == param3)
将始终计算为 false
,因为 param == param2
计算为 bool 而 bool(例如 true)不等于 9.0 (从你的例子)。
然后模板方法在没有 returning 的情况下退出,打印时您从堆栈中取出垃圾。
你的平等检查真的应该是第一个这样的:
if ((param == param2) && (param2 == param3))
return param;
然后你继续检查中间值,不要忘记在最后 else
个案例中 return 一些东西。
说到检查,如果只有 2 个值相等,您的逻辑就有缺陷。您还应该谨慎地直接比较 float
值,因为一些接近的值可能表示为相同的浮点值。
template<class T>
T doMiddle(T t1, T t2, T t3){
T arr[]={t1,t2,t3};
using std::begin; using std::end;
std::nth_element( begin(arr), begin(arr)+1, end(arr) );
return arr[1];
}
就可以了。为什么要重新发明轮子?