Solved

# Using Fractions

Posted on 2003-02-27
Medium Priority
432 Views
I am writing a program that I need to be able to have the user input fractions such as 1/2 and output them to say 1/2 or 3/4.  Does anyone have any suggestions on how to do this?
0
Question by:dude1337
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 6

Expert Comment

ID: 8038088
it would really help to know what you plan to do withy this input in between the input and output. you can take it in as a string. but more info is needed for any other options
0

Author Comment

ID: 8038165
The user will input the fraction and it will be multiplied and divided in different ways, maybe some addition and I want it to show the output of the answer and the original entry they put in.
0

LVL 2

Expert Comment

ID: 8038373
Dude, you're not trying to get easy answers for homework are you?  I hope not...  You need to state where you're at, and where you're stuck.  It really comes down to parsing strings into data, store them as objects, and then apply the rules of math to get your results, perhaps with a fraction simplification done as well.
brian
0

Author Comment

ID: 8038426
No, I am making a program that has nothing to do with homework.  It is a formula solving program to perform basic algebra formulas such as linear and quadratic formulas.  I am trying to get the user to input the sllope of a line, which most of the time is a fraction.  Then I have the program go through the point/slope formula and it ouputs the y-intercept and the slope again, which the user already put in.  That is why I need to use fractions because slope is easier to calculate as a fraction than a decimal.  I am doing this of my own free will.
0

LVL 8

Accepted Solution

akshayxx earned 150 total points
ID: 8038623
OK here is a suggestion ..no full source codes ..  make a class fraction like this

class struct fraction{
int num;  //numerator
int denom; //denominator
// now overload the operators for + - / and * .. and other basic operators

}

here is the way to add two class objects of type fraction

fraction f1,f2

to add f1 and f2.. and put result in f3.. ( though while doing operator overloading . u shud return the result in f1 itself .. )

f3.num=(f1.num*f2.denom+f2.num*f1.denom);
f3.denom=f1.denom*f2.denom;

example
1/4 + 1/2 = 6/8 ( not a pure fraction ) pure fraction shud be  3/4
with this formula u'll get fraction which are not in their "smallest" form .. (forgot the technical word) ..
for that use the concept of LCM and GCD..

for that the modified formula will be ( for addition)

f3.denom= LCM(f1.denom,f2.denom);
//LCM now already calculated and put in f3.denom
f3.num=  f1.num * f3.denom/f1.denom + f2.num * f3.denom/f2.denom;

similarly will be the case for substraction ..
and division and multiplications are relatively easy to implement..

I SEE THAT THIS IS HOME WORK PROBLEM ..AND WE ALREADY HAVE GIVEN YOU MORE THAN REQUIRED INFORMATION .. I WOULD ENCOURAGE OTHER EXPERTS THAT PLEASE DONT PROVIDE FULL SOURCE CODES..

0

LVL 8

Expert Comment

ID: 8038629
>>>class struct fraction{

heheh didnt i tell u people that i am a C programmer .. and i dont know C++ :)
0

Author Comment

ID: 8038667
Thanks, this helps, but it was not a homework problem.
0

LVL 8

Expert Comment

ID: 8038825
sorry .. but from the description of ur problem it looked like homework
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [sâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month7 days, 20 hours left to enroll