Advertisement
Guest User

Untitled

a guest
May 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. //Operator supported: +,-,*,/,%,^,(,)
  2. // Operands supported: all single character operands
  3. // Programa que transforma uma expressão parentizada em uma equivalente em notaçao polonesa
  4.  
  5. #include<stdio.h>
  6. #include<conio.h>
  7. #include<ctype.h>
  8.  
  9. #define MAX 50
  10.  
  11. typedef struct stack
  12. {
  13.     int data[MAX];
  14.     int top;
  15. }stack;
  16.  
  17. int precedence(char);
  18. double Resultado;
  19. void init(stack *);
  20. int empty(stack *);
  21. int full(stack *);
  22. int pop(stack *);
  23. void push(stack *,int);
  24. int top(stack *);   //value of the top element
  25. void infix_to_postfix(char infix[],char postfix[]);
  26.  
  27. void main()
  28. {
  29.     char infix[30],postfix[30];
  30.     printf("Enter an infix expression (operadores suportados +,-,*,/,%,^,(,)): "); //Operator supported: +,-,*,/,%,^,(,)
  31.     gets(infix);
  32.     infix_to_postfix(infix,postfix);
  33.     printf("\nPostfix expression: %s",postfix);
  34.     Resultado = sqrt((53+21)*2-sqrt(29+3*25));
  35.     printf("\\Resultado: %f", Resultado);
  36. }
  37.  
  38. void infix_to_postfix(char infix[],char postfix[])
  39. {
  40.     stack s;
  41.     char x,token;
  42.     int i,j;    //i-index of infix,j-index of postfix
  43.     init(&s);
  44.     j=0;
  45.  
  46.     for(i=0;infix[i]!='\0';i++)
  47.     {
  48.         token=infix[i];
  49.         if(isalnum(token))
  50.             postfix[j++]=token;
  51.         else
  52.             if(token=='(')
  53.                push(&s,'(');
  54.         else
  55.             if(token==')')
  56.                 while((x=pop(&s))!='(')
  57.                       postfix[j++]=x;
  58.                 else
  59.                 {
  60.                     while(precedence(token)<=precedence(top(&s))&&!empty(&s))
  61.                     {
  62.                         x=pop(&s);
  63.                         postfix[j++]=x;
  64.                     }
  65.                     push(&s,token);
  66.                 }
  67.     }
  68.  
  69.     while(!empty(&s))
  70.     {
  71.         x=pop(&s);
  72.         postfix[j++]=x;
  73.     }
  74.  
  75.     postfix[j]='\0';
  76. }
  77.  
  78. int precedence(char x)
  79. {
  80.     if(x=='(')
  81.         return(0);
  82.     if(x=='+'||x=='-')
  83.         return(1);
  84.     if(x=='*'||x=='/'||x=='%')
  85.         return(2);
  86.  
  87.     return(3);
  88. }
  89.  
  90. void init(stack *s)
  91. {
  92.     s->top=-1;
  93. }
  94.  
  95. int empty(stack *s)
  96. {
  97.     if(s->top==-1)
  98.         return(1);
  99.  
  100.     return(0);
  101. }
  102.  
  103. int full(stack *s)
  104. {
  105.     if(s->top==MAX-1)
  106.         return(1);
  107.  
  108.     return(0);
  109. }
  110.  
  111. void push(stack *s,int x)
  112. {
  113.     s->top=s->top+1;
  114.     s->data[s->top]=x;
  115. }
  116.  
  117. int pop(stack *s)
  118. {
  119.     int x;
  120.     x=s->data[s->top];
  121.     s->top=s->top-1;
  122.     return(x);
  123. }
  124.  
  125. int top(stack *p)
  126. {
  127.     return (p->data[p->top]);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement