Question: need help on 14.1 ... for 14.2 I have evrything but it says error on string in the rational cpp file... i have this ...


need help on 14.1 ... for 14.2 I have evrything but it says error on string in the rational cpp file... i have this ... progam is in c++
// main cpp
#include
void Prog14_1() {
}
void Prog14_2() { // Create and initialize two rational numbers r1 and r2. Rational r1(4, 2); Rational r2(2, 3);
// Test toString, add, substract, multiply, and divide cout
// Test intValue and double cout
// Test compareTo and equal cout
}
void Prog14_3() {
}
void Prog14_4() {
}
void Prog14_7() {
}
int main() { while (true) { system("cls"); cout > exercise; cout
// header file
#ifndef RATIONAL_H #define RATIONAL_H
class Rational { public: Rational(); Rational(long numerator, long denominator); long getNumerator() const; long getDenominator() const; Rational add(const Rational& secondRational) const; Rational subtract(const Rational& secondRational) const; Rational multiply(const Rational& secondRational) const; Rational divide(const Rational& secondRational) const; int compareTo(const Rational& secondRational) const; bool equals(const Rational& secondRational) const; int intValue() const; double doubleValue() const; string toString() const;
private: long r[2]; static long gcd(long n, long d); }; #endif
//rational cpp
#include
Rational::Rational() { r[0] = 0; r[1] = 1; }
Rational::Rational(long numerator, long denominator) { long factor = gcd(numerator, denominator); r[0] = ((denominator > 0) ? 1 : -1) * numerator / factor; r[1] = abs(denominator) / factor; }
long Rational::getNumerator() const { return r[0]; }
long Rational::getDenominator() const { return r[1]; }
/** Find GCD of two numbers */ long Rational::gcd(long n, long d) { long n1 = abs(n); long n2 = abs(d); int gcd = 1;
for (int k = 1; k
return gcd; }
Rational Rational::add(const Rational& secondRational) const { long n = r[0] * secondRational.getDenominator() + r[1] * secondRational.getNumerator(); long d = r[1] * secondRational.getDenominator(); return Rational(n, d); }
Rational Rational::subtract(const Rational & secondRational) const { long n = r[0] * secondRational.getDenominator() - r[1] * secondRational.getNumerator(); long d = r[1] * secondRational.getDenominator(); return Rational(n, d); }
Rational Rational::multiply(const Rational & secondRational) const { long n = r[0] * secondRational.getNumerator(); long d = r[1] * secondRational.getDenominator(); return Rational(n, d); }
Rational Rational::divide(const Rational & secondRational) const { long n = r[0] * secondRational.getDenominator(); long d = r[1] * secondRational.r[0]; return Rational(n, d); }
int Rational::compareTo(const Rational & secondRational) const { Rational temp = this->subtract(secondRational); if (temp.getNumerator()
bool Rational::equals(const Rational & secondRational) const { if (this->compareTo(secondRational) == 0) return true; else return false; }
int Rational::intValue() const { return getNumerator() / getDenominator(); }
double Rational::doubleValue() const { return 1.0 * getNumerator() / getDenominator(); }
string Rational::toString() const { char s1[20], s2[20]; itoa(r[0], s1, 10); // Convert int to string s1 itoa(r[1], s2, 10); // Convert int to string s2
if (r[1] == 1) return string(s1); else return string(strcat(strcat(s1, "/"), s2)); }
Section 4.2 4.1 (Use the Rational class) Write a program that computes the following summa- tion series using the Rational class: 98 99 99 100 *14.2 (Demonstrate the benefits of encapsulation) Rewrite the Rational class in Sec- tion 14.2 using a new internal representation for the numerator and denominator. Declare an array of two integers as follows: int r [2] Use r[0] to represent the numerator and r[1] to represent the denominator. The signatures of the functions in the Rational class are not changed, so a client
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
