?
Solved

Using Fractions

Posted on 2003-02-27
8
Medium Priority
?
432 Views
Last Modified: 2010-04-01
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
Comment
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
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
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

by:dude1337
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

by:bkrahmer
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dude1337
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

by:
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

by:akshayxx
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

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

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question