I NEED HELP WITH C++ PROGRAMMING USING THE INSTRUCTIONS AND CODE BELOW. IF I WIL
ID: 3564885 • Letter: I
Question
I NEED HELP WITH C++ PROGRAMMING USING THE INSTRUCTIONS AND CODE BELOW. IF I WILL RATE 5 STARS TO THE BEST ANSWER.
In the DoOp() method for class Divider, use the try-catch mechanism to prevent division by zero.
In C++, create an Exception class, and a DivideByZero sub-class that takes a string for its constructor, and use this in the try-catch mechanism.
Catch these exceptions in the Test() function/method to output the error message.
class BinaryOperator {
public:
BinaryOperator ( double op1, double op2 ) : fOp1 ( op1 ), fOp2 ( op2 ) {}
virtual double DoOp () const = 0;
protected:
const double fOp1;
const double fOp2;
}; // class BinaryOperator
class Adder : public BinaryOperator {
public:
Adder ( double op1, double op2 ) : BinaryOperator ( op1, op2 ) {}
virtual double DoOp () const;
}; // class Adder
class Subtractor : public BinaryOperator {
public:
Subtractor ( double op1, double op2 ) : BinaryOperator ( op1, op2 ) {}
virtual double DoOp () const;
}; // class Subtractor
class Multiplier : public BinaryOperator {
public:
Multiplier ( double op1, double op2 ) : BinaryOperator ( op1, op2 ) {}
virtual double DoOp () const;
}; // class Multiplier
class Divider : public BinaryOperator {
public:
Divider ( double op1, double op2 ) : BinaryOperator ( op1, op2 ) {}
virtual double DoOp () const;
}; // class v
void Test ();
Explanation / Answer
Here is the complete working program. I've posted only the program. The link to the main function and header files is: https://drive.google.com/folderview?id=0B0YuehXg4OhOQjJPMXYyZnpGM0U&usp=sharing
#include <iostream>
#include <stdexcept>
#include <vector>
#include <fstream>
#include <string>
#include "FileBinaryOperators.h"
double const DefaultOverflow = 1.0E10;
double const DefaultUnderflow = -1.0E10;
using std::cout;
using std::endl;
using std::vector;
double Adder::DoOp () const
{
double a = fop1 + fop2;
if(a<DefaultOverflow && a>DefaultUnderflow )
{
return fop1+fop2;
}
else
{
throw std::overflow_error ("OverflowError01: Check Adder.");
}
}
double Subtractor::DoOp() const
{
double a = fop1 - fop2;
if(a<DefaultOverflow && a>DefaultUnderflow)
{
return fop1-fop2;
}
else
{
throw std::overflow_error ("OverflowError02: Check Subtractor.");
}
}
double Multiplier::DoOp() const
{
double a = fop1*fop2;
if (a<DefaultOverflow && a> DefaultUnderflow)
{
return fop1*fop2;
}
else
{
throw std::overflow_error("OverflowError03: Check Multiplier.");
}
}
double Divider::DoOp() const
{
double a;
if(fop2 != 0 && a<DefaultOverflow && a> DefaultUnderflow )
return fop1/fop2;
else if (fop2 == 0)
throw std::runtime_error("Divided by 0 is undefined!");
else
throw std::overflow_error("OverflowError04: Check Division.");
}
void test()
{
vector<BinaryOperator*> binOp;
cout<<"Enter input file name:"<<endl;
std::string filename;
std::cin>>filename;
std::ifstream in_File(filename.c_str());
if(!in_File)
std::cerr<<"Can't open file: "<<filename<<", file doesn't exist or removed!"<<endl;
else
{
while(!in_File.eof())
{
try{
char opr;
double x,y;
in_File>>opr>>x>>y;
if(in_File.bad())
throw std::runtime_error("I/O stream currupted!");
switch(opr)
{
case '+':
binOp.push_back(new Adder(x,y));
break;
case '-':
binOp.push_back(new Subtractor(x,y));
break;
case '*':
binOp.push_back(new Multiplier(x,y));
break;
case '/':
binOp.push_back(new Divider(x,y));
break;
default:
throw std::runtime_error("Not invalid input.");
}
}
catch(std::runtime_error err)
{
std::cerr<<err.what()<<endl;
}
}
for(vector<BinaryOperator*>::iterator iter = binOp.begin(); iter != binOp.end(); ++iter)
{
try
{
cout<<(*iter) -> DoOp()<<endl;
}
catch(std::runtime_error err)
{
std::cerr<<err.what()<<endl;
}
catch (std::overflow_error excp)
{
std::cerr<<excp.what()<<endl;
}
delete *iter;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.