ramytamer

rostom

Jun 18th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. #define TYPE int
  7. #define MAXSIZE 200
  8.  
  9. typedef struct Stack
  10. {
  11.     int top;
  12.     TYPE data[MAXSIZE];
  13.  
  14. } Stack;
  15.  
  16. void reset(Stack *s);
  17. int isEmpty(Stack *s);
  18. int isFull(Stack *s);
  19. void push(Stack *s,TYPE val);
  20. TYPE pop(Stack *s);
  21. void display(Stack s);
  22. int chckop(char x);
  23.  
  24. int main()
  25. {
  26.     Stack s;
  27.     reset(&s);
  28.     int i;
  29.     char x[100];
  30.     scanf("%s",x);
  31.     for(i=0;i<strlen(x);i++) {
  32.         if(isalnum(x[i])) {
  33.             push(&s,x[i]-48);
  34.         } else {
  35.             int x1;
  36.             int x2;
  37.             x1=pop(&s);
  38.             x2=pop(&s);
  39.             if(chckop(x[i])==1) push(&s,x1+x2);
  40.             else if(chckop(x[i])==2) push(&s,x1-x2);
  41.             else if(chckop(x[i])==3) push(&s,x1*x2);
  42.             else if(chckop(x[i])==4) push(&s,x1/x2);
  43.         }
  44.     }
  45.     printf("%d",s.data[s.top]);
  46.     return 0;
  47. }
  48.  
  49. int chckop(char x) {
  50.     if(x=='+') return 1;
  51.     if(x=='-') return 2;
  52.     if(x=='*') return 3;
  53.     if(x=='/') return 4;
  54.     return 0;
  55. }
  56.  
  57. void reset(Stack *s)
  58. {
  59.     s->top = 0;
  60. }
  61. int isEmpty(Stack *s)
  62. {
  63.     return s->top==0;
  64. }
  65. int isFull(Stack *s)
  66. {
  67.     return s->top==MAXSIZE;
  68. }
  69. void push(Stack *s,TYPE val)
  70. {
  71.     if(!isFull(s)) s->data[(s->top)++] = val;
  72. }
  73. TYPE pop(Stack *s)
  74. {
  75.     return s->data[--(s->top)];
  76. }
  77. void display(Stack s)
  78. {
  79.     while(!isEmpty(&s)) printf("[%d]\n", pop(&s));
  80. }
Advertisement
Add Comment
Please, Sign In to add comment