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

write by C++ Write an application that mimics a mini-calculator. The program rea

ID: 3816073 • Letter: W

Question

write by C++

Write an application that mimics a mini-calculator. The program reads several lines of data from the input file “calc.txt”. Each line has two integer values and one of the following mathematical operators: +, -, *, /, %. The program should then output a meaningful result as shown below. Save your output in “results.txt”. Note that for division, if the denominator is zero, output the appropriate message. If the user enters another operator, show the message “Invalid mathematical operator”. Note: Create the input file. Your program should read all input-lines until end-of-file.

10 3 30 10 3 10 3 1 10 3 13 10 0 infinity as denominator is zero. 5 Invalid mathematical operator 10 10 100 Process returned 0 (0x0) execution time 0.331 s Press any key to continue

Explanation / Answer

include<iostream>
#include<fstream>
//to calculate execution time
#include<ctime>
using namespace std;

int main()
{
   ifstream in;
   ofstream out;
   //open input file
   in.open("calc.txt");
   //check if input file is open or not
   if (!in)
   {
       cout << "Not able to open input file calc.txt" << endl;
       return -1;
   }
   //open result.txt for writing
   out.open("result.txt");
   //check if output file is open or not for writing
   if (!out)
   {
       cout << "output file cannot be opened for writing" << endl;
   }
   //declare variable to hold two variables and char variable to hold operator
   int var1, var2;
   char op;
   //start timer
   int start_s = clock();
   while (!in.eof())
   {
       in >> var1 >> op >> var2;
       //output to screen and tex file result.txt
       switch (op)
       {
       case '+':
           cout << var1 << " " << op << " " << var2 << " = "<<var1+var2<<endl;
           //write to file
           out << var1 << " " << op << " " << var2 << " = " << var1 + var2 << endl;
           break;
       case '-':
           cout << var1 << " " << op << " " << var2 << " = " << var1 - var2 << endl;
           //write to file
           out << var1 << " " << op << " " << var2 << " = " << var1 - var2 << endl;
           break;
       case '*':
           cout << var1 << " " << op << " " << var2 << " = " << var1 * var2 << endl;
           //write to file
           out << var1 << " " << op << " " << var2 << " = " << var1 * var2 << endl;
           break;
       case '/':
           if (var2 == 0)
           {
               cout << var1 << " " << op << " " << var2 << " = " << "Infinity as denominator is zero" << endl;
               //write to file
               out << var1 << " " << op << " " << var2 << " = " << "Infinity as denominator is zero" << endl;

           }
           else
           {
               cout << var1 << " " << op << " " << var2 << " = " << var1 / var2 << endl;
               //write to file
               out << var1 << " " << op << " " << var2 << " = " << var1 / var2 << endl;
           }
           break;
       case '%':
           cout << var1 << " " << op << " " << var2 << " = " << var1 % var2 << endl;
           //write to file
           out << var1 << " " << op << " " << var2 << " = " << var1 % var2 << endl;
           break;
       default:
           cout << var1 << " " << op << " " << var2 << " = " << " .......Invalid mathematical operator." << endl;
           //write to file
           out << var1 << " " << op << " " << var2 << " = " << " .......Invalid mathematical operator." << endl;
           break;
       }
   }
   int stop_s = clock();
   cout << "Process returned 0(0x0) execution time : " << (stop_s - start_s) / double(CLOCKS_PER_SEC) * 1000 << " s" << endl;
   out << "Process returned 0(0x0) execution time : " << (stop_s - start_s) / double(CLOCKS_PER_SEC) * 1000 << " s" << endl;
}

---------------------------------------------------

//ouput

10 * 3 = 30
10 / 3 = 3
10 % 3 = 1
10 + 3 = 13
10 / 0 = Infinity as denominator is zero
10 - 3 = 7
10 ! 5 = .......Invalid mathematical operator.
10 * 10 = 100
Process returned 0(0x0) execution time : 24 s

result.txt

10 * 3 = 30
10 / 3 = 3
10 % 3 = 1
10 + 3 = 13
10 / 0 = Infinity as denominator is zero
10 - 3 = 7
10 ! 5 = .......Invalid mathematical operator.
10 * 10 = 100
Process returned 0(0x0) execution time : 24 s