如果我不想让一个整数超过100,是否有简单的方法来确保无论用户添加多少,该整数都不会超过100?
例如:
50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
如果我不想让一个整数超过100,是否有简单的方法来确保无论用户添加多少,该整数都不会超过100?
例如:
50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
试一下这个:
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;
template <class T> const T& min ( const T& a, const T& b ) { return !(b<a)?a:b; }
。 - jli注:
以下是一个非常简单的演示。
#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
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);
}
(50+60)-30 ≠ 50+(60-30)
。您真的想在每一步都截断结果吗?还是只在最后截断?那么“最后”是什么时候呢? - MSalters