1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
pychu [463]
3 years ago
14

Write a program that evaluates an arithmetic expression in postfix notation. The basic algorithm is contained in "Evaluating Pos

tfix Expressions" on page 377. Assume the input 6 7 8 Programming Projects 391 contains numbers (but no variables) as well as the arithmetic operations +, -, *, and /. Your program should allow the user to evaluate additional expressions until the user wants to end the program. You might also enhance your program so that the expression need not be well formed; if it is not well formed, then the user must reenter the expression.
Computers and Technology
1 answer:
Ronch [10]3 years ago
6 0

Answer:

#include<iostream>

#include<cctype>

#include<stack>

using namespace std;

// returns the value when a specific operator

// operates on two operands

int eval(int op1, int op2, char operate) {

switch (operate) {

case '*': return op2 * op1;

case '/': return op2 / op1;

case '+': return op2 + op1;

case '-': return op2 - op1;

default : return 0;

}

}

// evaluates the postfix operation

// this module neither supports multiple digit integers

// nor looks for valid expression

// However it can be easily modified and some additional

// code can be added to overcome the above mentioned limitations

// it's a simple function which implements the basic logic to

// evaluate postfix operations using stack

int evalPostfix(char postfix[], int size) {

stack<int> s;

int i = 0;

char ch;

int val;

while (i < size) {

ch = postfix[i];

if (isdigit(ch)) {

// we saw an operand

// push the digit onto stack

s.push(ch-'0');

}

else {

// we saw an operator

// pop off the top two operands from the

// stack and evalute them using the current

// operator

int op1 = s.top();

s.pop();

int op2 = s.top();

s.pop();

val = eval(op1, op2, ch);

// push the value obtained after evaluating

// onto the stack

s.push(val);

}

i++;

}

return val;

}

// main

int main() {

char postfix[] = {'5','6','8','+','*','2','/'};

int size = sizeof(postfix);

int val = evalPostfix(postfix, size);

cout<<"\nExpression evaluates to "<<val;

cout<<endl;

return 0;

}

or else you can try even this

#include <iostream.h>

#include <stdlib.h>

#include <math.h>

#include <ctype.h>

const int MAX = 50 ;

class postfix

{

private :

int stack[MAX] ;

int top, nn ;

char *s ;

public :

postfix( ) ;

void setexpr ( char *str ) ;

void push ( int item ) ;

int pop( ) ;

void calculate( ) ;

void show( ) ;

} ;

postfix :: postfix( )

{

top = -1 ;

}

void postfix :: setexpr ( char *str )

{

s = str ;

}

void postfix :: push ( int item )

{

if ( top == MAX - 1 )

cout << endl << "Stack is full" ;

else

{

top++ ;

stack[top] = item ;

}

}

int postfix :: pop( )

{

if ( top == -1 )

{

cout << endl << "Stack is empty" ;

return NULL ;

}

int data = stack[top] ;

top-- ;

return data ;

}

void postfix :: calculate( )

{

int n1, n2, n3 ;

while ( *s )

{

if ( *s == ' ' || *s == '\t' )

{

s++ ;

continue ;

}

if ( isdigit ( *s ) )

{

nn = *s - '0' ;

push ( nn ) ;

}

else

{

n1 = pop( ) ;

n2 = pop( ) ;

switch ( *s )

{

case '+' :

n3 = n2 + n1 ;

break ;

case '-' :

n3 = n2 - n1 ;

break ;

case '/' :

n3 = n2 / n1 ;

break ;

case '*' :

n3 = n2 * n1 ;

break ;

case '%' :

n3 = n2 % n1 ;

break ;

case '$' :

n3 = pow ( n2 , n1 ) ;

break ;

default :

cout << "Unknown operator" ;

exit ( 1 ) ;

}

push ( n3 ) ;

}

s++ ;

}

}

void postfix :: show( )

{

nn = pop ( ) ;

cout << "Result is: " << nn ;

}

void main( )

{

char expr[MAX] ;

cout << "\nEnter postfix expression to be evaluated : " ;

cin.getline ( expr, MAX ) ;

postfix q ;

q.setexpr ( expr ) ;

q.calculate( ) ;

q.show( ) ;

}

You might be interested in
A robot worker and a human worker are both vulnerable to
Lady bird [3.3K]

Answer:

fire

Explanation:

4 0
3 years ago
Which data type is 2.5?
Phoenix [80]

2.5 is a float.

Float stands for floating point. Floating point types are numbers with decimal places. I hope this helps!

6 0
2 years ago
Brainliest forrrrrrr frrrrrew right herrreeee
nikdorinn [45]

Answer:

Hi!

Explanation:

Thank You So Much For The Points.

5 0
2 years ago
Why doesnt brainly let me skip by watching video?
oksian1 [2.3K]

What do you want to skip in brainly? There isn't anything to skip that I can think of.

4 0
3 years ago
Uses of internet write in point.​
Vladimir [108]

Answer:

-Electronic mail. At least 85% of the inhabitants of cyberspace send and receive e-mail. ...

-Research.

-Downloading files.

-Discussion groups. ...

-Interactive games. ...

-Education and self-improvement. ...

-Friendship and dating. ...

-Electronic newspapers and magazines.

3 0
3 years ago
Other questions:
  • Randy is concerned about his company’s data security on the Internet. Because cellular signals are not encrypted, he is concerne
    6·1 answer
  • Your friend’s parents are worried about going over their budget for the month. Which expense would you suggest is NOT a need?
    10·2 answers
  • Force field meaning in science fiction
    6·1 answer
  • How can an Excel table be added to a Word document? Check all that apply.
    15·1 answer
  • 22. A<br> allows one computer to input data into another computer.
    6·1 answer
  • Can anybody answer this
    11·1 answer
  • Short note on first generation computer​
    10·1 answer
  • Can someone tell me what this means Higfaa
    6·1 answer
  • Differences between dot_mattix printer and a line printer
    12·1 answer
  • What tool allows you to search external competitive intelligence research?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!