算法实现
该程序共包含了一个接口、一个类和一个主类。Listter接口定义了顺序表中的方法名称。在类ArrayList中继承了该接口并实现了每一个方法。在Text类中实现一元稀疏多项式相加的方法,在主函数中构建实现多项式计算的过程。在完善创建多项式、加、减和乘的方法,最后在改写toString方法实现输出。
源码如下
import java.util.Scanner;
public class Test {
static ArrayList CalculationA;
static ArrayList CalculationB;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
CalculationA = new ArrayList();
CalculationB = new ArrayList();
init();
System.out.print("输入一元多项式A");
String A = in.next();
System.out.print("输入一元多项式B");
String B = in.next();
transformationA(A);//将A式子存储到顺序表
transformationB(B);//将B式子存储到顺序表
System.out.println(add());
System.out.println(subtract());
System.out.println(multiplicate());
}
private static void init() {
for (int i = 0; i < 1000; i++) {
CalculationA.add(0);
CalculationB.add(0);
}
}
private static void transformationA(String a) {
a = a.replaceAll("-", "+-");
String []s1 = a.split("[+]");
for (int i = 0; i < s1.length; i++) {
String temp = s1[i];
if (temp.equals("")) continue;
String []s2 = temp.split("\^");
String []s3 = temp.split("x");
int index = Integer.parseInt(s2[1]);
int number = Integer.parseInt(s3[0]);
CalculationA.set(index, number + CalculationA.get(index));
}
}
private static void transformationB(String a) {
a = a.replaceAll("-", "+-");
String []s1 = a.split("[+]");
for (int i = 0; i < s1.length; i++) {
String temp = s1[i];
if (temp.equals("")) continue;
String []s2 = temp.split("\^");
String []s3 = temp.split("x");
int index = Integer.parseInt(s2[1]);
int number = Integer.parseInt(s3[0]);
CalculationB.set(index, number + CalculationB.get(index));
}
}
public static String add() {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < 1000; i++) {
if (CalculationA.get(i) != 0 || CalculationB.get(i) != 0) {
int temp = CalculationA.get(i) + CalculationB.get(i);
if (temp != 0) {
if (temp < 0) {
ans.append(temp + "x^" + i);
}else {
ans.append("+" + temp + "x^" + i);
}
}
}
}
System.out.println("该两个式子相加:");
return ans.toString();
}
public static String subtract() {
StringBuilder ans = new StringBuilder();
for (int i = 0; i < 1000; i++) {
if (CalculationA.get(i) != 0 || CalculationB.get(i) != 0) {
int temp = CalculationA.get(i) - CalculationB.get(i);
if (temp != 0) {
if (temp < 0) {
ans.append(temp + "x^" + i);
}else {
ans.append("+" + temp + "x^" + i);
}
}
}
}
System.out.println("该两个式子相减:");
return ans.toString();
}
public static String multiplicate() {
ArrayList temp = new ArrayList();
for (int i = 0; i < 2000; i++) {
temp.add(0);
}
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
int x1 = CalculationA.get(i);
int x2 = CalculationB.get(j);
int num = x1*x2;
int index = i + j;
temp.set(index, temp.get(index) + num);
if (num != 0) {
}
}
}
StringBuilder ans = new StringBuilder();
for (int i = 0; i < 2000; i++) {
if (temp.get(i) != 0) {
if (temp.get(i) > 0) {
ans.append("+" + temp.get(i) + "x^" + i);
}else {
ans.append(temp.get(i) + "x^" + i);
}
}
}
System.out.println("该两个式子相乘:");
return ans.toString();
}
}
import java.util.Comparator; public class ArrayList{ //设置线性表的默认容量 private static int DEFAULT_CAPACITY = 10; private E[] data; private int size; public ArrayList() { this(DEFAULT_CAPACITY); } public ArrayList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("位置不合法"); } data = (E[])new Object[capacity]; size = 0; } public void add(E element) { add(size ,element); } public void add(int index, E element) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("位置不合法"); } if (size == data.length) { resize (data.length*2); } for (int i = size - 1; i >= index ;i++) { data[i+1] = data[i]; } data[index] = element; size++; } private void resize(int newLength) { E[] newData = (E[])new Object[newLength]; for (int i = 0;i < data.length;i++) { newData[i] = data[i]; } data = newData; } public void remove(E element) { int index = indexOf(element); if (index != -1) { remove(index); } } public E remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("位置不合法"); } E ret = data[index]; for (int i = index + 1;i DEFAULT_CAPACITY) { resize (data.length / 2); } return ret; } public E get(int index) { if (index < 0 || index >=size) { throw new IndexOutOfBoundsException("角标不合法"); } return data[index]; } public E set(int index, E element) { if (index < 0 || index >=size) { throw new IndexOutOfBoundsException("角标不合法"); } E ret = data[index]; data[index] = element; return ret; } public int size() { return size; } public int indexOf(E element) { for (int i = 0;i < data.length ;i++) { if (data[i].equals(element)) { return i; } } return -1; } public boolean contains(E element) { return indexOf(element) != -1; } public boolean isEmpty() { return size == 0; } public void clear() { data = (E[]) new Object[DEFAULT_CAPACITY]; size = 0; } public void sort(Comparator c) { if (c == null) { throw new IllegalArgumentException("角标不合发"); } int j = 0; E e = null; //O(n) for (int i = 1; i < size; i++) { e = data[i]; for(j = i; j > 0 && c.compare(data[j - 1], e) > 0; j--) { data[j] = data[j - 1]; } data[j] = e; } } }



