C++ 加减 100 位数字

标签 c++ bigint

所以它应该做的是能够接收一个 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;
}
}

最佳答案

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

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

在尝试使“add”做减法之前,您可能会受益于实现 subtract 方法。

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

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

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

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

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

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

关于C++ 加减 100 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21146276/

相关文章:

c++ - OpenProcess 函数返回无效句柄

c++ - C++中的延迟构造函数

c - gcc 中有 128 位整数吗?

string - Scala 将多行字符串转换为 BigInt

c++ - cin 和 boolean 输入

c++ - C++ 中的翻译单元与编译单元与目标文件与可执行文件与....

c++ - 是否有任何理由在没有方法的 header 上使用 extern "C"?

node.js - 如何在 Node.js 10 中从缓冲区读取/写入 bigint?

php - 如何转换 bigint 以防止 php 中的 sql 注入(inject)?

javascript - JavaScript 大整数不会在没有四舍五入的情况下转换为 String