Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.223.195.127
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0705) :  /home/jwang/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/jwang/homework4.cpp
// CS212 HW #4:  homework4.cpp
//
// Due:          February 27, 2008
//
// Author:       Amy Yurksaitis
//
// Instructor:   Dr. Wang
//
// Goal:         To allow users to input two fractions and then perform select operations
//               (adding, subtracting, mulitpling, and dividing).Then the answer is 
//		 output in its lowest form.
//
// Compile:      g++ homework4.cpp -o homework4.out
// Run:          ./homework4.out
//////////////////////////////////////////////////////////////////////////////////////



#include <iostream>
        
using namespace std;
        
class Fraction
{
        int numerator;
	int denominator;
public:
	void Reduce()
	{
                int n = numerator;
                int d = denominator;
                while( d != 0 )
                {
                        int r = n % d;
                        n = d;
                        d = r;
                }
                if ( n != 0 )
                {
                        numerator /= n;
                        denominator /= n;
        	}
		if (denominator < 0)
                {
                        numerator *= (-1);
                        denominator *= (-1);
                }
                return;
        }

        Fraction()     
        {       numerator = 0; denominator = 1;   }
        Fraction(int n, int d)  
        {       numerator = n; denominator = d;   }
        void Set(int n, int d)
        {       numerator = n; denominator = d;   }
        void Print() const
        {       cout << numerator << '/' << denominator;   }
	 Fraction Addition (Fraction f)
        {       Fraction t;
                int n;
		int d;
                n = numerator*f.denominator + denominator*f.numerator;
                d = denominator*f.denominator;
                t.Set(n, d);
		t.Reduce();
                return t;
        }
	 Fraction Subtraction (Fraction f)
        {       Fraction t;
                int n, d;
                n = (numerator*f.denominator) - (denominator*f.numerator);
                d = denominator*f.denominator;
                t.Set(n, d);
		t.Reduce();
                return t;
        }
	Fraction Multiplication (Fraction f)
        {       Fraction t;
                int n, d;
                n = numerator*f.numerator;
                d = denominator*f.denominator;
                t.Set(n, d);
		t.Reduce();
                return t;
        }
	Fraction Division (Fraction f)
        {       Fraction t;
                int n, d;
                n = numerator*f.denominator;
                d = denominator*f.denominator;
                t.Set(n, d);
		t.Reduce();
                return t;
        }
	
};
                
int main()
{      
	int n;
	int d;
        Fraction x;
	Fraction y;
	Fraction z;
        char dummy;
	char op;
        
        cout << "Input x (n/d, -1/1 to quit): ";
        cin >> n >> dummy >> d; 
        while( n!=-1 && d!=1 )
        {
                x.Set(n, d);
                cout << "Input y (n/d): ";
                cin >> n >> dummy >> d;
                y.Set(n, d);
                cout << "Please chose an operation? (+, -, *, /): ";
                cin >> op;
                if (op == '+')
			z = x.Addition(y);
		else if (op == '-') 
                        z=x.Subtraction(y);
		else if( op == '*')
                        z = x.Multiplication(y);
		else if (op =='/')
			z=x.Division(y); 
                else    cout << "ERROR!!\n\n";
  
   		cout <<"\n";
                x.Print();
                cout <<" " << op <<" ";
                y.Print();
                cout << " = "; 
                z.Print();
		cout <<"\n";
                cout << "********************************************" << endl;
		        	
       		cout << "\n"; 
                cout << "Input x (n/d, -1/1 to quit): ";
                cin >> n >> dummy >> d;   
        }
                
        return 0;
}


Stv3n404 - 2023