如何在biginteger类中减去这个问题 [英] How to solve this problem in subtract in biginteger class
本文介绍了如何在biginteger类中减去这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class BigInteger
{
public:
void Subtraction()
{
string string1,string2;
char x=0;
cout<<"Enter first number :";
cin>>string1;
cout<<"Enter second number :";
cin>>string2;
size_t n=max(string1.size(),string2.size());
if(n>string1.size())
string1=string(n-string1.size(),'0')+string1;
if(n>string2.size())
string2=string(n-string2.size(),'0')+string2;
string final(n,'0');
string::const_reverse_iterator s1 = string1.rbegin(), e = string1.rend(), s2 = string2.rbegin();
string::reverse_iterator f = final.rbegin();
while (s1!=e)
{
int ss1=*s1-'0';
if(*s1-'0'<*s2-'0')
{
int m=ss1+10;
++s1;
ss1--;
--s1;
x = m-(*s2-'0');
}
else
{
x=ss1-(*s2-'0');
}
*f = x + '0';
++s1;
++s2;
++f;
}
cout<< "final = " <<final << endl;
}
};
当我输入数字1 = 35
和数字2 = 6
结果= 39
这一行
ss1--;
问题是当从第二位减去一位没有变化发生在基本字符串
when I enter number 1= 35
and number 2=6
the result = 39
in this line
ss1--;
the problem is when subtract one from second digit no change happened in the basic string
推荐答案
对于数学,您应该将输入字符串转换为数字 ParseInt64 将为真正的大数字做这件事。
For mathematics you should convert the input string to a number ParseInt64 will do it for really big numbers.
long number1 = Int64.Parse(string1);
long number2 = Int64.Parse(string1);
//and now do the math
long result = number1 - number2;
如果您需要更大的数字,那么必须将数字从右侧。
if you need bigger numbers, so must split the numbers from the right side.
这篇关于如何在biginteger类中减去这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文