hi i really need help on making c++ problems about the long math problems like long addition,subtraction, multiplication and division i've only began learning awhile ago so i just needed some samples or algorythms for this, if not both would be appreciated, if not a link to some guides about this
example:
358
+ 235
---------
593

## All 5 Replies

As in

``````char n1[] = "358";
char n2[] = "235";
int carry = 0;

sum = carry + (n1 - '0') + (n2 - '0' );  // Add "8" and "5", as 8 and 5
result = sum % 10;  // ie 3
carry = result / 10;  // ie 1

// Then you do the same for n1 and n2``````

Essentially, you model the algorithm on how you would do the sum on paper. Start at the right hand end of the number and work to the left.

It's easy when the numbers are the same length, you just need a little more care to line up the numbers when you have say

``````char n1[] = "123456";
char n2[] = "78";``````

ohh i forgot im trying to use link list

> ohh i forgot im trying to use link list

It's all the same logic no matter what data structure you have.

Are you storing the most significant digit at the head of the list, or the least significant digit?

Program Usage:

bigInteger input.txt

Description:
The program should accept a file, say input.txt, containing a sequence of
bigInteger operators and operands. The output of the program is
the result of the bigInteger operations in sequence.
The format of each binary operation should be:

<operator>
<operand1>
<operand2>

while the format of unary operation should be:

<operator>
<operand>

The allowable binary operators are:
2. - : bigInteger subtraction
3. * : bigInteger multiplication
4. / : bigInteger division
5. % : bigInteger modulus
6. == : bigInteger equality
7. != : bigInteger inequality
8. < : bigInteger less than
9. > : bigInteger greater than
10. <= : bigInteger less than or equal
11. >= : bigInteger greater than or equal
12. >> : bigInteger input
13. << : bigInteger output

The allowable unary operators are:
1. ++ : bigInteger increment
2. -- : bigInteger decrement

Suppose the input is:

+
100203020332238819199291199191002
342628464392991019222318919191912

-
829328282817772656253476253567324
453920

-
453920
829328282817772656253476253567324

-
829328282817772656253476253567324
-453920

!=
920129292919292991919191919911991234
920129292919292991919191919911991234

++
123456789012345678901234567890123456789012345

==
682348767836487236487364786768763
682348767836487236487364786768763

The output of the program should be:

442831484725229838421600118382914
829328282817772656253476253113404
-829328282817772656253476253113404
829328282817772656253476253021244
0
123456789012345678901234567890123456789012346
1

Let the bigInteger class be declared as:

class bigInteger {
string value;
bool sign;
public:
bigInteger(void);
bigInteger(string);
bigInteger(int);
bigInteger(long);

// declaration of overloaded operators should appear here

friend ostream &operator<<(ostream &, bigInteger &);
friend istream &operator>>(istream &, bigInteger &);
};

really confusing

So where is the linked list, or anything else you spoke of 2 weeks ago?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.