Sreekar_0125

Untitled

Sep 13th, 2022
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<ctype.h>
  4. char stack[100];
  5. int top = -1;
  6. void push(char ch) {
  7.     top++;
  8.     stack[top] = ch;
  9. }
  10. void pop () {
  11.     top--;
  12. }
  13. int priority (char ch) {
  14.     if(ch=='^') {
  15.         return 3;
  16.     }
  17.     else if(ch=='*' || ch == '/' || ch == '%') {
  18.         return 2;
  19.     }
  20.     else if(ch=='+' || ch=='-') {
  21.         return 1;
  22.     }
  23.     else {
  24.         return -1;
  25.     }
  26. }
  27. int main () {
  28.     char s[100];
  29.     scanf("%s",s);
  30.     int i,l;
  31.     l = strlen(s);
  32.     for(i=0;i<l;i++) {
  33.         if(isalpha(s[i])) {
  34.             printf("%c",s[i]);
  35.         }
  36.         else if(s[i] == '(') {
  37.             push(s[i]);
  38.         }
  39.         else if(s[i] == ')') {
  40.             while(stack[top]!='(') {
  41.                 printf("%c",stack[top]);
  42.                 pop();
  43.             }
  44.             top--;
  45.         }
  46.         else {
  47.             while(priority(s[i]) <= priority(stack[top])) {
  48.                 printf("%c",stack[top]);
  49.                 pop();
  50.             }
  51.             push(s[i]);
  52.         }
  53.     }
  54.     for(int i=top;i>=0;i--) {
  55.         printf("%c",stack[i]);
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment