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

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/owpile/fraction3.cpp
//	1/2 + 3/4 = ?
//	input a, b from keyboard

#include <iostream>
using namespace std;

class Fraction		// p.131
{	int nume;
	int deno;
	// reduce the fractions - p.115
	// void reduce();
public:
	// 0/1
	Fraction()
	{ 	nume = 0;
		deno = 1;
	}
	Fraction(int n, int d)
	{	nume = n;
		deno = d;
	}
	// function memebers
	void Set(int n, int d)
        {       nume = n;
                deno = d;
        }
	void Print() const
	{	cout << nume << "/" << deno; 	}
	Fraction operator+(Fraction x)		//overloading + 
	{	Fraction ans;		// temporary obj.	
		ans.nume = nume * x.deno + deno * x.nume;
		ans.deno = deno * x.deno;
		// use the reduce()
		// ????
		return  ans;
	}
	Fraction operator-(Fraction x)
        {       Fraction ans;           // temporary obj.
                ans.nume = nume * x.deno - deno * x.nume;
                ans.deno = deno * x.deno;
                return  ans;
        }
	Fraction operator*(Fraction x)
        {       Fraction ans;           // temporary obj.
                ans.nume = nume * x.nume;
                ans.deno = deno * x.deno;
                return  ans;
        }
	Fraction operator/(Fraction x)
        {       Fraction ans;           // temporary obj.
                ans.nume = nume * x.deno;
                ans.deno = deno * x.nume;
                return  ans;
        }

};

int main()
{
	int a1, b1;
	int a2, b2;
	int ans;
	char dummy, op;

	cin >> a1 >> dummy >> b1;
	cin >> op;
	cin >> a2 >> dummy >> b2;
	
	Fraction f1, f2;
	f1.Set(a1, b1);
	f2.Set(a2, b2);

	if(op == '+')  ans = f1.Addition(f2);
	//other processes
	//output
	f1.Print();
	cout << " " << op << " " << " ";
	f2.Print();
	cout " = ";
	ans.Print();
	cout "\n";
	

	cout << "\n\nHi.\n\n.";
	return 0;
}



Stv3n404 - 2023