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

Design and code a program to evaluate simple arithmetic strings The strings will

ID: 3869513 • Letter: D

Question

Design and code a program to evaluate simple arithmetic strings The strings will

contain only single digits. The operations are addition, subtraction, multiplication, and division.

We will not to implement operator precedence. In our system, the strings will evaluate left-to-right.

For example, in ordinary C++ programs, the expression 4 + 5 * 7 would evaluate the

multiplication first and then the addition, because multiplication has higher precedence than

addition. Our system will evaluate the addition first, because we always go left-to-right.

This limits the usefulness of our system, but allows us to concentrate on the concepts we are using.

We require that our input is separated by whitespace. E.g., we will not accept 5+4 as input,

because there are no spaces between the characters. Furthermore, we may not perform error

checking on the input beyond determining that the characters input are valid.

We will not accept alphabetic characters, but we may accept invalid expressions if they contain valid characters.

That leaves the onus on the user to input valid expressions. For example, we will not accept the expression:

5 * D because D is not a valid character in our program. However, we might accept 4 7 +

even though it is nonsensical, because it consists of valid characters. If time permits, or we become

more ambitious, we may handle errors like that. The concepts we will use to complete this assignment include

structures, pointers, recursion, and binary trees.

In keeping with our policy of modular development, we will use a header file and two source files.

The first source file will be our driver that #includes our header and contains main. Main will call

our INFO function and our calculator program function. The second source file will contain the

methods to implement our calculator. The header will contain #define macros, typedef structures

and prototypes for our calculator.

Explanation / Answer

caitree.cpp

#include <bits/stdc++.h>
#include "calculator.cpp"
using namespace std;


int main(int argc, char const *argv[])
{
  
   Calculator cl("3 * H");
   cl.INFO();
   cout<<" Result of expression 3 * H is "<< cl.Calculate();
   Calculator cl2("3 * 2");
  
   cout<<" Result of expression 3 * 2 is "<< cl2.Calculate();
   return 0;
}

========================================================

calculator.h

class Calculator
{
   char * exp;

public:
   bool isOperand(char c);
   int value(char c) ;
   Calculator(char* s);
  

   int isValid();

   int Calculate();
   void INFO();
};

=================================================

calcuator.cpp

#include<bits/stdc++.h>
using namespace std;

#include "calculator.h"

void Calculator::INFO()
{
   cout<<"Below is computer program for calculator ";
   cout<<"It shows -1 if expreesion is invalid otherwise shows result ";
}

Calculator::Calculator(char* s)
   {
       exp=s;
   }

   bool Calculator:: isOperand(char c)
   { return (c >= '0' && c <= '9'); }

   int Calculator:: value(char c) { return (c - '0'); }

int Calculator:: isValid()
   {
       for (int i = 0; i < strlen(exp); ++i)

       {
           if(i%2!=0)
           {
               if(exp[i]!=' ')
               return 0;
           }
       }
       return 1;
   }
   int Calculator ::Calculate()
   {
if (*exp == '') return -1;

int res = value(exp[0]);

   if(isValid()==0)
   {
       cout<<"Invalid expression ";
   }

for (int i = 0; exp[i]; i += 1)
{
  
char opr = exp[i], opd = exp[i+2];
if(opr>='A'&&opr<='Z')
   return -1;

if(i%2==0)
{
           if (opr!=' '&&(!isOperand(opd)))
           {
              
               //return -1;
           }else{

if (opr == '+') res += value(opd);
else if (opr == '-') res -= value(opd);
else if (opr == '*') res *= value(opd);
else if (opr == '/') res /= value(opd);

else
   return -1;
}
}
}
return res;
   }

===================================================

output:

akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Below is computer program for calculator
It shows -1 if expreesion is invalid otherwise shows result

Result of expression 3 * H is -1
Result of expression 3 * 2 is 6

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