package project2;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//polynomial class
class Polynomial
{
Node head;
Polynomial()
{
head = null;
}
//insert Node
public void add(int coef, int power)
{
Node temp = new Node(coef, power, null);
if (head == null)
{
head = temp;
}
else {
Node p = null;
Node q = head;
//insert in front if exponent is higher
if (temp.getElement2() > q.getElement2())
{
temp.setNext(head);
head = temp;
}
else
{
//insert at middle or end
while ( q != null && q.getElement2() > temp.getElement2())
{
p = q;
q = q.getNext();
}
p.setNext(temp);
temp.setNext(q);
}
}
}
//add 2 polynomials together
public Polynomial addPoly(Polynomial poly1, Polynomial poly2){
Polynomial tempPoly = new Polynomial();
int power = (poly1.head.getElement2() > poly2.head.getElement2()) ? poly1.head.getElement2() : poly2.head.getElement2();
while (power >=0){
Node n1 = poly1.head;
while (n1 != null){
if (n1.getElement2() == power)
break;
n1 = n1.getNext();
}
Node n2 = poly2.head;
while (n2 != null){
if (n2.getElement2() == power)
break;
n2 = n2.getNext();
}
if((n1 != null) && (n2 != null))
tempPoly.add((n1.getElement1() + n2.getElement1()), n1.getElement2());
else if (n1 != null)
tempPoly.add(n1.getElement1(), n1.getElement2());
else if (n2 != null)
tempPoly.add(n2.getElement1(), n2.getElement2());
power--;
}
return tempPoly;
}
//Subtract 2 polynomials
public Polynomial subPoly(Polynomial poly1, Polynomial poly2){
Polynomial tempPoly = new Polynomial();
int power = (poly1.head.getElement2() > poly2.head.getElement2()) ? poly1.head.getElement2() : poly2.head.getElement2();
while (power >=0){
Node n1 = poly1.head;
while (n1 != null){
if (n1.getElement2() == power)
break;
n1 = n1.getNext();
}
Node n2 = poly2.head;
while (n2 != null){
if (n2.getElement2() == power)
break;
n2 = n2.getNext();
}
if((n1 != null) && (n2 != null))
tempPoly.add((n1.getElement1() + (n2.getElement1()* -1)), n1.getElement2());
else if (n1 != null)
tempPoly.add(n1.getElement1(), n1.getElement2());
else if (n2 != null)
tempPoly.add(n2.getElement1()* -1, n2.getElement2());
power--;
}
return tempPoly;
}
//Multiply 2 polynomials - not working
/*public Polynomial multPoly(Polynomial poly1, Polynomial poly2)
{
int coef;
int power;
Polynomial poly3 = new Polynomial();
Node n1 = poly1.head;
Node n2 = poly2.head;
while ( poly1 != null )
{
while ( poly2 != null )
{
coef = n1.Element1 * n2.Element1;
power = poly1.head.Element1 + poly2.head.Element1;
System.out.println(coef);
System.out.println(power);
n2 = n2.getNext();
poly3.add(coef, power) ;
}
n2 = poly2.head;
n1 = n1.getNext() ;
}
return poly3;
}*/
//Prepare the string using regular expression
public Polynomial prep(String s)
{
Polynomial poly = new Polynomial();
s= s.replaceAll("\\s+", "");
String str1 = "(-?[0-9]{1,})x\\^(-?[0-9]{1,})?";
String str2 = "(-?[0-9]{1,})x";
String str3 = "(-?[0-9]{1,})";
Pattern p = Pattern.compile(str1);
Pattern p2 = Pattern.compile(str2);
Pattern p3 = Pattern.compile(str3);
Matcher matcher = p.matcher(s);
Matcher matcher2 = p2.matcher(s);
Matcher matcher3 = p3.matcher(s);
int coef;
int exp;
int index = 0;
boolean matchFound;
do{
if(matchFound = matcher.find(index))
{
coef = Integer.parseInt(matcher.group(1));
exp = Integer.parseInt(matcher.group(2));
poly.add(coef,exp);
index = matcher.end();
}
else if (matchFound = matcher2.find(index))
{
String groupStr = matcher2.group(1);
poly.add(Integer.parseInt(matcher2.group(1)),1);
index = matcher2.end();
}
else if (matchFound = matcher3.find(index))
{
String groupStr = matcher3.group(1);
poly.add(Integer.parseInt(matcher3.group(1)),0);
index = matcher3.end();
}
else
matchFound = false;
}while(matchFound);
return poly;
}
//to string
public String toString()
{
Node current = head.getNext();
String output = "";
if(head != null)
{
if(head.Element1 == 0)
{
}
else if (head.Element2 == 0)
{
output += head.Element1;
}
else if (head.Element2 == 1)
{
output += head.Element1 + "x";
}
else
{
output += head.Element1 + "x^" + head.Element2;
}
}
while(current != null)
{
if(current.Element1 == 0)
{
}
else if (current.Element2 == 0)
{
if (current.Element1 > 0)
{
output += "+" + current.Element1;
}
else
output += current.Element1;
}
else if (current.Element2 == 1)
{
if (current.Element1 > 0)
{
output += "+" + current.Element1 + "x";
}
else
output += current.Element1 + "x";
}
else if (current.Element1 > 0)
{
output += "+" + current.Element1 + "x^" + current.Element2;
}
else
{
output += current.Element1 + "x^" + current.Element2;
}
current = current.getNext();
}
return output;
}
//node class
class Node
{
int Element1, Element2;
Node next;
public Node(int Element1,int Element2)
{
this.Element1 = Element1;
this.Element2 = Element2;
next = null;
}
public Node(int Element1, int Element2, Node n)
{
this.Element1 = Element1;
this.Element2 = Element2;
next = n;
}
public Node getNext()
{
return next;
}
public void setNext(Node next)
{
this.next = next;
}
public int getElement1() {
return Element1;
}
public void setElement1(int element1) {
Element1 = element1;
}
public int getElement2() {
return Element2;
}
public void setElement2(int element2) {
Element2 = element2;
}
}
}
//GUI
class MainWindow extends JFrame
{
JTextField jPoly1;
JTextField jPoly2;
JTextArea jResult;
JButton jAdd;
JButton jSub;
JButton jMult;
JButton jDiv;
JButton jExit;
MainWindow()
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
p1.setLayout(new GridBagLayout());
p2.setLayout(new GridBagLayout());
p3.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(6,6,3,3);
jPoly1 = new JTextField(20);
jPoly2 = new JTextField(20);
jResult = new JTextArea(1,20);
jAdd = new JButton("ADD +");
jSub = new JButton("SUB -");
jMult = new JButton("MULT *");
jDiv = new JButton("DIV /");
jExit = new JButton("EXIT");
jExit.setBackground(Color.orange);
JLabel label1 = new JLabel("Polynomial 1:");
JLabel label2 = new JLabel("Polynomial 2:");
JLabel label3 = new JLabel("Result: ");
c.gridx = 0;
c.gridy = 3;
p1.add(label1, c);
c.gridx = 5;
c.gridy = 3;
p1.add(jPoly1, c);
c.gridx = 10;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
p2.add(jAdd, c);
c.gridx = 15;
c.gridy = 3;
c.weightx = 0;
p2.add(jSub, c);
c.gridx = 0;
c.gridy = 6;
p1.add(label2, c);
c.gridx = 5;
c.gridy = 6;
p1.add(jPoly2, c);
c.gridx = 10;
c.gridy = 6;
p2.add(jMult, c);
c.gridx = 15;
c.gridy = 6;
c.fill = GridBagConstraints.HORIZONTAL;
p2.add(jDiv, c);
c.gridx = 0;
c.gridy = 9;
p1.add(label3, c);
c.gridx = 5;
c.gridy = 9;
p1.add(jResult, c);
c.weightx = 0.0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = 10;
c.gridy = 9;
p2.add(jExit, c);
p1.setBorder(BorderFactory.createTitledBorder("Calculator"));
p3.add(p1);
p3.add(p2);
add(p3);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Polynomial");
setSize(525,225);
setVisible(true);
class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Polynomial poly1 = new Polynomial();
Polynomial poly2 = new Polynomial();
Polynomial result = new Polynomial();
String input1 = jPoly1.getText();
String input2 = jPoly2.getText();
poly1 = poly1.prep(input1);
poly2 = poly2.prep(input2);
result = result.addPoly(poly1,poly2);
jResult.setText("");
jResult.append(result.toString());
}
}
class SubButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Polynomial poly1 = new Polynomial();
Polynomial poly2 = new Polynomial();
Polynomial result = new Polynomial();
String input1 = jPoly1.getText();
String input2 = jPoly2.getText();
poly1 = poly1.prep(input1);
poly2 = poly2.prep(input2);
result = result.subPoly(poly1,poly2);
jResult.setText("");
jResult.append(result.toString());
}
}
ExitButtonListener bl1 = new ExitButtonListener();
jExit.addActionListener(bl1);
AddButtonListener bl2 = new AddButtonListener();
jAdd.addActionListener(bl2);
SubButtonListener bl3 = new SubButtonListener();
jSub.addActionListener(bl3);
}
}
//Main
public class Project {
public static void main(String[] args) {
MainWindow mw = new MainWindow();
}
}