Advertisement
J00ker

Untitled

Mar 5th, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <climits>
  4. #define Nmax 1000
  5.  
  6. using namespace std;
  7.  
  8. struct Stiva
  9. {
  10. int st[Nmax];
  11. int top;
  12.  
  13. void Init()
  14. {
  15. top = -1;
  16. }
  17.  
  18. int Empty()
  19. {
  20. if(top == -1) return 1;
  21. return 0;
  22. }
  23.  
  24. int Full()
  25. {
  26. if(top == Nmax - 1) return 1;
  27. return 0;
  28. }
  29.  
  30. void Push(int x)
  31. {
  32. if(Full()) cout << "Stack Overflow!";
  33. else st[++top] = x;
  34. }
  35.  
  36. void Pop()
  37. {
  38. if(!Empty()) top--;
  39. }
  40.  
  41. int Top()
  42. {
  43. return st[top];
  44. }
  45. int Size()
  46. {
  47. return top+1;
  48. }
  49. };
  50.  
  51. int main()
  52. {
  53. int n;
  54. char s[100];
  55. Stiva st;
  56.  
  57. ifstream fin("stiva.in");
  58.  
  59. fin >> n;
  60. st.Init();
  61.  
  62. for(int i = 1; i <= n; i++)
  63. {
  64. int x;
  65. fin >> s;
  66. if(s == '(') st.Push(-1);
  67. else
  68. {
  69. if(st.Size() == 0) /// Stiva este vida
  70. cout << "0\n";
  71. else if(st.Top() == -1) /// a)
  72. {
  73. st.Pop();
  74. if(st.Size() > 0 && st.Top() > 0)
  75. {
  76. x = st.Top();
  77. x += 2;
  78. st.Pop();
  79. st.Push(x);
  80. }
  81. else st.Push(2);
  82. cout << st.Top() << "\n";
  83. }
  84. else /// b)In varful sitvei este o valoare > 0
  85. {
  86. x = st.Top();
  87. st.Pop();
  88. if(st.Size() > 0)
  89. {
  90. x += 2;
  91. st.Pop;
  92. st.Push(x);
  93. }
  94. if(st.Size() > 0)
  95. cout << st.Top() << "\n";
  96. else cout << "0\n";
  97.  
  98. }
  99. }
  100. }
  101.  
  102. fin.close();
  103.  
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement