Advertisement
GamerBhai02

1. Infix To Postfix Code

Nov 3rd, 2024 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | Source Code | 0 0
  1. https://onlinegdb.com/2lTyzz6y5
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #define MAX_SIZE 100
  6. char stack[MAX_SIZE];
  7. int top=-1;
  8. void push(char item){
  9.     if(top >= MAX_SIZE-1){
  10.         printf("Stack Overflow\n");
  11.         return;
  12.     }
  13.     stack[++top]=item;
  14. }
  15. char pop(){
  16.     if (top<0){
  17.         printf("Stack Underflow\n");
  18.         return -1;
  19.     }
  20.     return stack[top--];
  21. }
  22. int precedence(char symbol){
  23.     switch(symbol){
  24.         case '+':
  25.         case '-': return 1;
  26.         case '*':
  27.         case '/':
  28.         case '%': return 2;
  29.         case '^': return 3;
  30.         default: return 0;
  31.     }
  32. }
  33. int isOperator(char symbol){
  34.     return (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '%' || symbol == '^');
  35. }
  36. void infixToPostfix(char infix[],char postfix[]) {
  37.     int i=0;
  38.     int j=0;
  39.     char item, temp;
  40.     while(infix[i]!='\0' && infix[i]!='\n') {
  41.         item = infix[i];
  42.         if(isalnum(item))
  43.             postfix[j++] = item;
  44.         else if (item == '(')
  45.             push(item);
  46.         else if(item==')')
  47.             while (top!= -1 && (temp = pop())!='(')
  48.                 postfix[j++] = temp;
  49.         else if (isOperator(item)) {
  50.             while (top != -1 && precedence(stack[top]) >= precedence(item))
  51.                 postfix[j++] = pop();
  52.             push(item);
  53.         }
  54.         i++;
  55.     }
  56.     while(top!=-1)
  57.         postfix[j++]=pop();
  58.     postfix[j]='\0';
  59. }
  60. int evaluatePostfixExpression(char postfix[]) {
  61.     int top=-1;
  62.     int i=0;
  63.     char item;
  64.     int operand1, operand2,result;
  65.     while(postfix[i]!='\0'){
  66.         item=postfix[i];
  67.         if (isalnum(item))
  68.             stack[++top]=item-'0';
  69.         else if (isOperator(item)) {
  70.             operand2 = stack[top--];
  71.             operand1 = stack[top--];
  72.             switch(item){
  73.                 case '+':
  74.                     result = operand1 + operand2;
  75.                     break;
  76.                 case '-':
  77.                     result = operand1 - operand2;
  78.                     break;
  79.                 case '*':
  80.                     result = operand1 * operand2;
  81.                     break;
  82.                 case '/':
  83.                     result = operand1 / operand2;
  84.                     break;
  85.                 case '%':
  86.                     result = operand1 % operand2;
  87.                     break;
  88.                 case '^':
  89.                     result = 1;
  90.                     for (int j=0;j<operand2;j++)
  91.                         result *= operand1;
  92.                     break;
  93.             }
  94.             stack[++top]=result;
  95.         }
  96.         i++;
  97.     }
  98.     return stack[top];
  99. }
  100. int main() {
  101.     char infixExpression[MAX_SIZE];
  102.     char postfixExpression[MAX_SIZE];
  103.     printf("Infix expression: ");
  104.     scanf("%s", infixExpression);
  105.     infixToPostfix(infixExpression, postfixExpression);
  106.     printf("Postfix expression: %s\n", postfixExpression);
  107.     int result = evaluatePostfixExpression(postfixExpression);
  108.     printf("Result: %d\n", result);
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement