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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;
}

Debugging Information for Non-standard Test Case O
DEBUG INFO
Your instructor has chosen to give you the following information to help you debug your code and improve your
submission.
COMPILER STACK TRACE
None
PROGRAM EXECUTION STACK TRACE
None
INPUT OF THE TEST CASE
1 -1/3
2 4/5
3 -2/9
4 0/4
5 -8/0
6 n
YOUR CODE'S OUTPUT
Enter op1 (in the format of p/q):
3 Enter op2 (in the format of p/q):
4 The sum of op1 and op2 is:-133/15
6 Try again (Y/N)?
7 C's numerator is: -133
8 C's denominator is: 15
THE CORRECT OUTPUT OF THE TEST CASE
2 Enter op1 (in the format of p/q):
3 Enter op2 (in the format of p/q):
The sum' of op1 and op2 is:7/15
6 Try again (Y/N)?
7 C's numerator is: 7
9 C's denominator is: 15
10
*UNIX DIFF` OF CORRECT OUTPUT AND YOUR OUTPUT
4c4
< The sum of op1 and op2 is:-133/15
> The sum of op1 and op2 is:7/15
7c7,8
< C's numerator is: -133
> C's numerator is: 7
PRETTY DIFF
This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output
that you are missing, red indicates things in your output that shouldn't be there.
The character refers to newlines, so the green - character refers a newline you are missing in your output and the
red - refers to a newline you need to remove from your output.
Enter op1 (in the format of p/q):-
Enter op2 (in the format of p/q):d
The sum' of op1 and op2 is:-1337/15-
t.
6 Try again (Y/N)?-
7 C's numerator is: -1337-
9 C's denominator is: 15-
10
Transcribed Image Text:Debugging Information for Non-standard Test Case O DEBUG INFO Your instructor has chosen to give you the following information to help you debug your code and improve your submission. COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None INPUT OF THE TEST CASE 1 -1/3 2 4/5 3 -2/9 4 0/4 5 -8/0 6 n YOUR CODE'S OUTPUT Enter op1 (in the format of p/q): 3 Enter op2 (in the format of p/q): 4 The sum of op1 and op2 is:-133/15 6 Try again (Y/N)? 7 C's numerator is: -133 8 C's denominator is: 15 THE CORRECT OUTPUT OF THE TEST CASE 2 Enter op1 (in the format of p/q): 3 Enter op2 (in the format of p/q): The sum' of op1 and op2 is:7/15 6 Try again (Y/N)? 7 C's numerator is: 7 9 C's denominator is: 15 10 *UNIX DIFF` OF CORRECT OUTPUT AND YOUR OUTPUT 4c4 < The sum of op1 and op2 is:-133/15 > The sum of op1 and op2 is:7/15 7c7,8 < C's numerator is: -133 > C's numerator is: 7 PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing, red indicates things in your output that shouldn't be there. The character refers to newlines, so the green - character refers a newline you are missing in your output and the red - refers to a newline you need to remove from your output. Enter op1 (in the format of p/q):- Enter op2 (in the format of p/q):d The sum' of op1 and op2 is:-1337/15- t. 6 Try again (Y/N)?- 7 C's numerator is: -1337- 9 C's denominator is: 15- 10
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education