Advertisement
Asif_Anwar

Evaluation of a Postfix Expression

Nov 27th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. // Stack-Evaluation of a Postfix Expression
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #define STACK_MAX 1001
  8. int cnt = 0;
  9. typedef struct {
  10.     int top;
  11.     int data[STACK_MAX];
  12. } Stack;
  13.  
  14. void printStack(Stack *s)
  15. {
  16.     int i;
  17.     int n = s->top;
  18.     printf("Current state of stack(pass %d): ", ++cnt);
  19.    
  20.     for(i=0; i<n; i++) {
  21.         printf("%d ", s->data[i]);
  22.     }
  23.     printf("\n");
  24. }
  25.  
  26. int valueOf(char str[])
  27. {
  28.     int n = strlen(str);
  29.     int sum = 0;
  30.     for(int i=0; i<n; i++) {
  31.         sum = sum * 10 + (str[i]-'0');
  32.     }
  33.     return sum;
  34. }
  35.  
  36. void push(Stack *s, int item)
  37. {
  38.     if(s->top<STACK_MAX) {
  39.         s->data[s->top] = item;
  40.         s->top = s->top + 1;
  41.     }
  42.     else {
  43.         printf("Stack is full!\n");
  44.     }
  45. }
  46.  
  47. int pop(Stack *s)
  48. {
  49.     int item=-1;
  50.     if(s->top==0) {
  51.         return item;
  52.     }
  53.     else {
  54.         s->top = s->top-1;
  55.         item = s->data[s->top];
  56.     }
  57.     return item;
  58. }
  59.  
  60. int main()
  61. {
  62.     Stack s;
  63.     s.top = 0;
  64.     int n;
  65.     int a, b;
  66.     int i;
  67.     int value = 0;
  68.     int item = 0;
  69.     printf("Enter the size of postfix(1000 is the max stack size): ");
  70.     scanf("%d", &n);
  71.     getchar();
  72.     printf("Enter the postfix expression(Put spaces between the objects): ");
  73.     char arr[n][50];
  74.    
  75.     for(i=0; i<n; i++) {
  76.         scanf("%s", arr[i]);
  77.         int SZ = strlen(arr[i]);
  78.         arr[i][SZ] = '\0';
  79.     }
  80.     printf("\n");
  81.     for(i=0; i<n; i++) {
  82.         printf("\n");
  83.         item = 0;
  84.         a = -1, b= -1;
  85.         char str[50];
  86.         memset(str, '\0', sizeof(str));
  87.         strcpy(str, arr[i]);
  88.         int sz = strlen(str);
  89.         printf("Current object: %s\n", str);
  90.         if(sz==1) {
  91.             if(str[0]=='-'||str[0]=='+'||str[0]=='/'||str[0]=='*') {
  92.                
  93.                 if(s.top==1) {
  94.                     printf("There is only one operand in the stack.\n");
  95.                     printStack(&s);
  96.                     continue;
  97.                 }
  98.                 else if(s.top==0) {
  99.                     printf("The stack is Empty!\n");
  100.                     printStack(&s);
  101.                     continue;
  102.                 }
  103.                 a = pop(&s);
  104.                 b = pop(&s);
  105.                
  106.                 if(str[0]=='-') {
  107.                      value = b-a;
  108.                 }
  109.                 else if(str[0]=='+') {
  110.                     value = b+a;
  111.                 }
  112.                 else if(str[0]=='*') {
  113.                     value = b*a;
  114.                 }
  115.                 else if(str[0]=='/') {
  116.                     value = b/a;
  117.                 }
  118.                 push(&s, value);
  119.             }
  120.             else {
  121.                 item = valueOf(str);
  122.                 push(&s, item);
  123.             }
  124.         }
  125.         else {
  126.                 item = valueOf(str);
  127.                 push(&s, item);
  128.         }
  129.         printStack(&s);
  130.        
  131.         if(i==n-1) {
  132.             if(s.top>1) {
  133.                 printf("\nToo many operands left in the stack.\n");
  134.             }
  135.             else {
  136.                 printf("\nThe value of the postfix expression: %d\n", pop(&s));
  137.             }
  138.         }
  139.     }
  140.     return 0;
  141.    
  142. }
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement