问题
So what it's supposed to do is be able to take in a const char* str change it to an int then have it be converted back into a string for the output. But it is also supposed to be able to add and subtract these together. I'm passing my first two tests but something is going on with my addition, its giving me a negative number close to the answer but not the right one. Shortened it up a bit.
//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;
}
Here is where I'm having trouble
#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:
This looks like "home-work" but any way.
You would probably benefit from detecting negative values in the constructor and storing that information in a flag. This makes it easier to decide how to use the number in calculations. As Roddy said you would probably benefit from keeping the digits as numbers instead of characters, reasonably you would be implementing more calculations than displays in BigInt and you will not need to convert things for each calculation, just imagine what it would be like to handle multiplication and division like you do add.
You might benefit from implementing the subtract method before trying to make "add" do subtraction.
I would guess you have two main problems with subtraction, the four permutations of signs and "borrowing" instead of carry.
Have you planed for any compare method?
main() for testing would give you more value if you kept all your tests in it. The main in your question have only one assert. If you keep the asserts for the already implemented functionality you ensure that it keeps working while you add new behavior. Try to figure out your edge cases and keep a test for each. Also try to remember that you do not need to implement the whole functionality at once, verifying a small piece that you can see how to do might help you to reason about the rest of the problem.
Your "checkTest" function returns a boolean, use it to count the number of failed tests and return that to give you the ability to fail the build when any test fails.
You need to have a return value telling if any test failed, because in a larger build test-failures will disappear in the noise unless they "scream" at you, e.g. by failing the build.
I hope this helps you find a solution and learn from the problem.
来源:https://stackoverflow.com/questions/21146276/c-add-and-subtracting-100-digits-numbers