- Change the content of the file as follows:
//---------------------------------------------------------------------------
#include <iostream.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
class TCalculator
{
public:
__fastcall TCalculator();
__fastcall TCalculator(char* Oper1, char Opr, char* Oper2);
void __fastcall RequestOperands();
void __fastcall setOperand1(const char* Oper1);
double __fastcall getOperand1() const;
void __fastcall setOperand2(const char *Oper2);
double __fastcall getOperand2() const;
void __fastcall setOperator(const char Opr);
char __fastcall getOperator() const;
void __fastcall setOperation(const char* x, const char p, const char* y);
double __fastcall CalcResult() const;
void __fastcall DisplayResult() const;
private:
double Operand1;
double Operand2;
char Operator;
};
//---------------------------------------------------------------------------
__fastcall TCalculator::TCalculator()
{
}
//---------------------------------------------------------------------------
__fastcall TCalculator::TCalculator(char* Oper1, char Opr, char* Oper2)
{
setOperand1(Oper1);
setOperator(Opr);
setOperand2(Oper2);
}
//---------------------------------------------------------------------------
void __fastcall 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 __fastcall 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 __fastcall TCalculator::getOperand1() const
{
return Operand1;
}
//---------------------------------------------------------------------------
void __fastcall 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 __fastcall TCalculator::getOperand2() const
{
return Operand2;
}
//---------------------------------------------------------------------------
void __fastcall TCalculator::setOperator(const char Symbol)
{
if(Symbol != '+' && Symbol != '-' &&
Symbol != '*' && Symbol != '/')
throw Symbol;
Operator = Symbol;
}
//---------------------------------------------------------------------------
char __fastcall TCalculator::getOperator() const
{
return Operator;
}
//---------------------------------------------------------------------------
void __fastcall TCalculator::setOperation(const char* Oper1,
const char Opr, const char* Oper2)
{
setOperand1(Oper1);
setOperator(Opr);
setOperand2(Oper2);
}
//---------------------------------------------------------------------------
double __fastcall 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 __fastcall 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...";
getchar();
return 0;
}
//---------------------------------------------------------------------------
|
- Test the program and return to Bcb.
- 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.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
class TCalculator
{
public:
__fastcall TCalculator();
__fastcall TCalculator(char* Oper1, char Opr, char* Oper2);
void __fastcall RequestOperands();
void __fastcall setOperand1(const char* Oper1) throw(const char*);
double __fastcall getOperand1() const;
void __fastcall setOperand2(const char *Oper2) throw(const char*);
double __fastcall getOperand2() const;
void __fastcall setOperator(const char Opr) throw(const char);
char __fastcall getOperator() const;
void __fastcall setOperation(const char* x, const char p, const char* y);
double __fastcall CalcResult() const;
void __fastcall DisplayResult() const;
private:
double Operand1;
double Operand2;
char Operator;
};
//---------------------------------------------------------------------------
__fastcall TCalculator::TCalculator()
{
}
//---------------------------------------------------------------------------
__fastcall TCalculator::TCalculator(char* Oper1, char Opr, char* Oper2)
{
setOperand1(Oper1);
setOperator(Opr);
setOperand2(Oper2);
}
//---------------------------------------------------------------------------
void __fastcall 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 __fastcall 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 __fastcall TCalculator::getOperand1() const
{
return Operand1;
}
//---------------------------------------------------------------------------
void __fastcall 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 __fastcall TCalculator::getOperand2() const
{
return Operand2;
}
//---------------------------------------------------------------------------
void __fastcall TCalculator::setOperator(const char Symbol) throw(const char)
{
if(Symbol != '+' && Symbol != '-' &&
Symbol != '*' && Symbol != '/')
throw Symbol;
Operator = Symbol;
}
//---------------------------------------------------------------------------
.
.
.
|
- Test the program and return to Bcb.
|