Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.datastructure.linkedList;
- import java.awt.*;
- class main{
- public static void main(String[] args) {
- PolynomialSolver solver = new PolynomialSolver();
- DLinkedList list = new DLinkedList();
- solver.setPolynomial('A',new int[][]{new int[] {1,2}, new int[] {-1,1}});
- solver.setPolynomial('B',new int[][]{new int[] {4,2}, new int[] {-9,3}});
- solver.setPolynomial('C',new int[][]{new int[] {4,3}, new int[] {-1,0}});
- solver.setPolynomial('X',new int[][]{new int[] {-14,2}, new int[] {11,0},new int[]{ 13,7}} );
- solver.setPolynomial('N',new int[][]{new int[] {114,-2}, new int[] {-151,19}});
- int [][] ans =solver.multiply('A','B');
- for(int i=0;i<ans.length;i++){
- System.out.println("co is"+ans[i][0]);
- System.out.println("po is"+ans[i][1]);
- }
- }
- }
- class PolynomialSolver implements IPolynomialSolver {
- private DLinkedList list = new DLinkedList();
- private Polynomial ans = new Polynomial('R', null);
- @Override
- public void setPolynomial(char poly, int[][] terms) {
- boolean found = false;
- if (!list.isEmpty()) {
- list.setCursor(0);
- if (((Polynomial) list.getCurrent()).getName() == poly) {
- ((Polynomial) list.getCurrent()).setTerms(terms);
- found = true;
- } else {
- while (list.hasNext()) {
- if (((Polynomial) list.getNext()).getName() == poly) {
- ((Polynomial) list.getCurrent()).setTerms(terms);
- found = true;
- }
- }
- }
- }
- if (list.isEmpty() || !found) {
- list.add(new Polynomial(poly, terms));
- }
- } // done and tested
- @Override
- public String print(char poly) {
- if (list.isEmpty()) return "No Polynomial found";
- Polynomial polynomial = getPolynomial(poly);
- if (polynomial == null) return null;
- return polynomial.toString();
- } // done and tested
- private Polynomial getPolynomial(char poly) {
- if (list.isEmpty()) return null;
- list.setCursor(0);
- if (((Polynomial) list.getCurrent()).getName() == poly) {
- return (Polynomial) list.getCurrent();
- }
- while (list.hasNext()) {
- if (((Polynomial) list.getNext()).getName() == poly) {
- return (Polynomial) list.getCurrent();
- }
- }
- return null;
- } // done and tested
- @Override
- public void clearPolynomial(char poly) {
- if (list.isEmpty()) return;
- list.setCursor(0);
- if (((Polynomial) list.getCurrent()).getName() == poly) {
- list.removeCurrent();
- } else {
- while (list.hasNext()) {
- if (((Polynomial) list.getNext()).getName() == poly) {
- list.removeCurrent();
- break;
- }
- }
- }
- } // done and tested
- @Override
- public float evaluatePolynomial(char poly, float value) {
- Polynomial polynomial = getPolynomial(poly);
- if (polynomial == null) return Float.MAX_VALUE;
- if (polynomial.getTerms().isEmpty()) return 0;
- DLinkedList list = polynomial.getTerms();
- float sum = 0;
- list.setCursor(0);
- while (true){
- sum += ((float) ((Point)list.getCurrent()).x * (float) Math.pow((double) value, (double) ((Point)list.getCurrent()).y));
- if (list.hasNext()) {
- list.getNext();
- } else {
- break;
- }
- }
- return sum;
- } // done and tested
- private int[][] theEndOfWar(char poly1, char poly2, int sign) {
- Polynomial var1 = null;
- Polynomial var2 = null;
- Polynomial temp;
- for (int i = 0; i < list.size(); i++) {
- temp = (Polynomial) list.get(i);
- if (temp.name == poly1) {
- var1 = temp;
- }
- if (temp.name == poly2) {
- var2 = temp;
- }
- } if (var1 == null || var2 == null)
- return null;
- DLinkedList con = new DLinkedList();
- DLinkedList death = new DLinkedList();
- DLinkedList warPower = new DLinkedList();
- for (int i = 0; i < var1.listOfTerms.size(); i++) {
- boolean found = false;
- int die = 0;
- for (int j = 0; j < var2.listOfTerms.size(); j++) {
- if (((Point)var1.listOfTerms.get(i)).y == ((Point)var2.listOfTerms.get(j)).y) {
- found = true;
- die = j;
- break;
- }
- }
- if (found) {
- con.add(((Point)var1.listOfTerms.get(i)).y );
- int fire = 1;
- if (sign == 2)
- fire = -1;
- death.add(((Point)var1.listOfTerms.get(i)).x + fire * (((Point)var2.listOfTerms.get(die)).x ));
- if ((int) death.get(death.size() - 1) == 0) {
- death.remove(death.size() - 1);
- } else {
- warPower.add(((Point)var2.listOfTerms.get(i)).y);//var2.[i][1]
- }
- }
- }
- for (int i = 0; i < var1.listOfTerms.size(); i++) {
- if (!con.contains(((Point)var1.listOfTerms.get(i)).y)) {
- death.add(((Point)var1.listOfTerms.get(i)).x);
- warPower.add(((Point)var1.listOfTerms.get(i)).y);
- }
- }
- int fire = 1;
- if (sign == 2)
- fire = -1;
- for (int j = 0; j <var2.listOfTerms.size(); j++) {
- if (!con.contains(((Point)var2.listOfTerms.get(j)).y)) {
- death.add(fire * ((Point)var2.listOfTerms.get(j)).x);
- warPower.add(((Point)var2.listOfTerms.get(j)).y);
- }
- }
- int[][] ans = new int[death.size()][2];
- for (int i = 0; i < ans.length; i++) {
- ans[i][0] = (int) death.get(i);
- ans[i][1] = (int) warPower.get(i);
- }
- for (int i = 0; i < death.size(); i++) {
- if ((int) death.get(i) == 0) {
- death.remove(i);
- warPower.remove(i);
- i--;
- }
- }
- int p = 0;
- int c = 0;
- for (int i = 0; i < ans.length; i++) {
- for (int j = 0; j < ans.length - i - 1; j++) {
- if (ans[j][1] < ans[j + 1][1]) {
- p = ans[j][1];
- c = ans[j][0];
- ans[j][1] = ans[j + 1][1];
- ans[j][0] = ans[j + 1][0];
- ans[j + 1][1] = p;
- ans[j + 1][0] = c;
- }
- }
- }
- if (ans.length == 0) {
- int[][] ansh = new int[1][2];
- ansh[0][0] = 0;
- ansh[0][1] = 0;
- setPolynomial('R', ansh);
- return ansh;
- }
- setPolynomial('R', ans);
- return ans;
- }
- @Override
- public int[][] add(char poly1, char poly2) {
- return this.theEndOfWar(poly1, poly2, 1);// 1 for add two for substraction
- }
- @Override
- public int[][] subtract(char poly1, char poly2) {
- return this.theEndOfWar(poly1, poly2, 2);// 1 for add two for substraction
- }
- @Override
- public int[][] multiply(char poly1, char poly2) {
- Polynomial var1 = null;
- Polynomial var2 = null;
- Polynomial temp;
- for (int i = 0; i < list.size(); i++) {
- temp = (Polynomial) list.get(i);
- if (temp.name == poly1) {
- var1 = temp;
- }
- if (temp.name == poly2) {
- var2 = temp;
- }
- }
- if (var1 == null || var2 == null)
- return null;
- int p1 = 0;
- int c1 = 0;
- DLinkedList death = new DLinkedList();
- DLinkedList warPower = new DLinkedList();
- for (int i = 0; i < var1.listOfTerms.size(); i++) {
- for (int j = 0; j < var2.listOfTerms.size(); j++) {
- p1 = ((Point)var1.listOfTerms.get(i)).y + ((Point)var2.listOfTerms.get(j)).y;
- c1 = ((Point)var1.listOfTerms.get(i)).x * ((Point)var2.listOfTerms.get(j)).x;
- boolean found = false;
- for (int k = 0; k < warPower.size(); k++) {
- if (p1 == (int) warPower.get(k)) {
- death.set(k, (int) death.get(k) + c1);
- found = true;
- break;
- }
- }
- if (!found) {
- death.add(c1);
- warPower.add(p1);
- }
- }
- }
- for (int i = 0; i < death.size(); i++) {
- if ((int) death.get(i) == 0) {
- death.remove(i);
- warPower.remove(i);
- i--;
- }
- }
- int[][] ans = new int[death.size()][2];
- for (int i = 0; i < ans.length; i++) {
- ans[i][0] = (int) death.get(i);
- ans[i][1] = (int) warPower.get(i);
- }
- int p = 0;
- int c = 0;
- for (int i = 0; i < ans.length; i++) {
- for (int j = 0; j < ans.length - i - 1; j++) {
- if (ans[j][1] < ans[j + 1][1]) {
- p = ans[j][1];
- c = ans[j][0];
- ans[j][1] = ans[j + 1][1];
- ans[j][0] = ans[j + 1][0];
- ans[j + 1][1] = p;
- ans[j + 1][0] = c;
- }
- }
- }
- if (ans.length == 0) {
- int[][] ansh = new int[1][2];
- ansh[0][0] = 0;
- ansh[0][1] = 0;
- setPolynomial('R', ansh);
- return ansh;
- }
- setPolynomial('R', ans);
- return ans;
- }
- @Override
- public String toString() {
- if (list.isEmpty()) return "No Polynomials found";
- StringBuilder s = new StringBuilder("");
- list.setCursor(0);
- s.append(list.getCurrent()).append('\n');
- while (list.hasNext()) {
- s.append(list.getNext()).append('\n');
- }
- return s.toString();
- } // done and tested
- private class Polynomial {
- private final char name;
- private DLinkedList listOfTerms = new DLinkedList();
- Polynomial(char name, int[][] terms) {
- this.name = name;
- setTerms(terms);
- }
- char getName() {
- return name;
- }
- DLinkedList getTerms() {
- return listOfTerms;
- }
- public void setTerms(int[][] terms) {
- this.listOfTerms.clear();
- if (terms == null) return;
- for (int[] term : terms) {
- this.listOfTerms.add(new Point(term[0], term[1]));
- }
- }
- @Override
- public boolean equals(Object obj) {
- Polynomial polynomial = (Polynomial) obj;
- if (this.getTerms().size() != polynomial.getTerms().size()) return false;
- if (this.listOfTerms.size() != 0){
- this.listOfTerms.setCursor(0);
- polynomial.getTerms().setCursor(0);
- if (!((Point)this.listOfTerms.getCurrent()).equals((Point)polynomial.listOfTerms.getCurrent())){
- return false;
- }
- }
- return this.getName() == polynomial.getName();
- }
- private int[][] linkedListToArray(DLinkedList list){
- int[][] arr = new int[list.size()][2];
- list.setCursor(0);
- int i = 0;
- if (list.getCurrent() != null) {
- arr[i][0] = ((Point) list.getCurrent()).x;
- arr[i++][1] = ((Point) list.getCurrent()).y;
- }
- while (list.hasNext()){
- Point point = ((Point) list.getNext());
- arr[i][0] = point.x;
- arr[i++][1] = point.y;
- }
- return arr;
- }
- @Override
- public String toString() {
- int[][] terms = linkedListToArray(this.listOfTerms);
- StringBuilder s = new StringBuilder(this.name + ": ");
- for (int i = 0; i < terms.length; i++) {
- if (terms[i][0] != 0) { // total
- if (terms[i][1] == 0) { // absolute value
- if (i == 0) { // first term
- s.append(terms[i][0]);
- } else { // not first term
- if (terms[i][0] < 0) s.append("- ").append(Math.abs(terms[i][0]));
- else if (terms[i][0] > 0) s.append("+ ").append(terms[i][0]);
- }
- continue;
- }
- if (i == 0) { // first term
- if (terms[i][0] != 1) s.append(terms[i][0]);
- s.append('X').append('^').append(terms[i][1]);
- } else {
- if (terms[i][0] > 0) { //+ ve coefficient meddle term
- s.append("+ ");
- if (terms[i][0] != 1) s.append(terms[i][0]);
- s.append('X').append('^').append(terms[i][1]);
- } else { // -ve coefficient meddle term
- s.append("- ");
- if (terms[i][0] != -1) s.append(Math.abs(terms[i][0]));
- s.append('X').append('^').append(terms[i][1]);
- }
- }
- s.append(' ');
- }
- }
- return s.toString();
- } // done and tested
- } // done and tested
- }
- interface ILinkedList {
- /**
- * Inserts a specified element at the specified position in the list.
- * @param index
- * @param element */
- public void add(int index, Object element);
- /**
- * Inserts the specified element at the end of the list.
- * @param element
- */
- public void add(Object element);
- /**
- * @param index
- * @return the element at the specified position in this list.
- */
- public Object get(int index);
- /**
- * Replaces the element at the specified position in this list with the * specified element. * @param index * @param element */
- public void set(int index, Object element);
- /**
- * Removes all of the elements from this list. */
- public void clear();
- /**
- * @return true if this list contains no elements. */
- public boolean isEmpty();
- /**
- * Removes the element at the specified position in this list. * @param index */
- public void remove(int index);
- /**
- * @return the number of elements in this list. */
- public int size();
- /**
- * @param fromIndex * @param toIndex * @return a view of the portion of this list between the specified * fromIndex and toIndex, inclusively. */
- public ILinkedList sublist(int fromIndex, int toIndex);
- /**
- * @param o * @return true if this list contains an element with the same value as the * specified element. */
- public boolean contains(Object o);
- }
- interface IPolynomialSolver {
- /** * Set polynomial terms (coefficients & exponents)
- * @param poly
- * name of the polynomial
- * @param terms
- * array of [coefficients][exponents]
- */
- void setPolynomial(char poly, int[][] terms);
- /**
- * Print the polynomial in ordered human readable representation
- * @param poly
- * name of the polynomial
- * @return polynomial in the form like 27x^2+x-1
- */
- String print(char poly);
- /**
- * Clear the polynomial
- * @param poly
- * name of the polynomial
- */
- void clearPolynomial(char poly);
- /** Evaluate the polynomial @param poly
- * name of the polynomial
- * @param
- * poly the polynomial constant value
- * @return
- * the value of the polynomial
- */
- float evaluatePolynomial(char poly, float value);
- /**
- * Add two polynomials
- * @param poly1
- * first polynomial
- * @param poly2
- * second polynomial
- * @return the result polynomial
- */
- int[][] add(char poly1, char poly2);
- /**
- * Subtract two polynomials
- * @param poly1
- * first polynomial
- * @param poly2
- * second polynomial
- * @return the result polynomial
- */
- int[][] subtract(char poly1, char poly2);
- /**
- * Multiply two polynomials
- * @param poly1
- * first polynomial
- * @param poly2
- * second polynomial
- * @return the result polynomial
- */
- int[][] multiply(char poly1, char poly2);
- }
- class DLinkedList implements ILinkedList {
- private DNode head;
- private DNode tail;
- private int size = 0;
- private int cursorIndex = 0;
- private DNode cursorNode;
- public DLinkedList() {
- this(null);
- }
- public DLinkedList(Object obj) {
- head = new DNode(obj,null,null);
- tail = new DNode(null,null,head);
- head.setNext(tail);
- cursorNode = head;
- }
- @Override
- public void add(int index, Object element) throws RuntimeException{
- if (index > size || index < 0){
- throw new ArrayIndexOutOfBoundsException("index out of bound, index = " + index + " , size = " + size );
- } else if ( element == null ){
- throw new RuntimeException("null value isn't allowed to be added to to list");
- } else if (index == size) {
- add(element);
- return;
- } else if (index == 0){
- DNode newNode = new DNode(element,head,null);
- head.setPrevious(newNode);
- head = newNode;
- } else {
- DNode currentNode = head;
- for (int i = index; i > 0 ; i--) {
- currentNode = currentNode.getNext();
- }
- DNode newNode = new DNode(element,currentNode,currentNode.getPrevious());
- currentNode.getPrevious().setNext(newNode);
- currentNode.setPrevious(newNode);
- }
- size++;
- } // done and tested
- public int indexOf(Object obj){
- DNode currentNode = head;
- for (int i = 0; i < size ; i++) {
- if (currentNode.getData().equals(obj)) return i;
- currentNode = currentNode.getNext();
- }
- return -1;
- }
- @Override
- public void add(Object element) throws RuntimeException{
- if (element == null) {
- throw new RuntimeException("null value isn't allowed to be added to to list");
- }
- if (this.isEmpty()){
- head.setData(element);
- } else if (this.tail.getData() == null){
- tail.setData(element);
- } else {
- DNode newNode = new DNode(element,null,tail);
- tail.setNext(newNode);
- tail = newNode;
- }
- this.size++;
- } // done and tested
- @Override
- public Object get(int index) throws RuntimeException{
- DNode node = this.findNode(index);
- if (node == null){
- throw new RuntimeException("didn't find any element in that index " + index);
- }
- return node.getData();
- } // done and tested
- private DNode findNode(int index) throws RuntimeException{
- if (index >= size || index < 0){
- throw new RuntimeException("index out of bound, index = " + index + " , size = " + size);
- } else if (size == 1){
- return head;
- } else if (index == size - 1) return tail;
- DNode currentNode = head;
- for (int i = 0; i < index ; i++) {
- currentNode = currentNode.getNext();
- }
- return currentNode;
- } // done and tested
- private DNode findNode(Object obj){
- DNode currentNode = head;
- for (int i = 0; i < size ; i++) {
- currentNode = currentNode.getNext();
- if (currentNode.getData().equals(obj)) return currentNode;
- }
- return null;
- }
- @Override
- public void set(int index, Object element) {
- DNode node = findNode(index);
- node.setData(element);
- } // done and tested
- @Override
- public void clear() { // tested and worked
- this.head = new DNode(null,null,null);
- this.tail = new DNode(null,null,this.head);
- this.head.setNext(this.tail);
- size = 0;
- } // tested and worked
- @Override
- public boolean isEmpty() {
- return size == 0 || head.getData() == null;
- } // done and tested
- @Override
- public void remove(int index) {
- DNode node = findNode(index);
- remove(node);
- } // done and tested
- private void remove(DNode node){
- if (head.equals(node)) {
- if (head.getNext().getData() == null || head.getNext() == tail){
- head.setData(head.getNext().getData());
- head.getNext().setData(null);
- } else {
- head = head.getNext();
- head.setPrevious(null);
- }
- } else if (tail.equals(node)){
- if (tail.getPrevious() == head){
- tail.setData(null);
- } else {
- tail = tail.getPrevious();
- tail.setNext(null);
- }
- } else {
- node.getNext().setPrevious(node.getPrevious());
- node.getPrevious().setNext(node.getNext());
- }
- size--;
- }
- @Override
- public int size() {
- return this.size;
- } // done and tested
- @Override
- public ILinkedList sublist(int fromIndex, int toIndex) throws RuntimeException {
- if ((fromIndex > toIndex) || (fromIndex < 0 || fromIndex >= size) || (toIndex >= size)) {
- throw new RuntimeException("wrong indexes , start = " + fromIndex + " , end = " + toIndex + " , size = " + size);
- }
- DLinkedList list = new DLinkedList();
- DNode currentNode = findNode(fromIndex);
- for (int i = fromIndex ; i <= toIndex ; i++ ){
- list.add(currentNode);
- currentNode = currentNode.getNext();
- }
- return list;
- } // done and tested
- public void setCursor (int index){
- cursorNode = findNode(index);
- } // done and tested
- public Object getCurrent(){
- return this.cursorNode.getData();
- } // done and tested
- public Object getNext(){
- if (cursorNode.getNext() != null){
- cursorNode = cursorNode.getNext();
- cursorIndex++;
- return cursorNode.getData();
- }
- return null;
- } // done and tested
- public Object getPrevious(){
- if (cursorNode.getPrevious() != null){
- cursorNode = cursorNode.getPrevious();
- cursorIndex--;
- return cursorNode.getData();
- }
- return null;
- } // done and tested
- public boolean hasNext(){
- if (cursorNode.getNext() == null) return false;
- return cursorNode.getNext().getData() != null;
- } // done and tested
- public boolean hasPrevious(){
- if (cursorNode.getPrevious() == null) return false;
- return cursorNode.getPrevious().getData() != null;
- } // done and tested
- public int getCursorIndex (){
- return cursorIndex;
- } // done and tested
- public void removeCurrent(){
- if (this.isEmpty()) return;
- remove(cursorNode);
- if (this.isEmpty()) return;
- setCursor(0);
- } // done and tested
- @Override
- public boolean contains(Object o) {
- if (this.isEmpty()) return false;
- if (o == null) return false;
- DNode currentNode = head;
- while (currentNode != null) {
- if (currentNode.getData().equals(o)) return true;
- currentNode = currentNode.getNext();
- }
- return false;
- } // done and tested
- @Override
- public String toString() {
- StringBuilder s = new StringBuilder("[");
- DNode currentNode = head;
- for (int i = 0 ; i < size ; i++){
- s.append(currentNode.getData());
- if (i < size - 1) s.append(",");
- currentNode = currentNode.getNext();
- }
- s.append("]");
- return s.toString();
- } // done and tested
- private class DNode {
- private Object data;
- private DNode next;
- private DNode previous;
- public DNode(Object data, DNode next, DNode previous) {
- this.data = data;
- this.next = next;
- this.previous = previous;
- }
- public Object getData() {
- return data;
- }
- public void setData(Object data) {
- this.data = data;
- }
- public DNode getNext() {
- return next;
- }
- public void setNext(DNode next) {
- this.next = next;
- }
- public DNode getPrevious() {
- return previous;
- }
- public void setPrevious(DNode previous) {
- this.previous = previous;
- }
- @Override
- public String toString() {
- if (this.data == null) return null;
- return this.data.toString();
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) return true;
- DNode node = (DNode) obj;
- return this.getData().equals(node.getData()) &&
- this.getNext().equals(node.getNext())&&
- this.getPrevious().equals(node.getPrevious());
- }
- } // done and tested
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement