我正在使用 String Tokenizer 和 Linked Lists,并且此作业需要 Linked Lists。有很多行多项式(每行一个)的外部文件。使用字符串标记器和链表,我正在运行一个 while 循环,它在每次传递时捕获两行并将它们添加到链表中。将数字加载到链表后,目标是将这些多项式从它们的链表中添加在一起,并创建一个包含该多项式的新链表。
例如,文件中的前两行是这样的:
2x^4 -5x^3 +9x^2 -10
3x^4 -6x^3 +10x^2 -11
=
5x^4 -11x^3 +19x^2 -21
这是我的代码:
public class PolynomialAddition
{
static File dataInpt;
static Scanner inFile;
public static void main(String[] args) throws IOException
{
dataInpt=new File("C:\\llpoly.txt");
inFile=new Scanner(dataInpt);
StringTokenizer myTokens;
String line,polyTerm;
Node firstL=new Node();
Node secondL=new Node();
line=inFile.nextLine();
myTokens=new StringTokenizer(line);
polyTerm=myTokens.nextToken();
firstL.value=polyTerm.substring(0,polyTerm.indexOf("x"));
firstL.value2=polyTerm.substring(polyTerm.indexOf("^")+1);
}
}
这是我的节点类:
public class Node
{
public Object value;
public Object value2;
public Node next;
public Node()
{
value=null;
value2=null;
next=null;
}
public Node (Object value, Object value2, Node next)
{
this.value=value;
this.value2=value2;
this.next=next;
}
}
问题出现在此之后,其中某些行不完整,而它们必须添加到的行是完整的,例如 -12x^8 +5x^2 -3 和 8x^3 +2x
答案应该是 -12x^8 +8x^3 +5x^2 +2x -3
我能做些什么来解决这个问题?
最佳答案
好吧,经过长时间的聊天,这就是“我们”想出来的。我意识到这在某种程度上只是在脱口而出答案。
即便如此,在简洁风格的 Java 1.4 代码中有一个可靠的实现可以帮助您理解很多。
特别注意以表格形式打印结果,在其各自指数的列中对齐不同操作数的项。
代码
有两个文件:
节点.java
class Node {
int factor;
int exponent;
Node next;
public Node() {
factor = 0;
exponent = 0;
next = null;
}
public Node(int factor, int exponent, Node next) {
this.factor = factor;
this.exponent = exponent;
this.next = next;
}
public String toString() {
return String.format("%+4dx^%d ", new Integer[] { new Integer(factor), new Integer(exponent) });
}
}
多项式加法
import java.io.*;
import java.util.*;
public class PolynomialAddition {
static File dataInpt;
static Scanner inFile;
public static void main(String[] args) throws IOException {
dataInpt = new File("/tmp/input.txt");
inFile = new Scanner(dataInpt);
while (inFile.hasNextLine()) {
Node first = readPolynomial();
// printList(first);
Node second = readPolynomial();
// printList(second);
Node addition = addPolynomials(first, second);
// printList(addition);
printTabulated(first, second, addition);
System.out.println("\n");
}
}
private static Node addPolynomials(Node first, Node second) {
Node head = null, current = null;
while (null!=first || null!=second)
{
boolean pickfirst = false;
boolean haveBoth = (null!=first && null!=second);
Node node;
if (haveBoth && first.exponent == second.exponent)
{
node = new Node(first.factor + second.factor, first.exponent, null);
first = first.next;
second = second.next;
} else
{
pickfirst = first!=null &&
((second == null) || first.exponent > second.exponent);
if (pickfirst)
{
node = new Node(first.factor, first.exponent, null);
first = first.next;
} else
{
node = new Node(second.factor, second.exponent, null);
second = second.next;
}
}
if (current == null)
{
head = node;
current = head;
} else
{
current.next = node;
current = node;
}
}
return head;
}
private static void printTabulated(Node first, Node second, Node addition) {
String line1="", line2="", barline="", line3="";
while (addition != null)
{
String
part1 = " ",
part2 = " ",
part3 = " ";
if (null!=first && first.exponent == addition.exponent)
{
part1 = first.toString();
first = first.next;
}
if (null!=second && second.exponent == addition.exponent)
{
part2 = second.toString();
second = second.next;
}
part3 = addition.toString();
addition = addition.next;
line1 += part1;
line2 += part2;
barline += "-----------";
line3 += part3;
}
System.out.println(line1);
System.out.println(line2);
System.out.println(barline);
System.out.println(line3);
}
private static Node readPolynomial() {
String line = inFile.nextLine();
StringTokenizer myTokens = new StringTokenizer(line);
Node head = null, previous = null;
while (myTokens.hasMoreTokens()) {
Node current = new Node();
String term = myTokens.nextToken();
if (term.startsWith("+"))
term = term.substring(1);
current.factor = Integer.parseInt(
term.substring(0, term.indexOf("x")));
current.exponent = Integer.parseInt(
term.substring(term.indexOf("^") + 1));
if (previous == null)
{
head = current;
previous = head;
} else
{
previous.next = current;
previous = current;
}
}
return head;
}
private static void printList(Node head) {
for (Node ptr = head; ptr != null; ptr = ptr.next)
System.out.print(ptr);
System.out.println();
}
}
样本数据:
输入:
2x^4 -5x^3 +9x^2 -10x^0
3x^4 -6x^3 +10x^2 -11x^0
-2x^1 +4x^0
2x^1 -4x^0
8x^5 +6x^4 +5x^2 -3x^0
-12x^8 +2x^7 +14x^5
1x^5 +7x^2 +8x^1
-5x^4 -7x^3 -4x^2 -3x^0
10x^5 -3x^3 +4x^2 -234x^1 -12x^0
-5x^5 -2x^3 +10x^0
输出:
+2x^4 -5x^3 +9x^2 -10x^0
+3x^4 -6x^3 +10x^2 -11x^0
--------------------------------------------
+5x^4 -11x^3 +19x^2 -21x^0
-2x^1 +4x^0
+2x^1 -4x^0
----------------------
+0x^1 +0x^0
+8x^5 +6x^4 +5x^2 -3x^0
-12x^8 +2x^7 +14x^5
------------------------------------------------------------------
-12x^8 +2x^7 +22x^5 +6x^4 +5x^2 -3x^0
+1x^5 +7x^2 +8x^1
-5x^4 -7x^3 -4x^2 -3x^0
------------------------------------------------------------------
+1x^5 -5x^4 -7x^3 +3x^2 +8x^1 -3x^0
+10x^5 -3x^3 +4x^2 -234x^1 -12x^0
-5x^5 -2x^3 +10x^0
-------------------------------------------------------
+5x^5 -5x^3 +4x^2 -234x^1 -2x^0