Calculator.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import java.math.BigInteger;
  2. public class Calculator
  3. {
  4. public static void runCommandLoop() {
  5. Display display = new Display();
  6. Memory memory = new Memory();
  7. memory.resetMemory();
  8. DisplayMode conversion = new DisplayMode();
  9. int modeOrdinal = 0;
  10. int trigMode = 0; //0 - degrees (default); 1 - radians
  11. String initial = Console.getStringInput("Enter number");
  12. display.setDisplay(initial);
  13. while (true) {
  14. String command = Console.getStringInput("Choose a command");
  15. if (command.equalsIgnoreCase("off") || command.equalsIgnoreCase("exit")) {
  16. Console.println("Thank you for using our product!");
  17. break;
  18. }
  19. if (command.equalsIgnoreCase("c") || command.equalsIgnoreCase("clear")) {
  20. display.setDisplay("0");
  21. Console.println(display.getDisplay());
  22. String resetValue = Console.getStringInput("Enter number");
  23. display.setDisplay(resetValue);
  24. }
  25. if (command.equalsIgnoreCase("+") || command.equalsIgnoreCase("add")) {
  26. double first = Double.parseDouble(display.getDisplay());
  27. double second = Console.getDoubleInput("Enter number");
  28. double out = Core.add(first, second);
  29. Console.println(Double.toString(out));
  30. display.setDisplay(Double.toString(out));
  31. }
  32. if (command.equalsIgnoreCase("-") || command.equalsIgnoreCase("subtract")) {
  33. double first = Double.parseDouble(display.getDisplay());
  34. double second = Console.getDoubleInput("Enter number");
  35. double out = Core.subtract(first, second);
  36. Console.println(Double.toString(out));
  37. display.setDisplay(Double.toString(out));
  38. }
  39. if (command.equalsIgnoreCase("*") || command.equalsIgnoreCase("multiply")) {
  40. double first = Double.parseDouble(display.getDisplay());
  41. double second = Console.getDoubleInput("Enter number");
  42. double out = Core.multiply(first, second);
  43. Console.println(Double.toString(out));
  44. display.setDisplay(Double.toString(out));
  45. }
  46. if (command.equalsIgnoreCase("/") || command.equalsIgnoreCase("divide")) {
  47. double first = Double.parseDouble(display.getDisplay());
  48. double second = Console.getDoubleInput("Enter number");
  49. double out = Core.divide(first, second);
  50. if (Double.toString(out) == "NaN" || Double.toString(out) == "Infinity" || Double.toString(out) == "-Infinity") {
  51. display.errorDisplay();
  52. } else {
  53. Console.println(Double.toString(out));
  54. display.setDisplay(Double.toString(out));
  55. }
  56. }
  57. if (command.equalsIgnoreCase("sq")) {
  58. double first = Double.parseDouble(display.getDisplay());
  59. double out = Core.multiply(first, first);
  60. Console.println(Double.toString(out));
  61. display.setDisplay(Double.toString(out));
  62. }
  63. if (command.equalsIgnoreCase("sqrt")) {
  64. double first = Double.parseDouble(display.getDisplay());
  65. double out = Core.squareRoot(first);
  66. Console.println(Double.toString(out));
  67. display.setDisplay(Double.toString(out));
  68. }
  69. if (command.equalsIgnoreCase("^") || command.equalsIgnoreCase("pow")) {
  70. double first = Double.parseDouble(display.getDisplay());
  71. double second = Console.getDoubleInput("Enter number");
  72. double out = Core.exponentiation(first, second);
  73. Console.println(Double.toString(out));
  74. display.setDisplay(Double.toString(out));
  75. }
  76. if (command.equalsIgnoreCase("/x") || command.equalsIgnoreCase("inverse")) {
  77. double first = Double.parseDouble(display.getDisplay());
  78. double out = Core.inverse(first);
  79. if (Double.toString(out) == "NaN" || Double.toString(out) == "Infinity" || Double.toString(out) == "-Infinity") {
  80. display.errorDisplay();
  81. } else {
  82. Console.println(Double.toString(out));
  83. display.setDisplay(Double.toString(out));
  84. }
  85. }
  86. if (command.equalsIgnoreCase("+-") || command.equalsIgnoreCase("reverse")) {
  87. double first = Double.parseDouble(display.getDisplay());
  88. double out = Core.reverse(first);
  89. Console.println(Double.toString(out));
  90. display.setDisplay(Double.toString(out));
  91. }
  92. if (command.equalsIgnoreCase("m+")) {
  93. memory.addMemory(display.getDisplay());
  94. display.setDisplay(memory.recallMemory());
  95. Console.println(display.getDisplay());
  96. }
  97. if (command.equalsIgnoreCase("mrc")) {
  98. Console.println(memory.recallMemory());
  99. display.setDisplay(memory.recallMemory());
  100. }
  101. if (command.equalsIgnoreCase("mc")) {
  102. memory.resetMemory();
  103. Console.println(display.getDisplay());
  104. }
  105. if (command.equalsIgnoreCase("bin") || command.equalsIgnoreCase("binary")) {
  106. Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Binary, display.getDisplay()));
  107. }
  108. if (command.equalsIgnoreCase("hex") || command.equalsIgnoreCase("hexadecimal")) {
  109. Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Hexadecimal, display.getDisplay()));
  110. }
  111. if (command.equalsIgnoreCase("oct") || command.equalsIgnoreCase("octal")) {
  112. Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Octal, display.getDisplay()));
  113. }
  114. if (command.equalsIgnoreCase("dec") || command.equalsIgnoreCase("decimal")) {
  115. Console.println(conversion.switchDisplayMode(DisplayMode.Mode.Decimal, display.getDisplay()));
  116. }
  117. if (command.equalsIgnoreCase("switch")) {
  118. Console.println(conversion.switchDisplayMode(DisplayMode.Mode.values()[modeOrdinal], display.getDisplay()));
  119. if (modeOrdinal < 3) {
  120. modeOrdinal++;
  121. } else
  122. {
  123. modeOrdinal = 0;
  124. }
  125. }
  126. if (command.equalsIgnoreCase("round") || command.equalsIgnoreCase("R")) {
  127. double out = Custom.round(Double.parseDouble(display.getDisplay()));
  128. Console.println(Double.toString(out));
  129. display.setDisplay(Double.toString(out));
  130. }
  131. if (command.equalsIgnoreCase("abs")) {
  132. double out = Custom.abs(Double.parseDouble(display.getDisplay()));
  133. Console.println(Double.toString(out));
  134. display.setDisplay(Double.toString(out));
  135. }
  136. if (command.equalsIgnoreCase("deg")) {
  137. trigMode = 0;
  138. Console.println("Trigonometric units are degrees");
  139. }
  140. if (command.equalsIgnoreCase("rad")) {
  141. trigMode = 1;
  142. Console.println("Trigonometric units are radians");
  143. }
  144. if (command.equalsIgnoreCase("trigSwitch")) {
  145. if (trigMode == 0) {
  146. trigMode = 1;
  147. Console.println("Trigonometric units are radians");
  148. } else
  149. {
  150. trigMode = 0;
  151. Console.println("Trigonometric units are degrees");
  152. }
  153. }
  154. if (command.equalsIgnoreCase("sin")) {
  155. double first = Double.parseDouble(display.getDisplay());
  156. double out = Scientific.sine(first, trigMode);
  157. if (Double.toString(out) == "NaN") {
  158. display.errorDisplay();
  159. } else {
  160. Console.println(Double.toString(out));
  161. display.setDisplay(Double.toString(out));
  162. }
  163. }
  164. if (command.equalsIgnoreCase("cos")) {
  165. double first = Double.parseDouble(display.getDisplay());
  166. double out = Scientific.cosine(first, trigMode);
  167. if (Double.toString(out) == "NaN") {
  168. display.errorDisplay();
  169. } else {
  170. Console.println(Double.toString(out));
  171. display.setDisplay(Double.toString(out));
  172. }
  173. }
  174. if (command.equalsIgnoreCase("tan")) {
  175. double first = Double.parseDouble(display.getDisplay());
  176. double out = Scientific.tangent(first, trigMode);
  177. if (Double.toString(out) == "NaN") {
  178. display.errorDisplay();
  179. } else {
  180. Console.println(Double.toString(out));
  181. display.setDisplay(Double.toString(out));
  182. }
  183. }
  184. if (command.equalsIgnoreCase("asin")) {
  185. double first = Double.parseDouble(display.getDisplay());
  186. double out = Scientific.inverseSin(first, trigMode);
  187. if (Double.toString(out) == "NaN") {
  188. display.errorDisplay();
  189. } else {
  190. Console.println(Double.toString(out));
  191. display.setDisplay(Double.toString(out));
  192. }
  193. }
  194. if (command.equalsIgnoreCase("acos")) {
  195. double first = Double.parseDouble(display.getDisplay());
  196. double out = Scientific.inverseCosine(first, trigMode);
  197. if (Double.toString(out) == "NaN") {
  198. display.errorDisplay();
  199. } else {
  200. Console.println(Double.toString(out));
  201. display.setDisplay(Double.toString(out));
  202. }
  203. }
  204. if (command.equalsIgnoreCase("atan")) {
  205. double first = Double.parseDouble(display.getDisplay());
  206. double out = Scientific.inverseTangent(first, trigMode);
  207. if (Double.toString(out) == "NaN") {
  208. display.errorDisplay();
  209. } else {
  210. Console.println(Double.toString(out));
  211. display.setDisplay(Double.toString(out));
  212. }
  213. }
  214. if (command.equalsIgnoreCase("fact") || command.equalsIgnoreCase("x!")) {
  215. double first = Custom.round(Double.parseDouble(display.getDisplay()));
  216. BigInteger out = Scientific.factorial(first);
  217. Console.println(out.toString());
  218. display.setDisplay(out.toString());
  219. }
  220. }
  221. }
  222. }