Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将大数字乘以字符串-除非数字中有9,否则一切都很好 我必须在C++中创建一个类——BigInteger,它以字符串形式编写的非常大的数字。作为作业的一部分,我还必须预定义乘法,下面是我所做的: BigInteger& BigInteger::operator*(const BigInteger& rhs) { string tmp(num.length() + rhs.num.length(), '0'); //a string in which I'll be temporarily storing the result char carry = '0'; int d = 0; //I'll use this to move with one index to the left in the result for (int i = num.length() - 1; i >= 0; --i) //start with the multiplying from the end of the first number { carry = '0'; for (int j = rhs.num.length() - 1, z = tmp.length() - 1 - d; j >= 0; --j, --z) //start with the multiplying from the end of the second number and begin filling the result string (again from the end) { tmp[z] = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0')) + '0'; //basically add to the current number in the result the multiplication of the respective digits in the two original numbers, plus the carry from the previous mutiplication carry = ((tmp[z] - '0') / 10) + '0'; tmp[z] = ((tmp[z] - '0') % 10) + '0'; if (j == 0 && carry != '0') { tmp[z - 1] = carry; } } ++d; } if (carry != '0') { tmp[0] = carry; } else { tmp.erase(0, 1); } num = tmp; return *this; }_C++_String_Multiplication_Largenumber - Fatal编程技术网

将大数字乘以字符串-除非数字中有9,否则一切都很好 我必须在C++中创建一个类——BigInteger,它以字符串形式编写的非常大的数字。作为作业的一部分,我还必须预定义乘法,下面是我所做的: BigInteger& BigInteger::operator*(const BigInteger& rhs) { string tmp(num.length() + rhs.num.length(), '0'); //a string in which I'll be temporarily storing the result char carry = '0'; int d = 0; //I'll use this to move with one index to the left in the result for (int i = num.length() - 1; i >= 0; --i) //start with the multiplying from the end of the first number { carry = '0'; for (int j = rhs.num.length() - 1, z = tmp.length() - 1 - d; j >= 0; --j, --z) //start with the multiplying from the end of the second number and begin filling the result string (again from the end) { tmp[z] = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0')) + '0'; //basically add to the current number in the result the multiplication of the respective digits in the two original numbers, plus the carry from the previous mutiplication carry = ((tmp[z] - '0') / 10) + '0'; tmp[z] = ((tmp[z] - '0') % 10) + '0'; if (j == 0 && carry != '0') { tmp[z - 1] = carry; } } ++d; } if (carry != '0') { tmp[0] = carry; } else { tmp.erase(0, 1); } num = tmp; return *this; }

将大数字乘以字符串-除非数字中有9,否则一切都很好 我必须在C++中创建一个类——BigInteger,它以字符串形式编写的非常大的数字。作为作业的一部分,我还必须预定义乘法,下面是我所做的: BigInteger& BigInteger::operator*(const BigInteger& rhs) { string tmp(num.length() + rhs.num.length(), '0'); //a string in which I'll be temporarily storing the result char carry = '0'; int d = 0; //I'll use this to move with one index to the left in the result for (int i = num.length() - 1; i >= 0; --i) //start with the multiplying from the end of the first number { carry = '0'; for (int j = rhs.num.length() - 1, z = tmp.length() - 1 - d; j >= 0; --j, --z) //start with the multiplying from the end of the second number and begin filling the result string (again from the end) { tmp[z] = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0')) + '0'; //basically add to the current number in the result the multiplication of the respective digits in the two original numbers, plus the carry from the previous mutiplication carry = ((tmp[z] - '0') / 10) + '0'; tmp[z] = ((tmp[z] - '0') % 10) + '0'; if (j == 0 && carry != '0') { tmp[z - 1] = carry; } } ++d; } if (carry != '0') { tmp[0] = carry; } else { tmp.erase(0, 1); } num = tmp; return *this; },c++,string,multiplication,largenumber,C++,String,Multiplication,Largenumber,即使是123456788*887654321这样的大数字,一切都很好,但一旦我尝试将其中包含9的数字相乘(包括6789*9876这样的小数字),不仅中间的数字不正确,6789*9876和9876*6789之间也存在差异,包括“+”这样的符号在后一种情况下,撇号大致出现在中间 这里有没有人遇到过这样的问题,或者知道是什么原因造成的 编辑: 这里是预定义的字符串std::stringchar被签名,当您添加'0'并在以后用于进位和tmp[z]时,它会变为负数,因此最好将其存储在临时ìnt 6789*

即使是123456788*887654321这样的大数字,一切都很好,但一旦我尝试将其中包含9的数字相乘(包括6789*9876这样的小数字),不仅中间的数字不正确,6789*9876和9876*6789之间也存在差异,包括“+”这样的符号在后一种情况下,撇号大致出现在中间

这里有没有人遇到过这样的问题,或者知道是什么原因造成的

编辑:
这里是预定义的字符串
std::string
char
被签名,当您添加
'0'
并在以后用于
进位
tmp[z]
时,它会变为负数,因此最好将其存储在临时
ìnt

6789*9876
---------
           int(char(res + '0'))
-------------------------------
0+9*6+0=54 102
0+9*7+5=68 116
0+9*8+6=78 126
0+9*9+7=88 -120
8+8*6+0=56 104
8+8*7+5=69 117
8+8*8+6=78 126
8+8*9+7=87 -121
9+7*6+0=51 99
8+7*7+5=62 110
7+7*8+6=69 117
8+7*9+6=77 125
2+6*6+0=38 86
9+6*7+3=54 102
7+6*8+5=60 108
7+6*9+6=67 115
因此,您可以按如下方式更改代码:

    int res = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0'));
    carry = (res / 10) + '0';
    tmp[z] = (res % 10) + '0';
    if (j == 0)
    {
        tmp[z - 1] = carry;
    }

当您添加
'0'
并在以后用于
进位
tmp[z]
时,
std::string
char
被签名,它会变为负数,因此最好将其存储在一个临时
ìnt

6789*9876
---------
           int(char(res + '0'))
-------------------------------
0+9*6+0=54 102
0+9*7+5=68 116
0+9*8+6=78 126
0+9*9+7=88 -120
8+8*6+0=56 104
8+8*7+5=69 117
8+8*8+6=78 126
8+8*9+7=87 -121
9+7*6+0=51 99
8+7*7+5=62 110
7+7*8+6=69 117
8+7*9+6=77 125
2+6*6+0=38 86
9+6*7+3=54 102
7+6*8+5=60 108
7+6*9+6=67 115
因此,您可以按如下方式更改代码:

    int res = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0'));
    carry = (res / 10) + '0';
    tmp[z] = (res % 10) + '0';
    if (j == 0)
    {
        tmp[z - 1] = carry;
    }

什么是
biginger
?请出示a。您的代码不完整;特别是,它似乎缺少一个
main()
函数和至少一个
#include
。请输入您的代码,使其成为您问题的一部分(包括任何必要的输入,但最好不需要任何输入),然后我们可以尝试复制并解决它。顺便说一句,你还添加了一些代码片段,但它仍然不符合标准。mcve最重要的特性是,我们可以复制您的代码,以再现您因挑剔而观察到的相同行为,但
使用名称空间std
与您保存的4
std::
的字符数完全相同,因此简洁不是一个好借口:关于我最近在自己的代码中发现的一个bug的有趣故事:我手工编码的
intToStr()
在某些情况下返回的字符串带有
*(/)*,-*
,而不是我期望的(通常得到的)数字。错误很简单:
bool-minus=false;如果(value<0){value=-value;minus=true;}
在一种情况下失败:
INT\u MIN
。因此,下面的数字分隔对负数做了
%10
,负数也变为负数,因此添加到
'0'
中,提供了类似
*(/)*,-*
的内容。我想,告诉你这个是值得的…;-)调试愉快。
biginger
是什么?请出示a。您的代码不完整;特别是,它似乎缺少一个
main()
函数和至少一个
#include
。请输入您的代码,使其成为您问题的一部分(包括任何必要的输入,但最好不需要任何输入),然后我们可以尝试复制并解决它。顺便说一句,你还添加了一些代码片段,但它仍然不符合标准。mcve最重要的特性是,我们可以复制您的代码,以再现您因挑剔而观察到的相同行为,但
使用名称空间std
与您保存的4
std::
的字符数完全相同,因此简洁不是一个好借口:关于我最近在自己的代码中发现的一个bug的有趣故事:我手工编码的
intToStr()
在某些情况下返回的字符串带有
*(/)*,-*
,而不是我期望的(通常得到的)数字。错误很简单:
bool-minus=false;如果(value<0){value=-value;minus=true;}
在一种情况下失败:
INT\u MIN
。因此,下面的数字分隔对负数做了
%10
,负数也变为负数,因此添加到
'0'
中,提供了类似
*(/)*,-*
的内容。我想,告诉你这个是值得的…;-)调试愉快。
6789*9876
---------
           int(char(res + '0'))
-------------------------------
0+9*6+0=54 102
0+9*7+5=68 116
0+9*8+6=78 126
0+9*9+7=88 -120
8+8*6+0=56 104
8+8*7+5=69 117
8+8*8+6=78 126
8+8*9+7=87 -121
9+7*6+0=51 99
8+7*7+5=62 110
7+7*8+6=69 117
8+7*9+6=77 125
2+6*6+0=38 86
9+6*7+3=54 102
7+6*8+5=60 108
7+6*9+6=67 115
    int res = ((tmp[z] - '0') + (num[i] - '0') * (rhs.num[j] - '0') + (carry - '0'));
    carry = (res / 10) + '0';
    tmp[z] = (res % 10) + '0';
    if (j == 0)
    {
        tmp[z - 1] = carry;
    }