fraction addition ,subtaction, muiltiplication and division

Mar 25, 2012 at 1:53am
The program im working on needs takes in user input like this:

Enter Problem: 1/8 + 4/8


and outputs it like this

result: 5/8

How do you make the fraction output like that. How do you format it that way? Another question is how would I break down the user input the "/" in the input confuses me .

P.S I need to use functions
Mar 25, 2012 at 7:11am
1) you need to analyze string and find numbers in it.
2) you need to find dividend and divisor
3) to do calculations by fraction rules. http://en.wikipedia.org/wiki/Fraction_%28mathematics%29
4) return string "result"
Last edited on Mar 25, 2012 at 7:12am
Mar 25, 2012 at 12:32pm
i remember doing this a while back without strings. its something like
std::cin << anumber << a char << anumber1;
than some statement/set of statements to call upon functions that will do the calculation.
Mar 25, 2012 at 2:37pm
cin method will work but only if you use '>>' insertion operator rather than '<<' extraction operator....

+

u can also do it c functions;
-> scanf()
-> printf()

if u want to use functions!
Mar 25, 2012 at 4:42pm
Whoops. Used to my compiler doing the operator for me. Same concept though.
Mar 27, 2012 at 7:12pm
so to get the user input i should use this code

cin>>num1>>char1>>denum1>>char 2>>num2>>char3>>denum2;


num1= numerator
char1= "/" the division
denum1 = denominator

char 2= " " it ould be *,/,+,-

num 2 = numerator 2
char3 = /
denum 2 = denominator 2
Mar 28, 2012 at 12:35am
in theory yes, cin ignores whitespace so that should work.
Topic archived. No new replies allowed.