Question

0 Input: Output: Value: Under control of main function Under control of main function The purpose of this assignment is to beLab 7 Due Date: See Blackboard bool operator (const Rational& rhs) const; 4bool operator! (const Rationalk rhs) const; bool oLab 7 Due Date: See Blackboard cout << first << -- << second << << (first-second) << end! cout << first << != <<newuseracsunix /2336/07> cat 01.dat /lab07 first1/-2 second-3/1 result0/1 first-3/4 -first 3/4 129/6579 1935/249 true 129/657

Lab 7 Due Date: See Blackboard 142-83/1651. 127/-1079= false 143 -83/1651 - 127/-1079-true 144 -83/1651 127/-1079 false 145 -

This is my process, can you fix it for me?

istream& operator>>(istream& in, Rational& rat) int num, denom; if (in >>num > denom) rat.setNumerator(num); rat.setDenominatRational Rational::operator-() const // returns - (*this) return Rational(this->getNumerator)-1, this->getDenominator)); Rati

0 0
Add a comment Improve this question Transcribed image text
Answer #1

lab07.h
#ifndef LAB07_H
#define LAB07_H

#include <iostream>

using namespace std;

class Rational
{
// overloaded input operator initializes Rational rat from input stream in
friend istream& operator>>(istream& in, Rational& rat);

// overloaded output operator prints Rational rat to output stream out
friend ostream& operator<<(ostream& out, const Rational& rat);
public:
Rational();                                     // default constructor
Rational(int num, int denom);                   // additional constructor
void setNumerator(int num);                     // set numerator to num
void setDenominator(int denom);                 // set denominator to denom
int getNumerator() const;                       // returns numerator
int getDenominator() const;                     // returns denominator

void reduce();                                  // reduce to lowest terms
                                                  //   and normalize
Rational multiplicativeInverse() const;         // returns multiplicative
                                                  //   inverse of *this


Rational& operator=(const Rational& rhs);       // *this = rhs;
Rational operator+(const Rational& addend) const;// returns *this + addend
Rational operator-() const;                     // returns -(*this)
Rational operator-(const Rational& subtrahend) const;// returns *this - subtrahend
Rational operator*(const Rational& multiplicand) const;// returns *this * multiplicand
Rational operator/(const Rational& divisor) const;// returns *this / divisor

bool operator==(const Rational& rhs) const;     // *this == rhs
bool operator!=(const Rational& rhs) const;     // *this != rhs
bool operator< (const Rational& rhs) const;     // *this < rhs
bool operator<=(const Rational& rhs) const;     // *this <= rhs
bool operator> (const Rational& rhs) const;     // *this > rhs
bool operator>=(const Rational& rhs) const;     // *this >= rhs

private:
int r[2];                                       // r[0] represents numerator
                                                  // r[1] represents denominator
int gcd(int m, int n) const;                    // returns the greatest
                                                  //   common divisor of m
                                                  //   and n
int lcm(int m, int n) const;                    // returns the least common
                                                  //   multiple of m and n
};

#endif

lab07.cpp


#include <lab07.h>
#include <stdlib.h>


// overloaded input operator initializes Rational rat from input stream in
//to istream& operator>>(istream in, rational rat)
//istream Rational::read

// overloaded output operator prints Rational rat to output stream out
ostream& operator<<(ostream& out, const Rational& rat)
{
   out << rat.getNumerator() << '/' << rat.getDenominator();

   return out;
}


istream& operator>>(istream& in, Rational& rat)                    // Read Rational from input stream
{
   int num, denom;
  
   in >> num >> denom;
   rat.setNumerator(num);
   rat.setDenominator(denom);
  
   return in;
}

Rational& Rational::operator=(const Rational& rhs)                             // *this = rhs;
{
   if(this != &rhs)
   {
       this->setNumerator(rhs.getNumerator());
       this->setDenominator(rhs.getDenominator());
   }

  
   return *this;
}


Rational Rational::operator+(const Rational& addend) const                   // returns *this + addend
{
   Rational sum;    // declare a local rational object
   int lcm = this->lcm(this->getDenominator(), addend.getDenominator());
  
   sum.setNumerator(lcm * this->getNumerator() / this->getDenominator() +
                   lcm * addend.getNumerator() / addend.getDenominator());
   sum.setDenominator(lcm);
   sum.reduce();
  
   return sum;
                      
}



Rational Rational::operator-() const                                       // returns -(*this)
{
   return Rational(-1 * this->getNumerator(), this->getDenominator());
}



Rational Rational::operator-(const Rational& subtrahend) const                   // returns *this - subtrahend
{
   //return this->add(subtrahend.additiveInverse());
   //return this->operator+(subtrahend.operator-()); // works
   return *this + -subtrahend;
  
}

Rational Rational::operator*(const Rational& multiplicand) const               // returns *this * multiplicand
{
      Rational product;                   // declare a local rational object


      product.setNumerator(this->getNumerator() * multiplicand.getNumerator());
      product.setDenominator(this->getDenominator() * multiplicand.getDenominator());

      product.reduce();
      return product;
}

Rational Rational::operator/(const Rational& divisor) const // returns *this
{

   return this->operator*(divisor.multiplicativeInverse());
}

bool Rational::operator==(const Rational& rhs) const     // *this == rhs
{
   return (*this - rhs).getNumerator() == 0;
}


bool Rational::operator!=(const Rational& rhs) const     // *this != rhs
{
   return !(*this == rhs);
   return *this != rhs;
}

bool Rational::operator< (const Rational& rhs) const     // *this < rhs
{
   if((this->getNumerator() * rhs.getDenominator()) < (rhs.getNumerator() * this->getDenominator()))
   {
       return 1;
   }
   else
   {
       return 0;
   }
}


bool Rational::operator<=(const Rational& rhs) const     // *this <= rhs
{
   return (*this < rhs || *this == rhs);
}

bool Rational::operator>(const Rational& rhs) const     // *this > rhs
{
  
   return !(*this < rhs);
}
bool Rational::operator>=(const Rational& rhs) const     // *this >= rhs
{
   return (!(*this < rhs) || *this == rhs);
  
}

lab07main.C

#include <lab07.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
Rational first(1, -2), second(-3, 0), result;

cout << boolalpha;
cout << "first = " << first << " second = " << second
       << " result = " << result << endl;

while (cin >> first >> second)
{
    cout << "first = " << first;
    result = -first;
    result.reduce();
    cout << " -first = " << result << endl;

    cout << first << " + " << second << " = " << first + second << endl;
    cout << first << " - " << second << " = " << first - second << endl;
    cout << first << " * " << second << " = " << first * second << endl;
    cout << first << " / " << second << " = " << first / second << endl;

    cout << first << " == " << second << " = " << (first == second) << endl;
    cout << first << " != " << second << " = " << (first != second) << endl;
    cout << first << " < " << second << " = " << (first < second) << endl;
    cout << first << " <= " << second << " = " << (first <= second) << endl;
    cout << first << " > " << second << " = " << (first > second) << endl;
    cout << first << " >= " << second << " = " << (first >= second) << endl;
}

return EXIT_SUCCESS;
}

Add a comment
Know the answer?
Add Answer to:
This is my process, can you fix it for me? 0 Input: Output: Value: Under control...
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
  • 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...

  • 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...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • Hi I need a fix in my program. The program needs to finish after serving the...

    Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list. Requeriments: Headers: DynamicArray.h #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class DynamicArray { V* values; int cap; V dummy; public: DynamicArray(int = 2); DynamicArray(const DynamicArray&); ~DynamicArray() { delete[] values; } int capacity() const { return cap; } void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); }; template DynamicArray::DynamicArray(int cap) { this->cap = cap; values =...

  • I've posted 3 classes after the instruction that were given at start You will implement and...

    I've posted 3 classes after the instruction that were given at start You will implement and test a PriorityQueue class, where the items of the priority queue are stored on a linked list. The material from Ch1 ~ 8 of the textbook can help you tremendously. You can get a lot of good information about implementing this assignment from chapter 8. There are couple notes about this assignment. 1. Using structure Node with a pointer point to Node structure to...

  • I'm having trouble writing this code, can some help me? Step 1: Capturing the input The...

    I'm having trouble writing this code, can some help me? Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6...

  • Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;   ...

    Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;    minutes = 0;    isAfternoon = false;    //check to make sure there are 5 characters    if (//condition to check if length of string is wrong)    {        cout << "You must enter a valid military time in the format 00:00" << endl;    }    else    {        //check to make sure the colon is in the correct...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I have written my code for an employee management system that stores Employee class objects into...

    I have written my code for an employee management system that stores Employee class objects into a vector, I am getting no errors until I try and compile, I am getting the error: C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion). But I am not sure why any help would be great, Thank you! 1 2 3 4 5 6 7 8 9 10 11 12 13...

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