-1

Can you please help me buy showing how to create substraction of two long numbers? I found this code on the internet:

#include<iostream>
#include<string>
using namespace std;

int main(void)
{
// the two "numbers" to be added. Make them as long as you like.
     string numStr1;
     string numStr2;
     cout << "Enter 1st number: "; cin >> numStr1;
     cout << "Enter 2nd number: "; cin >> numStr2;

// keeping track of which string is longest using references
string& rLongStr = numStr1.length() > numStr2.length() ? numStr1 : numStr2;
string& rShortStr = numStr1.length() <= numStr2.length() ? numStr1 : numStr2;

// initialize the sum with the long string but with space for a final carry at the beginning
string numStrSum = '0' + rLongStr;// the '0' in case of a final carry

// must go through the strings backwards since the least
// significant digit is at the end, not the beginning.
string::reverse_iterator r_itShort, r_itSum;
r_itShort = rShortStr.rbegin();// point to last "digit" in the short string
r_itSum = numStrSum.rbegin();// same for sum string

// add the "digits" one by one from end to beginning of the short string
while( r_itShort != rShortStr.rend() )
{
    *r_itSum += *r_itShort - '0';// "add" the digits
    if( *r_itSum > '9' )// must carry a one to the next "digit"
    {
        *(r_itSum + 1) += 1;
        *r_itSum -= 10;
    }
    ++r_itShort;// move back 1 character
    ++r_itSum;// in each string
}
if( numStrSum.at(0) == '0' )// if 1st character is stiil '0'
    numStrSum.erase(0,1);// erase it

// output result
cout << numStrSum;
cout << endl;

return 0;
}

So, I could not make substraction of this two numbers. I tried smt like that:

while( r_itShort != rShortStr.rend() )
{
    *r_itSum -= *r_itShort + '0';
    if( *r_itSum > '9' )
    {
        *(r_itSum + 1) -= 1;
        *r_itSum += 10;
    }
    --r_itShort;
    --r_itSum;
}
if( numStrSum.at(0) == '0' )
    numStrSum.erase(0,1);

But it shows me the same. Can you please help me and tell what am I doing wrong? Thank You!

paskalnikita
  • 153
  • 1
  • 3
  • 12
  • 1
    I really don't think that "take an addition algorithm and swap all the signs" is a valid way to subtract. Some of those addition signs were moving you forward in the array, for instance. It looks like the addition is simulating primary school addition (base 10 with a carry value). You simply can't subtract with a carry value; it just doesn't make sense. – Silvio Mayolo Jan 11 '18 at 01:50
  • so, you mean, that i can't make subtraction generaly? or I should change an algorithm to solve it? – paskalnikita Jan 11 '18 at 02:04
  • This is caused by a (or some) simple logic error(s). Could you please add comments line by line to show your understanding of the logic of these codes? – xskxzr Jan 11 '18 at 02:58
  • ok, every line of code consist comment. the only problem i have is in while loop. i don't understand how can i get +10 for previous digit, which is 'upper'. As far as i understand substraction is possible? – paskalnikita Jan 11 '18 at 14:36

1 Answers1

-3

You can both input and output float / double / integer types to the iostream with cin/cout so string parsing wouldn't be a problem. You can just get them as double type.

If you really want to get these numbers as strings, you could use atof to convert them to floats. Then simply calculate what you need and either use std::to_string or just print directly.

Wando
  • 16
  • 1
  • if make smt like that: string numStr1="1234567"; string numStr2="123"; will it work? with atof i should create a new array and work with that? – paskalnikita Jan 11 '18 at 02:07
  • so, i created smt like that: int num1Size=numStr1.length(); char *num1= new char[num1Size]; for(int i=0;i – paskalnikita Jan 11 '18 at 02:31
  • 1
    You answered a different question (the OP asks what he is doing wrong). In addition, I don't think floating arithmetic is what the OP wants because of accuracy loss and limited range (compared to string). – xskxzr Jan 11 '18 at 02:34
  • do you think normal floats can store 10 digits? Not even close to thousands or more digits in a bigint type. Moreover [`atoi` family shouldn't be used](https://stackoverflow.com/q/17710018/995714) – phuclv Jan 11 '18 at 02:43
  • ok, every line of code consist comment. the only problem i have is in while loop. i don't understand how can i get +10 for previous digit, which is 'upper'. As far as i understand substraction is possible? – paskalnikita Jan 11 '18 at 14:19
  • Well, then like I've said use long double. It will change its size depending on the size of the digit. For tests, I've run a ultra-simple program and fed it with numbers 2k digits long and it works perfectly fine. – Wando Jan 11 '18 at 18:17
  • @Wando then you definitely don't understand what "floating-point" means. Try to store 123456789012345678901234567890 in a long double and try to print its **exact** value. What the OP is trying is [tag:arbitrary-precision] [tag:bigint] – phuclv Jan 12 '18 at 10:35