Question
C++ assignment help: Safe Rational Fractions In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make sure you have a working fractionType
C++ assignment help:
Safe Rational Fractions
In week 4 we completed Chapter 13 Programming Exercise #10 Page 974. Make sure you have a working fractionType class before starting this assignment. The template requirement from week 4 is not required for this assignment.
Your assignment this week is to make your fractionType class safe by using exception handling.
Use exceptions so that your program handles the exceptions division by zero and invalid input.
An example of invalid input would be entering a fraction such as 7 / 0 or 6 / a
An example of division by zero would be: 1 / 2 / 0 / 3
Use a custom Exception class called fractionException. The class must inherit from exception (see example in lecture and note the entire class can be implemented in the .h file as in the lecture).
Test your safe fractionType class with a main method that forces an invalid fraction and a divide by zero. The catch block in the main method must report which kind of error was encountered i.e. invalid fraction or divide by zero.
The following can be used to test an exception:
try {
fractionType
fractionType
fractionType
cout << fixed;
cout << showpoint;
cout << setprecision(2);
num3 = num1 / num2;
cout << num1 << " / " << num2 << " = " << num3 << endl;
}
catch (fractionException e) {
cout << "Exception caight in main: " << e.what() << endl;
}
Submission Guidelines:
Submit modified fractionType class (.h and .cpp file if exists)
Submit fractionException class (.h file and .cpp file if exists)
Submit main test .cpp program to test the exception handling
Now the codes used are the following;
Code used in week 4
Fractiontype.h #ifndef FRACTIONTYPE_H_INCLUDED #define FRACTIONTYPE_H_INCLUDED
#include using namespace std;
//the class for fractions class fractionType { int numerator; int denominator; public: //create a fraction=0/1 fractionType(); //create a fraction=a/b fractionType(int a, int b); //output fractionType(a,b) as a/b friend ostream& operator<<(ostream &cout, const fractionType &f); //input in the format "a/b" to the variable, then the variable will be fractionType(a,b) friend istream& operator>>(istream &cin, fractionType &f); // a/b + c/d = (ad + bc)/bd fractionType operator+(const fractionType &f) const; // a/b c/d = (ad bc)/bd fractionType operator-(const fractionType &f) const; // a/b * c/d = ac/bd fractionType operator*(const fractionType &f) const; // (a/b)/(c/d) = ad/bc, in which c/d !=0 fractionType operator/(const fractionType &f) const; // (a/b<=c/d) = (ad<=bc) bool operator<=(const fractionType &f) const { return (numerator*f.denominator <= denominator * f.numerator); } // (a/b>=c/d) = (ad>=bc) bool operator>=(const fractionType &f) const { return (numerator*f.denominator >= denominator * f.numerator); } // (a/b==c/d) = (ad==bc) bool operator==(const fractionType &f) const { return (numerator*f.denominator == denominator * f.numerator); } // (a/b!=c/d) = (ad!=bc) bool operator!=(const fractionType &f) const { return (numerator*f.denominator != denominator * f.numerator); } bool operator>(const fractionType &f) const { return !(*this <= f); } bool operator<(const fractionType &f) const { return !(*this >= f); } };
#endif // FRACTIONTYPE_H_INCLUDED
Mainprogram.cpp
#include #include #include "fractionType.h"
using namespace std;
int main() { fractionType num1(5, 6); //Line 1 fractionType num2; //Line 2 fractionType num3; //Line 3
cout << fixed; //Line 4 cout << showpoint; //Line 5 cout << setprecision(2); //Line 6
cout << "Line 7: Num1 = " << num1 << endl; //Line 7 cout << "Line 8: Num2 = " << num2 << endl; //Line 8
cout << "Line 9: Enter the fraction " << "in the form a / b: "; //Line 9 cin >> num2; //Line 10 cout << endl; //Line 11
cout << "Line 12: New value of num2 = " << num2 << endl; //Line 12
num3 = num1 + num2; //Line 13
cout << "Line 14: Num3 = " << num3 << endl; //Line 14 cout << "Line 15: " << num1 << " + " << num2 << " = " << num1 + num2 << endl; //Line 15 cout << "Line 16: " << num1 << " * " << num2 << " = " << num1 * num2 << endl; //Line 16
num3 = num1 - num2; //Line 17
cout << "Line 18: Num3 = " << num3 << endl; //Line 18 cout << "Line 19: " << num1 << " - " << num2 << " = " << num1 - num2 << endl; //Line 19 cout << "Line 20: (" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl; //Line 20 cout << "Line 21: (" << num1 << ") <= (" << num2 << ") = " << (num1 <= num2) << endl; //Line 21 cout << "Line 22: (" << num1 << ") == (" << num2 << ") = " << (num1 == num2) << endl; //Line 22 cout << "Line 23: (" << num1 << ") >= (" << num2 << ") = " << (num1 >= num2) << endl; //Line 23 cout << "Line 24: (" << num1 << ") != (" << num2 << ") = " << (num1 != num2) << endl; //Line 24 cout << "Line 25: (" << num1 << ") > (" << num2 << ") = " << (num1 > num2) << endl; //Line 25 cout << "Line 26: (" << num1 << ") < (" << num2 << ") = " << (num1 < num2) << endl; //Line 26 system("Pause"); }
Fractiontype.cpp
#include "fractionType.h"
//create a fraction=0/1 fractionType::fractionType() { numerator = 0; denominator = 1; }
//create a fraction=a/b fractionType::fractionType(int a, int b) { if (b != 0) { numerator = a; denominator = b; } else { cout << "The denominator can't be 0!" << endl; } }
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