Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. #define STACKSIZE 10
  6.  
  7. struct a_stack
  8. {
  9.     int top = 0;
  10.     int data[STACKSIZE];
  11. };
  12.  
  13. bool is_full(struct a_stack);
  14. bool is_stack_empty(struct a_stack);
  15. void showstack(struct a_stack);
  16. void init(struct a_stack*);
  17. void push(struct a_stack*, int);
  18. int  pop(struct a_stack*);
  19.  
  20. int main()
  21. {
  22.     struct a_stack my_stack;
  23.     int i = 0;
  24.     int x = 0;
  25.     char expression[20] = "324121.";
  26.  
  27.     init(&my_stack);
  28.  
  29.     i = 0;
  30.     while (expression[i] != '.')
  31.     {
  32.         switch (expression[i])
  33.         {
  34.         case '0': push(&my_stack, 0); break;
  35.         case '1': push(&my_stack, 1); break;
  36.         case '2': push(&my_stack, 2); break;
  37.         case '3': push(&my_stack, 3); break;
  38.         case '4': push(&my_stack, 4); break;
  39.         case '5': push(&my_stack, 5); break;
  40.         case '6': push(&my_stack, 6); break;
  41.         case '7': push(&my_stack, 7); break;
  42.         case '8': push(&my_stack, 8); break;
  43.         case '9': push(&my_stack, 9); break;
  44.         case '*' : push(&my_stack, pop(&my_stack) * pop(&my_stack)); break;
  45.         case '/': push(&my_stack, pop(&my_stack) / pop(&my_stack)); break;
  46.         case '+': push(&my_stack, pop(&my_stack) + pop(&my_stack)); break;
  47.         case '-': push(&my_stack, pop(&my_stack) - pop(&my_stack)); break;
  48.         default: std::cout << "Not Implemented expression "<< i << expression[i] << std::endl;
  49.         }
  50.         i++;
  51.     }
  52.     showstack(my_stack);
  53.     //
  54.     // result of computation should be at top of stack
  55.     // pop result and print it so that the user knows.
  56.     return 0;
  57. }
  58.  
  59. int  pop(struct a_stack* my_stack)
  60. {
  61.     int x = 0;
  62.     my_stack->top--;
  63.     x = my_stack->data[my_stack->top];
  64.     my_stack->data[my_stack->top] = -1;
  65.     return x;
  66. }
  67.  
  68.  
  69. void push(struct a_stack* my_stack, int item)
  70. {
  71.     my_stack->data[my_stack->top] = item;
  72.     my_stack->top++;
  73. }
  74.  
  75. void init(struct a_stack* my_stack)
  76. {
  77.     my_stack->top = 0;
  78. }
  79.  
  80.  
  81. void showstack(struct a_stack my_stack)
  82. {
  83.     cout << my_stack.top << " items " << endl;
  84.     for (int i = 0; i < my_stack.top; i++)
  85.         cout << i << "  :  " << my_stack.data[i] << endl;
  86. }
  87.  
  88. bool is_stack_empty(struct a_stack my_stack)
  89. {
  90.     if (my_stack.top == 0)
  91.         return true;
  92.     else
  93.         return false;
  94. }
  95.  
  96. bool is_full(struct a_stack my_stack)
  97. {
  98.     if (my_stack.top > STACKSIZE - 1)
  99.         return true;
  100.     else
  101.         return false;
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement