Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace ConsoleApp6
  5. {
  6. class Stack
  7. {
  8. int[] Buf;
  9. int n;
  10. int top;
  11. bool empty, full;
  12. public int nSt { get { return n; } }
  13. public bool Empty { get { return empty; } private set { empty = value; } }
  14. public bool Full { get { return full; } private set { full = value; } }
  15. public Stack(int n)
  16. {
  17. if (n == 0) throw new Exception("01");
  18. this.n = n;
  19. Buf = new int[n];
  20. top = 0;
  21. Empty = true;
  22. Full = false;
  23. }
  24. public void push(int t)
  25. {
  26. if (Full) throw new Exception("21");
  27. Buf[top++] = t;
  28. if (top >= n) Full = true;
  29. Empty = false;
  30. }
  31. public int pop()
  32. {
  33. if (Empty) throw new Exception("11");
  34. if (top == 0) Empty = true;
  35. return Buf[--top];
  36. }
  37. class Program
  38. {
  39. static void Main()
  40. {
  41. int i;
  42. Stack oSt;
  43. int r;
  44. StreamReader R = new StreamReader("in.txt");
  45. StreamWriter W = new StreamWriter("out.txt");
  46. int[] A;
  47. string[] As;
  48. int n;
  49. As = R.ReadLine().Split(',');
  50. n = As.Length;
  51. A = new int[n];
  52. for (i = 0; i < n; i++) A[i] = Convert.ToInt32(As[i]);
  53. for (i = 0; i <= 4; i++)
  54. {
  55. try
  56. {
  57. switch (i)
  58. {
  59. case 0: oSt = new Stack(i); break;
  60. case 1: oSt = new Stack(1); r = oSt.pop(); break;
  61. case 2:
  62. oSt = new Stack(1);
  63. oSt.push(i); oSt.push(i + 1); break;
  64. case 3:
  65. oSt = new Stack(3);
  66. {
  67. for (int j = 0; j < oSt.nSt; j++)
  68. {
  69. oSt.push(j + 1);
  70. }
  71. W.WriteLine("Stack 3: O'key");
  72. for (int j = 0; j < oSt.nSt; j++)
  73. {
  74. W.Write(" " + oSt.pop());
  75. }
  76. W.WriteLine();
  77. }
  78. break;
  79. case 4:
  80. oSt = new Stack(n);
  81. {
  82. for (int j = 0; j < oSt.nSt; j++)
  83. oSt.push(A[j]);
  84. W.WriteLine("Stack 4: O'key");
  85. while (!oSt.Empty)
  86. W.Write(" " + oSt.pop());
  87. W.WriteLine();
  88. }
  89. break;
  90. }
  91. }
  92. catch (Exception ob)
  93. {
  94. switch (ob.Message[0])
  95. {
  96. case '0':
  97. W.WriteLine("size=0"); break;
  98. case '1':
  99. W.WriteLine("read error"); break;
  100. case '2':
  101. W.WriteLine("write error"); break;
  102. }
  103. }
  104. }
  105. R.Close();
  106. W.Close();
  107. }
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement