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

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...
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...
Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A...
Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A base class, Cylinder, will be created, with its derived classes, also called child classes or sub-classes. First, create a parent class, Cylinder. Create a constant for pi since you will need this for any non-square containers. Use protected for the members. Finally, create a public function that sets the volume. // The formula is: V = pi * (r^2) * h Task 2: Create...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT