java多项式加减法_Java多项式加法

好吧,在聊天中长时间劳动之后,这就是我们想出来的.我意识到这只是在某种程度上脱口而出.

即便如此,在干净的Java 1.4代码中实现可靠的实现可以帮助您理解.

特别注意以表格形式打印结果,在各自指数的列中对齐不同操作数的术语.

有两个文件:

Node.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) });

}

}

PolynomialAddition.java

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值