Thread: simplfying fractions

  1. #1
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

    simplfying fractions

    Hi guys,


    Ok this is more to do with laziness than anything else but I was wondering how would you create a program that cancels down fractions into its simplest form. I've managed to do it for positive integers but I have a problem when dealing with negative integers.

    Remember these rules:

    Code:
     + / + = +
     + / -  =  -
      - / + =  -
     -  /  - =  +
    
    so:
    
    =  -6
        ---
        -10
    
    =   3
        ---
         5
    Possible solutions include using the absolute function, or perhaps converting the integer to a string and then checking to see if the first element in that array is equal to '-' or '+' and then doing the necessary arithmetic manipulations.

    If you can see a quick easy solution, I would be most grateful.

    Here's my code so far, note it only handles positive integers!


    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
    
    int main()
    {
      int top_part;
      int bottom_part;
      int smallest;
      
      
    
      
      cout<<"Enter top part of fraction:";
      cin>>top_part;
      
      cout<<"Enter bottom part of fraction:";
      cin>>bottom_part;
      
      if (top_part>bottom_part)
      {
          smallest=bottom_part;
      }
      if (top_part<bottom_part)
      {
          smallest=top_part;
      }
      
      
              double it,its,newtop,newbot;
              for (int a=1; a<=smallest; a++)
              {
                  
                  it=top_part%a;
                  its=bottom_part%a;
                  
                  
                          if (it==0)
                          {
                              if (its==0)
                              {
                                  newtop=top_part/a;
                                  newbot=bottom_part/a;
                              }
                          }
              }
              cout<<"When cancelled down..."<<endl;
              cout<<newtop<<"/"<<newbot;
              
              
              int stop;
              cin>>stop;                    
      
      	
      return 0;
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    There's a bunch of ways of testing if a number's negative....

    Code Fun with jverkoey!

    Code:
    if(val<0) // Negative
    Code:
    if(abs(val)!=val) // Negative
    And remember, unsigned variables can't be negative. Also remember to use fabs for double values and fabsf for float values!

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Possible solutions include using the absolute function
    That seems like the easiest:

    if absolute value of numerator == numerator, then numerator_flag = true;
    if absolute value of denominator == denominator, then denominator = true;

    Then, you could use a switch with the 4 operations to get the sign--> true(plus) or false(minus).

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by treenef
    Hi guys,


    Here's my code so far, note it only handles positive integers!
    If you are happy with your code for positive integers, you could consider something like this

    Code:
      cin>>bottom_part;
    
      int sign = (((top_part>0)&&(bottom_part<0)) || ((top_part<0)&&(bottom_part>0)));
      top_part = abs(top_part);
      bottom_part = abs(bottom_part);
      
      if (top_part>bottom_part)

    Then:

    Code:
      cout<<"When cancelled down..."<<endl;
      if (sign) {
        cout << "- ";
      }
      cout<<newtop<<"/"<<newbot;
    Regards,

    Dave

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Might be clearer to use XOR:
    Code:
    //if top_part -ve XOR bottom_part -ve
    bool negsign = (top_part < 0 || bottom_part < 0) && !(top_part < 0 && bottom_part < 0);
    //make both non-negative
    top_part = abs(top_part);
    bottom_part = abs(bottom_part);
    After all, it is easy to say in words that "if numerator or denominator is negative, but not both negative, then fraction is negative".

    Incidentally, wouldnt it be somewhat better to find the GCD, and then divide both by that?
    Of course, then there's a missing check for division by zero.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 05:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM