fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. string[] input = new string[]{"(","*","(","+","2","3",")","(","-","5","3",")",")"};
  8.  
  9. foreach(string a in input){
  10. Console.Write(a);
  11. }
  12. Console.WriteLine();
  13. stack(input);
  14. }
  15.  
  16. static void stack(string[] data){
  17.  
  18. string[] stk = new string[100];
  19.  
  20. for(int i=0;i<100;i++){
  21. stk[i]=null;
  22. }
  23.  
  24. for(int i=0;i<data.Length;i++){
  25. Console.WriteLine("data["+i+"] : "+data[i]);
  26. if(data[i] ==")" ){
  27. Console.WriteLine("pop start");
  28. stk = pop(stk,data[i]);
  29. }
  30. // else if(data[i] !="*" || data[i] !="/" ||data[i] !="+" ||data[i] !="-"){
  31. // cal(stk,data[i]);
  32. // }
  33. else{
  34.  
  35. stk = push(stk,data[i]);
  36. }
  37. }
  38. Console.WriteLine();
  39. foreach(string b in stk){
  40. Console.Write(b);
  41. }
  42.  
  43. }
  44.  
  45. static string[] push(string[] stk,string d){
  46.  
  47. for(int i=0;i<100;i++){
  48. if(stk[i] == null){
  49. stk[i] = d;
  50. //Console.WriteLine("push : " + stk[i]);
  51. break;
  52. }
  53. }
  54.  
  55.  
  56.  
  57.  
  58. return stk;
  59. }
  60.  
  61. static string[] pop(string[] stk,string d){
  62.  
  63. int sum = 0;
  64. int[] num = new int[10];
  65. string cal = "";
  66.  
  67. for(int i=0;i<10;i++){
  68. num[i]=0;
  69. }
  70.  
  71.  
  72.  
  73. for(int i=99;i>=0;i--){
  74. // Console.WriteLine("stk : " +stk[i]);
  75. if(stk[i] != null){
  76.  
  77. if(stk[i] == "("){
  78. Console.WriteLine("cal go");
  79.  
  80. int cnt = 0;
  81. for(int j = 9;j>=0;j--){
  82. if(num[j]!=0){
  83. sum = num[j];
  84. cnt = j;
  85. Console.WriteLine("cnt : " + cnt);
  86. break;
  87. }
  88. }
  89.  
  90. for(int j = 0;j<cnt;j++){
  91. if(num[j]!=0){
  92.  
  93. if(cal == "+"){
  94. sum += num[j];
  95. }else if(cal == "-"){
  96. sum = sum-num[j];
  97. }else if(cal == "*"){
  98. sum *= num[j];
  99. }else{
  100. sum /= num[j];
  101. }
  102.  
  103. }
  104. }
  105. stk[i]=sum.ToString();
  106. Console.WriteLine("cal_data : " + sum);
  107. Console.Write("stk : ");
  108. foreach(string stkd in stk){
  109. Console.Write(stkd);
  110. }
  111. Console.WriteLine();
  112. break;
  113. }else if(stk[i] =="*" || stk[i] =="/" || stk[i] =="+" || stk[i] =="-"){
  114. Console.WriteLine("cal save" + stk[i]);
  115. cal = stk[i];
  116. stk[i]=null;
  117. }else{
  118. Console.WriteLine("num save");
  119. for(int j = 0;j<10;j++){
  120. if(num[j]==0){
  121. num[j]=Int32.Parse(stk[i]);
  122. stk[i]=null;
  123. Console.WriteLine("pop : " + num[j]);
  124. break;
  125. }
  126. }
  127.  
  128. }
  129.  
  130. }
  131. }
  132. return stk;
  133. }
  134.  
  135.  
  136.  
  137. }
Success #stdin #stdout 0s 131200KB
stdin
Standard input is empty
stdout
(*(+23)(-53))
data[0] : (
data[1] : *
data[2] : (
data[3] : +
data[4] : 2
data[5] : 3
data[6] : )
pop start
num save
pop : 3
num save
pop : 2
cal save+
cal go
cnt : 1
cal_data : 5
stk : (*5
data[7] : (
data[8] : -
data[9] : 5
data[10] : 3
data[11] : )
pop start
num save
pop : 3
num save
pop : 5
cal save-
cal go
cnt : 1
cal_data : 2
stk : (*52
data[12] : )
pop start
num save
pop : 2
num save
pop : 5
cal save*
cal go
cnt : 1
cal_data : 10
stk : 10

10