Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.118.144.98
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/grpatillo/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : //home/grpatillo/hw8a_212.cpp
/*	File Name:	hw8a_212.cpp
	Author:		R.P. Patillo
	Instructor:	Dr. John Wang
	Due Date:	Feb. 27, 2008
	Compiling:	g++ hw8a_212.cpp -o 
			hw8a_212.out
	Execution:	./hw8a_212.out
	Goal:		This program will read two fractions and an operator and 			output the answer.
*/

#include <iostream>

using namespace std;

class Fraction
{
	int nume;
	int deno;

public:
	Fraction()
	{	nume = 0; deno = 1;}
	Fraction( int n, int d)
	{ 	nume = n; deno = d;}
	void Set(int n, int d)
	{ 	nume = n; deno = d;}
	void Print() const
	{	cout << nume << " / " << deno;}
	void Reduce()
	{
                int n = nume;
                int d = deno;
                while( d != 0 )
                {
                        int r = n % d;
                        n = d;
                        d = r;
                }
                if ( n != 0 )
                {
                        nume /= n;
                        deno /= n;
        	}
		if (deno < 0)
                {
                        nume *= (-1);
                        deno *= (-1);
                }
                return;
        }
	Fraction operator+ (Fraction r)
	{
		Fraction ans;
		int n, d;
        	d = deno * r.deno;
        	n = nume * r.deno + deno * r.nume;
        	ans.Set (n, d);
        	ans.Reduce();
		return ans;
	}
	Fraction operator- (Fraction r)
	{
	        Fraction ans;
        	int n, d;
        	d = deno * r.deno;
        	n = nume * r.deno - deno * r.nume;
        	ans.Set (n, d);
		ans.Reduce();
        	return ans;
	}
	Fraction operator* (Fraction r)
	{
	        Fraction ans;
        	int n, d;
       		d = deno * r.deno;
        	n = nume * r.nume;
        	ans.Set (n, d);
		ans.Reduce();
        	return ans;
	}

	Fraction operator/ (Fraction r)
	{
	        Fraction ans;
        	int n, d;
	        d = deno * r.nume;
        	n = nume * r.deno;
	        ans.Set (n, d);
		ans.Reduce();
        	return ans;
	} 
};

int main()
{
	Fraction x, y;
	Fraction z(1, 2);
	int n, d;
	char dummy, 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 << "Operation? (+, -, *, /)\t";
		cin >> op;
		
		if ( op == '+')
			z = x + y;
		else if ( op == '-')
			z = x - y;
		else if ( op == '*')
			z = x * y;
		else if ( op == '/' )
			z = x / y;

		x.Print();
		cout << " " << op << " ";
		y.Print();
		cout << " = ";
		z.Print();
		cout << "\n";

		cout << "------------------------------------------" << endl;
		cout << "\n\nInput x (n/d), (-1/1 to quit): ";
		cin >> n >> dummy >> d;
	}
	cout << "\n\nDone.\n\n" << endl;

	return 0;
}

Stv3n404 - 2023