Você está na página 1de 6

lgorithm:(Taken from wikipedia)

While there are tokens to be read:

Read a token . If the token is a number, then add it to the output queue. If the token is a function token, then push it onto the stack. If the token is a function argument separator (e.g., a comma): Until the topmost element of the stack is a left parenthesis, pop the element from the stack and push it onto the output queue. If no left parentheses are encountered, either the separator was misplaced or parentheses were mismatched. If the token is an operator, o1, then: while there is an operator, o2, at the top of the stack, and either

o1 is associative or left-associative and its precedence is less than (lower precedence) or equal to that of o2, or o1 is right-associative and its precedence is less than (lower precedence) that of o 2, pop o2 off the stack, onto the output queue; push o1 onto the stack.

If the token is a left parenthesis, then push it onto the stack. If the token is a right parenthesis: Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue. Pop the left parenthesis from the stack, but not onto the output queue. If the token at the top of the stack is a function token, pop it and onto the output queue. If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.

When there are no more tokens to read:


Exit.

While there are still operator tokens in the stack: If the operator token on the top of the stack is a parenthesis, then there are mismatched parenthesis. Pop the operator onto the output queue.

view source print? 01.#include <stdio.h> 02.#define size 10 03.char stack[size]; 04.int tos=0,ele; 05.void push(); 06.char pop(); 07.void show(); 08.int isempty(); 09.int isfull(); 10.char infix[30],output[30]; 11.int prec(char); 12.int main() 13.{ 14.int i=0,j=0,k=0,length; 15.char temp; 16.printf("\nEnter an infix expression:"); 17.scanf("%s",infix); 18.printf("\nThe infix expresson is %s",infix); 19.length=strlen(infix); 20.for(i=0;i<= prec(stack[tos-1]) )

21.{ 22.temp=pop(); 23.printf("\n the poped element is :%c",temp); 24.output[j++]=temp; 25.push(infix[i]); 26.printf("\n The pushed element is :%c",infix[i]); 27.show(); 28.} 29.else 30.{ 31.push(infix[i]); 32.printf("\nThe pushed element is:%c",infix[i]); 33.show(); 34.} 35.} 36.else 37.{ 38.if(infix[i]=='(') 39.{ 40.push(infix[i]); 41.printf("\nThe pushed-- element is:%c",infix[i]); 42.}

43.if(infix[i]==')') 44.{ 45.temp=pop(); 46.while(temp!='(') 47.{output[j++]=temp; 48.printf("\nThe element added to Q is:%c",temp); 49.//temp=pop(); 50.printf("\n the poped element is :%c",temp); 51.temp=pop();} 52.} 53.} 54.} 55.} 56.printf("\nthe infix expression is: %s",output); 57.} 58.while(tos!=0) 59.{ 60.output[j++]=pop(); 61.} 62.printf("the infix expression is: %s\n",output); 63.} 64.//Functions for operations on stack

65.void push(int ele) 66.{ 67.stack[tos]=ele; 68.tos++; 69.} 70.char pop() 71.{ 72.tos--; 73.return(stack[tos]); 74.} 75.void show() 76.{ 77.int x=tos; 78.printf("--The Stack elements are....."); 79.while(x!=0) 80.printf("%c, ",stack[--x]); 81.} 82. 83.//Function to get the precedence of an operator 84.int prec(char symbol) 85.{ 86.if(symbol== '(')

87.return 0; 88.if(symbol== ')') 89.return 0; 90.if(symbol=='+' || symbol=='-') 91.return 1; 92.if(symbol=='*' || symbol=='/') 93.return 2; 94.if(symbol=='^') 95.return 3; 96.return 0; 97.}

Você também pode gostar