阶乘溢出判断算法与实现
本帖最后由 spy 于 2015-3-27 18:15 编辑#include <iostream.h>
#include <limits.h>
class Factoria
{
public
Factorial(unsigned short);
unsigned long getFactorial();
bool inRange();
private:
unsigned short num;
};
Factorial::Factorial(unsigned short num)
{
this->num = num;
}
unsigned long Factorial::getFactorial()
{
unsigned long result = 1;
for ( int i = 2; i <= num; i++ )
{
result *= i;
}
return result;
}
bool Factorial::inRange()
{
unsigned long max = ULONG_MAX;
for ( int i = num; i > 1; i-- )
{
max /= i;
}
if ( max < 1 )
{
return false;
}
else
{
return true;
}
}
int main()
{
unsigned short num;
cout << "请输入一个整数: ";
cin >> num;
Factorial factorial(num);
if ( factorial.inRange() )
{
cout << num << "的阶乘是: " << factorial.getFactorial() << "\n\n";
}
else
{
cout << "阶乘溢出!\n\n";
}
return 0;
} 请问怎么实现阶层溢出比如一个物品单价 是99怎么实现阶层溢出呢.谢谢!:loveliness: Htbs、 发表于 2015-3-27 19:36
请问怎么实现阶层溢出比如一个物品单价 是99怎么实现阶层溢出呢.谢谢!
你说的是怎么实现缓冲区溢出吗? spy 发表于 2015-3-27 20:08
你说的是怎么实现缓冲区溢出吗?
恩,谢谢楼主.
页:
[1]