Question

In: Computer Science

TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...

TASK:

Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.

  

REQUIREMENTS:

- Your simulator must work with both single digit operands and multiple digit operands.

- You must use your own stack template class or library <stack>.

- You must develop a design document to how your project works.

- Your program must produce a correct result if an input expression is correct, otherwise an error message should be given.

- You must put your functions and variables together to form a class—calculator.

Solutions

Expert Solution

CODE IN C++:

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

//it is a method to find the precedence of operators
int precedence(char operand){
   if(operand == '+'||operand == '-')
   return 1;
   if(operand == '*'||operand == '/')
   return 2;
   return 0;
}

// It is functin to perform arithmetic operations.
int applyOp(int x, int y, char operand){
   switch(operand){
       case '+': return x + y;
       case '-': return x - y;
       case '*': return x * y;
       case '/': return x / y;
   }
}

// it is function to return the value after evaluating expression
int evaluate(string tokens){
   int i;
  

   stack <int> values;
  
  
   stack <char> ops;
  
   for(i = 0; i < tokens.length(); i++){
      
      
       if(tokens[i] == ' ')
           continue;
      
      
       else if(tokens[i] == '('){
           ops.push(tokens[i]);
       }
      
      
       else if(isdigit(tokens[i])){
           int val = 0;
          
          
           while(i < tokens.length() &&
                       isdigit(tokens[i]))
           {
               val = (val*10) + (tokens[i]-'0');
               i++;
           }
          
           values.push(val);
       }
      
      
       else if(tokens[i] == ')')
       {
           while(!ops.empty() && ops.top() != '(')
           {
               int val2 = values.top();
               values.pop();
              
               int val1 = values.top();
               values.pop();
              
               char op = ops.top();
               ops.pop();
              
               values.push(applyOp(val1, val2, op));
           }
          
          
           if(!ops.empty())
           ops.pop();
       }
      
      
       else
       {
          
           while(!ops.empty() && precedence(ops.top())
                               >= precedence(tokens[i])){
               int val2 = values.top();
               values.pop();
              
               int val1 = values.top();
               values.pop();
              
               char op = ops.top();
               ops.pop();
              
               values.push(applyOp(val1, val2, op));
           }
          
          
           ops.push(tokens[i]);
       }
   }
  
  
   while(!ops.empty()){
       int val2 = values.top();
       values.pop();
              
       int val1 = values.top();
       values.pop();
              
       char op = ops.top();
       ops.pop();
              
       values.push(applyOp(val1, val2, op));
   }
  
  
   return values.top();
}

int main() {
cout << evaluate("5 * 6 + 4 - 3")<<endl;
   cout << evaluate("9 + 7 * 3") << endl;
   cout << evaluate("65 * 2 + 12") << endl;
   cout << evaluate("78 * ( 2 + 12 )") << endl;
   cout << evaluate("13 * ( 2 + 12 ) / 14")<<endl;
   return 0;
}


OUTPUT:


Related Solutions

Integer value calculator: Write a program using C++ that acts as a calculator. The program should...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should ask the user for two numbers and an operator. Then the program must print the result using the two input values and the operator. Prompts and input requirements. Ask the user for two integer values and a value that indicates the operation. Likely you will want to choose the normal operators: + - * / %. Output Requirements: Make your output easy to understand,...
Write a simple Calculator program using Scheme programming language. It should be able to do following...
Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT