有java高手吗!求编一个程序。内容是 通过键盘输入两个一元多项式headA和headB,要求能够
有java高手吗!求编一个程序。内容是通过键盘输入两个一元多项式headA和headB,要求能够按照指数升序排列,输出多项式。对两个一元多项式进行相减运算,结果存储到he...
有java高手吗!求编一个程序。内容是 通过键盘输入两个一元多项式headA和headB,要求能够按照指数升序排列,输出多项式。对两个一元多项式进行相减运算,结果存储到headA中并输出
展开
3个回答
展开全部
两个类,构造多项式。
怎么输入自己设计一下
//多项式节点
public class PolyNode {
private int cofficient;
//多项式节点系数
private int index;
//多项式节点指数
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getCofficient() {
return cofficient;
}
public void setCofficient(int cofficient) {
this.cofficient = cofficient;
}
public PolyNode(int _cofficient,int _index) {
this.cofficient = _cofficient;
this.index = _index;
}
public PolyNode() {
this(0,0);
}
public PolyNode(PolyNode node) {
this(node.cofficient,node.index);
}
}
import java.util.*;
//派生一个多项式排序类
class SortType implements Comparator<PolyNode>{
public int compare(PolyNode o1, PolyNode o2) {
if(o1.getIndex() == o2.getIndex())
return o1.getCofficient() - o2.getCofficient();
else
return o1.getIndex() - o2.getIndex();
}
}
public class Polynomial {
//多项式列表
ArrayList<PolyNode> polylist;
//多项式列表是否为空
public boolean isEmpty() {
return this.polylist.isEmpty();
}
//添加多项式
public void AddPolyNode(PolyNode node) {
this.polylist.add(node);
Collections.sort(this.polylist, new SortType());
}
//合并同类项
public void MergeSame() {
int len = this.polylist.size();
int current = 0;
int next = current + 1;
while(current < len) {
next = current + 1;
while(next < len) {
if(this.polylist.get(current).getIndex() != this.polylist.get(next).getIndex()) {
next++;
}
else {
this.polylist.get(current).setCofficient(this.polylist.get(current).getCofficient() + this.polylist.get(next).getCofficient());
//将next的系数合并到current
this.polylist.remove(next);
len--;
//删除第next个项
}
}
current++;
}
for(int i = 0; i < this.polylist.size();) {
//删除系数为0的项
if(this.polylist.get(i).getCofficient() == 0){
this.polylist.remove(i);
continue;
}
i++;
}
}
//多项式加法运算
public static Polynomial Plus(Polynomial p1,Polynomial p2) {
Polynomial result = new Polynomial();
int len1 = p1.polylist.size();
int len2 = p2.polylist.size();
int i = 0,j = 0;
while(i < len1 && j < len2){
//如果指数相等,合并两项
if(p1.polylist.get(i).getIndex() == p2.polylist.get(j).getIndex()) {
int k = p1.polylist.get(i).getCofficient() + p2.polylist.get(j).getCofficient();
result.AddPolyNode(new PolyNode(k,p1.polylist.get(i).getIndex()));
i++;
j++;
}
else if(p1.polylist.get(i).getIndex() < p2.polylist.get(j).getIndex()) {
result.AddPolyNode(new PolyNode(p1.polylist.get(i)));
i++;
}
else {
result.AddPolyNode(new PolyNode(p2.polylist.get(j)));
j++;
}
}
while(i < len1) {
result.AddPolyNode(new PolyNode(p1.polylist.get(i)));
i++;
}
while(j < len2) {
result.AddPolyNode(new PolyNode(p2.polylist.get(j)));
j++;
}
result.MergeSame(); //合并同类项
return result;
}
//多项式乘法运算
public static Polynomial Multiply(Polynomial p1,Polynomial p2) {
Polynomial result = new Polynomial();
int len1 = p1.polylist.size();
int len2 = p2.polylist.size();
int i = 0,j = 0;
while(i < len1) {
j = 0;
while(j < len2) {
int k = p1.polylist.get(i).getCofficient()*p2.polylist.get(j).getCofficient();
int e = p1.polylist.get(i).getIndex() + p2.polylist.get(j).getIndex();
result.AddPolyNode(new PolyNode(k,e));
j++;
}
i++;
}
result.MergeSame(); //合并同类项
return result;
}
//打印多项式
public String toString() {
StringBuilder str = new StringBuilder("");
int num = this.polylist.size();
for(int i = num - 1; i >= 0; i--) {
str.append((this.polylist.get(i).getCofficient() > 0 ? (i == num - 1 ? "" : "+") : "-") +
(Math.abs(this.polylist.get(i).getCofficient()) == 1 && this.polylist.get(i).getIndex() > 0 ? "" : Math.abs(this.polylist.get(i).getCofficient())) +
(this.polylist.get(i).getIndex() > 0 ? "X" + (this.polylist.get(i).getIndex() > 1 ? "^" + this.polylist.get(i).getIndex() : "") : "")
);
}
return str.toString();
}
//Constructor
public Polynomial() {
polylist = new ArrayList<PolyNode>();
//初始化多项式列表
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询