This program works just need to code it using the Exception handling method inst
ID: 3625642 • Letter: T
Question
This program works just need to code it using the Exception handling method instead of the IF statment.
Please use the Exception handling method (try, catch, throw) to check the input for denominator. If user inputs 0 for denominator, it will display an error message and assign the denominator to 1. Thank you
//header section
#include
using namespace std;
//class rational
class Rational
{
public:
Rational(int numerator, int denominator);
Rational(int numerator); // sets denominator to 1
Rational(); // sets numerator to 0, denominator to 1
friend Rational operator+(const Rational&,const Rational&);
friend Rational operator-(const Rational&,const Rational&);
friend Rational operator*(const Rational&,const Rational&);
friend Rational operator/(const Rational&,const Rational&);
friend ostream& operator <<(ostream&,const Rational&);
friend istream& operator >>(istream&,Rational&);
private: int n;
int d;
};
void normalize(int &n, int &d);
//definitions for functions
Rational::Rational(int numer, int denom)
{
normalize(numer, denom);
n = numer;
d = denom;
}
//sets denominator to 1
Rational::Rational(int numer): n(numer), d(1)
// See the initializer appendix
{
//body deliberately empty
}
// sets numerator to 0, denominator to 1
Rational::Rational():n(0), d(1)
// see initializer appendix
{
//body deliberately empty
}
Rational operator +(const Rational& left,
const Rational& right)
{
int numer = left.n * right.d + left.d * right.n;
int denom = left.d * right.d;
normalize(numer, denom);
Rational local(numer, denom);
return local;
}
Rational operator -(const Rational& left,
const Rational& right)
{
int numer = left.n * right.d - left.d * right.n;
int denom = left.d * right.d;
normalize(numer, denom);
Rational local (numer, denom);
return local;
}
Rational operator *(const Rational& left,
const Rational& right)
{
Rational product;
int numer = left.n * right.n;
int denom = left.d * right.d;
normalize(numer, denom);
product = Rational(numer, denom);
return product;
}
Rational operator/(const Rational& left,
const Rational& right)
{
Rational quotient;
int numer = left.n * right.d;
int denom = left.d * right.n;
if(right.n==0)
{
cout<<"The results of division is Error:
Cannot divide by zero"<
}
else
{
normalize(numer, denom);
quotient = Rational(numer, denom);
}
return quotient;
}
//precondition: all relational operators require d > 0
istream& operator >>(istream& in_str, Rational& right)
{
char ch;
in_str >> right.n >> ch >> right.d;
if (ch != '/')// properly done, we would set iostream
{// to fail here in case of error.
cout << "bad input format for operator >>. Aborting!"
<< endl;
exit (1);
}
if(right.d==0)
{
cout << "Error: You enter 0 for
denominator is illegal and will set to 1."<
right.d=1;
}
normalize(right.n, right.d);
return in_str;
}
ostream& operator <<(ostream& out_str,const Rational& right)
{
char ch;
out_str << right.n << '/' << right.d;
return out_str;
}
//postcondition: return value is gcd of the absolute values
//of m and n depends on function int abs(int); declared in
//cstdlib
int gcd(int m, int n)
{
int t;
m = abs (m); // don't care about signs.
n = abs (n);
if(n < m) // make m >= n so algorithm will work!
{
t = m;
m = n;
n = t;
}
int r;
r = m % n;
while(r != 0)
{
r = m%n;
m = n;
n = r;
}
return m;
}
//postcondition: n and d (to be numerator and denominator
//of a fraction)have all common factors removed, and d > 0.
void normalize(int& n, int& d)
{
// remove common factors:
int g = gcd(n, d);
if(n!=0)
{
n = n/g;
d = d/g;
}
if(n > 0 && d < 0 || n < 0 && d < 0)
{
n = -n;
d = -d;
}
}
//main function
int main()
{
Rational x, y, z;
int count;
cout << "Please enter number of loop: ";
cin >> count;
for (int i = 0 ; i < count; i++)
{
cout << "Please enter the numerator and denominator for the first rational"<
cin >> x;
cout << "You entered the equivalent of: " << x << endl;
cout << "Please enter the numerator and denominator for the second rational"<
cin >>y;
cout << "You entered the equivalent of: " << y << endl;
cout << "The reuslt of addition is :";
cout << x + y << endl;
cout << "The reuslt of subtration is :" ;
cout << x - y << endl;
cout << "The reuslt of multiplication is :";
cout << x * y << endl;
cout << "The reuslt of division is :";
cout <
}
//pause system for a while
system("pause");
return 0;
}//end main
Explanation / Answer
// Excepration.cpp : Defines the entry point for the console application.
//
#include
"stdafx.h"
//header section
#include
<iostream>
using
namespace std;
//class rational
class
Rational
public
:
Rational(
Rational(
Rational();
friend
Rational operator+(const Rational&,const Rational&);
friend
Rational operator-(const Rational&,const Rational&);
friend
Rational operator*(const Rational&,const Rational&);
friend
Rational operator/(const Rational&,const Rational&);
friend
ostream& operator <<(ostream&,const Rational&);
friend
istream& operator >>(istream&,Rational&);
private
: int n;
int
d;
void
normalize(int &n, int &d);
//definitions for functions
Rational::Rational(
{
normalize(numer, denom);
n = numer;
d = denom;
}
//sets denominator to 1
Rational::Rational(
// See the initializer appendix
{
//body deliberately empty
}
// sets numerator to 0, denominator to 1
Rational::Rational():n(0), d(1)
// see initializer appendix
{
//body deliberately empty
}
Rational
{
int
numer = left.n * right.d + left.d * right.n;
int
denom = left.d * right.d;
return
local;
const
Rational& right)
int
numer = left.n * right.d - left.d * right.n;
int
denom = left.d * right.d;
return
local;
const
Rational& right)
int
numer = left.n * right.n;
int
denom = left.d * right.d;
return
product;
const
Rational& right)
int
numer = left.n * right.d;
int
denom = left.d * right.n;
if
(right.n==0)
else
{
normalize(numer, denom);
quotient = Rational(numer, denom);
}
return
quotient;
//precondition: all relational operators require d > 0
istream&
{
char
ch;
if
(ch != '/')
{
cout << "bad input format for operator >>. Aborting!"
<< endl;
exit (1);
}
if
(right.d==0)
return
in_str;
{
char
ch;
return
out_str;
//postcondition: return value is gcd of the absolute values
//of m and n depends on function int abs(int); declared in
//cstdlib
int
gcd(int m, int n)
int
t;
m = abs (m);
n = abs (n);
if
(n < m)
{
t = m;
m = n;
n = t;
}
int
r;
while
(r != 0)
return
m;
//postcondition: n and d (to be numerator and denominator
//of a fraction)have all common factors removed, and d > 0.
void
normalize(int& n, int& d)
// remove common factors:
int
g = gcd(n, d);
if
(n!=0)
if
(n > 0 && d < 0 || n < 0 && d < 0)
//main function
int
main()
int
count;
for
(int i = 0 ; i < count; i++)
try
{
cout << "Please enter the numerator and denominator for the first rational"<<endl;
cin >> x;
cout << "You entered the equivalent of: " << x << endl;
cout << "Please enter the numerator and denominator for the second rational"<<endl;
cin >>y;
cout << "You entered the equivalent of: " << y << endl;
cout << "The reuslt of addition is :";
cout << x + y << endl;
cout << "The reuslt of subtration is :" ;
cout << x - y << endl;
cout << "The reuslt of multiplication is :";
cout << x * y << endl;
cout << "The reuslt of division is :";
cout << x/y<<endl;
}
{
cout<<"Error:Denominator cannot be zero"<<endl;
}
}
//pause system for a while
system("pause");
return
0;
}
{
cout << "Please enter number of loop: ";
cin >> count;
{
Rational x, y, z;
{
n = -n;
d = -d;
}
}
{
n = n/g;
d = d/g;
}
{
}
{
r = m%n;
m = n;
n = r;
}
r = m % n;
// make m >= n so algorithm will work!
{
}
out_str << right.n << '/' << right.d;
}
ostream&
{
cout << "Error: You enter 0 for denominator is illegal and will set to 1.";
cin>>right.d;
}
normalize(right.n, right.d);
// properly done, we would set iostream
in_str >> right.n >> ch >> right.d;
}
{
cout<<"The results of division is Error:Cannot divide by zero"<<endl;
}
{
Rational quotient;
}
Rational
normalize(numer, denom);
product = Rational(numer, denom);
{
Rational product;
}
Rational
normalize(numer, denom);
Rational local (numer, denom);
{
}
Rational
normalize(numer, denom);
Rational local(numer, denom);
};
{
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.