C++ Modify Lab Five such that you use functions for each math calculation (Add,
ID: 3593316 • Letter: C
Question
C++
Modify Lab Five
such that you use functions for each math calculation (Add, Subtract,
Multiply, and Divide).
Pass in parameters to each function for the values to use and the
functions
will
return the result
.
Use a function to read in the numbers involved. These
numbers will be doubles. Also write
a
function
that reads in
the operator and returns a
boolean
–
true if the operator is valid, false if not valid. This function will have two
parame
te
rs. First is a
“C
-
type”
string of characters containing the v
alid operators. The
second is a
reference parameter where the operator will be placed if the operator entered
is valid.
Explanation / Answer
//Modified Lab Five code(please find below) :
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
void getFirstNumber(double);
void getSecondNumber(double);
void getOperator(char &);
void Add(char &);
void Subtract(char &);
void Result(char);
double Number1, Number2;
char OPR;
bool test1, test2;
bool test3;
double Result1;
double Result2;
int main()
{
getFirstNumber(Number1);
do
{
getOperator(OPR);
getSecondNumber(Number2);
Result(OPR);
}
while(OPR);
}
void getFirstNumber(double One)
{
One = Number1;
cout << "First Number: ";
cin >> Number1;
if((Number1 >= 0) || (Number1 <= 0))
{
test1 = true;
}
else
{
cout << "Invalid Number" << endl;
getFirstNumber(Number1);
}
}
void getSecondNumber(double Number2)
{
cout << "please enter the Second or Next number: ";
cin >> Number2;
if ((Number2 >= 0) || (Number2 <= 0))
{
test2 = true;
}
else
{
cout << "Not a Valid Number" << endl;
getSecondNumber(Number2);
}
}
void getOperator(char &OPR)
{
cout << "Enter the Operator: ";
cin >> OPR;
if((OPR != 'C' && OPR != 'c') || (OPR != 'X' && OPR != 'x'))
{
switch(OPR)
{
case '+':
case '-':
case '*':
case '/':
test3 = true;
break;
case 'C':
case 'c':
cout << "The Program is Cleared " << endl;
test3 = true;
break;
case 'X':
case 'x':
cout << " --------TERMINATED--------- " << endl;
exit(0);
default:
if((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
{
cout << "Must Be An Operator(+-*/)" << endl;
cout << "Enter an Operator: ";
cin >> OPR;
}
}
}
if ((OPR != '+') && (OPR != '-') && (OPR != '*') && (OPR != '/'))
{
getFirstNumber(Number1);
}
else
if((OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
{
getSecondNumber(Number2);
}
}
void Add(char &OPR)
{
if((OPR != '-') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
{
Result1 = Number1 + Number2;
}
}
void Subtract(char &OPR)
{
if((OPR != '+') && (OPR != '*') && (OPR != '/') && (OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
{
Result2 = Number1 + Number2;
}
}
void Result(char OPR)
{
if ((OPR != 'C' && OPR != 'c') && (OPR != 'X' && OPR != 'x'))
{
switch (OPR)
{
case '+':
Add(OPR);
cout << "The Result is " << Result1 << endl;
break;
case '-':
Subtract(OPR);
cout << "The Result is " << Result2 << endl;
break;
case 'C':
case 'c':
cout << "The Program Cleared " << endl;
test3 = true;
break;
case 'X':
case 'x':
cout << "You're Terminated :P/n" << endl;
exit (0);
default:
cout << "**Must Be A Number**" << endl;
}
if(OPR = '+')
{
Number1 = Result1;
}
else
{
Number1 = Result2;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.