# Problem sum,multiply,divide in C++

Page 1 of 1

## 4 Replies - 1576 Views - Last Post: 29 December 2009 - 11:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=147391&amp;s=e61f5d9cfa6b5ea7ce2886d49b289b17&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 darmawan efendi

• New D.I.C Head

Reputation: 2
• Posts: 28
• Joined: 03-November 08

# Problem sum,multiply,divide in C++

Posted 29 December 2009 - 12:55 AM

Hi guys..
I have a problem in C++..
I want to sum number but in that number have a variable..
for example :

```condition 1 : 8M+3
condition 2 : 10M+5

the result from condition 1 + condition 2 is : 18M+8

```

How I can sum both them in C++?
not only sum but multiply, divide, and so on..
I use atoi is very difficult...

Help me ... Thank you all..

This post has been edited by darmawan efendi: 29 December 2009 - 01:12 AM

Is This A Good Question/Topic? 0

## Replies To: Problem sum,multiply,divide in C++

### #2 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Problem sum,multiply,divide in C++

Posted 29 December 2009 - 01:09 AM

[rules][/rules]

• Saucy!

Reputation: 6180
• Posts: 23,882
• Joined: 23-August 08

## Re: Problem sum,multiply,divide in C++

Posted 29 December 2009 - 06:29 AM

I think we need to see more code to understand what you're getting at. Is the data in scientific notation or some special format? What is the exact wording of your assignment?

### #4 NickDMax

• Can grep dead trees!

Reputation: 2253
• Posts: 9,245
• Joined: 18-February 07

## Re: Problem sum,multiply,divide in C++

Posted 29 December 2009 - 08:24 AM

The first step I would take is to define a datastructure that can hold the data in some usable form.

For example for holding polynomials like:

c + a x + b x^2 + c x^3... an x^n

one often uses a vector, where element vect[0] is the constant c, vect[1] is a, vect[2] is b, vect[n] is an..

This easily allows you to add like terms of single variable polynomials...

for example you would have:

8M+3 becomes 3 | 8
10M+5 becomes 5 | 10

when you add up the elements you get 8 | 18 in the vector which results in:

18M + 8

### #5 GenHornet18

• Darken the Radar

Reputation: 36
• Posts: 629
• Joined: 19-October 09

## Re: Problem sum,multiply,divide in C++

Posted 29 December 2009 - 11:45 AM

A more elaborate approach which I used in an assignment awhile back, was holding the equation in a char array (string) and checking each element for variables and then using the data given around each element to solve/simplify the equation. Using a template would likely be easier however, as my way ended up to be the more brute force tactic.