Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I want to check if my C++ code follows the requirements or how can I fix it because the parts circled in red are

Hello I want to check if my C++ code follows the requirements or how can I fix it because the parts circled in red are different from the expected output and both were tested using the same IDE. Thanks.

Code:

Frac.h

#ifndef FRAC_H #define FRAC_H #include #include #include

using namespace std;

class Frac;

ostream& operator > (istream& strm, Frac&);

class Frac { private: int num; int den; int gcd(int a, int b) { int max, min; a = abs(a); b = abs(b);

if (a

while (max % min != 0) { int tmp = max % min; max = min; min = tmp; }

return(min); } void lowTerms(int& num, int& den);

public: Frac() { num = 0; den = 1; }

Frac(int n) { num = n; den = 1; }

Frac(int n, int d) { num = n / gcd(n, d); den = d / gcd(n, d); }

Frac(string s);

Frac(const Frac& rhs) { num = rhs.num; den = rhs.den; }

Frac operator=(const Frac& rhs) { num = rhs.num; den = rhs.den; }

void setNum(int); void setDen(int);

int getNum() const; int getDen() const;

Frac operator + (const Frac& rhs); Frac operator - (const Frac& rhs); Frac operator * (const Frac& rhs); Frac operator / (const Frac& rhs);

Frac operator++(); Frac operator++(int); Frac operator--(); Frac operator--(int);

bool operator == (const Frac& f2); bool operator != (const Frac& f2); bool operator (const Frac& f2); bool operator = (const Frac& f2);

operator int() { return (num) / den; } operator float() { float temp; temp = static_cast(num) / static_cast(den); return temp; } friend ostream& operator > (istream& strm, Frac& f);

};

void Frac::setNum(int n) { num = n; }

void Frac::setDen(int d) { den = d; }

int Frac::getDen()const { return den; } int Frac::getNum()const { return num; }

void Frac::lowTerms(int& num, int& den) { int div = gcd(num, den); num = num / div; den = den / div; }

Frac::Frac(string s) { stringstream ss; ss > num; char ch; ss >> ch; ss >> den; }

Frac Frac::operator + (const Frac& rhs) { Frac temp; temp.num = (num * rhs.den) + (rhs.num * den); temp.den = den * rhs.den; temp.lowTerms(temp.num, temp.den); return temp; }

Frac Frac::operator - (const Frac& rhs) { Frac temp; temp.num = (num * rhs.den) - (rhs.num * den); temp.den = den * rhs.den; temp.lowTerms(temp.num, temp.den); return temp; }

Frac Frac::operator * (const Frac& rhs) { Frac temp; temp.num = num * rhs.num; temp.den = den * rhs.den; temp.lowTerms(temp.num, temp.den); return temp; }

Frac Frac::operator / (const Frac& rhs) { Frac temp; temp.num = num * rhs.den; temp.den = den * rhs.num; temp.lowTerms(temp.num, temp.den); return temp; }

Frac Frac::operator++() { num += den; lowTerms(num, den); return *this; } //postfix operator ++ Frac Frac::operator++(int) { Frac* temp = new Frac(this->num, this->den); this->num += this->den; lowTerms(num, den); return *temp; } //postfix operator -- Frac Frac::operator--(int) { Frac* temp = new Frac(this->num, this->den); this->num -= this->den; lowTerms(num, den); return *temp; }

Frac Frac::operator--() { num -= den; lowTerms(num, den); return *this; }

bool Frac::operator == (const Frac& f2) { return (num * f2.den == den * f2.num); }

bool Frac::operator != (const Frac& f2) { return (num * f2.den != den * f2.num); }

bool Frac::operator

bool Frac::operator > (const Frac& f2) { return (num * f2.den > den* f2.num); }

bool Frac::operator

bool Frac::operator >= (const Frac& f2) { return (num * f2.den >= den * f2.num); }

ostream& operator

istream& operator >> (istream& strm, Frac& rhs) { char c; strm >> rhs.num; strm >> c; strm >> rhs.den; rhs.lowTerms(rhs.num, rhs.den); return strm; } #endif

TestFrac.cpp

#include "Frac.h" #include #include #include

using namespace std;

int main() { Frac x(3, 4); Frac y(1, 2); Frac getSetTest; Frac f2(3, 5), f3(9, 2); //for testing type conversions int num; int den;

cout

cout

cout

cout

cout , =, !=, ==."

if (x != y) { cout

if (x > y) { cout y is: true" y is: false"

if (x

if (x >= y) { cout = y is: true" = y is: false"

if (x

cout

Frac f1; cout >"; cout > f1; cout

cout

cout

cout

cout

cout

My output:

image text in transcribed

image text in transcribed

Expected Output:

image text in transcribed

Requirements:

image text in transcribed

image text in transcribed

Created Frac x(3, 4) as 3/4 Created Frac y(1, 2) as 1/2 String constructed: s: 6/7 Copy constructed z as x: 3/4 Assigned 'assignedFrac' as z: 3/4 Part-1.2 Getters and Setters getSet Test is initialized to: 0/1 setting getSetTest.num to 3 Its num is now: 3 setting getSetTest.den to 4 Its den is now: 4 / Part-1.3 Operators basic math: +, -, * x + y is: 5/4 x - y is: 1/4 x + y is: 3/8 x / y is: 3/2 pre and post ++ and -- (4 different types) 3/2 is in c in c c++ c-- --c ++ 3/21/23/23/25/2 , , !=, =. comparators: , x == y is: false x != y is: true x > y is: true x = y is: true x > Enter a fraction to test iostream operators 5/6 You have entered: 5/6 Part-2 Operators in one statement v. separated statements. 5/6 5/6 5/6 11/6 5/6 11/6 5/6 -1/6 -1/6 5/6 -1/6 5/6 Why the output for these two identical sequences are not the same? Testing for string Frac ("5/6") using postfix ++ and -- This sequence is in one statement: 5/6 11/6 5/6 5/6 11/6 5/6 The one below is broken into 6 steps: 5/6 5/6 -1/6 -1/6 5/6 -1/6 pre and postfix ++ and -- (4 different types) 3/2 is c in c , =, !=, == x >y is: true x = y is: true x > Enter a fraction to test iostream operators: 5/6 You have entered: 5/6 Part-2 Operators in one statement v. separated statements. 11/6 11/6 11/6 11/6 11/6 11/6 11/6 5/6 5/6 11/6 5/6 11/6 1. Constructors: * you may combine the first 3 Contructors below into one. Default: numerator default as 0, denominator default as 1 One (1) argument Fill Constructor: (int numerator) numerator only, denominator default as 1 Two (2) arguments Fill Constructor: (int numerator, int denominator) String Constructor: (string s); where s in the format of "3/4" 2. Necessary getters and setters 3. Support the following operators: basic math: +, - *./ pre and postfix ++ and -- (4 different types) comparators: , =, !=,== 4. Type conversion operators, to convert Frac numbers to integer or floating point numbers: cout > operator To include the Special ++/-- Test Pattern (inside the testFrace s as part of your final test program. Created Frac x(3, 4) as 3/4 Created Frac y(1, 2) as 1/2 String constructed: s: 6/7 Copy constructed z as x: 3/4 Assigned 'assignedFrac' as z: 3/4 Part-1.2 Getters and Setters getSet Test is initialized to: 0/1 setting getSetTest.num to 3 Its num is now: 3 setting getSetTest.den to 4 Its den is now: 4 / Part-1.3 Operators basic math: +, -, * x + y is: 5/4 x - y is: 1/4 x + y is: 3/8 x / y is: 3/2 pre and post ++ and -- (4 different types) 3/2 is in c in c c++ c-- --c ++ 3/21/23/23/25/2 , , !=, =. comparators: , x == y is: false x != y is: true x > y is: true x = y is: true x > Enter a fraction to test iostream operators 5/6 You have entered: 5/6 Part-2 Operators in one statement v. separated statements. 5/6 5/6 5/6 11/6 5/6 11/6 5/6 -1/6 -1/6 5/6 -1/6 5/6 Why the output for these two identical sequences are not the same? Testing for string Frac ("5/6") using postfix ++ and -- This sequence is in one statement: 5/6 11/6 5/6 5/6 11/6 5/6 The one below is broken into 6 steps: 5/6 5/6 -1/6 -1/6 5/6 -1/6 pre and postfix ++ and -- (4 different types) 3/2 is c in c , =, !=, == x >y is: true x = y is: true x > Enter a fraction to test iostream operators: 5/6 You have entered: 5/6 Part-2 Operators in one statement v. separated statements. 11/6 11/6 11/6 11/6 11/6 11/6 11/6 5/6 5/6 11/6 5/6 11/6 1. Constructors: * you may combine the first 3 Contructors below into one. Default: numerator default as 0, denominator default as 1 One (1) argument Fill Constructor: (int numerator) numerator only, denominator default as 1 Two (2) arguments Fill Constructor: (int numerator, int denominator) String Constructor: (string s); where s in the format of "3/4" 2. Necessary getters and setters 3. Support the following operators: basic math: +, - *./ pre and postfix ++ and -- (4 different types) comparators: , =, !=,== 4. Type conversion operators, to convert Frac numbers to integer or floating point numbers: cout > operator To include the Special ++/-- Test Pattern (inside the testFrace s as part of your final test program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

Evaluate the impact of unions on nurses and physicians.

Answered: 1 week ago

Question

Describe the impact of strikes on patient care.

Answered: 1 week ago

Question

Evaluate long-term care insurance.

Answered: 1 week ago