如果我不希望整数超过100,是否有任何简单的方法来确保整数不超过100,无论用户添加多少?你能设置一个整数(C++)的最大限制吗?
例如,
50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
在此先感谢
如果我不希望整数超过100,是否有任何简单的方法来确保整数不超过100,无论用户添加多少?你能设置一个整数(C++)的最大限制吗?
例如,
50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
在此先感谢
下面是一个简单的ADT为通用BoundedInt的一个非常简单的和比较完整的例子。
注:
一个非常简单的演示如下。
#include <limits>
#include <iostream>
#include <boost/operators.hpp>
template <
typename Int=unsigned int,
Int Max=std::numeric_limits<Int>::max()>
struct BoundedInt : boost::operators<BoundedInt<Int, Max> >
{
BoundedInt(const Int& value) : _value(value) {}
Int get() const { return std::min(Max, _value); }
operator Int() const { return get(); }
friend std::ostream& operator<<(std::ostream& os, const BoundedInt& bi)
{ return std::cout << bi.get() << " [hidden: " << bi._value << "]"; }
bool operator<(const BoundedInt& x) const { return get()<x.get(); }
bool operator==(const BoundedInt& x) const { return get()==x.get(); }
BoundedInt& operator+=(const BoundedInt& x) { _value = get() + x.get(); return *this; }
BoundedInt& operator-=(const BoundedInt& x) { _value = get() - x.get(); return *this; }
BoundedInt& operator*=(const BoundedInt& x) { _value = get() * x.get(); return *this; }
BoundedInt& operator/=(const BoundedInt& x) { _value = get()/x.get(); return *this; }
BoundedInt& operator%=(const BoundedInt& x) { _value = get() % x.get(); return *this; }
BoundedInt& operator|=(const BoundedInt& x) { _value = get() | x.get(); return *this; }
BoundedInt& operator&=(const BoundedInt& x) { _value = get() & x.get(); return *this; }
BoundedInt& operator^=(const BoundedInt& x) { _value = get()^x.get(); return *this; }
BoundedInt& operator++() { _value = get()+1; return *this; }
BoundedInt& operator--() { _value = get()-1; return *this; }
private:
Int _value;
};
使用范例:
typedef BoundedInt<unsigned int, 100> max100;
int main()
{
max100 i = 1;
std::cout << (i *= 10) << std::endl;
std::cout << (i *= 6) << std::endl;
std::cout << (i *= 2) << std::endl;
std::cout << (i -= 40) << std::endl;
std::cout << (i += 1) << std::endl;
}
10 [hidden: 10]
60 [hidden: 60]
100 [hidden: 120]
60 [hidden: 60]
61 [hidden: 61]
有了一个完全的C++ 11兼容的编译器,你甚至可以定义一个用户自定义文字转换:
typedef BoundedInt<unsigned int, 100> max100;
static max100 operator ""_b(unsigned int i)
{
return max100(unsigned int i);
}
所以,你可以写
max100 x = 123_b; // 100
int y = 2_b*60 - 30; // 70
试试这个:
std::min(50 + 40, 100);
std::min(50 + 50, 100);
std::min(50 + 60, 100);
std::min(50 + 90, 100);
http://www.cplusplus.com/reference/algorithm/min/
另一种办法是每次操作后,使用此:
if (answer > 100) answer = 100;
您可以编写包含超载operator+
,operator-
等运算符重载的例子自己的类IntegerRange,看到复杂的类here。
最简单的方法是创建一个保存该值的类,而不是使用整型变量。
class LimitedInt
{
int value;
public:
LimitedInt() : value(0) {}
LimitedInt(int i) : value(i) { if (value > 100) value = 100; }
operator int() const { return value; }
LimitedInt & operator=(int i) { value = i; if (value > 100) value = 100; return *this; }
};
您可能会遇到麻烦,结果不符合预期。结果应该是70或90?
LimitedInt q = 2*60 - 30;
C++不允许覆盖(重定义)基本类型的运算符。
如果自定义整数类(如上面建议的)不属于你的“简单方法”的定义,那么你的问题的答案是没有,没有简单的方法来做你想做的。
我知道这是一个旧帖子,但我认为它可能对某些人仍然有用。我用这个来设定上限和下限:
bounded_value = max(min(your_value,upper_bound),lower_bound);
你可以在这样的函数中使用它:
float bounded_value(float x, float upper, float under){
return max(min(x,upper),lower);
}
只不过是简单的更简单,如果 – Mansuro
@Mansuro是不会停止你在编译时。 – corsiKa
写一堂课? –