diff --git a/.idea/artifacts/calculatorp.xml b/.idea/artifacts/calculatorp.xml new file mode 100644 index 0000000..aace6f3 --- /dev/null +++ b/.idea/artifacts/calculatorp.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/calculatorp + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/calculator.iml b/calculator.iml deleted file mode 100644 index c90834f..0000000 --- a/calculator.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/src/calculatorp.jar b/src/calculatorp.jar new file mode 100644 index 0000000..bd90030 Binary files /dev/null and b/src/calculatorp.jar differ diff --git a/src/com/GUI/GUI.form b/src/com/GUI/GUI.form new file mode 100644 index 0000000..489b00f --- /dev/null +++ b/src/com/GUI/GUI.form @@ -0,0 +1,218 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/GUI/GUI.java b/src/com/GUI/GUI.java new file mode 100644 index 0000000..e37b900 --- /dev/null +++ b/src/com/GUI/GUI.java @@ -0,0 +1,502 @@ +package com.GUI; + +import javax.swing.*; +import java.awt.event.*; +import com.op; + +/** + * Created by abrahamon on 18/06/16. + */ +public class GUI extends JFrame{ + + public JPanel panelMain; + private JPanel panelInput; + private JPanel panelButtons; + private JButton a7Button; + private JButton delButton; + private JButton a8Button; + private JButton a9Button; + private JButton eButton; + private JButton a4Button; + private JButton a5Button; + private JButton a6Button; + private JButton mulButton; + private JButton clearButton; + private JButton a1Button; + private JButton a0Button; + private JButton a2Button; + private JButton a3Button; + private JButton susButton; + private JButton equalButton; + private JButton espaceButton; + private JButton barButton; + private JButton addButton; + private JLabel dataLabel; + private JButton plotButton; + + private String term; + private String terms; + private String lastOpe; + op operations; + + public GUI(){ + + super("Polynomial Calculator"); + super.setContentPane(panelMain); + super.setBounds(200,100,475,300); //The bounds start(X,Y) bounds(X,Y) + super.setResizable(false); + super.setDefaultCloseOperation(super.EXIT_ON_CLOSE); + super.setVisible(true); + operations = new op(); + term =""; + terms =""; + lastOpe= ""; + + //listeners for each botton + a0Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('0'); + } + }); + a1Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('1'); + } + }); + a2Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('2'); + } + }); + a3Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('3'); + } + }); + a4Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('4'); + } + }); + a5Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('5'); + } + }); + a6Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('6'); + } + }); + a7Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('7'); + } + }); + a8Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('8'); + } + }); + a9Button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('9'); + } + }); + espaceButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('s'); + } + }); + barButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('|'); + + } + }); + clearButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('c'); + } + }); + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('+'); + } + }); + susButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('-'); + } + }); + mulButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('*'); + } + }); + eButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('e'); + } + }); + delButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('d'); + } + }); + equalButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + keyPressed('='); + } + }); + //listeners for the keys + a7Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a8Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a9Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a4Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a5Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a6Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a1Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a2Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a3Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + a0Button.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + espaceButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed('s'); + } + }); + barButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + eButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + mulButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + susButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + addButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + delButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + clearButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + equalButton.addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + super.keyTyped(e); + GUI.this.keyPressed(e.getKeyChar()); + } + }); + + + + } + //every listener calls this method, which selects the propper action + private void keyPressed(char key){ + if(term=="Syntax error"){term="";} + if(key=='0'){ + term = term +"0"; + dataLabel.setText(term); + } + else if(key=='1'){ + term = term +"1"; + dataLabel.setText(term); + } + else if(key=='2'){ + term = term +"2"; + dataLabel.setText(term); + } + else if(key=='3'){ + term = term +"3"; + dataLabel.setText(term); + } + else if(key=='4'){ + term = term +"4"; + dataLabel.setText(term); + } + else if(key=='5'){ + term = term +"5"; + dataLabel.setText(term); + } + else if(key=='6'){ + term = term +"6"; + dataLabel.setText(term); + } + else if(key=='7'){ + term = term +"7"; + dataLabel.setText(term); + } + else if(key=='8'){ + term = term +"8"; + dataLabel.setText(term); + } + else if(key=='9'){ + term = term +"9"; + dataLabel.setText(term); + } + else if(key=='s'){ + term = term +" "; + dataLabel.setText(term); + } + else if(key=='|'){ + term = term +"|"; + dataLabel.setText(term); + } + else if(key=='+'){ + addition(); + } + else if(key=='-'){ + subtraction(); + } + else if(key=='*'){ + multiplication(); + } + else if(key=='E' || key=='e'){ + evaluate(); + } + else if(key=='D' || key=='d'){ + delete(); + } + else if(key=='C' || key=='c' ){ + clear(); + } + else if(key=='='){ + getResult(); + } + else{System.out.println("Unknown: "+key);} + } + + //Methods for each of the operations + private void addition(){ + term=operations.delSpace(term.replace("|",",")); + term = operations.addSym(term); + if ( operations.validate(term)){ + lastOpe="+"; + if(terms==""){ + terms=term; + term=""; + } + dataLabel.setText(term); + }else{ + term = "Syntax error"; + dataLabel.setText(term); + } + } + + private void subtraction(){ + boolean flag= true; + if(term=="" || term=="Syntax error"){ + term = term+"-" ; + flag=false; + }else{ + for(int i=0;i2 ) { + term= (operations.eval(operations.addSym(terms), num1, num2) +""); + dataLabel.setText(term); + return; + } + } + term=operations.delSpace(term.replace("|",",")); + term = operations.addSym(term); + if ( operations.validate(term)){ + if(lastOpe=="+") { + term = operations.sumPol(terms,term).replace("+ ",""); + }else if(lastOpe=="-"){ + term = operations.substPol(terms,term).replace("+ ",""); + }else if (lastOpe=="*"){ + term = operations.multiPol(terms,term).replace("+ ",""); + } + + if(term==""){ + dataLabel.setText(""); + }else{ + dataLabel.setText(term.replace("," , " | "));} + }else{ + term = "Syntax error"; + dataLabel.setText(term); + } + } + + private void evaluate(){ + lastOpe="e"; + terms=term; + term=""; + dataLabel.setText(term); + } + + + +} diff --git a/src/com/GUI/plotter.form b/src/com/GUI/plotter.form new file mode 100644 index 0000000..0a3b3cd --- /dev/null +++ b/src/com/GUI/plotter.form @@ -0,0 +1,13 @@ + +
+ + + + + + + + + + +
diff --git a/src/com/GUI/plotter.java b/src/com/GUI/plotter.java new file mode 100644 index 0000000..d6a907f --- /dev/null +++ b/src/com/GUI/plotter.java @@ -0,0 +1,98 @@ +package com.GUI; + +import javax.swing.*; +import java.awt.*; + +/** + * Created by abrahamon on 18/06/16. + */ +public class plotter extends JFrame { + private static Graphics gBuf = null; + private static GraphPaperCanvas canvas = null; + private static Image vm = null; + private int x, y; + private int w, h; + + public plotter(String name, int x, int y ){ + if ( canvas == null ) { + setTitle(name); + setSize(340,370); + setLocation(20,50); + + canvas = new GraphPaperCanvas(null); + getContentPane().add(canvas); + setVisible(true); + + vm = canvas.createImage(1100,950); + gBuf = vm.getGraphics(); + canvas.setVm(vm); + + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + + this.x = x; + this.y = y; + w = 300; + h = 300; + + drawBounds(); + gBuf.setColor( Color.GREEN ); + } + + public void drawBounds() { + Color cur = gBuf.getColor(); + gBuf.setColor( Color.LIGHT_GRAY ); + for ( int d=0; d 10 || px < -10 || py > 10 || py < -10 ) + return; + px *= w/20; + py *= h/20; + px += w/2 + 1; + py = h/2 - py + 1; + gBuf.drawLine( x+(int)px, y+(int)py, x+(int)px, y+(int)py ); + canvas.repaint(); + } + +} + + + + +class GraphPaperCanvas extends Canvas { + private Image vm; + + public GraphPaperCanvas( Image vm ) { + this.vm = vm; + setBackground( Color.white ); + } + + public void setVm( Image vm ) { + this.vm = vm; + } + + @Override + public void paint( Graphics g ) { + g.drawImage(vm,0,0,this); + } + + @Override + public void update(Graphics g) { paint(g); } + +} diff --git a/src/com/Main.java b/src/com/Main.java new file mode 100644 index 0000000..2230601 --- /dev/null +++ b/src/com/Main.java @@ -0,0 +1,25 @@ +package com; + +import javax.swing.*; +import java.util.*; +import com.GUI.*; + +public class Main { + + public static void main(String[] args) { + + GUI userInterface = new GUI(); + + + //While flag to capture the instructions of the user + boolean flag = true; + while (flag){ + System.out.println("Ingres the operation"); + Scanner scan = new Scanner(System.in); + String s = scan.next(); + System.out.println(s); + if(s.contains("exit")){flag=false;System.out.println("Command promt finished");} + } + + } +} diff --git a/src/com/main.java b/src/com/main.java index e1449da..43176e0 100644 --- a/src/com/main.java +++ b/src/com/main.java @@ -6,10 +6,106 @@ public class main{ String eq1= "123|245|145"; String eq2 = "143|225|144"; public static void main(String[] args) { - String eq2 = "+143|+125|+144|+125|-544|+225"; - operations op = new operations(); + + + String eq2 = "+ 100 2 5,+ 2 2 5,- 3 3 5,- 2 4 3,+ 3 4 3"; + String eq1 = "+143|+122|+144"; + String eq3 = "+122|-523"; + + // op ope = new op(); + + //System.out.println(ope.reduceEQ(eq2)); + // System.out.println(ope.getSum("-1","+2")); + + String [] print = eq2.split(","); + int len = print.length; + String result = ""; + for (int i = 0; i < len; i++){ + if (i != len-1) + result += print[i]+" | "; + else + result += print[i]; + } + System.out.println(result); + + + + + + + + + + + + + + + + /* System.out.println(eq2); System.out.println(op.reduceEQ(eq2.replace("|",","))); + System.out.println("EQ1 = " + eq1); + System.out.println("EQ3 = " +eq3); + System.out.println(op.substPol(eq1.replace("|",","),eq3.replace("|",","))); + + String numAux1 = "-15023"; + String numAux2 = "-15045"; + System.out.println( op.multiPol(eq1.replace("|",","),eq3.replace("|",","))); + + System.out.println(op.eval(eq3.replace("|",","),2,3)); + + + + + + + + + + + + + + //Agrega el signo de positivo a las que no lo contienen + String [] p = eq2.split(","); + for (int i = 0 ; i < p.length;i++){ + if (p[i].charAt(0) == '-' || p[i].charAt(0) == '+'){ + System.out.println("Si"); + } + else{p[i] = "+"+p[i] ;} + } + String result = ""; + for (int i = 0 ; i < p.length;i++){ + if (p[i] == "null"){ + + }else{ + result += p[i] + "|"; + } + } + System.out.println(result.substring(0,result.length()-1)); + + + //Comprueba si un A0 es igual a 0 y lo elimina de la ecuacion + /*for (int i = 0 ; i < p.length;i++){ + if (p[i].charAt(1) == '0'){ + p[i] = "null"; + } + else{} + } + String result = ""; + for (int i = 0 ; i < p.length;i++){ + if (p[i] == "null"){ + + }else{ + result += p[i] + "|"; + } + } + + + + System.out.println(result.substring(0,result.length()-1)); + */ } diff --git a/src/com/op.java b/src/com/op.java new file mode 100644 index 0000000..9b99fb0 --- /dev/null +++ b/src/com/op.java @@ -0,0 +1,257 @@ +package com; +import java.lang.*; +/** + * Created by tvlenin on 20/06/16. + */ +public class op { + + + public String reduce(String pData){ + String ans=""; + String [] equ = pData.split(","); + String [] data1; + String [] data2; + for(int i=0; i numb1) + result = "+ " + (numb2-numb1) ; + else + result = "- "+(numb1-numb2) +""; + } + else if (symbol1 == '+' && symbol2 == '-'){ + if (numb1 == numb2 ) + flag = true; + else if(numb1 > numb2) + result ="+ " + (numb1-numb2) ; + else + result = "- "+(-numb1+numb2)+""; + } + else{ + result = "+ "+(numb1+numb2) ; + } + + if(flag) + result ="null"; + return result; + } + + public String insertspace(String pData){ + String ans; + ans = pData; + return ans; + } + + public boolean validate(String pData){ + pData= pData.replace('|',','); + boolean ans = true; + String [] a = pData.split(","); + String [] term; + for(int i=0; i numb2) return result; } + public String sumPol(String num1, String num2){ + String newNum = num1+","+num2; + return reduceEQ(newNum); + } + public String substPol(String num1, String num2){ + String result = ""; + String [] eqAux = num2.split(","); + int eqLength = eqAux.length; + for (int i =0 ; i < eqLength ; i++){ + if(eqAux[i].charAt(0) == '-'){ + eqAux[i] = eqAux[i].replace("-","+"); + }else{ + eqAux[i] = eqAux[i].replace("+","-"); + } + + } + for ( int k = 0; k < eqLength; k++){ + if(k != eqLength-1 ){ + result +=eqAux[k] + ","; + }else{ + result +=eqAux[k] ; + } + } + + return sumPol(num1,result); + } + public String multiPol(String num1, String num2){ + String result = ""; + String numAux1 = ""; + String numAux2 = ""; + String numAux3 = ""; + String [] eqAux1 = num1.split(","); + String [] eqAux2= num2.split(","); + int eqLength1 = eqAux1.length; + int eqLength2 = eqAux2.length; + + for (int i = 0 ; i < eqLength1 ; i++){ + numAux1 = eqAux1[i]; + for (int j = 0; j < eqLength2; j++){ + numAux2 = eqAux2[j]; + if (numAux1.charAt(0) == numAux2.charAt(0)){numAux3 += "+";} + else{numAux3 += "-";} + numAux3+= Integer.parseInt(numAux1.substring(1,numAux1.length()-2)) * Integer.parseInt(numAux2.substring(1,numAux2.length()-2)); + numAux3 += Integer.parseInt(numAux1.charAt(numAux1.length()-2)+"") + Integer.parseInt(numAux2.charAt(numAux2.length()-2)+"") ; + numAux3 += Integer.parseInt(numAux1.charAt(numAux1.length()-1)+"") + Integer.parseInt(numAux2.charAt(numAux2.length()-1)+"") ; + result+=numAux3+"|"; + numAux3 = ""; + } + //result+=numAux3+"|"; + } + + result=result.substring(0,result.length()-1); + + + return result; + } + public int eval (String poly, int x ,int y){ + int result = 0; + int numAux = 0; + String [] eqAux = poly.split(","); + int eqLength = eqAux.length; + + for(int i = 0; i < eqLength ; i++ ){ + numAux += Integer.parseInt(eqAux[i].substring(0,eqAux[i].length()-2)) * (int)Math.pow(x,Integer.parseInt(eqAux[i].charAt(eqAux[i].length()-2)+""))* (int)Math.pow(y,Integer.parseInt(eqAux[i].charAt(eqAux[i].length()-1)+"")); + + result +=numAux; + numAux = 0; + } + + + + return result; + } + + } +