Advertisement
Dar954826

Reverse Polish Notation (RPN)

Sep 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. char stack[100];
  5. short top = -1;
  6. char cima()
  7. {
  8.     return top == -1 ? 0 : stack[top];
  9. }
  10.  
  11. char pop()
  12. {
  13.     char s = top == -1 ? 0 : stack[top];
  14.     stack[top--] = 0;
  15.     return s;
  16. }
  17.  
  18. void push(char x)
  19. {
  20.     if (top != 99)
  21.         stack[++top] = x;
  22. }
  23.  
  24.  
  25. int par=0;
  26. int priority(char x)
  27. {
  28.     switch (x)
  29.     {
  30.     case '(':
  31.         par++;return 0;
  32.     case ')':
  33.         if(--par<0){
  34.             printf("\nUnbalanced parenthesis\n");
  35.             exit(1);
  36.         }return 1;
  37.     case '=':
  38.         return 2;
  39.     case '+':
  40.     case '-':
  41.         return 3;
  42.     case '*':
  43.     case '/':
  44.         return 4;
  45.     case '^':
  46.         return 5;
  47.     }
  48.     char z[]="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  49.     for(int a=0;a<strlen(z);a++)if(z[a]==x)return-1;
  50.     return -2;
  51. }
  52.  
  53. void printOp(char z[], int pun)
  54. {
  55.     if (pun)
  56.     {
  57.         z[pun] = 0;
  58.         printf("%s ", z);
  59.     }
  60. }
  61. void spellCheck(char x[]){
  62.     int p,last=0;
  63.     for (int a = 0; a < strlen(x); a++){
  64.         p = priority(x[a]);
  65.         if(p>2&&last>2){
  66.             printf("\nConsecutive operator\n");
  67.             exit(2);
  68.         }
  69.         last=p;
  70.     }
  71.     if(par!=0){
  72.         printf("\nUnbalanced parenthesis\n");
  73.         exit(1);
  74.     }
  75. }
  76.  
  77. void parse(char x[])
  78. {
  79.     char z[50];
  80.     short pun = 0;
  81.     int p;
  82.     printf("Notazione polacca inversa: ");
  83.     for (int a = 0; a < strlen(x); a++)
  84.     {
  85.         p = priority(x[a]);
  86.         if (p==-2);
  87.         else if (p == -1)
  88.             z[pun++] = x[a];
  89.         else
  90.         {
  91.             printOp(z, pun);
  92.             pun = 0;
  93.             if (p == 0)
  94.                 push(x[a]);
  95.             else if (p == 1)
  96.             {
  97.                 while (cima() != '(')
  98.                 {
  99.                     printf("%c ", pop());
  100.                 }
  101.                 pop();
  102.  
  103.             }
  104.             else if (priority(cima()) < p)
  105.                 push(x[a]);
  106.             else if (priority(cima()) >= p)
  107.             {
  108.                 while (priority(cima()) >= p && cima() != '(' && cima() != 0)
  109.                     printf("%c ", pop());
  110.                 push(x[a]);
  111.             }
  112.         }
  113.  
  114.     }
  115.     printOp(z, pun);
  116.     while (cima() != 0)
  117.         printf("%c ", pop());
  118.     printf("\n");
  119. }
  120.  
  121. int main()
  122. {
  123.     char str[200];
  124.     printf("Inserire espressione: ");
  125.     gets(str);
  126.     spellCheck(str);
  127.     parse(str);
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement