Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.calculator;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.PrintWriter;
  6. import java.util.Scanner;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class operation implements Calculator {
  11. static int theEnd=0;
  12. private double firstValued;
  13. private double secondValued;
  14. private int operator; // 1 for add 2 for substruction 3 for multiplication 4 for division
  15. private String result;
  16. private Boolean error;
  17. private String errorM = "";
  18. private String cuF;
  19. //private static String[] LastArray = new String[5];
  20. private static String[] ArrayHere = new String[5];
  21. private static int sizeArrayHere=0;
  22. // private static int sizeOfOpreation;
  23.  
  24. operation() {
  25. operator = 0;
  26. result = "";
  27. error = false;
  28. // for (int i = 0; i < 5; i++) LastArray[i] = "";
  29. // sizeOfOpreation=0;
  30. //load();
  31. }
  32.  
  33. @Override
  34. public void input(String s) {
  35. String sp = "^(-?[0-9]*?|[0-9]*?\\.[0-9]*?)([+\\-*/])(-?[0-9]*?|[0-9]*?\\.[0-9]*)(=.)?";
  36. Pattern pattern = Pattern.compile(sp);
  37. Matcher matcher = pattern.matcher(s);
  38.  
  39. if (matcher.matches()) {
  40. cuF = s;
  41. String v = matcher.group(1);
  42. String v2 = matcher.group(2);
  43. String v3 = matcher.group(3);
  44. if(v.length()==0||v3.length()==0){
  45. error = true;
  46. errorM = "Wrong Input";
  47. return;
  48. }
  49. String p2 = "[0-9]*?\\.[0-9]*?";
  50.  
  51. firstValued = Double.parseDouble(v);
  52. secondValued = Double.parseDouble(v3);
  53.  
  54. switch (v2) {
  55. case "+":
  56. operator = 1;
  57. break;
  58. case "-":
  59. operator = 2;
  60. break;
  61. case "*":
  62. operator = 3;
  63. break;
  64. case "/":
  65. if ( secondValued == 0) {
  66. error = true;
  67. errorM = "Can't divide by 0";
  68. } else operator = 4;
  69. break;
  70. }
  71. } else {
  72. error = true;
  73. errorM = "Wrong Input";
  74. }
  75.  
  76. }
  77.  
  78.  
  79. @Override
  80. public String getResult() {
  81. if (!error) {
  82. double r = op(firstValued, secondValued);
  83. result=Double.toString(r);
  84. sA();
  85. // save();
  86. // save();
  87. return result;
  88. } else {
  89. return errorM;
  90. }
  91. }
  92.  
  93. private void sA() {
  94. if(sizeArrayHere<5){
  95. ArrayHere[sizeArrayHere++]=cuF;
  96. // System.out.println(ArrayHere[sizeArrayHere-1]);
  97. return;
  98. }
  99. for(int i=0;i<4;i++){
  100. ArrayHere[i]=ArrayHere[i+1];
  101. }
  102. ArrayHere[4]=cuF;
  103. }
  104.  
  105.  
  106.  
  107. // public int getSize(){
  108. // return sizeOfOpreation;
  109. // }
  110. // public String getElement(int x){
  111. // return LastArray[x-1] ;
  112. // }
  113.  
  114.  
  115.  
  116.  
  117. private double op(double firstValued, double secondValued) {
  118. if (operator == 1) return firstValued + secondValued;
  119. if (operator == 2) return firstValued - secondValued;
  120. if (operator == 3) return firstValued * secondValued;
  121. else return firstValued / secondValued;
  122. }
  123.  
  124.  
  125. @Override
  126. public String current() {
  127. if(theEnd==-1)return null;
  128. return LastArray[theEnd];
  129. }
  130.  
  131. @Override
  132. public String prev() {
  133.  
  134. if(theEnd==0||theEnd==-1)return null;
  135. return LastArray[--theEnd];
  136.  
  137. }
  138.  
  139. @Override
  140. public String next() {
  141.  
  142. if(theEnd==sizeOfOpreation-1||theEnd==-1)return null;
  143. return LastArray[++theEnd];
  144. }
  145.  
  146. @Override
  147. public void save() {
  148. // if(sizeOfOpreation<5){
  149. // LastArray[sizeOfOpreation++]=currenthere();
  150. // }else {
  151. // for(int i=0;i<4;i++){
  152. // LastArray[i]=LastArray[i+1];
  153. // }
  154. // LastArray[4]=currenthere();
  155. // }
  156. PrintWriter printWriter = null;
  157. try {
  158. printWriter = new PrintWriter(new File("read.txt"));
  159. } catch (FileNotFoundException e) {
  160. e.printStackTrace();
  161. }
  162. for(int i=0;i<sizeArrayHere;i++) {
  163. printWriter.println(ArrayHere[i]);
  164. }
  165. printWriter.flush();
  166. }
  167.  
  168.  
  169. private String currentheree() {
  170. return cuF+"="+result;
  171. }
  172.  
  173. @Override
  174. public void load() {
  175. theEnd=0;
  176. sizeOfOpreation=0;
  177. try {
  178. Scanner scanner = new Scanner(new File("read.txt"));
  179. int i=0;
  180. while (scanner.hasNextLine()) {
  181. String line = scanner.nextLine();
  182. if(!line.matches(".*?[0-9].*?"))break;
  183. LastArray[i++]=line;
  184. sizeOfOpreation++;
  185. theEnd++;
  186. }
  187. theEnd--;
  188. } catch (FileNotFoundException e) {
  189. System.out.println(e.getMessage());
  190. }
  191. }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement