Question
need help with this program dont know why it does not want to run ... this is what i have this program is in c++
need help with this program dont know why it does not want to run ... this is what i have this program is in c++
// Header file
#ifndef RATIONAL_H #define RATIONAL_H #include
// Define function operators for augmented operators Rational operator+=(const Rational& secondRational); Rational operator-=(const Rational& secondRational); Rational operator*=(const Rational& secondRational); Rational operator/=(const Rational& secondRational); // Define function operator [] int& operator[](int index);
// Define function operators for prefix ++ and -- Rational& operator++(); Rational& operator--();
// Define function operators for postfix ++ and -- Rational operator++(int dummy); Rational operator--(int dummy);
// Define function operators for unary + and - Rational operator+(); Rational operator-();
// Define the > operators friend ostream& operator>(istream& , Rational&);
private: long r[2]; static long gcd(long n, long d); };
// Define nonmember function operators for relational operators bool operator(const Rational& r1, const Rational& r2); bool operator>=(const Rational& r1, const Rational& r2); bool operator==(const Rational& r1, const Rational& r2); bool operator!=(const Rational& r1, const Rational& r2);
// Define nonmember function operators for arithmetic operators Rational operator+(const Rational& r1, const Rational& r2); Rational operator-(const Rational& r1, const Rational& r2); Rational operator*(const Rational& r1, const Rational& r2); Rational operator/(const Rational& r1, const Rational& r2); #endif
// 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)); }
void Rational :: print_fraction() { if(getDenominator()
Rational Rational:: operator+=(const Rational& secondRational) { *this = add(secondRational); return *this; }
Rational Rational:: operator-=(const Rational& secondRational) { *this = subtract(secondRational); return *this; }
Rational Rational:: operator*=(const Rational& secondRational) { *this = multiply(secondRational); return *this; }
Rational Rational:: operator/=(const Rational& secondRational) { *this = divide(secondRational); return *this; }
// main file
#include
using namespace std;
void Prog14_1() { const int SIZE = 10; Rational sum; for(int i =1; i cout } void Prog14_2() { const int SIZE = 10; Rational sum; for(int i =1; i cout } int getDenominator(int size) { int result = 1; for (int i = 0; i Rational getFraction(string& decimal) { int position = decimal.find("."); if (position == string::npos) return Rational(atoi(decimal.data()), 1); string integerPart = decimal.substr(0, position); string fractionalPart = decimal.substr(position + 1); string nominator = integerPart + fractionalPart; return Rational(atoi(nominator.data()), getDenominator(fractionalPart.size())); } void Prog14_10() { string decimal; cin.ignore(); cout cout } int main() { while (true) { system("cls"); cout > exercise; cout
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