Calculator.java 10KB

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