Problem3 is below: Problem 2 (6 points) Write a program called StructCalc.cpp. T
ID: 3706947 • Letter: P
Question
Problem3 is below: Problem 2 (6 points) Write a program called StructCalc.cpp. The program should have functions for add, subtract, multiply, and divide. Each of these four functions will accept a structure called Operands as an input argument. You must demonstrate the program works by writing amainthat tests each function as shown in the exccution sample given below. The programm MUST contain the following features: Declaration of a structure called Operands that contains two member variables of type double. Four functions that each have an input parameter of type Operands Each function will return a double that corresponds to the result of adding, subtracting, multiplying, or dividing the two member variables of the input parameter. The first member variable corresponds to the first operand and the second member variable corresponds to the second operand. o You must check that the second operator is not zero in main ) to prevent a divide-by-zero error A sample run of the program using multiple inputs is shown below Please enter two operands: 5 0 Here are the results of add subtract/multiply/ divide: Add Subtract: Multiple: Divide:5.00 Would you like to enter more operands (Y/N)? y Please enter two operands: 10 20 .00 0.00 0.00 0.00- Undefined 5. 00 + 5.00 5.00 5.00 5.00 0.00 Here are the results of add / subtract / multiply divide: Add Subtract:10.00- Multiple 10.00 Divide 10.00/ 20.00 Nould you like to enter more operands (Y/N)? y Please enter two operands: 20 -30 10.0020.0030.00 20.00-10.00 20.00200.00 0.50 Here are the results of add / subtract/ multiply divide: Add Subtract20.00 --30.00 Multiple: 20.00* -30.00= -600.00 Divide : 20.00/ -30.00= -0.67 Hould you like to enter more operands (Y/N)? n :20.00+-30.00-10.00 50.00 Press any key to continue -. -Explanation / Answer
//Calculator.h
#pragma once
class Calculator
{
double op1, op2;
public:
Calculator();
void set(double val1, double val2);
double getOp1();
double getOp2();
double add();
double sub();
double mul();
double div();
};
-----------------------------------------------------------------------
//Calculator.cpp
#include"Calculator.h"
Calculator::Calculator()
{
op1 = 0;
op2 = 0;
}
void Calculator::set(double val1, double val2)
{
op1 = val1;
op2 = val2;
}
double Calculator::add()
{
return op1 + op2;
}
double Calculator::sub()
{
return op1 - op2;
}
double Calculator::mul()
{
return op1 * op2;
}
double Calculator::div()
{
return op1 / op2;
}
double Calculator::getOp1()
{
return op1;
}
double Calculator::getOp2()
{
return op2;
}
---------------------------------------------------------------------------
//CAlcClassTest.cpp
#include<iostream>
#include<iomanip>
#include"Calculator.h"
using namespace std;
int main()
{
double op1, op2;
Calculator cal;
char ch;
do
{
//for fixed decimal represented use head file iomanip functions
cout << fixed << setprecision(2);
cout << "Please enter two operands: ";
cin >> op1 >> op2;
cal.set(op1, op2);
cout << "Here are the results of add/ subtract / multiply / divide: " << endl;
cout << "Add : " << cal.getOp1() << " + " << cal.getOp2() << " = " << right<<cal.add() << endl;
cout << "Subtract : " << cal.getOp1() << " - " << cal.getOp2() << " = " << right<<cal.sub() << endl;
cout << "Multiply : " << cal.getOp1() << " * " << cal.getOp2() << " = " << right<<cal.mul() << endl;
if(op2 == 0)
cout << "Divide : " << cal.getOp1() << " / " << cal.getOp2() << " = " << "Undefined" << endl;
else
cout << "Divide : " << cal.getOp1() << " / " << cal.getOp2() << " = " << right<<cal.div() << endl;
cout << "Would you like to enter more operands(y/n)? ";
cin >> ch;
} while (ch != 'n' );
}
----------------------------------------------------------
//output
Please enter two operands: 5 0
Here are the results of add/ subtract / multiply / divide:
Add : 5.00 + 0.00 = 5.00
Subtract : 5.00 - 0.00 = 5.00
Multiply : 5.00 * 0.00 = 0.00
Divide : 5.00 / 0.00 = Undefined
Would you like to enter more operands(y/n)? y
Please enter two operands: 10 20
Here are the results of add/ subtract / multiply / divide:
Add : 10.00 + 20.00 = 30.00
Subtract : 10.00 - 20.00 = -10.00
Multiply : 10.00 * 20.00 = 200.00
Divide : 10.00 / 20.00 = 0.50
Would you like to enter more operands(y/n)? y
Please enter two operands: 20 -30
Here are the results of add/ subtract / multiply / divide:
Add : 20.00 + -30.00 = -10.00
Subtract : 20.00 - -30.00 = 50.00
Multiply : 20.00 * -30.00 = -600.00
Divide : 20.00 / -30.00 = -0.67
Would you like to enter more operands(y/n)? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.