Answer to Question #326333 in C++ for khan

Question #326333


class Rational {


public:

Rational(int n=0,int d=1);

Rational(const Rational &copy);


Rational operator = (const Rational &x);


Rational operator+(const Rational &x) const;

Rational operator-(const Rational &x) const;

Rational operator*(const Rational &x) const;

Rational operator/(const Rational &x) const;


Rational operator += (const Rational &x);

Rational operator -= (const Rational &x);

Rational operator *= (const Rational &x);

Rational operator /= (const Rational &x);


bool operator == (const Rational & other) const;

bool operator < (const Rational & other) const;

bool operator > (const Rational & other) const;

bool operator <= (const Rational & other) const;

bool operator >= (const Rational & other) const;


operator string() const;

~Rational();

};


ostream& operator<<(ostream& output, const Rational &);

istream& operator>>(istream& input, Rational&);


1
Expert's answer
2022-04-09T17:52:45-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 Rational
{
	int num;
	int den;
public:
	Rational(int n = 0, int d = 1)
	{
		if (d == 0)
			cout << "Denomerator can`t be 0!";
		else
		{
			num = n;
			den = d;
		}
	}


	Rational(const Rational &c):num(c.num),den(c.den){}
	void simplify() 
	{
		int tempN = num;
		int tempD = den;
		simplifyFun(tempN, tempD);
		num=tempN;
		den=tempD;
	}
	Rational operator= (const Rational &x)
	{
		Rational tmp;
		tmp.num = x.num;
		tmp.den = x.den;
		return tmp;
	}
	Rational operator+(const Rational &x) const
	{
		Rational 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;
	}


	Rational operator-(const Rational &x) const
	{
		Rational 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;
	}
	Rational operator*(const Rational &x) const
	{
		Rational tmp;
		Rational tmp2(x.num,den);
		Rational tmp3(num,x.den);
		int a = tmp2.den;
		int b = tmp2.num;
		int c = tmp3.den;
		int d = tmp3.num;
		tmp.num = b*d;
		tmp.den = a*c;
		return tmp;
	}
	Rational operator/(const Rational &x) const
	{
		Rational tmp1(num, den);
		Rational tmp2(x.num, x.den);
		return tmp1*tmp2;
	}


	Rational operator += (const Rational &x)
	{
		return *this + x;
	}
	Rational operator -= (const Rational &x)
	{
		return *this - x;
	}
	Rational operator *= (const Rational &x)
	{
		return *this*x;
	}
	Rational operator /= (const Rational &x)
	{
		return *this / x;
	}


	bool operator == (const Rational & other) const
	{
		return (num == other.num&&den == other.den);
	}
	bool operator < (const Rational & other) const
	{
		int l = num*other.den;
		int r = den*other.num;
		return (l<r);
	}
	bool operator > (const Rational & other) const
	{
		int l = num*other.den;
		int r = den*other.num;
		return (l>r);
	}
	bool operator <= (const Rational & other) const
	{
		return ((*this < other) || (*this == other));
	}
	bool operator >= (const Rational & other) const
	{
		return ((*this > other) || (*this == other));
	}
	operator string() const
	{
		return to_string(num) + "/" + to_string(den);
	}
	~Rational() {};
	friend ostream& operator<<(ostream& output, const Rational &);
	friend istream& operator >> (istream& input, Rational&);
};




ostream& operator<<(ostream& out, const Rational& obj) {
	out << obj.num;
	if (obj.num != 0 && obj.den != 1) {
		out << "/" << obj.den;
	}
	return out;
}
istream& operator >> (istream& in, Rational& obj)
{
string inputstr;
  int num = 0;
  int sign = 1;
  bool slashExist = false;
  bool dotExist = false;
  bool validInput = true;
  int virtualDenominator = 1;
  cin >> inputstr;
  for (int i = 0; i < inputstr.size(); i++) {
    char temp = inputstr[i];
    if (temp == '.') {
      if (dotExist == false && slashExist == false && i != 0) {
        dotExist = true;
      }
      else {
        validInput = false;
        break;
      }
    }
    else if (temp == '/') {
      if (dotExist == false && slashExist == false && i != 0) {
        slashExist = true;
        obj.num=sign*num;
        num = 0;
        sign = 1;
      }
      else {
        validInput = false;
        break;
      }
    }
    else if (temp == '-') {
      if (i == 0){
        sign = -sign;
      }
      else if (inputstr[i-1] == '/') {
        sign = -sign;
      }
      else {
        validInput = false;
        break;
      }
    }
    else if (temp <= '9' && temp >= '0') {
      if (dotExist) {
        if (virtualDenominator > INT_MAX/10) {
          cout << "this frational is too long to handle.";
          validInput = false;
          break;
        }
        else { 
          virtualDenominator *= 10;
        }
      }
      if (num > INT_MAX/10) {
          cout << "this number is too long to handle.";
          validInput = false;
          break;
      }
      num *= 10;
      num += inputstr[i]-'0';
    }
    else {
      validInput = false;
      break;
    }
  }
  
  if (validInput == false) {
    obj.num=0;
    obj.den=1;
    cout << "Input is not valid! The whole set to 0" << endl;
  }
  else {
    if (slashExist == true) {
      obj.den=sign*num;
    }
    else if (dotExist) {
      obj.num=sign*num;
      obj.den=virtualDenominator;
    }
    else {
      obj.num=sign*num;
      obj.den=1;
    }
  }
  obj.simplify();
  return in;
}


int main()
{
	Rational a, b;
	cout << "Please, input rational a, b: ";
	cin >> a>>b;
	cout << a + b<<endl;
	cout << a - b << endl;
	cout << a * b  << endl;
	cout << a / b << endl;
	cout << (a += b) << endl;
	cout << (a -= b) << endl;
	cout << (a == b) << endl;
	cout << (a >= b) << endl;
	cout << (a <= b) << endl;
	cout << (a *= b) << endl;
	cout << (a /= b) << endl;
}

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