4 Replies - 1458 Views - Last Post: 29 December 2009 - 11:45 AM Rate Topic: -----

#1 darmawan efendi  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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.. :D
I use atoi is very difficult...

Help me ... Thank you all.. :D

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  Icon User is offline

  • code > sword
  • member icon

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

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

Posted 29 December 2009 - 01:09 AM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1