Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my code: #include <iostream> using namespace std; void main() { double D

ID: 3865214 • Letter: H

Question

Here is my code:

#include <iostream>

using namespace std;

void main()

{
   double D1(0);
   double D2(0);
   double D3(0);
   char   Operator;
   const bool T(true), F(false);

   do
   {
       cout << " Enter the first number:";
       cin >> D1;
       cout << " Please enter an operator (+ - * / or type X to exit or C to restart): " << endl;
       cin >> Operator;
   } while (Operator == 'C');

   while (!F)
   {
       switch (Operator)
       {
       case '+':
           cout << "Enter second number : ";
           cin >> D2;
           D3 = D1 + D2;
           cout << D1 << " + " << D2 << " = " << D3 << endl;
           T;
           break;

       case '-':
           cout << "Enter second number : ";
           cin >> D2;
           D3 = D1 - D2;
           cout << D1 << " - " << D1 << " = " << D3 << endl;
           T;
           break;

       case '*':
           cout << "Enter second number : ";
           cin >> D2;
           D3 = D1*D2;
           cout << D1 << " * " << D2 << " = " << D3 << endl;

           T;
           break;

       case'/':
           cout << "Enter second number : ";
           cin >> D2;
           if (D2 == 0)
           {
               cout << "Undefined answer(s). Enter new divisor.." << endl;
               cin >> D2;
           }
           else D3 = D1 / D2;
           cout << D1 << " / " << D2 << " = " << D3 << endl;
           T;
           break;

       case 'C':
       case 'c':
           D1 = 0;
           cout << " Enter first number ";
           cin >> D1;
           T;
           break;
       case 'X':
       case 'x':
           F;
           exit(0);
           break;
       default:
           F;
           cout << "Invalid response " << Operator << " - Please enter a valid operation - Enter X to quit or enter C to clear" << endl;
       }
   }
   D1 = D3;
   cout << "Enter new operator" << endl;
}

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 string of character s containing the valid operators. The parameters. First is a "C-typ second is a reference parameter where the operator will be placed if the operator entered is valid.

Explanation / Answer

#include <iostream>
using namespace std;
double D1(0);
double D2(0);
double D3(0);
char oper;
char& op = oper;
bool valid_operator_check;
std::string valid_operators("+-*/");
bool checkValidOperator(string valid_operators,char* input_oper){
   for(int i=0; i<valid_operators.size(); i++){
       if(*input_oper == valid_operators[i]){
           valid_operator_check = true;
           oper = valid_operators[i];
           break;  
       }
   }
   return valid_operator_check;
}

double add_numbers(double num1, double num2){
   double result = num1 + num2;
   return result;
}
double subtract_numbers(double num1, double num2){
   double result = num1 - num2;
   return result;
}
double multiply_numbers(double num1, double num2){
   double result = num1 * num2;
   return result;
}
double divide_numbers(double num1, double num2){
   double result;
   if(num2 == 0){
       cout<<"Enter the second number again"<<endl;
       cin>>num2;
       result = num1 / num2;
   }
   result = num1 / num2;
   return result;
}
void read_numbers(){
   cout<<" Enter the first number";
   cin>>D1;
   cout<<" Enter the second number";
   cin>>D2;
}
int main()
{
   bool oper_test;
read_numbers();
cout<<"Enter the operator ";
cin>>oper;
oper_test = checkValidOperator(valid_operators,&op);
char& new_oper = op;
if(oper_test == true){
   switch(new_oper){
       case '+' :
           cout<<"Result of addition = "<<add_numbers(D1,D2)<<endl;
           break;
       case '-' :
           cout<<"Result of subtraction = "<<subtract_numbers(D1,D2)<<endl;
           break;
       case '*' :
           cout<<"Result of multiplication = "<<multiply_numbers(D1,D2)<<endl;
           break;
       case '/' :
           cout<<"Result of division = "<<divide_numbers(D1,D2)<<endl;
           break;
       case 'X':
   case 'x':
exit(1);
   break;
       default:
   cout << "Invalid response " << oper_test << " - Please enter a valid operation - Enter X to quit or enter C to clear" << endl;
       }
   }
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote