class Rational * represents a Rational number. Remember rational means ratio-nal * which means there is a numerator and denominator having * integer values. Using good ADT techniques, we have made member * variable private (also known as instance variables) and made member * functions public.
I am having issues when I run my c++ code it passes all of the test cases besides this one can, someone please see what I am doing wrong. Thank you :)
Code:
// Add appropriate headers
/* KEEP THIS COMMENT
* class Rational
* represents a Rational number. Remember rational means ratio-nal
* which means there is a numerator and denominator having
* integer values. Using good ADT techniques, we have made member
* variable private (also known as instance variables) and made member
* functions public.
*/
#include<iostream>
using namespace std;
class Rational{
private:
int n, d;
public:
// constructor - default
Rational(){
n = 0;
d = 1;
}
//constructor with 1 parameter
Rational(int n){
this->n = n;
this->d = 1;
}
//constructor with 2 parameters
Rational(int n,int d){
this->n = n;
this->d = d;
}
//getters
int getNumerator(){
return n;
}
int getDenominator(){
return d;
}
//mutators
void setNumerator(int n){
this->n = n;
}
void setDenominator(int d){
this->d = d;
}
// input function, body is definded outside class
void input();
// to display "this" Rational number
void output(){
cout<<n<<"/"<<d<<endl;
}
// set Nr and Dr of "this" Rational number with sum of r1 and r2
void sum(Rational r1, Rational r2);
// check if "this" Rational number is equal to Rational number "r"
bool isEqual(Rational r){
return this->n==r.getNumerator() && this->d==r.getDenominator();
}
};
// outside class definition of member function - sum
void Rational :: sum(Rational r1, Rational r2){
int a = r1.getNumerator();
int b = r1.getDenominator();
int c = r2.getNumerator();
int d = r2.getDenominator();
// set numerator and denominator of "this" Rational number
setNumerator(a*d + b*c);
setDenominator(b*d);
}
// outside class definition of member function - input()
void Rational :: input(){
string str="";
cin>>str;
int n,d;
bool checkInput = false;
while(checkInput == false){
n=d=0;
int i;
for(i=0;i<str.size();++i){
if(str[i]=='/') break;
n = n*10 + (str[i]-'0');
}
// i is currently pointing to '/' or end of string if '/' didn't
// exist in input string so increment it for reading Denominator
++i;
for(;i<str.size();++i){
d = d*10 + (str[i]-'0');
}
if(d!=0) {
// correct input
checkInput=true;
}
else{
// either input string didn't contain '/'
// or Denominator, d = 0
cout<<"wrong input."<< endl;
cout << "\nEnter a rational (p/q):";
cin>>str;
}
}
// set numerator and denominator of "this" Rational number
setNumerator(n);
setDenominator(d);
}
int main()
{
// ToDo: declare three rational objects using the default constructor
Rational op1, op2, C;
char answer;
// Main loop to read in rationals and compute the sum
do {
cout << "\nEnter op1 (in the format of p/q):" ;
// ToDo: use your input member function to read the first rational
op1.input();
cout << "\nEnter op2 (in the format of p/q):";
// ToDo: use your input member function to read the second rational
op2.input();
// ToDo: use the third rational to call Sum with first and second as parameters
C.sum(op1, op2);
cout << "\nThe sum of op1 and op2 is:";
// ToDo: ouptput the third rational
C.output();
//cout << endl;
cout << "\nTry again (Y/N)?";
cin >> answer;
} while (answer == 'y' || answer == 'Y');
// ToDo: test getters on rational that has the sum above.
cout << "\nC's numerator is: " << C.getNumerator();
cout << "\nC's denominator is: " << C.getDenominator();
cout<<endl;
// TODO: Use two constructors to declare a whole number 3/1 and a 4/5
Rational D(3);
Rational E(4,5);
// TODO: Use output to print both rationals
//D.output();
//E.output();
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps