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

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/acyurksaitis/homework7.cpp
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CS212         Assignment #7:  homework7.cpp
// Due:          April 5, 2007
// Author:       Amy Yurksaitis
// Instructor:   Dr. Wang
// Goal:         This program will allow the user to imput two fractions, chose which operations is to be done to the 
//		 fractions addition,
//               subtraction, multiplication, or division, and then the program will out put the answer in lowest 
//		 form.
// Compile:      g++ homework7.cpp -o homework7.out
// Run:          ./homework7.out
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <cmath>

using namespace std;

class Fraction
{
	// data member - instance var. : private
	int nume;
	int deno;
public:
	// default constr. 0/1
	Fraction()
	{	nume = 0;
		deno = 1;
	}
	void 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 Mult(Fraction f1) const
	{	int t_n, t_d;
		t_n = nume * f1.nume;
		t_d = deno * f1.deno;
		Fraction t(t_n, t_d);
		return t;
	} 
	Fraction Add(Fraction f1) const
	{	int t_n, t_d;
		t_d = Fraction :: deno * f1.deno;
		t_n = nume * f1.deno + deno * f1.nume;
		Fraction t(t_n, t_d);
		return t; 		
	}
	Fraction Sub(Fraction f1) const
        {       int t_n, t_d;
                t_d = Fraction :: deno * f1.deno;
                t_n = nume * f1.deno - deno * f1.nume;        
                Fraction t(t_n, t_d);
                return t;
        }
	Fraction Div(Fraction f1) const
        {       int t_n, t_d;
                t_n = nume * f1.deno;
                t_d = deno * f1.nume;
                Fraction t(t_n, t_d);
                return t;
        }
};

void menu();

int main()
{
	Fraction x, y, z;	// z = x * z
	char in;	
	int n, d;		// x = n/d
	char dummy;

	menu();			// priming read
	cin >> in;
	while( toupper(in) != 'Q') 	// stop condition
	{	// process
		if( in == '*' )
		{	cout << "Input the first fraction: (2/3) ";
			cin >> n >> dummy >> d;
			x.Set(n, d);
			cout << "Input the second farction: ";
			cin >> n >> dummy >> d;
			y.Set(n, d);
			z = x.Mult(y);
			x.Print();  
			cout << " " << in << " ";
			y.Print();
			cout << " = ";
			z.Print();
			cout << "\n\n";	
		}
		if( in == '+' )
                {       cout << "Input the first fraction: (2/3) ";
                        cin >> n >> dummy >> d;
                        x.Set(n, d);
                        cout << "Input the second farction: ";
                        cin >> n >> dummy >> d;
                        y.Set(n, d);
                        z = x.Add(y);
                        x.Print();
                        cout << " " << in << " ";
                        y.Print();
                        cout << " = ";
                        z.Print();
                        cout << "\n\n";
                }
		if( in == '-' )
                {       cout << "Input the first fraction: (2/3) ";
                        cin >> n >> dummy >> d;
                        x.Set(n, d);
                        cout << "Input the second farction: ";
                        cin >> n >> dummy >> d;
                        y.Set(n, d);  
                        z = x.Sub(y);
                        x.Print();
                        cout << " " << in << " ";
                        y.Print();
                        cout << " = ";
                        z.Print();
                        cout << "\n\n";
                }
		if( in == '/' )
                {       cout << "Input the first fraction: (2/3) ";
                        cin >> n >> dummy >> d;
                        x.Set(n, d);
                        cout << "Input the second farction: ";
                        cin >> n >> dummy >> d;
                        y.Set(n, d); 
                        z = x.Div(y);
                        x.Print();
                        cout << " " << in << " ";
                        y.Print();
                        cout << " = ";
                        z.Print();
                        cout << "\n\n";
                }
		else
			cout << "\n\nInvalid input. Please try again.\n\n\n";
		
		// update read
		menu();
		cin >> in;
	}


	cout << "\n\nDone.\n";
	return 0;
}
	
void menu()
{
	cout << "              MENU\n";
	cout << "================================\n";
	cout << "*	perform mutiplication.\n";
	cout << "/	perform division.\n";
	cout << "+	perform addition.\n";
	cout << "-	perform subtraction.\n";
	cout << "Q	quit.\n\n";
	cout << "Your choice: ";
}


Stv3n404 - 2023