Subtracting two long positive integers in an array c++ [duplicate]

半腔热情 提交于 2019-12-11 03:57:34

问题


I'm relatively new to programming :).

Suppose I wanted to create a program which prompts a user to enter two positive numbers up to 50 digits long, which then subtracts the second number from the first number.

For example:

The user enters the first positive number: 239834095803945862440385983452184985298358

second number: 939542309853120721934217021372984729812

===========================================================================

Program outputs the difference: 238894553494092741718901766430812000568564

OR, if negative: -29837430045

===========================================================================

Each digit of the numbers will be stored as individual elements in an array. Here is how I am currently taking in the user input:

int read_array(int int_array[], int MAX_SIZE) {
    char number;
    int count = 0;
    //set all array entries to 0. 
    for (int i = 0; i < MAX_SIZE; i++){
        int_array[i] = 0;
    }

    do { //processes each individual char in the istream
        cin.get(number);
        // puts char on to the array until it hits the
        // end of the number (end of the line)
        if(( number != '\n') && (count < MAX_SIZE) && (isdigit(number))) {
            int_array[count] = int(number) - int('0');
        }  
        count++; //increments count
    } while (number != '\n');

    //tests if number is too large
    int digitcount = count - 1;
    if (digitcount > MAX_SIZE) {
        cout << endl << "ERROR: The number is above 50 digits!" << endl;
        return 0;
    }

THE PROBLEM:

HOW to do the subtraction is eluding me.I have been trying to solve this problem for two weeks and it's most likely something trivial I have missed.

I have tried:

  1. Converting array of elements back into one whole int
  2. Writing my own program to do long subtraction on the numbers

etc...

However, the output is ONLY successful up until a certain number of digits and/or if they're positive/negative numbers. I'm stumped and I'm not sure what the best way is to subtract two positive number arrays to get a successful output that can accommodate for positive and negative numbers as shown in the example. ANY HELP GREATLY APPRECIATED :).

EDIT: my attempts:

#include "long_sub.h"
#include <sstream>
#include <vector>

using namespace std;

int long_sub(int a[], int b[], const int size) {
    stringstream ss;
    int const sizes = 50;
    int c = 0; //borrow number
    int borrow = 1; // the '1' that gets carried to the borrowed number
    int r[sizes];

    for (int i = 0; i < size; i++) {
        r[i] = 0;
    }    
    //initialise answer array to 0.
    for (int i = size - 1; i >= 0; i--) {
        //handles zeros
        if (a[i] < b[i] && a[i]) {
            //takes the borrow from the next unit and appends to a.
            ss << borrow << a[i];
            ss >> c;
            ss.clear(); // clears stringstream for next potential borrow.

            int temp = c - b[i];
            r[i] = abs(temp);
        } else {
            int temp = a[i] - b[i];
            r[i] = abs(temp);
        }
    }

    for (int i = 0; i <= size - 1; i++ ) {
        cout << r[i];
    }
    cout << endl;
    return r[sizes];
}

回答1:


So the solution to this is pretty much the same as when you do it by hand.

If we have:

 4321
-1234

You'd take the last digits in the two numbers, subtract the one below from the one above, 1 - 4 - which of course means you have to borrow from the next digit up, so we remmeber that, and then come up with 7. Now, take the next digit [and remember the "borrow"], and subtract 3 from 2-1 = 8.

The exact same thing is how you do subtraction on large numbers in computer - do one bit at a time, and if you "borrow", then you need to take that with you to the next step.



来源:https://stackoverflow.com/questions/14875445/subtracting-two-long-positive-integers-in-an-array-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!