Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "fraction.h"
- int main()
- {
- int num1 = 0, num2 = 0, den1 = 0, den2 = 0;
- std::cout << "Enter numerator and denominator of fraction 1 separated by whitespace:";
- std::cin >> num1 >> den1;
- std::cout << "Enter numerator and denominator of fraction 2 separated by whitespace:";
- std::cin >> num2 >> den2;
- fraction a(num1, den1);
- fraction b(num2, den2);
- std::cout << " Reduced Fraction 1= ";
- a.display();
- std::cout << "\tReduced Fraction 2= ";
- b.display();
- cout << endl;
- fraction result;
- result = a.add(b);
- std::cout << "\n Sum of Fraction 1 and Fraction 2 is:";
- result.display();
- result = a.subtract(b);
- std::cout << "\n Difference of Fraction 1 and Fraction 2 is:";
- result.display();
- result = a.multiply(b);
- std::cout << "\n Product of Fraction 1 and Fraction 2 is:";
- result.display();
- result = a.divide(b);
- std::cout << "\n Quotient of Fraction 1 and Fraction 2 is:";
- result.display();
- if (a.isEqual(b))
- std::cout << "\n Fraction 1 is equal to Fraction 2";
- if (a.isNotEqual(b))
- std::cout << "\n Fraction 1 is not equal to Fraction 2";
- if (a.greaterThan(b))
- std::cout << "\n Fraction 1 is greater than Fraction 2";
- if (a.lessThan(b))
- std::cout << "\n Fraction 1 is less than Fraction 2";
- std::cout << std::endl;
- system("pause");
- return 0;
- }
- //***********************************************SOURCE**************************************^^^^^^^^
- #ifndef FRACTION_H
- #define FRACTION_H
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace std;
- // Regular prototypes
- //int stringToNumber(const string &Text);
- int GCD(int, int);
- int LCM(int, int);
- class fraction{
- public: // Access Specifier
- int numerator;
- int denominator; // Can never be 0
- // Function Prototypes
- fraction(int num = 0, int denom = 1);
- void setNumDen(int num, int denom);
- void reduce();
- void add(const fraction &f);
- void subtract(const fraction &f);
- void multiply(const fraction &f);
- void divide(const fraction &f);
- void isEqual();
- void isNotEqual();
- void greaterThan();
- void lessThan();
- void display();
- };
- // Member functions definitions
- fraction::fraction(int num, int denom)
- {
- setNumDen(num, denom);
- }
- void fraction::setNumDen(int num, int denom)
- {
- numerator = num;
- denominator = denom;
- if (numerator > 0 && denominator < 0)
- {
- numerator = -numerator;
- denominator = -denominator;
- }
- if (numerator < 0 && denominator < 0)
- {
- numerator = -numerator;
- denominator = -denominator;
- }
- reduce();
- }
- void fraction::reduce()
- {
- int myGCD = 0;
- myGCD = GCD(abs(numerator), abs(denominator));
- numerator /= myGCD;
- denominator /= myGCD;
- }
- void fraction::add(const fraction & fun)
- {
- int leastComDen;
- }
- void fraction::subtract(const fraction & fun)
- {
- }
- void fraction::multiply(const fraction & fun)
- {
- }
- void fraction::divide(const fraction & fun)
- {
- }
- void fraction::isEqual()
- {
- }
- void fraction::isNotEqual()
- {
- }
- void fraction::greaterThan()
- {
- }
- void fraction::lessThan()
- {
- }
- void fraction::display()
- {
- if (denominator == 0)
- cout << numerator;
- else
- cout << numerator << "/" << denominator;
- }
- //int main()
- //{
- //
- // fraction fractionObject, fractionObject2;
- //
- // fractionObject.setNumDen();
- // fractionObject2.setNumDen();
- // // Reducing and displaying the reduced fractions
- // fractionObject.reduce();
- // fractionObject2.reduce();
- // cout << "Reduced Fraction 1 = " << fractionObject.numerator << "/" << fractionObject.denominator << "\t" << "Reduced Fraction 2 = " << fractionObject2.numerator << "/" << fractionObject2.denominator << endl;
- // // Adding and displaying the fractions
- //
- //
- //
- // system("pause");
- // return 0;
- //}
- // Function to convert string to number
- //int stringToNumber(const string &Text)//Text not by const reference so that the function can be used with a
- //{ //character array as argument
- // stringstream ss(Text);
- // int result;
- // return ss >> result ? result : 0;
- //}
- // result=GCD(a,b)
- int GCD(int a, int b) {
- int temp = 0;
- while (b != 0) {
- temp = b;
- b = a%b;
- a = temp;
- }
- return a;
- }
- // result=LCM(a,b);
- int LCM(int a, int b) {
- int result = 0;
- result = a * (b / LCM(a, b));
- return result;
- }
- #endif
- ////*****************************************************************************************************************************************************************************************************************************************
- //#include <iostream>
- //#include <string>
- //#include <sstream>
- //using namespace std;
- //
- //// Regular prototypes
- //int stringToNumber(const string &Text);
- //int GCD(int, int);
- //int LCM(int, int);
- //
- //class fraction{
- //
- //public: // Access Specifier
- // int numerator;
- // int denominator; // Can never be 0
- // int numerator2;
- // int denominator2; // Also can never be 0
- //
- //
- //
- // // Function Prototypes
- // fraction();
- // void setNumDen();
- // void reduce();
- // void add(const fraction &f);
- //
- //
- //};
- //
- //// Member functions definitions
- //fraction::fraction()
- //{
- // numerator = 0;
- // denominator = 0;
- // numerator2 = 0;
- // denominator2 = 0;
- //}
- //
- //void fraction::setNumDen()
- //{
- // string numString;
- // string denString;
- // string numString2;
- // string denString2;
- //
- // do{
- // cout << "Enter a numerator and denominator of fraction 1 separated by whitespace: ";
- // getline(cin, numString, ' ');
- // getline(cin, denString);
- // if (denString == "0")
- // cout << endl << "Please enter a number that isn't zero." << endl;
- // } while (denString == "0"); // Making sure denominator is not zero
- //
- // do{
- // cout << "Enter a numerator and denominator of fraction 2 separated by whitespace: ";
- // getline(cin, numString2, ' ');
- // getline(cin, denString2);
- // if (denString2 == "0")
- // cout << endl << "Please enter a number that isn't zero." << endl;
- // } while (denString2 == "0"); // Making sure denominator is not zero
- //
- // numerator = stringToNumber(numString);
- // denominator = stringToNumber(denString);
- // numerator2 = stringToNumber(numString2);
- // denominator2 = stringToNumber(denString2);
- //
- //}
- //
- //void fraction::reduce()
- //{
- // int leastCommonMultiple = 0;
- // int leastCommonMultiple2 = 0;
- //
- // leastCommonMultiple = LCM(numerator, denominator);
- // leastCommonMultiple2 = LCM(numerator2, denominator2);
- //
- // numerator /= leastCommonMultiple;
- // denominator /= leastCommonMultiple;
- // numerator2 /= leastCommonMultiple2;
- // denominator2 /= leastCommonMultiple2;
- //}
- //
- //void fraction::add(const fraction & fun)
- //{
- // int leastComDen;
- //
- //
- //
- //}
- //
- //int main()
- //{
- //
- // fraction fractionObject;
- //
- // fractionObject.setNumDen();
- // // Reducing and displaying the reduced fractions
- // fractionObject.reduce();
- // cout << "Reduced Fraction 1 = " << fractionObject.numerator << "/" << fractionObject.denominator << "\t" << "Reduced Fraction 2 = " << fractionObject.numerator2 << "/" << fractionObject.denominator2 << endl;
- // // Adding and displaying the fractions
- //
- //
- //
- // system("pause");
- // return 0;
- //}
- //
- //// Function to convert string to number
- //int stringToNumber(const string &Text)//Text not by const reference so that the function can be used with a
- //{ //character array as argument
- // stringstream ss(Text);
- // int result;
- // return ss >> result ? result : 0;
- //}
- //
- //// result=GCD(a,b)
- //int GCD(int a, int b) {
- // int temp = 0;
- //
- // while (b != 0) {
- // temp = b;
- // b = a%b;
- // a = temp;
- // }
- // return a;
- //}
- //
- //// result=LCM(a,b);
- //int LCM(int a, int b) {
- // int result = 0;
- //
- // result = a * (b / LCM(a, b));
- // return result;
- //}
Advertisement
Add Comment
Please, Sign In to add comment