473,396 Members | 1,998 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,396 software developers and data experts.

Fraction Program - Reducing it to the lowest terms

41
Hello everyone,

I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms.
However, I'm not sure how the algorithm of reducing fractions works.
Here is part of my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. //Header File
  3.  
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class fraction
  8. {
  9. private:
  10.     int numerator;
  11.     int denominator;
  12. public:
  13.     fraction() : numerator(0), denominator(0)  //Constructor (no args) 
  14.     { }
  15.  
  16.     fraction(int num, int den=1); //(Fraction F2) : numerator(num), denominator(den) //Constructor (two args)
  17.  
  18.     friend fraction operator +(fraction f1, fraction f2);
  19.     friend fraction operator-(fraction f1, fraction f2);
  20.     friend fraction operator*(fraction f1, fraction f2);
  21.     friend fraction operator/(fraction f1, fraction f2);
  22.     friend istream& operator >> (istream& s, fraction& e);
  23.     friend ostream& operator<< (ostream& out, fraction& f);
  24. };
  25.  
  26. //fraction.cpp
  27.  
  28. #include <iostream>
  29. #include <string>
  30. #include "fraction.h"
  31. using namespace std;
  32.  
  33.  
  34. fraction operator+(fraction f1, fraction f2)
  35. {
  36.     fraction temp;
  37.  
  38.     temp.numerator = f1.numerator * f2.denominator + f1.denominator * f2.numerator;
  39.     temp.denominator = f1.denominator * f2.denominator;
  40.  
  41.     return temp;
  42.  }
  43.  
  44.  
  45. ostream& operator<<(ostream& out, fraction& f)
  46. {
  47.     out << f.numerator << "/" << f.denominator;
  48.     return out;
  49. }
  50.  
  51. istream& operator >> (istream& s, fraction& f)
  52. {
  53.     cout << "\nPlease type the numerator: ";
  54.     cin >> f.numerator;
  55.     cout << "Please type the denominator: ";
  56.     cin >> f.denominator;
  57.     return s;
  58. }
  59.  
  60. //calc.cpp
  61.  
  62. #include <iostream>
  63. #include <string>
  64. #include "fraction.h"                                                                                                             
  65. using namespace std;
  66.  
  67. int main() 
  68.     fraction f1;
  69.     fraction f2;
  70.     fraction answer;
  71.     fraction f3;
  72.  
  73.  
  74.     char symbol = ' ';
  75.     string nome;
  76.  
  77.    while (true)
  78.    { 
  79.     cout << "A\tADD\n";
  80.     cout << "S\tSUBTRACT\n";
  81.     cout << "M\tMULTIPLY\n";
  82.     cout << "D\tDIVIDE\n";
  83.     cout << "E\tEXIT\n";
  84.     cout << "\nWhat operation do you want to use? ";
  85.         cin >> symbol;
  86.         if (symbol == 'e' || symbol == 'E')
  87.             exit(0);
  88.  
  89.      switch (symbol) 
  90.      { 
  91.      case 'A':
  92.          cout << "\nPlease type values for fraction 1 and fraction 2\n";
  93.          cin >> f1 >> f2;
  94.          cout << "\nFraction 1 = " << f1 << "\nFraction 2 = " << f2;
  95.          f3 = f1 + f2;
  96.          cout << "\n\bThe sum of F1 and F2 is: " << f3 << "\n" << endl;
  97.          break;
  98.  
  99.    }
  100.  
  101.    return 0;
  102. }
  103.  
Thanks for all the support,

Doug
Sep 17 '07 #1
4 14416
Ganon11
3,652 Expert 2GB
How do you reduce a fraction in real life?

Hint: Look up something called the greatest common divisor.
Sep 17 '07 #2
d0ugg
41
Thanks for the help.
I already got the algorithm to reduce the fraction.

My header file is looking like this now

Expand|Select|Wrap|Line Numbers
  1.  
  2. // This program was created by Douglas Alves
  3. // CS 1410
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class fraction
  9. {
  10. private:
  11.     int numerator;
  12.     int denominator;
  13. public:
  14.     fraction() : numerator(0), denominator(0)  //Constructor (no args) 
  15.     { }
  16.  
  17.     fraction(int num, int den=1); //(Fraction F2) : numerator(num), denominator(den) //Constructor (two args)
  18.  
  19.     friend fraction operator +(fraction f1, fraction f2);
  20.     friend fraction operator-(fraction f1, fraction f2);
  21.     friend fraction operator*(fraction f1, fraction f2);
  22.     friend fraction operator/(fraction f1, fraction f2);
  23.     friend istream& operator >> (istream& s, fraction& e);
  24.     friend ostream& operator<< (ostream& out, fraction& f);
  25.     int GreatestCommonDivisor(int n, int d);
  26.     void Simplify() 
  27.     {
  28.  
  29.     int gcd, absNum = abs(numerator);
  30.     if(numerator != 0 && absNum != 1 && denominator != 1) {
  31.         gcd = GreatestCommonDivisor(absNum, denominator);
  32.         if(gcd > 1) {
  33.             numerator = numerator / gcd;
  34.             denominator = denominator / gcd;
  35.         }
  36.     }
  37. }
  38. };
  39.  
  40.  
-

The compiler is giving me this error:

fraction.obj : error LNK2019: unresolved external symbol "public: int __thiscall fraction::GreatestCommonDivisor(int,int)" (?GreatestCommonDivisor@fraction@@QAEHHH@Z) referenced in function "public: void __thiscall fraction::Simplify(void)" (?Simplify@fraction@@QAEXXZ)


I'm not sure what I did wrong..
I would appreciate any help.

Thanks once again,

~doug
Sep 17 '07 #3
d0ugg
41
Nevermind.. I already found the error.
Thank you for the help,

~doug
Sep 18 '07 #4
labx
1
will you mind putting the full source code please?
I also want to know how to get the lowest term of a fraction.
Sep 1 '11 #5

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

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
9
by: arun.hallan | last post by:
I need to derive fractions from decimals as so: 0.3333 = 1/3 0.6666 = 2/3 The decimal could also be 0.33, or 0.3333333 but the point is that it that the fraction needs to be extracted. ...
63
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
3
by: Stolas | last post by:
Hello all, I'm attempting to create a Fraction Calculator. But for some reasons on my RationalTest program it is giving me an error that getAdd in Rational cannot be applied to (Rational). I'm very...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.