?
Solved

Using Fractions

Posted on 2003-02-27
8
Medium Priority
?
451 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
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
Independent Software Vendors: 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!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

615 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