Question
C++

prefix 3. Add four basic mathematical operators +, -, *, and 7, the six comparison operators < <= > >= ==, and !=, and the in
1) Programming Exercise: p. 352 / 3 Use this code to test your program: Rational_testing.cpp
entre include <iostres include Rational3. using namespace std; int main() 5 - Rational (7.2). - tiamat 20 Rational Rational
0 0
Add a comment Improve this question Transcribed image text
Answer #1

note : plz follow the space indentation exactly as per in the program screenshot.So that u wont get any errors.

====================================

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

class Rational {
public:
// Constructors
Rational();
Rational(int num, int den);
Rational(int num);
// Member functions
int get_numerator();
int get_denominator();
Rational operator + (const Rational & f) const; // operator+()
Rational operator - (const Rational & f) const; // operator-()
Rational operator * (const Rational & f) const; // operator*()
Rational operator / (const Rational & f) const; // operator*()
bool operator != (const Rational & f) const; // operator!=()
bool operator == (const Rational & f) const; // operator==()
bool operator <= (const Rational & f) const; // operator<=()
bool operator >= (const Rational & f) const; // operator>=()
bool operator < (const Rational & f) const; // operator<()
bool operator > (const Rational & f) const; // operator>()
/**
* Friend function declarations
* Output Rational number to an output stream, Example 3+4i
*/
friend std::ostream & operator << (std::ostream & dout,
Rational & );
/**
* read Rational number to an input stream
*/
friend std::istream & operator >> (std::istream & din, Rational & );
Rational normalize();
int gcd(int x, int y);
private:
int numerator;
int denominator;
};


Rational::Rational()
{
this->numerator=0;
this->denominator=1;
}
Rational::Rational(int num, int den)
{
this->numerator=num;
this->denominator=den;   
      
}
Rational::Rational(int num)
{
this->numerator=num;
this->denominator=1;
}
// Member functions
int Rational::get_numerator()
{
return numerator;
}
int Rational::get_denominator()
{
return denominator;
}
  
int Rational::gcd(int x, int y) {
while (y != 0) {
int temp = x % y;
x = y;
y = temp;
}
return x;
}
Rational Rational::normalize() {
int num = this->numerator;
int den = this->denominator;
if (num < 0 && den < 0) {
num = num * -1;
den = den * -1;
} else if (den < 0) {
num = num * -1;
den = den * -1;
}
int g=gcd(num,den);
  
Rational r(num/g, den/g);
return r;
}
Rational Rational::operator+(const Rational& f) const
{

int a = numerator;
int b = denominator;
int c = f.numerator;
int d = f.denominator;
int sumnumer = (a * d + b * c);
int denom = (b * d);
Rational frac(sumnumer,denom);
frac=frac.normalize();
return frac;
}
Rational Rational::operator-(const Rational& f) const
{
  
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
int subnumer = (a * d - b * c);
int denom = (b * d);
Rational frac(subnumer,denom);
frac=frac.normalize();
return frac;
}
Rational Rational::operator*(const Rational& f) const
{
  
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
int mulnumer = (a * c);
int muldenom = (b * d);
Rational frac(mulnumer,muldenom);
frac=frac.normalize();
return frac;
}
Rational Rational::operator/(const Rational& f) const
{

int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
int divnumer = (a * d);
int divdenom = (c * b);
Rational frac(divnumer,divdenom);
frac=frac.normalize();
return frac;
}
bool Rational::operator<=(const Rational& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;

if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;
}
bool Rational::operator>=(const Rational& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;

if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;
}
bool Rational::operator<(const Rational& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;

if ((n1 / d1) < (n2 / d2))
return true;
else
return false;
}
bool Rational::operator>(const Rational& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;
}
bool Rational::operator!=(const Rational& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;
}
bool Rational::operator==(const Rational& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
  
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;
}

// Function implementation which display Rational numbers by using the operator overloading '<<'
ostream& operator<<(ostream& dout,Rational& c)
{

dout <<"("<< c.get_numerator() << "/" << c.get_denominator()<<")";
return dout;
}
// Function implementation which read Rational numbers by using the operator overloading '>>'
istream& operator>>(istream& din, Rational& c)
{
char ch;
din >> c.numerator;
din >> ch;
din >> c.denominator;
return din;
}


int main() {
   //Declaring variables
Rational r1=Rational(2,4),r2=Rational(3,6),r3=Rational(1,3),r4=Rational(3,10),r5=Rational(7,2),r6=Rational(20,6);
cout<<"Created fraction 2/4 , got :"<<r1<<", Should have received 1/2."<<endl;
cout<<"Created fraction 3/6 , got :"<<r2<<", Should have received 1/2."<<endl;
cout<<"Created fraction 1/3 , got :"<<r3<<endl;
  
cout<<"Created fraction 3/10 , got :"<<r4<<endl;
cout<<"Created fraction 7/2 , got :"<<r5<<endl;
cout<<"Created fraction 20/6 , got :"<<r6<<endl;
  
// Adding
Rational r=(r1+r2);
cout<<r1<<"+"<<r2<<"="<<r<<endl;
r=(r1+r3);
cout<<r1<<"+"<<r3<<"="<<r<<endl;
r=(r1+r4);
cout<<r1<<"+"<<r4<<"="<<r<<endl;
  
// Subtraction
r=(r1-r2);
cout<<r1<<"-"<<r2<<"="<<r<<endl;
r=(r1-r3);
cout<<r1<<"-"<<r3<<"="<<r<<endl;
r=(r1-r4);
cout<<r1<<"-"<<r4<<"="<<r<<endl;
  

// Multiplcation
r=(r1*r2);
cout<<r1<<"*"<<r2<<"="<<r<<endl;
r=(r1*r3);
cout<<r1<<"*"<<r3<<"="<<r<<endl;
r=(r1*r4);
cout<<r1<<"*"<<r4<<"="<<r<<endl;
  
// Multiplcation
r=(r1/r2);
cout<<r1<<"/"<<r2<<"="<<r<<endl;
r=(r1/r3);
cout<<r1<<"/"<<r3<<"="<<r<<endl;
r=(r1/r4);
cout<<r1<<"/"<<r4<<"="<<r<<endl;
  
r=(r1/r2);
cout<r1<<" < "<<r2<" is "<<r<<endl;
  
  
  
   return 0;
}

====================================Thank yOu

Add a comment
Know the answer?
Add Answer to:
C++ prefix 3. Add four basic mathematical operators +, -, *, and 7, the six comparison...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix)...

    (1) Declaration and implementation of complex class, overloading operator +, -, != , /, --(prefix), --(postfix) as friend functions.         (2)The program should be in Separating Class Specification, Implementation(complex.h,complex.cpp), and Client Code(Task2.cpp).             (3) Run the program and capture screenshots of output. Declaration and implementation of complex class, overloading operator +, -, != , /=, --(prefix), --(postfix) as friend function */ #include "Complex.h" #include <iostream> using namespace std; int main(){       Complex c1(5, 4), c2(2, 10), c3;       cout <<...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

    I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b + c/d = (ad + bc)/bd a/b – c/d =...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • 7- (50 points) Write a simple calculator in Python which supports four basic mathematical operators (1.e....

    7- (50 points) Write a simple calculator in Python which supports four basic mathematical operators (1.e. + - / *). You need to define a module which supports addition, deletion, multiplication and subtraction. Then import your module to your calculator file and use it in your code. The following example shows how your basic calculator needs to work. Select operation: 1.Add 2.Subtract 3.Multiply 4.Divide Enter your choice(1/2/3/4): 3 Enter first number: 12 Enter second number: 14 12 * 14 =...

  • c++ When the students started selling cookies, they were told that the students who sell the...

    c++ When the students started selling cookies, they were told that the students who sell the maximum number of bóxes will have 10% of the money they generate donated to their favorite charitable organization. So, in addition to the output your program generated in Exercise 18, your program should output: 1. The names of all the students selling the maximum number of boxes 2. The amount that will be donated to their favorite charitable organization. Ch5_Ex19Data.txt 1 3.50 2 Sara...

  • C++ only I'm using code lite editor as well Please add comments and pre and post...

    C++ only I'm using code lite editor as well Please add comments and pre and post conditions too 9:16 AM .oooo Verizon GI 98% Use C++ only Please include comments and pre post conditions am also using a new editor called code lite write a class for rational numbers. Each 15 object in the class should have two inte values that define the rational number: the numerator and the denominator. For example, the fraction 516 would have a denominator of...

  • C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.]...

    C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT