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
Write a C++ program that reads students' names followed by their test scores. The program should output each students' name foll
Mashutka [201]

Answer:

#include<iostream>

#include<conio.h>

using namespace std;

struct studentdata{

char Fname[50];

char Lname[50];

int marks;

char grade;

};

main()

{

studentdata s[20];

for (int i=0; i<20;i++)

   {

cout<<"\nenter the First name of student";

cin>>s[i].Fname;

cout<<"\nenter the Last name of student";

cin>>s[i].Lname;

cout<<"\nenter the marks of student";

cin>>s[i].marks;

}  

 

for (int j=0;j<20;j++)

{

if (s[j].marks>90)

{

 s[j].grade ='A';

}

else if (s[j].marks>75 && s[j].marks<90)

{

   s[j].grade ='B';

}

else if (s[j].marks>60 && s[j].marks<75)

{

 s[j].grade ='C';

}

else

{

 s[j].grade ='F';

}

}

int highest=0;

int z=0;

for (int k=0;k<20; k++)  

{

if (highest<s[k].marks)

{

 highest = s[k].marks;

 z=k;

}

 

}

cout<<"\nStudents having highest marks"<<endl;

 

cout<<"Student Name"<< s[z].Fname<<s[z].Lname<<endl;

cout<<"Marks"<<s[z].marks<<endl;

cout<<"Grade"<<s[z].grade;

getch();  

}

Explanation:

This program is used to enter the information of 20 students that includes, their first name, last name and marks obtained out of 100.

The program will compute the grades of the students that may be A,B, C, and F. If marks are greater than 90, grade is A, If marks are greater than 75 and less than 90, grade is B. For Mark from 60 to 75 the grade is C and below 60 grade is F.

The program will further found the highest marks and than display the First name, last name, marks and grade of student who have highest marks.

6 0
3 years ago
On which tab can you find the margins button
insens350 [35]
In the tab key you alwAy use that to center
4 0
4 years ago
Read 2 more answers
GAMER OYUNCU KOLTUĞU % YERLİ
rewona [7]

Answer:

what does that even say omg

3 0
4 years ago
Why we need to measure the amperage of an electric current?​
Romashka [77]

Answer:

Explanation:It is very important to have a way to measure and quantify the flow of electrical current. When current flow is controlled it can be used to do useful work. The flow of electrons is measured in units called amperes. The term amps is often used for short

5 0
3 years ago
The process known as AAA (or “triple A”) security involves three components. _____________ means ensuring that an authenticated
kirill115 [55]

Answer:

The process known as AAA (or “triple A”) security involves three components. Authorization means ensuring that an authenticated user is allowed to perform the requested action.

7 0
4 years ago
Other questions:
  • A number used to encrypt data is called a(n ________. signature key cookie escrow
    10·1 answer
  • One suggested means of gaining eye contact before reversing is to:____.
    12·1 answer
  • Data arranged and stored in a data set
    9·1 answer
  • To hide gridline when you display or print a worksheet
    14·1 answer
  • Why don’t we need to know and memorize the IP addresses for our favorite websites?
    15·1 answer
  • You are building a gaming computer and you want to install a dedicated graphics card that has a fast GPU and 1GB of memory onboa
    6·1 answer
  • True or False: Mapping annotations are exclusive - an annotated method will only be accessible to requests sent to a matching UR
    14·1 answer
  • What kind of files are automatically blocked in outlook?
    7·1 answer
  • Order the steps for changing the settings of an outlook data file
    10·1 answer
  • In Python, which comparison operator means "less than or equal to"?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!