- Change the content of the file as follows:
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
//---------------------------------------------------------------------------
class TCalculator
{
public:
TCalculator();
TCalculator(char* Oper1, char Opr, char* Oper2);
void RequestOperands();
void setOperand1(const char* Oper1);
double getOperand1() const;
void setOperand2(const char *Oper2);
double getOperand2() const;
void setOperator(const char Opr);
char getOperator() const;
void setOperation(const char* x, const char p, const char* y);
double CalcResult() const;
void DisplayResult() const;
private:
double Operand1;
double Operand2;
char Operator;
};
//---------------------------------------------------------------------------
TCalculator::TCalculator()
{
}
//---------------------------------------------------------------------------
TCalculator::TCalculator(char* Oper1, char Opr, char* Oper2)
{
setOperand1(Oper1);
setOperator(Opr);
setOperand2(Oper2);
}
//---------------------------------------------------------------------------
void TCalculator::RequestOperands()
{
char Number1[40], Number2[40];
char Oper;
try {
cout << "To proceed, enter\n";
cout << "First Number: "; cin >> Number1;
cout << "An Operator: "; cin >> Oper;
cout << "Second Number: "; cin >> Number2;
setOperand1(Number1);
setOperator(Oper);
setOperand2(Number2);
CalcResult();
}
catch(const char n)
{
cout << "\nOperation Error: " << n << " is not a valid operator";
}
catch(const char *BadOperand)
{
cout << "\nError: " << BadOperand << " is not a valid number";
}
catch(const int n)
{
cout << "\nBad Operation: Division by " << n << " not allowed";
}
}
//---------------------------------------------------------------------------
void TCalculator::setOperand1(const char* Oper1)
{
for(unsigned int i = 0; i < strlen(Oper1); i++)
if( (!isdigit(Oper1[i])) && (Oper1[i] != '.') )
throw Oper1;
Operand1 = atof(Oper1);
}
//---------------------------------------------------------------------------
double TCalculator::getOperand1() const
{
return Operand1;
}
//---------------------------------------------------------------------------
void TCalculator::setOperand2(const char* Oper2)
{
for(unsigned int i = 0; i < strlen(Oper2); i++)
if( (!isdigit(Oper2[i])) && (Oper2[i] != '.') )
throw Oper2;
Operand2 = atof(Oper2);
}
//---------------------------------------------------------------------------
double TCalculator::getOperand2() const
{
return Operand2;
}
//---------------------------------------------------------------------------
void TCalculator::setOperator(const char Symbol)
{
if(Symbol != '+' && Symbol != '-' &&
Symbol != '*' && Symbol != '/')
throw Symbol;
Operator = Symbol;
}
//---------------------------------------------------------------------------
char TCalculator::getOperator() const
{
return Operator;
}
//---------------------------------------------------------------------------
void TCalculator::setOperation(const char* Oper1,
const char Opr, const char* Oper2)
{
setOperand1(Oper1);
setOperator(Opr);
setOperand2(Oper2);
}
//---------------------------------------------------------------------------
double TCalculator::CalcResult() const
{
double R;
// Perform an operation based on the user's choice
switch(Operator)
{
case '+':
R = Operand1 + Operand2;
break;
case '-':
R = Operand1 - Operand2;
break;
case '*':
R = Operand1 * Operand2;
break;
case '/':
try {
if( Operand2 == 0 )
throw "Division by zero not allowed";
R = Operand1 / Operand2;
}
catch(const char *Str)
{
cout << "\nBad Operator: " << Str;
}
break;
}
return R;
}
//---------------------------------------------------------------------------
void TCalculator::DisplayResult() const
{
double Result;
CalcResult();
Result = CalcResult();
// Display the result of the operation
cout << "\n" << Operand1 << " " << Operator << " "
<< Operand2 << " = " << Result;
}
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
TCalculator Calc;
double Number1, Number2, Result;
char Oper;
cout << "This program allows you to perform an operation on two numbers\n";
Calc.RequestOperands();
Calc.DisplayResult();
cout << "\nPress any key to continue...";
return 0;
}
//---------------------------------------------------------------------------
|
- Test the program and return to your programming environment.
- Just as done with the functions, you can show that a function throws
one or more exceptions in its declaration. To do this, in the class,
on the right side of the function that throws an exception, type the
throw keyword followed by parentheses in which you would provide the
type of exception that the following would throw.
To apply this, change the function as follows:
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
//---------------------------------------------------------------------------
class TCalculator
{
public:
TCalculator();
TCalculator(char* Oper1, char Opr, char* Oper2);
void RequestOperands();
void setOperand1(const char* Oper1) throw(const char*);
double getOperand1() const;
void setOperand2(const char *Oper2) throw(const char*);
double getOperand2() const;
void setOperator(const char Opr) throw(const char);
char getOperator() const;
void setOperation(const char* x, const char p, const char* y);
double CalcResult() const;
void DisplayResult() const;
private:
double Operand1;
double Operand2;
char Operator;
};
//---------------------------------------------------------------------------
TCalculator::TCalculator()
{
}
//---------------------------------------------------------------------------
TCalculator::TCalculator(char* Oper1, char Opr, char* Oper2)
{
setOperand1(Oper1);
setOperator(Opr);
setOperand2(Oper2);
}
//---------------------------------------------------------------------------
void TCalculator::RequestOperands()
{
char Number1[40], Number2[40];
char Oper;
try {
cout << "To proceed, enter\n";
cout << "First Number: "; cin >> Number1;
cout << "An Operator: "; cin >> Oper;
cout << "Second Number: "; cin >> Number2;
setOperand1(Number1);
setOperator(Oper);
setOperand2(Number2);
CalcResult();
}
catch(const char n)
{
cout << "\nOperation Error: " << n << " is not a valid operator";
}
catch(const char *BadOperand)
{
cout << "\nError: " << BadOperand << " is not a valid number";
}
catch(const int n)
{
cout << "\nBad Operation: Division by " << n << " not allowed";
}
}
//---------------------------------------------------------------------------
void TCalculator::setOperand1(const char* Oper1) throw(const char*)
{
for(unsigned int i = 0; i < strlen(Oper1); i++)
if( (!isdigit(Oper1[i])) && (Oper1[i] != '.') )
throw Oper1;
Operand1 = atof(Oper1);
}
//---------------------------------------------------------------------------
double TCalculator::getOperand1() const
{
return Operand1;
}
//---------------------------------------------------------------------------
void TCalculator::setOperand2(const char* Oper2) throw(const char*)
{
for(unsigned int i = 0; i < strlen(Oper2); i++)
if( (!isdigit(Oper2[i])) && (Oper2[i] != '.') )
throw Oper2;
Operand2 = atof(Oper2);
}
//---------------------------------------------------------------------------
double TCalculator::getOperand2() const
{
return Operand2;
}
//---------------------------------------------------------------------------
void TCalculator::setOperator(const char Symbol) throw(const char)
{
if(Symbol != '+' && Symbol != '-' &&
Symbol != '*' && Symbol != '/')
throw Symbol;
Operator = Symbol;
}
//---------------------------------------------------------------------------
.
.
.
|
- Test the program and return to your programming environment.
|