123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import java.util.*;
  2. import java.lang.*;
  3. /**
  4. * Write a description of class Calculator here.
  5. *
  6. * @author (your name)
  7. * @version (a version number or a date)
  8. */
  9. public class Calculator
  10. {
  11. //Checks the array for the operator b
  12. public static boolean OperatorChecker(String[] s, String b){
  13. for(int i = 0;i < s.length; i++){
  14. if(s[i].equals(b)){
  15. return true;
  16. }
  17. }
  18. return false;
  19. }
  20. //Inverts the current element of the num array
  21. public static void SignInvert(String[] op, Double[] num){
  22. for(int i = 0; i < op.length; i++){
  23. if(op[i].equals("sgn")){
  24. num[i] *= -1;
  25. Console.DeleteOp(0,op);
  26. Console.DisplayCurrent(num,op);
  27. Console.SaveParameter(Console.getStringInput(" "), op);
  28. }
  29. }
  30. }
  31. // Calculates the inversion of the double
  32. public static void Inverter(String[] op, Double[] num){
  33. for(int i = 0; i < op.length; i++){
  34. if(op[i].equals("inv")){
  35. num[i] = 1/num[i];
  36. Console.DeleteOp(0,op);
  37. Console.DisplayCurrent(num,op);
  38. Console.SaveParameter(Console.getStringInput("Enter operator"), op);
  39. }
  40. }
  41. }
  42. public static void MemStore(String[] op,Double[] num, Double store){
  43. for(int i = 0; i < op.length; i++){
  44. if(op[i].equals("m+")){
  45. store = num[i];
  46. Console.DeleteOp(0,op);
  47. Console.println(store + " is stored in memory");
  48. Console.SaveParameter(Console.getStringInput("Enter operator"), op);
  49. }
  50. else if(op[i].equals("mc")){
  51. store = 0.0;
  52. Console.DeleteOp(0,op);
  53. Console.println("MEMORY CLEARED");
  54. Console.SaveParameter(Console.getDoubleInput("Enter a number"), num);
  55. }
  56. else if(op[i].equals("mrc")){
  57. num[i] = store;
  58. Console.DeleteOp(0,op);
  59. Console.println(store + " ---> " + num[i]);
  60. Console.DisplayCurrent(num,op);
  61. Console.SaveParameter(Console.getStringInput("Enter operator"), op);
  62. }
  63. }
  64. }
  65. public static Double factorial(Double number){
  66. if(number == 1){
  67. return number;
  68. }
  69. number *= factorial(number-1);
  70. return number;
  71. }
  72. public static void FactorialFinish(String[] op, Double[] num){
  73. for(int i = 0; i < op.length; i++){
  74. if(op[i].equals("!")){
  75. if(num[i] == 1){
  76. num[i] = num[i];
  77. }
  78. num[i] *= factorial(num[i-1]);
  79. for(int j = i; j < op.length -1; j++){
  80. op[j] = op[j+1];
  81. }
  82. Console.DisplayCurrent(num,op);
  83. Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
  84. }
  85. }
  86. }
  87. public static void Trig(String[] op, Double[]num){
  88. for(int i = 0; i < num.length; i++){
  89. if(op[i].equals("sin")){
  90. num[i] = Math.sin(Math.toRadians(num[i]));
  91. Console.DeleteOp(i,op);
  92. Console.DisplayCurrent(num,op);
  93. Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
  94. }
  95. else if(op[i].equals("cos")){
  96. num[i] = Math.cos(Math.toRadians(num[i]));
  97. Console.DeleteOp(i,op);
  98. Console.DisplayCurrent(num,op);
  99. Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
  100. }
  101. else if(op[i].equals("tan")){
  102. num[i] = Math.tan(Math.toRadians(num[i]));
  103. Console.DeleteOp(i,op);
  104. Console.DisplayCurrent(num,op);
  105. Console.SaveParameter(Console.getStringInput("Enter an Operator"), op);
  106. }
  107. else{
  108. //Just passing through
  109. }
  110. }
  111. }
  112. public static void ClearMem(String[] op, Double[] num){
  113. for(int i = 0; i < op.length; i++){
  114. if(op[i].equals("clr")){
  115. Arrays.fill(num, null);
  116. Arrays.fill(op, "0");
  117. Console.DisplayCurrent(num,op);
  118. }
  119. }
  120. }
  121. //Does all the Basic computing in PEMDAS form.
  122. public static void BasicComputing(String[] op, Double[] num){
  123. if(Calculator.OperatorChecker(op,"=")){
  124. for(int check = 0; check < op.length; check++){
  125. for(int i = 0; i < num.length; i++){
  126. if(op[i].equals("^")){
  127. num[i] = Math.pow(num[i], num[i+1]);
  128. Console.ArrShifter(i,op, num);
  129. }
  130. else if(op[i].equals("rt")){
  131. num[i] = Math.pow(num[i], (1/num[i+1]));
  132. Console.ArrShifter(i,op, num);
  133. }
  134. else{
  135. //Just passing through
  136. }
  137. }
  138. for(int i = 0; i < num.length; i++){
  139. if(op[i].equals("*")){
  140. num[i] = (num[i] * num[i+1]);
  141. Console.ArrShifter(i,op, num);
  142. }
  143. else if(op[i].equals("/")){
  144. num[i] = (num[i] / num[i+1]);
  145. Console.ArrShifter(i,op, num);
  146. }
  147. else{
  148. //Just passing through
  149. }
  150. }
  151. for(int i = 0; i < num.length; i++){
  152. if(op[i].equals("+")){
  153. num[i] = (num[i] + num[i+1]);
  154. Console.ArrShifter(i,op, num);
  155. }
  156. else if(op[i].equals("-")){
  157. num[i] = (num[i] - num[i+1]);
  158. Console.ArrShifter(i,op, num);
  159. }
  160. else if (op[i].equals("=")) {
  161. for(int j = i; j < op.length -1; j++){
  162. op[j] = op[j+1];
  163. }
  164. break;
  165. }
  166. else{
  167. //Just passing through
  168. }
  169. Calculator.BasicComputing(op, num);
  170. }
  171. }
  172. }
  173. }
  174. }