fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #define MAX 100
  6. int stack[MAX];
  7. int top = -1;
  8. void push(int operand)
  9. {
  10. if (top >= MAX - 1)
  11. {
  12. printf("stack Overflow\n");
  13. exit(1);
  14. }
  15. stack[++top] = operand;
  16. }
  17. int pop(){
  18. if (top == -1){
  19. printf("stack underflow\n");
  20. exit(1);
  21. }
  22. return stack[top--];
  23. }
  24. int evaluatePostfix(char* postfix){
  25. int i = 0;
  26. while (postfix[i] != '\0'){
  27. char current = position[i];
  28. if (isdigit(current)) {
  29. push(current - '0');
  30. }
  31. else{
  32. int operand2 = pop();
  33. int operand1 = pop();
  34. switch (current){
  35. case '+':
  36. push(operand1 + operand2);
  37. break;
  38. case '-':
  39. push(operand1 - operand2);
  40. break;
  41. case '*':
  42. push(operand1 * operand2);
  43. break;
  44. case '/':
  45. if (operand2 == 0){
  46. printf("division by zero error")
  47. exit(1);
  48. }
  49. push(operand1 / operand2);
  50. break;
  51. default:
  52. printf("Invalid operator: %c\n",current);
  53. exit();
  54. }
  55. }
  56. i++;
  57. }
  58. return stack[top];
  59. }
  60. int main(){
  61. char postfix[MAX];
  62. printf("enter the postfix expression:");
  63. scanf("%s", postfix);
  64. int result = evaluatePostfix(postfix);
  65. printf("result of the postfix expression: %d\n,result");
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0.03s 25712KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
int stack[MAX];
int top = -1;
void push(int operand)
{
  if (top >= MAX - 1)
  {
    printf("stack Overflow\n");
    exit(1);
  }
  stack[++top] = operand;
}
int pop(){
  if (top == -1){
    printf("stack underflow\n");
    exit(1);
  }
  return stack[top--];
}
int evaluatePostfix(char* postfix){
  int i = 0;
  while (postfix[i] != '\0'){
    char current = position[i];
    if (isdigit(current)) {
      push(current - '0');
    }
    else{
      int operand2 = pop();
      int operand1 = pop();
      switch (current){
        case '+':
        push(operand1 + operand2);
        break;
        case '-':
        push(operand1 - operand2);
        break;
        case '*':
        push(operand1 * operand2);
        break;
        case '/':
        if (operand2 == 0){
          printf("division by zero error")
          exit(1);
        }
        push(operand1 / operand2);
        break;
        default:
        printf("Invalid operator: %c\n",current);
        exit();
      }
    }
    i++;
  }
  return stack[top];
}
int main(){
  char postfix[MAX];
  printf("enter the postfix expression:");
  scanf("%s", postfix);
  int result = evaluatePostfix(postfix);
  printf("result of the postfix expression: %d\n,result");
  return 0;
}