I posted this before and got a good response from a user and I appreciated it. B
ID: 3542313 • Letter: I
Question
I posted this before and got a good response from a user and I appreciated it. But I think what they provided for me was much more complicated than what I should submit for my homework.
I NEED GOOD NOTES ON THIS SO I CAN REFER TO MY BOOK AND LEARN IT EASIER :)
I need some help with C++ Programming assignment.
(Rational Class) Create a class called Rational for programming arithmetic with fractions. Write a program to test your class.
Use integer variables to represent the private data of the class -- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction
2
4
would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks:
A) adding two rational numbers. The result should be stored in reduced form.
B) Subtracting two rational numbers. The result should be stored in reduced form.
C) Multiplying two rational numbers. The result should be stored in reduced form.
D) Dividing two rational numbers. The result should be stored in reduced form.
E) Printing rational numbers in the form a/b, where a is the numerator and b is the demoninator.
F) Printing Rational numbers in floating point format.
Important: I need this to run in dev c++. I have already looked at the existing answers to this question on Chegg and there aren't any good programs that actually run.
Important: I NEED good notes on this. Please consider this.
Explanation / Answer
#include <iostream>
using namespace std;
class Rational {
private:
int numerator;
int denominator;
public:
//constructor when no initializers are provided
//Default values taken as 1 for both
Rational() {
numerator = 1;
denominator = 1;
}
//constructor when initializers are provided
//first for the numerator and second for the denominator
Rational(int x, int y) {
numerator = x;
denominator = y;
//calculating GCD
while (y != 0) {
int r = x % y;
x = y;
y = r;
}
// Expressing in reduced form
numerator = numerator / x;
denominator = denominator / x;
}
//addition function PART A
Rational add(Rational X, Rational Y) {
int x = X.numerator * Y.denominator + Y.numerator * X.denominator;
int y = X.denominator * Y.denominator;
Rational result(x, y); //this constructor anyways converts it to regular form
return result;
}
//subtraction function PART B
Rational subtract(Rational X, Rational Y) {
int x = X.numerator * Y.denominator - Y.numerator * X.denominator;
int y = X.denominator * Y.denominator;
Rational result(x, y); //this constructor anyways converts it to regular form
return result;
}
//multiply function PART C
Rational multiply(Rational X, Rational Y) {
int x = X.numerator * Y.numerator;
int y = X.denominator * Y.denominator;
Rational result(x, y); //this constructor anyways converts it to regular form
return result;
}
//divide function PART D
Rational divide(Rational X, Rational Y) {
int x = X.numerator * Y.denominator;
int y = X.denominator * Y.numerator;
Rational result(x, y); //this constructor anyways converts it to regular form
return result;
}
void printRational(Rational X)
{
cout <<X.numerator << "/"<< X.denominator << endl;
}
void printFloat(Rational X)
{
cout <<X.numerator*1.0/X.denominator << endl;
}
};