Question:
program for postfix evaluation?
ajith k
2006-09-11 03:08:02 UTC
i want a program in c that can b used to convert arithmetic to postfix evaluatoin
Three answers:
Answer Answer
2006-09-11 03:34:48 UTC
#include

#include

#include

#include

#define MAXSIZE 20



char p[10];

int arr[MAXSIZE];

char arr1[MAXSIZE];

int top,top1;



void create1(){

top1 = 0;

}



void create(){

top = 0;

}



void push1(char c){

if(top1 == MAXSIZE){

printf("Stack1 is full.");

return;

}

arr1[top1] = c;

top1++;

}



char pop1(void){

if(top1 == 0){

printf("Stack1 is empty.");

return -1;

}

top1--;

return arr1[top1];



}



void push(int c){

if(top == MAXSIZE){

printf("Stack is full.");

return;

}

arr[top] = c;

top++;

}



int pop(void){

if(top == 0){

printf("Stack is empty.");

return -1;

}

top--;

return arr[top];



}



int polish(char p[]){

create();

int len = strlen(p);

int i,val;

for(i=0; i
if(p[i]!='+' && p[i]!='-' && p[i]!='*' && p[i]!='/'){

int num = p[i]-48;

push(num);

}

else if(p[i]=='+' || p[i]=='-' || p[i]=='*' || p[i]=='/'){

int y = pop();

int x = pop();

if(p[i]=='+')

val = x+y;

else if(p[i]=='-')

val = x-y;

else if(p[i]=='*')

val = x*y;

else if(p[i]=='/')

val = x/y;

push(val);

}

}

return pop();

}



void Infix2Postfix(char e[]){

create1();

push1('#');

int i,j;

i=j=0;

do{

if(e[i]!='+' && e[i]!='-' && e[i]!='*' && e[i]!='/' && e[i]!='(' && e[i]!=')' && e[i]!='$'){

p[j] = e[i];

i++;

j++;

}

else if(e[i] == '('){

push1(e[i]);

i++;

}

else if(e[i] == ')'){

char x = pop1();

while(x != '('){

p[j] = x;

x = pop1();

j++;

}

i++;

}

else{

if( (e[i+1]>='0' && e[i+1]<='9') || e[i+1]=='(' ){

push1(e[i]);

i++;

}

else if(e[i]=='$'){

while(top1 != 0){

if(arr1[top1-1] == '#')

break;

p[j] = pop1();

j++;

}

push1(e[i]);

}

}

}while(arr1[top1-1] != '$');

p[j] = '\0';

}



void main(void){

char e[10];

clrscr();

printf("Input a inifix notation : ");

gets(e);

fflush(stdin);

int l;

l = strlen(e);

e[l] = '$';

l++;

e[l] = '\0';

Infix2Postfix(e);

while(1){

printf("The value is %d",polish(p));

printf("\n\nInput a inifix notation : ");

gets(e);

fflush(stdin);

if(strcmp(e,"end")==0)

exit(0);

l = strlen(e);

e[l] = '$';

l++;

e[l] = '\0';

Infix2Postfix(e);

}

}
?
2016-11-07 07:16:50 UTC
the previous answer summarized the answer. to furnish you an occasion, take 24*seventy 4-+ test the postfix expression, left to precise. If the character is a variety, place it interior the stack. If the character is an operator, pop the final 2 numbers of the stack. for this reason, the stack could contain 2 , 4. while the multiplication sign is study, 2 and four are popped from the stack. the two numbers are extra desirable as in line with the operator's instructions and the cost 8 is pushed on the stack. the subsequent characters are 7 and four. After examining 4, the stack sounds like 8,7,4. the subsequent character is an operator or a minus sign. provided that, the values 4 and seven are popped. 7 - 4 is evaluated which aspects 3. 3 is further to the stack to furnish 8,3 with the aid of fact the present stack. The addition operator is then retrieved which aspects an exceedingly final output of 8 + 3 = 11.
Joe_Young
2006-09-11 03:10:49 UTC
ajith here is how you do it.



http://www.google.co.uk/search?hl=en&q=program+for+postfix+evaluation&btnG=Search&meta=


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...