1
我正在讀取.txt文件以創建多項式。我在實際打印多項式時遇到了困難(在將它們放入鏈表之後)。我真的不知道如何去 '聯' 的鏈表,多項式方法...從Int文本文件創建一個多項式。
文本文件:
P1 = 3 5 1 -1 0 8
P2 = 5 6 2 -1 1 7 0 -4
p3 = p1 + p2
p4 = p3 - p1
代碼:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Polynomial {
public Term first;
public Term last;
private int[] coef; // coefficients
private int deg; // degree of polynomial (0 for the zero polynomial)
// a * x^b
public Polynomial(int a, int b) {
coef = new int[b + 1];
coef[b] = a;
deg = degree();
}
// return the degree of this polynomial (0 for the zero polynomial)
public int degree() {
int d = 0;
for (int i = 0; i < coef.length; i++)
if (coef[i] != 0)
d = i;
return d;
}
// return c = a + b
public Polynomial plus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++)
c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++)
c.coef[i] += b.coef[i];
c.deg = c.degree();
return c;
}
// return (a - b)
public Polynomial minus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++)
c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++)
c.coef[i] -= b.coef[i];
c.deg = c.degree();
return c;
}
// convert to string representation
public String toString() {
if (deg == 0)
return "" + coef[0];
if (deg == 1)
return coef[1] + "x + " + coef[0];
String s = coef[deg] + "x^" + deg;
for (int i = deg - 1; i >= 0; i--) {
if (coef[i] == 0)
continue;
else if (coef[i] > 0)
s = s + " + " + (coef[i]);
else if (coef[i] < 0)
s = s + " - " + (-coef[i]);
if (i == 1)
s = s + "x";
else if (i > 1)
s = s + "x^" + i;
}
return s;
}
// test client
public static void main(String[] args) throws IOException {
// Welcome message
System.out
.println("Welcome! The following program processes single-variable polynomials represented as linked lists.\n"
+ "Test Data will appear below, and is also saved to a text file (userSpecification.txt) \n"
+ "-------------------------------" + "\n");
String content = new String();
String name = new String();
File file = new File("polynomialTest.txt");
LinkedList<String> list = new LinkedList<String>();
try {
Scanner sc = new Scanner(new FileInputStream(file));
while (sc.hasNext()) {
name = sc.next();
content = sc.nextLine();
// ..just checking things as they come in.
System.out.println("name " + name + " content " + content);
list.add(content);
}
sc.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
System.out.println("\nProgram terminated Safely...");
}
Iterator<String> i = list.iterator();
while (i.hasNext()) {
System.out.println(name + i.next() + "\n");
}
private class Term {
int coef;
int expo;
Term next;
Term(int coef, int expo, Term n) {
this.coef = coef;
this.expo = expo;
this.next = n;
}
}
}
所需的輸出:
P1 = 5X^3 – 4X + 8
P2 = 6X^5 -2X^2 +7X -4
P3 = 6X^5 +5X^3 -2X^2 +3X +4
P4 = 6X^5 -2X^2 +7X -4
現在輸出:
Project #2
Welcome! The following program processes single-variable polynomials represented as linked lists.
-------------------------------
P1 = 3 5 1 -1 0 8
P2 = 5 6 2 -1 1 7 0 -4
p3 = p1 + p2
p4 = p3 - p1
[P1 = 3 5 1 -1 0 8, P2 = 5 6 2 -1 1 7 0 -4, p3 = p1 + p2, p4 = p3 - p1]
它必須是:P1 = 5X^3 - X + 8,您的第一個等式不正確,或者您的輸入數據不正確。 – eternaln00b 2012-02-11 05:44:05
謝謝,我並沒有太擔心數字,因爲我得到的多項式實際顯示(如預期的輸出)。雖然好,但我會改變的! – tommy1370 2012-02-12 19:33:26