fork(2) download
  1. import java.util.Scanner;
  2.  
  3. class Ideone {
  4.  
  5. public static void main (String[] args) throws java.lang.Exception {
  6. Scanner in = new Scanner(System.in);
  7.  
  8. // 非负整数 小于10000
  9. // 2+3*4-2+5+3*2-100
  10. String line;
  11. String right;
  12. String left;
  13. String middle;
  14. String sign1;
  15. String sign2;
  16.  
  17. line = in.nextLine();
  18. int lineLength = line.length();
  19.  
  20. if (lineLength > 0) {
  21. do {
  22.  
  23. // 获取最右边的加减号
  24. int addIndex = line.lastIndexOf("+");
  25. int subIndex = line.lastIndexOf("-");
  26. int mulIndex = line.lastIndexOf("*");
  27. int divIndex = line.lastIndexOf("/");
  28.  
  29. // 加减乘除 不存在,
  30. // 或者只有最前面的负号, 则终止循环, 输出结果
  31. if (addIndex == -1 && mulIndex == -1 && divIndex == -1) { // end start
  32. if (subIndex == -1) {
  33. break;
  34. } else if (subIndex != -1) {
  35. int subSecondIndex = line.lastIndexOf("-", subIndex - 1);
  36. if (subIndex == 0 && subSecondIndex == -1) {
  37. break;
  38. }
  39. }
  40. } // end end
  41.  
  42. if (addIndex == -1 && subIndex == -1) { // 加减 无
  43. if (divIndex == -1 && mulIndex == -1) { // 加减 乘除 无
  44. System.out.println(line);
  45. } else { // 乘除 只有
  46. line = getMulDivResult(line);
  47. }
  48. } else { // 加减 有
  49. // 以最右边的加减号为断点, 开始截取字符串
  50. int asIndex = getMaxIndex(addIndex, subIndex);
  51.  
  52. if (divIndex == -1 && mulIndex == -1) { // 加减 只有
  53. line = getAddSubResult(line);
  54. } else { // 加减乘除 都有
  55. // 从最后一个加减号截取来的字符串
  56. String str2 = line.substring(asIndex + 1);
  57.  
  58. middle = String.valueOf(line.charAt(asIndex));
  59. left = line.substring(0, asIndex);
  60. int mdIndex = getMaxIndex(mulIndex, divIndex);
  61.  
  62. right = str2;
  63. if (mdIndex > asIndex) { // 在最后一个加减号的后面还存在乘除
  64. right = getMulDivResult(str2);
  65. line = left + middle + right;
  66. } else if (mdIndex < asIndex) { // 在最后一个加减号的后面, 只是单个数字
  67. // 查找最后一个加减号之前, 即紧挨着的加减号, 截取其中的乘除字符串
  68.  
  69. int add2Index = line.lastIndexOf("+", asIndex - 1);
  70. int sub2Index = line.lastIndexOf("-", asIndex - 1);
  71. // 在最后一个加减号, 左边全是乘除, 右边随意
  72. if (add2Index == -1 && sub2Index == -1) {
  73. left = line.substring(0, asIndex);
  74. left = getMulDivResult(left);
  75. right = line.substring(asIndex + 1);
  76. middle = String.valueOf(line.charAt(asIndex));
  77. line = left + middle + right;
  78. } else { // 有加减 有乘除
  79. int leftIndex = getMaxIndex(line.lastIndexOf("+", mdIndex - 1),
  80. line.lastIndexOf("-", mdIndex - 1));
  81. int rightIndex = getMinIndex(line.indexOf("+", mdIndex + 1),
  82. line.indexOf("-", mdIndex + 1));
  83. if (leftIndex != -1) {
  84. sign1 = String.valueOf(line.charAt(leftIndex));
  85. left = line.substring(0, leftIndex);
  86. } else {
  87. sign1 = "";
  88. left = "";
  89. }
  90. if (rightIndex != -1) {
  91. sign2 = String.valueOf(line.charAt(rightIndex));
  92. } else {
  93. sign2 = "";
  94. }
  95.  
  96. middle = line.substring(leftIndex + 1, rightIndex);
  97. middle = getMulDivResult(middle);
  98. right = line.substring(rightIndex + 1);
  99.  
  100. line = left + sign1 + middle + sign2 + right;
  101.  
  102. }
  103.  
  104. }
  105.  
  106. }
  107. }
  108. } while (line.indexOf("+") != -1 || line.indexOf("-") != -1 || line.indexOf("*") != -1
  109. || line.indexOf("/") != -1);
  110. System.out.println(line);
  111. } // end if
  112.  
  113. in.close();
  114.  
  115. // 提示:一种可能的做法是,将整个式子作为字符串读入,
  116. // 然后找出其中最后一个+或-,在此位置将其截成两段,
  117. // 分别计算后再做+或-,以此类推。
  118. // 另,用Integer.parseInt(s)可以从一个字符串得到整数。
  119. }
  120.  
  121. /**
  122.   * 单纯乘除计算
  123.   *
  124.   * @param line
  125.   * @return
  126.   */
  127. public static String getMulDivResult(String line) {
  128. String retStr = null;
  129.  
  130. String leftStr = null;
  131. String endStr = null;
  132.  
  133. do {
  134. if (endStr != null) {
  135. line = leftStr + endStr;
  136. }
  137. int index = getMinIndex(line.indexOf("*"), line.indexOf("/"));
  138. String sign = String.valueOf(line.charAt(index));
  139. leftStr = line.substring(0, index);
  140. int index2 = getMinIndex(line.indexOf("*", index + 1), line.indexOf("/", index + 1));
  141. String rightStr = null;
  142. if (index2 != -1) {
  143. rightStr = line.substring(index + 1, index2);
  144. endStr = line.substring(index2);
  145. } else if (index2 == -1) {
  146. rightStr = line.substring(index + 1);
  147. endStr = line.substring(index + rightStr.length() + 1);
  148. }
  149.  
  150. switch (sign) {
  151. case "*":
  152. leftStr = String.valueOf(Integer.parseInt(leftStr) * Integer.parseInt(rightStr));
  153. break;
  154. case "/":
  155. leftStr = String.valueOf(Integer.parseInt(leftStr) / Integer.parseInt(rightStr));
  156. break;
  157. }
  158. retStr = leftStr + endStr;
  159.  
  160. } while (endStr.length() > 1);
  161.  
  162. return retStr;
  163. }
  164.  
  165. /**
  166.   * 单纯加减计算
  167.   *
  168.   * @param line
  169.   * 10-4+5
  170.   * @return
  171.   */
  172. public static String getAddSubResult(String line) {
  173. String retStr = null;
  174.  
  175. String leftStr = null;
  176. String endStr = null;
  177.  
  178. do {
  179. if (endStr != null) {
  180. line = leftStr + endStr;
  181. }
  182. int index = getMinIndex(line.indexOf("+"), line.indexOf("-"));
  183. if (index == 0) {
  184. index = getMinIndex(line.indexOf("+", index + 1), line.indexOf("-", index + 1));
  185. }
  186. String sign = String.valueOf(line.charAt(index));
  187. leftStr = line.substring(0, index);
  188.  
  189. int index2 = getMinIndex(line.indexOf("+", index + 1), line.indexOf("-", index + 1));
  190. String rightStr = null;
  191. if (index2 != -1) {
  192. rightStr = line.substring(index + 1, index2);
  193. endStr = line.substring(index2);
  194. } else if (index2 == -1) {
  195. rightStr = line.substring(index + 1);
  196. endStr = line.substring(index + rightStr.length() + 1);
  197. }
  198.  
  199. switch (sign) {
  200. case "+":
  201. leftStr = String.valueOf(Integer.parseInt(leftStr) + Integer.parseInt(rightStr));
  202. break;
  203. case "-":
  204. leftStr = String.valueOf(Integer.parseInt(leftStr) - Integer.parseInt(rightStr));
  205. break;
  206. }
  207. retStr = leftStr + endStr;
  208.  
  209. } while (endStr.length() > 1);
  210.  
  211. return retStr;
  212. }
  213.  
  214. /**
  215.   * 获取非-1, 较大数字的下标
  216.   *
  217.   * @param index1
  218.   * @param index2
  219.   * @return
  220.   */
  221. public static int getMaxIndex(int index1, int index2) {
  222. int max = -1; // 提前假设两者都是-1
  223. if (index1 > index2) {
  224. max = index1;
  225. }
  226. if (index1 < index2) {
  227. max = index2;
  228. }
  229. return max;
  230. }
  231.  
  232. /**
  233.   * 获取非-1, 较小数字的下标
  234.   *
  235.   * @param index1
  236.   * @param index2
  237.   * @return
  238.   */
  239. public static int getMinIndex(int index1, int index2) {
  240. int ret = 1;
  241. if (index1 == -1) {
  242. ret = index2;
  243. } else if (index2 == -1) {
  244. ret = index1;
  245. } else if (index1 > index2) {
  246. ret = index2;
  247. } else if (index1 < index2) {
  248. ret = index1;
  249. }
  250. return ret;
  251.  
  252. }
  253.  
  254. }
Success #stdin #stdout 0.07s 4575232KB
stdin
1+1
stdout
2