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
To add a glow effect to WordArt text, which of the following should be done?
g100num [7]
Number 2 is the correct answer
8 0
3 years ago
6. Choose the TWO conditions that could be used for an IF-statement, without a compile-time error. Assume variable str is a Stri
KengaRu [80]

Answer:

  1. 4 ==5
  2. "six"== 6

Explanation:

3 0
3 years ago
2. The part of the computer that provides access to the Internet is the A. monitor. B. keyboard. C. system unit. D. modem.
allochka39001 [22]
The modem provides access to the internet.
4 0
3 years ago
Read 2 more answers
Examine about the Internal &amp; External Fragmentation methods give an example for each. essay​
Tju [1.3M]

Internal Fragmentation occurs when a process needs more space than the size of allotted memory block or use less space. External Fragmentation occurs when a process is removed from the main memory. Internal Fragmentation occurs when Paging is employed. External Fragmentation occurs when Segmentation is employed.

3 0
3 years ago
When are the malls going to open
Amiraneli [1.4K]
Idk maybe at 6:00pm today cuz most of the malls are close. Happy Thanksgiving! :D
3 0
3 years ago
Read 2 more answers
Other questions:
  • An administrator working on a Windows Server 2016 Server Core installation needs to disable DHCP on all interfaces on the server
    12·1 answer
  • Evaluating how current, credible, and unbiased a source is ensures:
    7·1 answer
  • A formal log-on procedure is the operating system’s last line of defense against unauthorized access.
    10·1 answer
  • What is application launchers functions​
    15·1 answer
  • You have just read about a new security patch that has been made available for your Windows system, so you install the patch as
    6·1 answer
  • The World Health Organization decided that addiction to video games is considered a mental health disorder. Do you agree or disa
    10·1 answer
  • Finish the program by choosing the correct terms.
    7·2 answers
  • Complete the function favoriteFlower(). Note that the program will not run as is because the function is incomplete. The purpose
    15·1 answer
  • Electronic evidence on computer storage media that is not visible to the average user is called​ ________.
    14·1 answer
  • Describe the type of gameplay seen during early video games (ex. Spacewar!, Pong).
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!