473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simplifying fractions

Hey all, I'm working on a program that takes two fractions as input and performs an operation on them (+,-,*,/). I've overloaded all the operators and written a function to reduce the result to lowest terms, but I'm having a small problem with negative fractions. It's reducing them right, but it puts the negative sign on the denominator instead of the numerator (ie 1 / -2 ). I want to have the negative sign on the numerator ( -1 / 2 ). It's probably a really easy fix that's looking me straight in the face, but after looking at this code for as long as I have, my brain is just too fried. Can anyone help?

Expand|Select|Wrap|Line Numbers
  1.     rationalNumbers reduce(const rationalNumbers& fraction)
  2.     {
  3.         rationalNumbers reduced;
  4.         reduced.numerator=fraction.numerator;
  5.         reduced.denominator=fraction.denominator;
  6.         int temp=reduced.numerator*reduced.denominator;
  7.         if (temp>0) //If the result is a positive number
  8.         {
  9.             for (temp; temp>1; temp--)
  10.             {
  11.                 if ((reduced.numerator%temp==0) && (reduced.denominator%temp==0))
  12.                 {
  13.                     reduced.numerator/=temp;
  14.                     reduced.denominator/=temp;
  15.                 }
  16.             }
  17.         }
  18.         else if (temp<0) //If the result is a negative number
  19.         {
  20.             for (temp; temp<-1; temp++)
  21.             {
  22.                 if ((reduced.numerator%temp==0) && (reduced.denominator%temp==0))
  23.                 {
  24.                     reduced.numerator/=temp;
  25.                     reduced.denominator/=temp;
  26.                 }
  27.             }
  28.         }
  29.         return reduced;
  30.     }
Nov 8 '07 #1
2 8076
Ganon11
3,652 Expert 2GB
Why not add an explicit check? If the denominator is negative, but the numerator is positive, multiply them both by -1.
Nov 8 '07 #2
Thanks, that definitely does it. After trying to understand overloading operators all day (I hate the syntax), I think I'm looking at things and trying to make them more difficult than they should be. That was a very simple fix. :D


Expand|Select|Wrap|Line Numbers
  1.         if ((reduced.numerator>0) && (reduced.denominator<0))
  2.         {
  3.             reduced.numerator*=-1;
  4.             reduced.denominator*=-1;
  5.         }
Nov 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: skip | last post by:
I'm looking for a solution (or ideas about a solution) to the problem that strftime(3) and strptime(3) don't understand time increments of less than one second. Most operating systems can provide...
2
by: karp | last post by:
Lets say I have the following class in order to add two fractions, class Fraction{ public: Fractions::Fraction(int numer = 0, int denom = 1) { valeurNumerateur = numer; valeurDenominateur =...
33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the...
2
by: Mori | last post by:
Hi, Can someone supply a code example of displaying a string with a fractional part, say 5 and 7 16ths. I cannot find an example of how to use the Encoding object (if that is what you use). ...
4
by: Bob | last post by:
Hi All, Was wondering if in C# there is an elegant way of displaying and or calculating fractions. The case: we have an app that measures/slices dices etc and all our internal measures and...
1
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
5
by: gubbachchi | last post by:
Hi all, How to convert the fractions into decimals and vice versa in php. I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store...
0
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.