fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6.  
  7. namespace DentakuConsole
  8. {
  9. class Program
  10. {
  11. public static readonly String OP_ADD = "+";
  12. public static readonly String OP_SUB = "-";
  13. public static readonly String OP_MUL = "*";
  14. public static readonly String OP_DIV = "/";
  15. public static readonly String OP_MOD = "%";
  16.  
  17. static void Main(string[] args)
  18. {
  19. try
  20. {
  21. var items = parse(Console.In);
  22.  
  23. var n1 = Double.Parse(items[0]);
  24. items.RemoveAt(0);
  25.  
  26. for (var et = items.GetEnumerator(); et.MoveNext();)
  27. {
  28. if (et.Current.Equals(OP_ADD))
  29. {
  30. if (et.MoveNext())
  31. {
  32. n1 = Dentaku.Add(n1, Double.Parse(et.Current));
  33. }
  34. }
  35. else if (et.Current.Equals(OP_SUB))
  36. {
  37. if (et.MoveNext())
  38. {
  39. n1 = Dentaku.Sub(n1, Double.Parse(et.Current));
  40. }
  41. }
  42. else if (et.Current.Equals(OP_MUL))
  43. {
  44. if (et.MoveNext())
  45. {
  46. n1 = Dentaku.Mul(n1, Double.Parse(et.Current));
  47. }
  48. }
  49. else if (et.Current.Equals(OP_DIV))
  50. {
  51. if (et.MoveNext())
  52. {
  53. n1 = Dentaku.Div(n1, Double.Parse(et.Current));
  54. }
  55. }
  56. else if (et.Current.Equals(OP_MOD))
  57. {
  58. if (et.MoveNext())
  59. {
  60. n1 = Dentaku.Mod(n1, Double.Parse(et.Current));
  61. }
  62. }
  63. else
  64. {
  65. throw new Exception("OP_ERROR");
  66. }
  67. }
  68.  
  69. Console.WriteLine("result=" + n1);
  70. }
  71. catch (Exception e)
  72. {
  73. Debug.WriteLine(e);
  74. }
  75. }
  76.  
  77. private static List<String> parse(TextReader reader)
  78. {
  79. var items = new List<String>();
  80.  
  81. var sb = new StringBuilder();
  82. var i = -1;
  83.  
  84. while ((i = reader.Read()) != -1)
  85. {
  86. switch (i)
  87. {
  88. case '\r':
  89. case '\n':
  90. if (sb.Length != 0)
  91. {
  92. items.Add(sb.ToString());
  93. }
  94. return items;
  95. case ' ':
  96. case ' ':
  97. case ',':
  98. case ',':
  99. break;
  100. case '0':
  101. case '1':
  102. case '2':
  103. case '3':
  104. case '4':
  105. case '5':
  106. case '6':
  107. case '7':
  108. case '8':
  109. case '9':
  110. sb.Append(i - '0');
  111. break;
  112. case '0':
  113. case '1':
  114. case '2':
  115. case '3':
  116. case '4':
  117. case '5':
  118. case '6':
  119. case '7':
  120. case '8':
  121. case '9':
  122. sb.Append(i - '0');
  123. break;
  124. case '.':
  125. case '.':
  126. sb.Append('.');
  127. break;
  128. case '+':
  129. case '+':
  130. if (sb.Length == 0)
  131. {
  132. sb.Append('0');
  133. }
  134. items.Add(sb.ToString());
  135. sb.Clear();
  136. items.Add(OP_ADD);
  137. break;
  138. case '-':
  139. case '-':
  140. if (sb.Length == 0)
  141. {
  142. sb.Append('0');
  143. }
  144. items.Add(sb.ToString());
  145. sb.Clear();
  146. items.Add(OP_SUB);
  147. break;
  148. case '*':
  149. case '*':
  150. case '×':
  151. items.Add(sb.ToString());
  152. sb.Clear();
  153. items.Add(OP_MUL);
  154. break;
  155. case '/':
  156. case '/':
  157. case '÷':
  158. items.Add(sb.ToString());
  159. sb.Clear();
  160. items.Add(OP_DIV);
  161. break;
  162. case '%':
  163. case '%':
  164. items.Add(sb.ToString());
  165. sb.Clear();
  166. items.Add(OP_MOD);
  167. break;
  168. }
  169. }
  170.  
  171. if (sb.Length != 0)
  172. {
  173. items.Add(sb.ToString());
  174. }
  175. return items;
  176. }
  177. }
  178.  
  179. public class Dentaku
  180. {
  181. public static double Add(double n1, double n2)
  182. {
  183. return n1 + n2;
  184. }
  185.  
  186. public static double Sub(double n1, double n2)
  187. {
  188. return n1 - n2;
  189. }
  190.  
  191. public static double Mul(double n1, double n2)
  192. {
  193. return n1 * n2;
  194. }
  195.  
  196. public static double Div(double n1, double n2)
  197. {
  198. if (n2 == 0)
  199. {
  200. throw new Exception("Div NaN");
  201. }
  202.  
  203. return n1 / n2;
  204. }
  205.  
  206. public static double Mod(double n1, double n2)
  207. {
  208. if (n2 == 0)
  209. {
  210. throw new Exception("Mod NaN");
  211. }
  212.  
  213. return n1 % n2;
  214. }
  215. }
  216. }
  217.  
Success #stdin #stdout 0.04s 23952KB
stdin
Standard input is empty
stdout
Standard output is empty