Hey guys I'm having an issue with my program. So let me explain what it's supposed to do. So the program has a class of BigInt and the main stores a c-string into a BigInt num. So it will store this c-string convert the c-string into a int return the int then it will convert the int to a string. Then it will also subtract and there will be negatives which is where the tens complement comes into place I think. I'm getting past the first two tests but now when doing the edition I'm getting a negative number but not the right number, any help would be awesome. Thanks in advanced. Bare with me it's a lot of code and I'm refreshing my C++. Also there are more tests but since there is a content limit I just put the first 3.
You are using 48 as a magic number. You should be using '0' instead.
Your internal format is confusing you. You are storing the number as digit values '0' through '9' and '-' (which you have not accounted for).
I would store the sign of the number in a distinct boolean field, instead of in the numArr[] itself.
I would also store the digits as the values 0..9, not '0'..'9'. This means you only need to perform the digit to value conversions during to/from string operations, instead of repeatedly during every arithmetic operation you perform.
Make yourself a great big comment block about how it is all done. Then you can refer back to it every time you want to manipulate the bignum.
I would also be inclined to store the number least-significant value to most in the array, but that is me.
When it comes time to add and subtract, both routines must be aware of the sign of the argument values and be prepared to switch to the other method as appropriate:
postive + positive --> add, result is positive
positive + negative --> subtract, result is sign of larger number
negative + positive --> subtract, result is sign of larger number
negative + negative --> add, result is negative
positive - positive --> subtract, result is sign of larger number
positive - negative --> add, result is positive
negative - positive --> add, result is negative
negative - negative --> subtract, result is sign of larger number
The ten's complement should be done during the subtraction operation, not separate from it.
Thank you, I'm really gonna sit down(well stand up) and do this on my dry erase board this weekend and go through all the steps of what needs to be done. Today I totally redid the BigInt(const char* str) so I would like your opinion if this looks better and works better. Thanks for the help so far. So now when I do cout << this->numArr[i]; it gives me this which is showing all the numbers right and adding 0 where the '-' is, is this a better approach you think? Also if it is better how do I go about converting this to the string?
BigInt::BigInt(constchar* str) {
// TODO: CONVERT C-STRING TO BIGINT
int length = NUM_DIGITS - strlen(str);
int negative = 0;
for (int i = 0; i < 100; i++) {
numArr[i] = 0;
}
if(str[length-1] == '-'){
this->numArr[length-1] = 9;
}
for(int i = length, k = 0; i < 100; i++, k++){
char temp = str[k];
this->numArr[i] = atoi(&temp);
}
if(negative){
this->tensComplement();
}
}
Also here is convert to string but it gives me an output with the right number at the end with leading zeros how can I get rid of those zeros and also the negative is gone so what do I do with that any suggestions.
1 2 3 4 5 6 7 8 9 10 11 12 13
string BigInt::convertToString(){
// TODO: VALUE IN numArr CONVERTED TO STRING
string str;
ostringstream convert;
for(int i = 0; i < 100; i++)
{
convert << numArr[i];
str = convert.str();
i-=i;
}
return str;
}
class BigInt
{
public:
BigInt();
BigInt(constchar*);
BigInt add(const BigInt&);
BigInt operator+(const BigInt&);
BigInt subtract(const BigInt&);
BigInt operator-(const BigInt&);
string convertToString();
private:
staticconstint NUM_DIGITS = 100;
int numArr[NUM_DIGITS];
bool isNegative;
void tensComplement();
};
BigInt::BigInt()
{
// Always initialize your objects -- the default ctor must do it too
for (int n = 0; n < NUM_DIGITS; n++)
numArr[n] = 0;
isNegative = false;
}
BigInt::BigInt(constchar* str)
{
// 'str' looks like either "123" or "-123"
// In a text string, the least significant digit is last
// In our array, the least significant digit is first
// First, initialize the default value
for (int n = 0; n < NUM_DIGITS; n++)
numArr[n] = 0;
isNegative = false;
// Next convert the string argument to a BigInt
if (!str) return;
if (*str == '-')
{
isNegative = true;
str++;
}
for (int n = 0; *str && n < NUM_DIGITS; str++, n++)
{
// Get the digit's value
int digit_value = *str - '0';
// Validate it
if (digit_value < 0 || digit_value > 9) break;
// Assign it
numArr[n] = digit_value;
}
// All done. You might want to check that *str is '\0'
// and that isNegative is not true if all the elements are 0
}
I really appreciate the help you have been a huge help. So now I checked the numarr and it seems to be storing all the numbers without the '-' and then the isNegative is showing 0 and 1 for each number that is positive and negative. So my question now that I'm recoding the convertostring function is would I just run through the array and if is negative to add the "-" sign back in front while converting it back to a string the values should still be stored there correct? Also when printing out the string I get 9990000000000000etc since I'm not using vector would the push_back still work for this to get rid of the zeros? Again thanks for the help you have been a life saver.
Edit: Sorry for all the questions it's been awhile since i've done C++ and this project piled on me and I wasn't as prepared as I thought. Here is what I have I'm passing the 999 test but I'm getting 739 instead of -739 how do I add that negative sign back in?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
string BigInt::convertToString(){
// TODO: VALUE IN numArr CONVERTED TO STRING
int count;
for(int i = 0; i < NUM_DIGITS; i++){
int x = numArr[i];
while(x != 0){
count = count * 10;
count = count + x% 10;
x = x/10;
}
}
string str = to_string(count);
if(isNegative == true){
}
return str;
}
So I've been messing around with the
if(isNegative == true){
// iI've tried
str = '-' + str;
// and tried
str.insert('-');
}
I can't seem to get this part working or am I just going way of the program? The first one gives me some -4000 number and the second will give me -39 instead of -739 would I need to make a loop to go through the away moving it over 1 to be able to insert the str?
Okay so one more hint haha here is what I have now for the program http://pastebin.com/uUMEigJ0 I put it here because it's a large code. So now I need to add them together with since this is a huge different way of going about it I think I'm not sure where to begin. Any tips?
Edit: Here is what I have for adding right now, which is giving me the wrong result I think I'm doing my carrying wrong?
Edit 2: This is what I have now, still getting wrong numbers but closer wrong numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13
BigInt BigInt::add(const BigInt& rightOperand){
BigInt objToReturn("0");
// TODO: ADD LOGIC HERE
for (int i = 0; i < NUM_DIGITS; i++) {
objToReturn.numArr[i] += this->numArr[i] + rightOperand.numArr[i];
if (objToReturn.numArr[i] > 9) {
//Reduce by 10
objToReturn.numArr[i] -= 10;
objToReturn.numArr[i-1] = objToReturn.numArr[i-1]+1;
}
}
return objToReturn;
}
As of right now the array prints out 73900000000000..... And right array prints the 987100000...... I understand the carry part I've tried that out few times I guess my issue is how do I get these arrays to just flip the actual digits ? So to be 93700000... And 17890000000...... Everytime I try to do this it is leading with zeros and then number flipped at the end but it lines up the same as forward and backward so how do I go about flipping just those digits at the beginning and storing them at the beginning still? Thanks again.