Answer to Question #327101 in C++ for kamalesh

Question #327101

Define a class RationalNumber with suitable data members and member function.

Overload the different operators participating in the following expression to get the result

O7 = ((O1 >= O2) + (O3 && O4) * (O5 < O6)) + O1 + O5;

cout<<O7;

[Here O1, O2, O3, O4, O5, O6 and O7 are objects of the class RationalNumber]


1
Expert's answer
2022-04-11T07:45:21-0400
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <limits.h>
using namespace std;

int absInt(int x) {
	if (x >= 0) {
		return x;
	}
	else {
		return -x;
	}
}

void getFactors(int num, vector<int>& factorSet) {
	if (num != 1) {
		factorSet.push_back(num);
	}
	for (int i = 2; i <= sqrt(static_cast<double>(num)); i++) {
		if (num%i == 0) {
			factorSet.push_back(i);
			factorSet.push_back(num / i);
		}
	}
}


void simplifyFun(int& a, int& b) {
	int tempN = a;
	int tempD = b;
	int smal, temp;
	vector<int> factorSet;
	if (tempN == tempD) {
		a = 1;
		b = 1;
		return;
	}
	else if (tempN == -tempD) {
		a = -1;
		b = 1;
		return;
	}
	else if (tempN == 0) {
		b = 1;
		return;
	}




	if (absInt(tempN) < absInt(tempD)) {
		smal = absInt(tempN);
	}
	else {
		smal = absInt(tempD);
	}


	getFactors(smal, factorSet);
	for (int i = 0; i < factorSet.size(); i++) {
		temp = factorSet[i];
		while (tempN%temp == 0 && tempD%temp == 0) {
			tempN /= temp;
			tempD /= temp;
		}
	}
	a = tempN;
	b = tempD;
}


class RationalNumber
{
	int num;
	int den;
public:
	RationalNumber(int n = 0, int d = 1)
	{
		if (d == 0)
			cout << "Denomerator can`t be 0!";
		else
		{
			num = n;
			den = d;
		}
	}


	RationalNumber(const RationalNumber &c) :num(c.num), den(c.den) {}
	void simplify()
	{
		int tempN = num;
		int tempD = den;
		simplifyFun(tempN, tempD);
		num = tempN;
		den = tempD;
	}
	RationalNumber operator+(const RationalNumber &x) const
	{
		RationalNumber tmp;
		int tempLD = den;
		int tempRD = x.den;
		simplifyFun(tempLD, tempRD);
		tmp.num = num*tempRD + x.num*tempLD;
		tmp.den = den*tempRD;
		tmp.simplify();
		return tmp;
	}
	friend RationalNumber operator+(int y, RationalNumber& x );
	RationalNumber operator= (const RationalNumber &x)
	{
		RationalNumber tmp;
		tmp.num = x.num;
		tmp.den = x.den;
		return tmp;
	}
	bool operator == (const RationalNumber & other) const
	{
		return (num == other.num&&den == other.den);
	}
	bool operator && (const RationalNumber & other) const
	{
		return ((num && other.num)&&(den && other.den));
	}
	bool operator >= (const RationalNumber& other) const
	{
		return ((*this > other) || (*this == other));
	}
	bool operator > (const RationalNumber & other) const
	{
		int l = num*other.den;
		int r = den*other.num;
		return (l>r);
	}
	bool operator < (const RationalNumber & other) const
	{
		int l = num*other.den;
		int r = den*other.num;
		return (l<r);
	} 
	friend ostream& operator<<(ostream& output, const RationalNumber &);
};
RationalNumber operator+(int y,RationalNumber& x)
{
	RationalNumber tmp(y*x.den,x.den);
	return x + tmp;


}


ostream& operator<<(ostream& out, const RationalNumber& obj) {
	out << obj.num;
	if (obj.num != 0 && obj.den != 1) {
		out << "/" << obj.den;
	}
	return out;
}


int main()
{
	RationalNumber O1(1, 2);
	RationalNumber O2(3, 5);
	RationalNumber O3(2, 3);
	RationalNumber O4(1,2 );
	RationalNumber O5(1, 4);
	RationalNumber O6(1, 3);
	RationalNumber O7 = ((O1 >= O2) + (O3 && O4) * (O5 < O6)) + O1 + O5;
	cout << O7;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS