Postfix notation is an unambiguous way of writing an arithme

Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if \"(exp1)op(exp2)\" is a normal fully parenthesized expression whose operation is op, the postfix version of this is \"pexp1 pexp2 op\", where pexp1 is the postfix version of exp1 and pexp2 is the postfix version of exp2. The postfix version of a single number or variable is just that number or variable. So, for example, the postfix version of \"((5 + 2) * (8 - 3))/4\" is \"5 2 + 8 3 - * 4/\". a. Use pseudocode to describe a way of evaluating an expression in postfix notation by using a stack. (Your method should return a Boolean value to indicate whether the expression is valid or not.) b. provide java code for your method and test examples.

Solution

#include<stdio.h>

#include<ctype.h>

#include<string.h>

char stack[30];

int top=-1;

int push(char);

int pop();

int priority(char);

void main()

{

char in[30],post[30],ch;

int i,j,l;

printf(“\ enter the string(infix Expression): “);

gets(in);

l=srtlen(in);

printf(“\ the length of the given string is: %d\ ”,l);

for(i=0,j=0;i<l;i++)

{

if(isalpha(in[i]) || isdigit(in[i]))

             post[j++]=in[i];

   else {

          if(in[i]==‘(‘)

                     push(in[i]);

          else if(in[i]==‘)’)

               {

                   while ((ch=pop())!=‘(‘)

                   post[j++]=ch;

                }

          else

               {

          while(priority(in[i])<=priority(stack[top]))

             post[j++]=pop();

             push(in[i]);

               }

          } // end of else

} // end of for

while(top!=-1)

post[j++]=pop();

post[j]=\'\\0\';

printf(\"\ Equivalent infix to postfix is:%s \",post);

}//main

int priority (char c) {

switch(c) {

case \'+\':

case \'-\': return 1;

case \'*\':

case \'/\': return 2;

case \'$\': return 3;

case \'^\': return 4;

}//switch

return 0;

}//priority

int push(char c) {

stack[++top]=c;

return 0;

}//push

int pop(){

return (stack[top--]);

}//pop

 Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if \
 Postfix notation is an unambiguous way of writing an arithmetic expression without parentheses. It is defined so that if \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site