Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with this program dont know why it does not want to run ... this is what i have this program is in c++

image text in transcribed

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 #include #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; void print_fraction(); double ret_floating(); Rational(int numerator); // Suitable for type conversion

// 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 #include "Rational.h"

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 #include "Rational.h" #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

operators to At 14.10 (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and display the number in a fraction. (Hint: read the decimal number as a string, extract the integer part and fractional part from the strin and use the Rational class to obtain a rational number for the decimal number, Here are some sample runs: Enter a decimal number: 3.25 -Enter The fraction number is 13/4 Enter a decimal number: 0.45452 Enter The fraction number is 11363/25000

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

Students also viewed these Databases questions

Question

If a random variable X is defined such that find p, and 2.

Answered: 1 week ago

Question

Cotton shirts $30 $20 25 40 70 s 80

Answered: 1 week ago