【问题标题】:C++ Add and Subtracting 100 digits numbersC++ 100位数字加减法
【发布时间】:2014-01-15 19:17:40
【问题描述】:

所以它应该做的是能够接受 const char* str 将其更改为 int 然后将其转换回字符串以用于输出。但它也应该能够将这些加减在一起。我正在通过我的前两个测试,但是我的加法发生了一些事情,它给了我一个接近答案的负数,但不是正确的。缩短了一点。

//For testing
int main()
{
BigInt result;

BigInt num1("999");
BigInt num2("4873");
BigInt num3("-739");
checkTest("Test 1", "999", num1.convertToString());
checkTest("Test 2", "-739", num3.convertToString());
result = num3.add(num4);
checkTest("Test 3", "-10610", result.convertToString());
return 0; 
}

这就是我遇到麻烦的地方

#include <iostream>

using namespace std;

class BigInt
{
public:

//An empty constructor, the {} is an empty body
BigInt() {}
BigInt(const char*);
BigInt add(const BigInt&);
BigInt operator+(const BigInt&);
BigInt subtract(const BigInt&);
BigInt operator-(const BigInt&);
string convertToString();

private:
static const int NUM_DIGITS = 100;
int numArr[NUM_DIGITS + 1];
void tensComplement();
};
BigInt::BigInt(const char* str) {
// TODO: CONVERT C-STRING TO BIGINT
int len = strlen(str) - 1;
int zero = NUM_DIGITS - 1;
for (int i = 0; i < NUM_DIGITS; i++){
    numArr[i] = 48;
}
for (int i = len; i >= 0; i--){
    numArr[zero] = str[i];
    zero--;
}
}

BigInt BigInt::add(const BigInt& rightOperand) {
BigInt objToReturn("0");
// TODO: ADD LOGIC HERE
int carry = 0;
for (int i = 100; i > 0; i--){
    int left = this->numArr[i] - 48;
    int right = rightOperand.numArr[i] - 48;
    int total = left + right;
    total += carry;
    if (total > 9){
        carry = 1;
    }else{
        carry = 0;
    }
    total = total % 10;

    objToReturn.numArr[i] = total + 48;
}
//num1 is the this object
cout << this->numArr[NUM_DIGITS];

//num2 is the rightOperand object
cout << rightOperand.numArr[NUM_DIGITS];

return objToReturn;
}

BigInt BigInt::operator+(const BigInt& rightOperand){
return add(rightOperand);
}

string BigInt::convertToString(){
// TODO: VALUE IN numArr CONVERTED TO STRING
int count = 0;
string str;
if(numArr[0] == 57){
    tensComplement();
}
for (int i = 0; i < NUM_DIGITS; i++){
    if(numArr[i] == 48 && count == 0){

    }else{
        str.push_back(numArr[i]);
        count++;
    }
}
return str;
}

void BigInt::tensComplement(){
// TODO: TENS COMPLEMENT OF THIS NUMBER
for (int i = 0; i <= 100; i++) {
    numArr[i] = 9 - numArr[i];
}
numArr[NUM_DIGITS] += 1;
for(int i = NUM_DIGITS; i >= 1; i--){
    if(numArr[i] == 10){
        numArr[i] = 0;
        numArr[i - 1] += 1;
    }
}
if(numArr[0] == 1){
    numArr[0] = 9;
}
}
//This helps with testing.
bool checkTest(string testName, string whatItShouldBe, string whatItIs) {

if (whatItShouldBe == whatItIs) {
    cout << "Passed " << testName << " last digit was: " << whatItIs.at(whatItIs.length()-1) << endl;
    return true;
}
else {
    if (whatItShouldBe == "") {
        cout << "**Failed test " << testName << " ** " << endl << "   Output was "<< whatItIs << endl << "   Output should have been blank. " << endl;
    } else {
        cout << "**Failed test " << testName << " ** " << endl << "   Output was "<< whatItIs << endl << "   Output should have been " << whatItShouldBe << endl;
    }
    return false;
}
}

【问题讨论】:

  • -1,Pastebins 死了,所以永远存在。
  • 代码太多了。
  • 你在哪里处理负数?而且您在数组中“向后”存储内容的代码似乎注定要失败,NUM_DIGITSD+1/-1 随处可见。将 LS 数字存储在 numArr[0] 中,不要将它们保存为字符 '0' 到 '9',将它们保存为数字 0-9。
  • 处理负数是我遇到的问题,不知道从哪里解决?

标签: c++ bigint


【解决方案1】:

这看起来像是“家庭作业”,但无论如何。

您可能会受益于在构造函数中检测负值并将该信息存储在标志中。这使得更容易决定如何在计算中使用数字。 正如 Roddy 所说,您可能会从将数字保留为数字而不是字符中受益,合理地,您将实现比 BigInt 中显示的更多的计算,并且您不需要为每个计算转换东西,想象一下处理乘法会是什么样子和像你一样添加的除法。

在尝试“加”做减法之前,您可能会从实现减法中受益。

我猜你在减法上有两个主要问题, 符号的四种排列和“借”而不是进位。

您是否计划过任何比较方法?

main() 如果您将所有测试都保留在其中,则用于测试的方法会给您带来更多价值。 您问题中的主要内容只有一个断言。 如果您保留已实现功能的断言,您可以确保它在添加新行为时继续工作。 试着找出你的边缘情况并为每一种情况进行测试。 还请记住,您不需要一次实现整个功能,验证一小部分您可以看到如何做可能会帮助您推理其余问题。

您的“checkTest”函数返回一个布尔值,用它来计算失败测试的数量并返回它,让您能够在任何测试失败时使构建失败。

您需要有一个返回值来说明是否有任何测试失败,因为在较大的构建中,测试失败将在噪音中消失,除非它们对您“尖叫”,例如构建失败。

我希望这可以帮助您找到解决方案并从问题中吸取教训。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多