Question
I need help with this problemI am trying to add this program to my file of programs ... Program is in c++ // I have
I need help with this problemI am trying to add this program to my file of programs ... Program is in c++
// I have this for my header file
#ifndef RATIONAL_H #define RATIONAL_H #include
using namespace std; 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
// And have this for my cpp file
#include
using namespace std; 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)); }
// Have this for my main file
#include
void Prog14_1() { double sum = 0; //Use for loop block to create objects and invoke functions for 100 times. for(int i = 1; i
}
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() { StackOfIntegers s; s.push(1); s.push(2);
s[0] = 999;
cout
}
void Prog14_7() {
}
int main() { while (true) { system("cls"); cout > exercise; cout 4.1 (Use the Rational class) Write a program that computes the following summa- tion series using the Rational class: 98 99 99 100
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started