java 連結串列實現多項式加法!
阿新 • • 發佈:2019-02-03
<p style="color:rgb(85, 85, 85);"> </p>package pers.mao.test.ACM; import java.util.Scanner; class Node { int coef; int exp; Node next; public Node(int coef, int exp) { this.coef = coef; this.exp = exp; this.next = null; } } class PolyLinked { public Node frist; public Node last; public void createNode(int coef, int exp) { Node newNode = new Node(coef, exp); if (frist == null) { frist = newNode; last = newNode; } else { last.next = newNode; last = newNode; } } public int size() { Node newNode; int i=0; if(frist==null) return 0; if(frist==last) return 1; newNode=frist; if(newNode.next!=last) newNode.next=newNode; i++; return i; } public void printPolyList() { Node current = frist; StringBuilder print = new StringBuilder(""); //用於顯示錶達式; while (current != null) { if (current.coef != 0 && current.exp == 1) { //指數為1,StringBuilder中有資料,若係數為負數則刪除之前的加號(注意加號前後都需空格),再追加新的資料; if (current.coef < 0 && print.length() >= 2) print.delete(print.length() - 2, print.length() - 1); //指數為1,若係數為1則不顯示係數; if (current.coef == 1) { print.append("X + "); }else { print.append(current.coef + "X + "); } } else if (current.coef != 0 && current.exp == 0) { //指數為0,即常數項; //指數為0,StringBuilder中有資料,若係數為負數則刪除之前的加號(注意加號前後都需空格),再追加新的資料; if (current.coef < 0 && print.length() >= 2) print.delete(print.length() - 2, print.length() - 1); print.append(current.coef); } else if (current.coef != 0 && current.exp != 0) { //指數大於1; //StringBuilder中有資料,若係數為負數則刪除之前的加號(注意加號前後都需空格),再追加新的資料; if (current.coef < 0 && print.length() >= 2) print.delete(print.length() - 2, print.length() - 1); if (current.coef == 1) { print.append( "X^" + current.exp + " + "); } else if (current.coef == -1) { print.append("-"+"X^" + current.exp + " + "); } else { print.append(current.coef + "X^" + current.exp + " + "); } } current = current.next; } if (last.coef == 0) print.delete(print.length() - 2, print.length() - 1); System.out.print(print.toString()); } public PolyLinked Polyadd(PolyLinked b) { int max = Math.max(this.frist.exp, b.frist.exp); int i = 0; int[][] sum = new int[max + 1][2]; PolyLinked tem = new PolyLinked(); PolyLinked a = new PolyLinked(); a = this; while (a.frist != null && b.frist != null) { if (a.frist.exp == b.frist.exp) { sum[i][0] = a.frist.coef + b.frist.coef; sum[i][1] = a.frist.exp; a.frist = a.frist.next; b.frist = b.frist.next; i++; } else if (a.frist.exp > b.frist.exp) { sum[i][0] = a.frist.coef; sum[i][1] = a.frist.exp; a.frist = a.frist.next; i++; } else if (b.frist.exp > a.frist.exp) { sum[i][0] = b.frist.coef; sum[i][1] = b.frist.exp; b.frist = b.frist.next; i++; } } while (a.frist != null) { sum[i][0] = a.frist.coef; sum[i][1] = a.frist.exp; a.frist = a.frist.next; i++; } while (b.frist != null) { sum[i][0] = b.frist.coef; sum[i][1] = b.frist.exp; b.frist = b.frist.next; i++; } int maxnuber = i - 1; for (int j = 0; j < maxnuber + 1; j++) { tem.createNode(sum[j][0], sum[j][1]); } return tem; } } public class PolyLinkedAdd { public static void main(String[] args) { int[] coef = new int[10]; int[] exp = new int[10]; PolyLinked a = new PolyLinked(); PolyLinked b = new PolyLinked(); PolyLinked c = new PolyLinked(); Scanner scan = new Scanner(System.in); // 接收鍵盤的輸入 String str = scan.nextLine(); int line = 0; while (!str.equals(" ")) { String[] num = str.split(" "); coef[line] = Integer.parseInt(num[0]); exp[line] = Integer.valueOf(num[1]); line++; str = scan.nextLine(); } scan.close(); /* flag 用於儲存輸入的0 和-1的位置,以便將兩組資料分開 */ int flag[] = new int[2]; int ii = 0; for (int j = 0; j < line; j++) { if (coef[j] == 0 && exp[j] == -1) { flag[ii] = j; ii++; } } /* * data用於儲存兩個多項式的各項係數,長度分別為各多項式的最高項加1 * flag[0]為exp[]中,第一次出現0,-1的位置,所以加1就表示第二組多項式的第一個元素 */ int[] data1 = new int[exp[0] + 1]; int[] data2 = new int[exp[flag[0] + 1] + 1]; /* 前兩個for迴圈,先給多項式的所有係數,初始化為0,後兩個for迴圈再為非零項賦值 */ for (int k = 0; k < data1.length; k++) { data1[k] = 0; } for (int k = 0; k < data2.length; k++) { data2[k] = 0; } for (int k = 0; k < flag[0]; k++) { data1[data1.length - 1 - exp[k]] = coef[k]; } for (int k = flag[0] + 1; k < flag[1]; k++) { data2[data2.length - 1 - exp[k]] = coef[k]; } /* 輸出顯示各多項式! */ System.out.print("原多項式: \nA="); for (int i = 0; i < data1.length; i++) a.createNode(data1[i], data1.length - i - 1); for (int i = 0; i < data2.length; i++) b.createNode(data2[i], data2.length - i - 1); a.printPolyList(); System.out.print("\nB="); b.printPolyList(); System.out.print("\n多項式相加的結果: \nC="); c = a.Polyadd(b); c.printPolyList(); } }