Guest User

Untitled

a guest
Dec 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Calculator
  5. {
  6. static int totaltimes = 0, successtimes = 0, steps = 0, errortimes = 0;
  7. static File times=new File("times.txt");
  8. static Stack<Character> operator = new Stack<>();
  9. public static int Calculate(char op, int a1, int a2) throws Exception
  10. {
  11. switch(op)
  12. {
  13. case '+':
  14. steps++;
  15. System.out.print("第"+steps+"步"+":");
  16. System.out.printf("%d%c%d%c%d",a2,op,a1,'=',a2+a1);
  17. System.out.println();
  18. return a2 + a1;
  19. case '-':
  20. steps++;
  21. System.out.print("第"+steps+"步"+":");
  22. System.out.printf("%d%c%d%c%d",a2,op,a1,'=',a2-a1);
  23. System.out.println();
  24. return a2 - a1;
  25. case '*':
  26. steps++;
  27. System.out.print("第"+steps+"步"+":");
  28. System.out.printf("%d%c%d%c%d",a2,op,a1,'=',a2*a1);
  29. System.out.println();
  30. return a2 * a1;
  31. case '/':
  32. steps++;
  33. if(a1==0)
  34. {
  35. errortimes++;
  36. BufferedWriter bwt1=new BufferedWriter(new FileWriter(times));
  37. bwt1.write(totaltimes+""+successtimes+""+errortimes+"");
  38. bwt1.flush();
  39. bwt1.close();
  40. throw new Exception("Illegal parameter!");
  41. }
  42. System.out.print("第"+steps+"步"+":");
  43. System.out.printf("%d%c%d%c%d",a2,op,a1,'=',a2/a1);
  44. System.out.println();
  45. return a2 / a1;
  46. default:
  47. Integer.valueOf(-0);
  48. }
  49. errortimes++;
  50. BufferedWriter bwt2=new BufferedWriter(new FileWriter(times));
  51. bwt2.write(totaltimes+""+successtimes+""+errortimes+"");
  52. bwt2.flush();
  53. bwt2.close();
  54. throw new Exception("Illegal operator!");
  55. }
  56. public static int GetResult(String expr) throws Exception
  57. {
  58. Stack<Integer> number= new Stack<>();
  59. char[] arr = expr.toCharArray();
  60. int len = arr.length;
  61. for(int i = 0; i < len; i++){
  62. Character ch = arr[i];
  63. if(ch >= '0' && ch <= '9')
  64. number.push(Integer.valueOf(ch - '0'));
  65. else
  66. number.push(Calculate(ch, number.pop(), number.pop()));
  67. }
  68. return number.pop();
  69. }
  70. public static String Transform(String expression)
  71. {
  72. char[] arr = expression.toCharArray();
  73. int len = arr.length;
  74. String res = "";
  75. for(int i = 0; i < len; i++){
  76. char temp = arr[i];
  77. if(temp == ' ')
  78. continue;
  79. if(temp >= '0' && temp <= '9')
  80. {
  81. res+=temp;
  82. continue;
  83. }
  84. if(temp == '(')
  85. operator.push(temp);
  86. if(temp == '+' || temp == '-')
  87. {
  88. while(!operator.empty() && (operator.peek() != '('))
  89. res+= operator.pop();
  90. operator.push(temp);
  91. continue;
  92. }
  93. if(temp == '*' || temp == '/')
  94. {
  95. while(!operator.empty() && (operator.peek() == '*' || operator.peek() == '/'))
  96. res+= operator.pop();
  97. operator.push(temp);
  98. continue;
  99. }
  100. if(temp == ')')
  101. {
  102. while(!operator.empty() && (operator.peek() != '('))
  103. res += operator.pop();
  104. operator.pop();
  105. continue;
  106. }
  107. }
  108. while(!operator.empty())
  109. res += operator.pop();
  110. return res;
  111. }
  112.  
  113. public static void main(String[] args) throws Exception
  114. {
  115. Scanner s = new Scanner(System.in);
  116. String str =null;
  117. while(true)
  118. {
  119. str=s.next();
  120. if(str.equals("end"))
  121. {
  122. System.out.println("EXIT");
  123. break;
  124. }
  125. if (!times.exists())
  126. times.createNewFile();
  127. File expression = new File("expression.txt");
  128. BufferedReader readline = new BufferedReader(new FileReader(times));
  129. if (readline.readLine() != null) {
  130. //read
  131. BufferedReader br = new BufferedReader(new FileReader(times));
  132. String string = br.readLine();
  133. String[] newstring = string.split("");
  134. totaltimes = Integer.valueOf(newstring[0]);
  135. successtimes = Integer.valueOf(newstring[1]);
  136. errortimes = Integer.valueOf(newstring[2]);
  137. br.close();
  138. }
  139.  
  140.  
  141.  
  142. totaltimes++;
  143.  
  144. //write expression
  145. BufferedWriter bw = new BufferedWriter(new FileWriter(expression));
  146. bw.write(str);
  147. bw.flush();
  148. bw.close();
  149.  
  150.  
  151. steps=0;
  152. System.out.println("总共运行" + totaltimes + "次;" + "运行成功" + successtimes + "次;" + "错误运行" + errortimes + "次。");
  153. System.out.println(GetResult(Transform(str)));
  154. successtimes++;
  155.  
  156.  
  157. //write times
  158. BufferedWriter bwt = new BufferedWriter(new FileWriter(times));
  159. bwt.write(totaltimes + "" + successtimes + "" + errortimes + "");
  160. bwt.flush();
  161. bwt.close();
  162. }
  163. s.close();
  164. //5+2*(3*(3-1*2+1))
  165. }
  166. }
Add Comment
Please, Sign In to add comment