Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need to check if my c++ program follows the requirements for part 1. Thanks Code: Frac.h #pragma once #ifndef FRAC_H #define FRAC_H #include

Hello, I need to check if my c++ program follows the requirements for part 1. Thanks

image text in transcribed

Code:

Frac.h

#pragma once #ifndef FRAC_H #define FRAC_H #include #include

using namespace std;

class Frac;

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

class Frac { private: int num; int den; long gcd(long a, long b); 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; den = 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 n) { num = n; } void setDen(int d) { den = d; }

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

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::lowTerms(int& num, int& den) { int low; int high;

if (num > den) { low = den; high = num; } else { high = den; low = num; }

for (int i = low; i > 0; i--) { if ((low % i == 0) && (high % i == 0)) { num = num / i; den = den / i; } } }

Frac::Frac(string s) { stringstream ss(s); ss >> num; char peekchar = ss.peek(); if (ss && peekchar == '/') { ss.get(); 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; }

Frac Frac::operator++(int) { Frac temp(num, den); num += den; lowTerms(temp.num, temp.den); return temp; }

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

Frac Frac::operator--(int) { Frac temp(num, den); num -= den; lowTerms(temp.num, temp.den); return temp; }

bool Frac::operator == (const Frac& f2) { bool status; if (num == f2.num && den == f2.den) { status = true; } else { status = false; } return status; }

bool Frac::operator != (const Frac& f2) { bool status; if (num != f2.num || den != f2.den) { status = true; } else { status = false; } return status; }

bool Frac::operator

bool Frac::operator > (const Frac& f2) { bool status; if (num > f2.num&& den > f2.den) { status = true; } else { status = false; } return status; }

bool Frac::operator

bool Frac::operator >= (const Frac& f2) { bool status; if (num > f2.num&& den > f2.den) { status = true; } else if (num == f2.num && den == f2.den) { status = true; } else { status = false; } return status; }

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 f2(7, 2), f3(7, 2); //for testing type conversions int num; int den;

cout

cout

Frac z(x); cout

Frac v = x + y; cout

Frac a = x - y; cout

Frac b = x * y; cout

Frac c = x / y; cout

cout , = y."

if (x != y) { cout

if (x > y) { cout

if (x

if (x >= y) { cout

if (x

cout

cout

//string constructor test Frac s("6/7"); cout

cout

cout

return 0; }

Part-1 To complete the definition of all Five (5) Required Frac class method groups: 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

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

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions