我们期中考试的题目,要求如下
//创建一个CNumber的类
//支持任意精度整数
//加法,乘法,减法
//除法,阶乘和开方运算
//并给出侧试程序
哪位大侠会啊,给菜鸟我个c++的原程序,快啊
谢谢啊
我们期中考试的题目,要求如下
//创建一个CNumber的类
//支持任意精度整数
//加法,乘法,减法
//除法,阶乘和开方运算
//并给出侧试程序
哪位大侠会啊,给菜鸟我个c++的原程序,快啊
谢谢啊
给你一个求阶乘的例子
///////////////////////////////////
CString CNumber::Fac(int n)
{
CString m_etext;
BOOL minus = FALSE;
if(n<0)
{
n = -n;
if(n%2 != 0) minus = TRUE;
}
int j,jinwei,i,m;
m = n;
char ch[MAX];
int BaseNum[MAX],TotalNum[MAX],Num[MAX],Pos[MAX];
for(i=0;i<MAX;i++)
{
BaseNum[i]=TotalNum[i]=Num[i]=Pos[i]=0;
ch[i] = '\0';
}
i = MAX-1;
while( m>=10 )
{
BaseNum[i] = m % 10;
m = m / 10;
i--;
}
BaseNum[i] = m;
TotalNum[MAX-1]=1;
//the process of factorial
while (n > 0)
{
//The low digit is at the end of the array
//The array TotalNum saves the last result
//The array Num saves the result in the center process
//The array Pos saves the single digit of BaseNum multiply the TotalNum
for(i=MAX-1;i>=0;i--)
{
jinwei = 0;
for(j=MAX-1;j>=0;j--)
{
m = TotalNum[j]*BaseNum[i]+jinwei;
Pos[j] = m % 10;
jinwei = m / 10;
}
int l;
for(j=1;j<=MAX-1-i;j++)
{
for( l=1;l<MAX;l++) Pos[l-1] = Pos[l];
Pos[MAX-1] = 0;
}
jinwei = 0;
for(j=MAX-1;j>=0;j--)
{
m = Num[j]+Pos[j]+jinwei;
Num[j] = m % 10;
jinwei = m / 10;
}
for(j=0;j<MAX;j++) Pos[j]=0;
}
for(j=0;j<MAX;j++)
TotalNum[j] = Num[j];
for(j=0;j<MAX;j++) Num[j]=0;
int jiewei = 1;
//BaseNum[]--;
for(j=MAX-1;j>=0;j--)
if(BaseNum[j]>=jiewei)
{
BaseNum[j] -= jiewei;
jiewei = 0;
}
else
{
BaseNum[j] = BaseNum[j] + 10 - jiewei;
jiewei = 1;
}
n = n - 1;
}
i=0;
while (0 == TotalNum[i++]) {}
i -= 1;
for(j=0;i<MAX;i++,j++)
ch[j] = (char) (int('0')+TotalNum[i]);
if (minus) m_etext = "-";
else m_etext = "";
int LengthCh = strlen(ch);
m=0;//The m saves the amount of 0s that are at the end
for(i=MAX-1;i>=0;i--)
{
if (('0' == ch[i])||('\0' == ch[i]))
{
if ('0' == ch[i]) m++;
ch[i] = '\0';
}
else
break;
}
m_etext += ch;
if ((m>0)&&(LengthCh<=MAXSHOW-5))
for(i=1;i<=m;i++) m_etext += '0';
else if ((m>0)&&(LengthCh>MAXSHOW-5))
{
m_etext += "e+";
itoa(m,ch,10);
m_etext += ch;
}
return m_etext;
}