Advertisement
Guest User

wag3

a guest
Oct 18th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.calculator.cs37;
  2. import java.awt.EventQueue;
  3.  
  4. import java.util.EventObject;
  5. import javax.swing.JTextArea;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JTextField;
  9. import javax.swing.JLabel;
  10. import java.awt.Font;
  11. import javax.swing.JButton;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14. import javax.swing.SwingConstants;
  15.  
  16. import java.io.File;
  17.  
  18. import java.io.FileNotFoundException;
  19. import java.io.FileReader;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.PrintWriter;
  24. import java.io.UnsupportedEncodingException;
  25. import java.io.BufferedReader;
  26. import java.util.ArrayList;
  27. import javax.management.RuntimeErrorException;
  28. import java.util.Arrays;
  29. import java.util.List;
  30. import java.util.ArrayList;
  31.  
  32. public class Calc implements Calculator {
  33. String operator = null;
  34. double [] n = new double[2];
  35. double ans = 0;
  36. String nums1="" ;
  37. String nums2="" ;
  38. String expression="" ;
  39. String result="" ;
  40. int current1 = -1,prev=0,next=0 ;
  41. int shift = 0 ;
  42. String [] process = new String [5];
  43.  
  44.  
  45. File file ;
  46. public void input(String s) {
  47. if (!s.matches("^-?\\d+(\\.\\d+)?(\\s+)?[-+*\\/](\\s+)?-?\\d+(\\.\\d+)?(\\s+)?$")) {//^ start of the line , $ end of the line , d digit , [..] any one ,
  48. operator = "false" ;
  49. //throw new RuntimeException("you enterd non valid expression");
  50. }else {
  51. if (current1 < 4 ) {
  52. current1 ++ ;
  53. }else { // shifting
  54. shift = 1 ;
  55. current1 = 4 ;
  56. }
  57. //remove spaces
  58. s = s.replaceAll("//s+", "");
  59. String [] nums = s.split("[-+*\\/]");
  60. String [] operat = s.split("\\d+(\\.\\d+)?");
  61. operator = operat[1].charAt(0)+"";
  62. int i = 0 ;
  63. for (String number : nums) {
  64. if (!number.isEmpty()) {
  65. if (i==0 && s.charAt(0)=='-') { //negative
  66. number="-"+number ;
  67. n[i] = Double.parseDouble(number);
  68. }else if (i == 1 && (nums[1].equals("") || nums.length == 4)) { //negative
  69. number = "-" + number;
  70. n[i] = Double.parseDouble(number);
  71. }
  72.  
  73. else {
  74. n[i] = Double.parseDouble(number);
  75.  
  76. }
  77. i++;
  78. }
  79.  
  80.  
  81. }
  82. }
  83.  
  84.  
  85. }
  86. /* Return the result of the current operations or throws a runtime
  87. exception */
  88. public String getResult() {
  89. if (operator.equals("false")) {
  90. throw new RuntimeException ("You Enterd non valid expression");
  91. }else if (operator.charAt(0) == '+') {
  92. ans = n[0]+n[1];
  93.  
  94. }else if (operator.charAt(0) == '*') {
  95. ans = n[0]*n[1];
  96. }else if (operator.charAt(0) == '-') {
  97. ans = n[0]-n[1];
  98. }else if (operator.charAt(0) == '/') {
  99. if (n[1] != 0) {
  100. ans = n[0]/n[1];
  101. }else {
  102. throw new RuntimeException ("You can't divide by 0");
  103. }
  104.  
  105. }
  106. //answer = String.format("%.16f", result);
  107. result = String.format("%.16f", ans);
  108. // result = Double.toString(ans);
  109. nums1 = Double.toString(n[0]);
  110. nums2=Double.toString(n[1]);
  111. expression=nums1+operator + nums2+"=" +result ;
  112. if (shift == 0 && current1 > -1) {
  113. process[current1]=expression;
  114. }else if (shift == 1){ // shifting
  115. for (int i=0 ; i<4 ; i++) {
  116. process[i] =process[i+1];
  117. }
  118. process[4] = expression ;
  119. }
  120. return result ;
  121. }
  122. /* return the current formula */
  123. public String current () {
  124. if (current1 < -1 || current1 > 4) {
  125. return null ;
  126. }else {
  127. return process[current1];
  128. }
  129. }
  130. /* return the last operation in String format, or Null if no more history
  131. available, will update current */
  132. public String prev() {
  133. if (current1 == -1 ) {
  134. throw new RuntimeException ("You Enterd only one");
  135. }else {
  136. prev = current1 - 1;
  137. next = current1 ;
  138. current1 = prev ;
  139. }
  140. return process[current1];
  141. }
  142. /* return the next operation in String format, or Null if no more history
  143. available, will update current */
  144. public String next() {
  145. if (current1 == 4) {
  146. throw new RuntimeException ("there is no next operation of this ");
  147. }else {
  148. next = current1 + 1 ;
  149. prev = current1 ;
  150. current1 = next ;
  151. }
  152. return process[current1];
  153. }
  154. /* Save in file the last 5 done Operations */
  155. public void save() {
  156. File file = new File ("hendo.txt");
  157. PrintWriter pw ;
  158. try {
  159. pw= new PrintWriter (file);
  160. for (int i=0 ; i<=current1 ; i++) {
  161. pw.write(process[i]);
  162. pw.write("\n");
  163. }
  164. pw.close();
  165.  
  166.  
  167. } catch (IOException e) {
  168. // TODO Auto-generated catch block
  169. e.printStackTrace();
  170. }
  171. }
  172.  
  173.  
  174. /* Load from file the saved operations */
  175. public void load()
  176. {
  177.  
  178. List<String> temp = Arrays.asList(process);
  179. try {
  180. FileReader fr = new FileReader("hendo.txt");
  181. int i;
  182. try {
  183. String st = "";
  184. String[] s1 = new String[2];
  185. temp.clear();
  186. current1 = -1;
  187. int index=0;
  188. while ((i = fr.read()) != -1) {
  189. if(index!=0&&i=='\n') {
  190. s1[1]= getResult() ;
  191. }
  192.  
  193. if (i != '\n') {
  194. st = st + (char) i;
  195. } else {
  196. index++;
  197. input(st);
  198. s1[0] = st;
  199. st = "";
  200. }
  201. }
  202. prev = temp.size() ;
  203. } catch (IOException e) {
  204. // TODO Auto-generated catch block
  205. e.printStackTrace();
  206. }
  207. } catch (FileNotFoundException e) {
  208. // TODO Auto-generated catch block
  209. e.printStackTrace();
  210. }
  211.  
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement