<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();
}
}
java 链表实现多项式加法!
最新推荐文章于 2024-07-14 15:47:25 发布