Java 多项式作为链表的加法和乘法
我已经把我的代码完全打印出来,并进行了适当的组织(还不包括注释),我完全被困在用两个多项式执行加法和乘法上。谁能给我指出正确的方向吗Java 多项式作为链表的加法和乘法,java,list,polynomials,Java,List,Polynomials,我已经把我的代码完全打印出来,并进行了适当的组织(还不包括注释),我完全被困在用两个多项式执行加法和乘法上。谁能给我指出正确的方向吗 //***************************************************************************************************************** // Polynomial.java // February 21st, 2016 //***********************
//*****************************************************************************************************************
// Polynomial.java
// February 21st, 2016
//*****************************************************************************************************************
/* Assignment 3 for IT Data Structures Module 6
Polynomials as linked lists; stores two
polynomials as two separate linked lists
made up of nodes and allows for addition
and multiplication of the polynomials
*/
import java.util.Scanner;
public class Polynomial
{
private static class Node
{
private int coef;
private int expo;
public Node next;
public Node(int c, int e, Node n)
{
coef = c;
expo = e;
next = n;
}
public int getCoef()
{
return coef;
}
public int getExpo()
{
return expo;
}
public Node getNext()
{
return next;
}
public void setNext(Node n)
{
next = n;
}
}
private Node head = null;
private Node tail = null;
private int size = 0;
public Polynomial()
{
}
public int size()
{
return size;
}
public boolean isEmpty()
{
return size == 0;
}
public void addTerm(int c, int e)
{
Node newest = new Node(c, e, null);
if( isEmpty() )
{
head = newest;
}
else
{
tail.setNext(newest);
}
tail = newest;
size++;
}
public void print()
{
String poly = "";
for(Node i = head; i != null; i = i.next)
{
if(i.coef > 0)
{
poly = poly + " + " + i.coef + "x^" + i.expo;
}
else if(i.coef < 0)
{
poly = poly + " - " + (-i.coef) + "x^" + i.expo;
}
}
System.out.println(poly + "\n");
}
public Polynomial add(Polynomial b)
{
Polynomial a = this;
Polynomial c = new Polynomial();
Node x = a.head;
Node y = b.head;
while( x!= null || y != null)
{
Node t = null;
if (x == null)
{
t = new Node(y.coef, y.expo, y.next);
}
else if (y == null)
{
t = new Node(x.coef, x.expo, x.next);
}
else if (x.expo > y. expo)
{
t = new Node(x.coef, x.expo, x.next);
}
else if (x.expo < y.expo)
{
t = new Node(y.coef, y.expo, y.next);
}
else
{
int coef = x.coef + y.coef;
int expo = x.expo;
Node next = y.next;
x = x.next;
y = y.next;
if (coef == 0)
{
continue;
}
t = new Node(coef, expo, next);
}
c.tail.next = t;
c.tail = c.tail.next;
}
return c;
}
public Polynomial multiply(Polynomial b)
{
Polynomial a = this;
Polynomial c = new Polynomial();
for(Node x = a.head; x != null; x = x.next)
{
Polynomial temp = new Polynomial();
for(Node y = b.head; y != null; y = y.next)
{
temp.tail.next = new Node(x.coef * y.coef, x.expo + y.expo, temp.tail.next);
temp.tail = temp.tail.next;
}
c = c.add(temp);
}
return c;
}
public static void main(String args[])
{
Polynomial p = new Polynomial();
System.out.println("NEW POLYNOMIAL p(x): What order will this polynomial be?");
Scanner scan = new Scanner(System.in);
int o = scan.nextInt();
System.out.println("Enter values for the coefficient and exponent of the first term with each value followed the return key.");
Scanner scan2 = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
p.addTerm(a, b);
for (int i = 1; i < o; i++)
{
System.out.println("Enter values for the coefficient and exponent of the next term with each value followed by the return key.");
int c = scan.nextInt();
int d = scan.nextInt();
p.addTerm(c, d);
}
System.out.println("p(x) => ");
p.print();
Polynomial q = new Polynomial();
System.out.println("NEW POLYNOMIAL q(x): What order will this polynomial be?");
Scanner scan3 = new Scanner(System.in);
int o2 = scan.nextInt();
System.out.println("Enter values for the coefficient and exponent of the first term with each value followed the return key.");
Scanner scan4 = new Scanner(System.in);
int e = scan.nextInt();
int f = scan.nextInt();
q.addTerm(e, f);
for (int j = 1; j < o2; j++)
{
System.out.println("Enter values for the coefficient and exponent of the next term with each value followed by the return key.");
int g = scan.nextInt();
int h = scan.nextInt();
q.addTerm(g, h);
}
System.out.println("q(x) => ");
q.print();
System.out.println("Would you like to add p(x) and q(x)? Type 'yes' or 'no'. ");
Scanner scan5 = new Scanner(System.in);
String option = scan.next();
if( option.equals("yes") )
{
p.add(q);
System.out.println("The sum of p(x) and q(x) is: ");
p.add(q).print();
}
else if( option.equals("no") )
{
System.out.println("Would you like to multiply p(x) and q(x)? Type 'yes' or 'no'. ");
Scanner scan6 = new Scanner(System.in);
String option2 = scan.next();
if( option2.equals("yes") )
{
p.multiply(q);
System.out.println("The product of p(x) and q(x) is: ");
p.multiply(q).print();
}
else
{
System.out.println("Program terminated.");
System.exit(0);
}
}
System.out.println("Program terminated.");
System.exit(0);
}
}
//*****************************************************************************************************************
//多项式.java
//2016年2月21日
//*****************************************************************************************************************
/*IT数据结构模块6作业3
多项式作为链表;存储两个
作为两个独立链表的多项式
由节点组成,允许添加
和多项式的乘法
*/
导入java.util.Scanner;
公共类多项式
{
私有静态类节点
{
私有内部系数;
私人国际展览;
公共节点下一步;
公共节点(int c、int e、Node n)
{
coef=c;
世博会=e;
next=n;
}
公共int getCoef()
{
返回系数;
}
公开展览
{
回归博览会;
}
公共节点getNext()
{
下一步返回;
}
公共void setNext(节点n)
{
next=n;
}
}
私有节点头=null;
私有节点tail=null;
私有整数大小=0;
公共多项式()
{
}
公共整数大小()
{
返回大小;
}
公共布尔值为空()
{
返回大小==0;
}
公共无效添加项(整数c、整数e)
{
节点最新=新节点(c、e、null);
if(isEmpty())
{
头=最新的;
}
其他的
{
tail.setNext(最新);
}
尾巴=最新的;
大小++;
}
公开作废印刷品()
{
字符串poly=“”;
for(节点i=head;i!=null;i=i.next)
{
如果(i.coef>0)
{
poly=poly+“+”+i.coef+“x^”+i.expo;
}
否则如果(i.coef<0)
{
多边形=多边形+“-”+(-i.coef)+“x^”+i.expo;
}
}
System.out.println(poly+“\n”);
}
公共多项式加法(多项式b)
{
多项式a=这个;
多项式c=新多项式();
节点x=a.head;
节点y=b.head;
而(x!=null | | y!=null)
{
节点t=null;
如果(x==null)
{
t=新节点(y.coef,y.expo,y.next);
}
如果(y==null),则为else
{
t=新节点(x.coef,x.expo,x.next);
}
否则如果(x.expo>y.expo)
{
t=新节点(x.coef,x.expo,x.next);
}
否则如果(x.expo”;
p、 打印();
多项式q=新多项式();
新多项式q(x):这个多项式的阶数是多少;
Scanner scan3=新扫描仪(System.in);
int o2=scan.nextInt();
System.out.println(“输入第一项的系数和指数值,每个值后跟返回键”);
Scanner scan4=新扫描仪(System.in);
int e=scan.nextInt();
int f=scan.nextInt();
q、 addTerm(e,f);
对于(int j=1;j”;
q、 打印();
System.out.println(“是否要添加p(x)和q(x)?键入“是”或“否”);
扫描仪scan5=新扫描仪(System.in);
字符串选项=scan.next();
如果(选择