fork download
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. template<class T>
  7. class Stack
  8. {
  9. private:
  10. T *s;
  11. int N;
  12.  
  13. public:
  14. Stack(int maxn)
  15. {
  16. s = new T[maxn];
  17. N = 0;
  18. }
  19. int empty()const
  20. {
  21. return N == 0;
  22. }
  23. void push(T k)
  24. {
  25. s[N++] = k;
  26. }
  27. T pop()
  28. {
  29. return s[--N];
  30. }
  31. };
  32.  
  33. int main()
  34. {
  35. //postfix evaluation
  36. const char *a = "3 4 5*+";
  37. int N = strlen(a);
  38.  
  39. Stack<int>save(N);
  40.  
  41. for (int i = 0; i < N; i++)
  42. {
  43. if (a[i]=='+')
  44. save.push(save.pop() + save.pop());
  45.  
  46. if (a[i]=='*')
  47. save.push(save.pop() * save.pop());
  48.  
  49. if (a[i] >= '0' && a[i] <= '9')
  50. {
  51. save.push(0);
  52. while (a[i] >= '0' && a[i] <= '9')
  53. save.push(10 * save.pop() + a[i++] - '0');
  54. // i--;
  55. }
  56. }
  57.  
  58. cout << save.pop() << " " << endl;
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 2812KB
stdin
Standard input is empty
stdout
9