Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.calculator.cs37;
- import java.awt.EventQueue;
- import java.util.EventObject;
- import javax.swing.JTextArea;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import javax.swing.JLabel;
- import java.awt.Font;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.SwingConstants;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.io.BufferedReader;
- import java.util.ArrayList;
- import javax.management.RuntimeErrorException;
- import java.util.Arrays;
- import java.util.List;
- import java.util.ArrayList;
- public class Calc implements Calculator {
- String operator = null;
- double [] n = new double[2];
- double ans = 0;
- String nums1="" ;
- String nums2="" ;
- String expression="" ;
- String result="" ;
- int current1 = -1,prev=0,next=0 ;
- int shift = 0 ;
- String [] process = new String [5];
- File file ;
- public void input(String s) {
- if (!s.matches("^-?\\d+(\\.\\d+)?(\\s+)?[-+*\\/](\\s+)?-?\\d+(\\.\\d+)?(\\s+)?$")) {//^ start of the line , $ end of the line , d digit , [..] any one ,
- operator = "false" ;
- //throw new RuntimeException("you enterd non valid expression");
- }else {
- if (current1 < 4 ) {
- current1 ++ ;
- }else { // shifting
- shift = 1 ;
- current1 = 4 ;
- }
- //remove spaces
- s = s.replaceAll("//s+", "");
- String [] nums = s.split("[-+*\\/]");
- String [] operat = s.split("\\d+(\\.\\d+)?");
- operator = operat[1].charAt(0)+"";
- int i = 0 ;
- for (String number : nums) {
- if (!number.isEmpty()) {
- if (i==0 && s.charAt(0)=='-') { //negative
- number="-"+number ;
- n[i] = Double.parseDouble(number);
- }else if (i == 1 && (nums[1].equals("") || nums.length == 4)) { //negative
- number = "-" + number;
- n[i] = Double.parseDouble(number);
- }
- else {
- n[i] = Double.parseDouble(number);
- }
- i++;
- }
- }
- }
- }
- /* Return the result of the current operations or throws a runtime
- exception */
- public String getResult() {
- if (operator.equals("false")) {
- throw new RuntimeException ("You Enterd non valid expression");
- }else if (operator.charAt(0) == '+') {
- ans = n[0]+n[1];
- }else if (operator.charAt(0) == '*') {
- ans = n[0]*n[1];
- }else if (operator.charAt(0) == '-') {
- ans = n[0]-n[1];
- }else if (operator.charAt(0) == '/') {
- if (n[1] != 0) {
- ans = n[0]/n[1];
- }else {
- throw new RuntimeException ("You can't divide by 0");
- }
- }
- //answer = String.format("%.16f", result);
- result = String.format("%.16f", ans);
- // result = Double.toString(ans);
- nums1 = Double.toString(n[0]);
- nums2=Double.toString(n[1]);
- expression=nums1+operator + nums2+"=" +result ;
- if (shift == 0 && current1 > -1) {
- process[current1]=expression;
- }else if (shift == 1){ // shifting
- for (int i=0 ; i<4 ; i++) {
- process[i] =process[i+1];
- }
- process[4] = expression ;
- }
- return result ;
- }
- /* return the current formula */
- public String current () {
- if (current1 < -1 || current1 > 4) {
- return null ;
- }else {
- return process[current1];
- }
- }
- /* return the last operation in String format, or Null if no more history
- available, will update current */
- public String prev() {
- if (current1 == -1 ) {
- throw new RuntimeException ("You Enterd only one");
- }else {
- prev = current1 - 1;
- next = current1 ;
- current1 = prev ;
- }
- return process[current1];
- }
- /* return the next operation in String format, or Null if no more history
- available, will update current */
- public String next() {
- if (current1 == 4) {
- throw new RuntimeException ("there is no next operation of this ");
- }else {
- next = current1 + 1 ;
- prev = current1 ;
- current1 = next ;
- }
- return process[current1];
- }
- /* Save in file the last 5 done Operations */
- public void save() {
- File file = new File ("hendo.txt");
- PrintWriter pw ;
- try {
- pw= new PrintWriter (file);
- for (int i=0 ; i<=current1 ; i++) {
- pw.write(process[i]);
- pw.write("\n");
- }
- pw.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /* Load from file the saved operations */
- public void load()
- {
- List<String> temp = Arrays.asList(process);
- try {
- FileReader fr = new FileReader("hendo.txt");
- int i;
- try {
- String st = "";
- String[] s1 = new String[2];
- temp.clear();
- current1 = -1;
- int index=0;
- while ((i = fr.read()) != -1) {
- if(index!=0&&i=='\n') {
- s1[1]= getResult() ;
- }
- if (i != '\n') {
- st = st + (char) i;
- } else {
- index++;
- input(st);
- s1[0] = st;
- st = "";
- }
- }
- prev = temp.size() ;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement