📄 polynomial.java
字号:
/**
* @(#)Polynomial.java
*
*
* @author
* @version 1.00 2006/10/25
*/
package Polynomial;
import java.io.*;
import java.lang.*;
public class Polynomial {
private LinkedList terms;
private LinkedListItr itr;
public Polynomial() {
zeroPolynomial();
itr=terms.zeroth();
}
/** This method is to initialize the polynomial
**/
public void zeroPolynomial(){
terms=new LinkedList();
}
public LinkedListItr getFirst(){
return itr;
}
public void print(){
LinkedListItr itr=terms.first();
CoefAndExp p=null;
if(itr==null)
System.out.println("The result of adding these two polynomial is zero.");
else{
p=itr.retrieve();
p.print(true);
itr.advance();
for(;!itr.isPastEnd();itr.advance()){
p=itr.retrieve();
p.print(false);
}
System.out.println();
}
}
private void build(int[] coef,int[] exp){
CoefAndExp parameter;
LinkedListItr itr=terms.zeroth();
for(int i=0;i<coef.length&&exp[i]!=-1;i++){
if(exp[i]>0){
parameter=new CoefAndExp(coef[i],exp[i]);
terms.insert(parameter,itr);
itr.advance();
}
else if(exp[i]==0){
parameter=new CoefAndExp(coef[i],0);
itr.advance();
terms.insert(parameter,itr);
}
}
}
public void read(){
String poly="";
int length=0;
char operator='1';
boolean positive=false;
int next=0;
int start=0;
int i=0;
System.out.println("Please enter a polynomial(such as 6x^5+2x^2-x+2):");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try{
poly=in.readLine();
}catch(IOException e){}
int[] coef=new int[poly.length()];
int[] exp=new int[poly.length()];
for(int j=0;j<poly.length();j++)
exp[j]=-1;
while(next!=-1&&poly.length()>1&&next!=poly.length()-1){
next=0;
next=poly.indexOf("x",next);
if(next!=-1){
operator=poly.charAt(0);
if(operator=='+'){
positive=true;
start=1;
}
else if(operator=='-'){
positive=false;
start=1;
}
else if(operator<='9'&&operator>='1')
positive=true;
if(start==next)
coef[i]=1;
else if(start!=next){
coef[i]=Integer.parseInt(poly.substring(start,next));
if(!positive)
coef[i]=-coef[i];
}
if(next==poly.length()-1){
exp[i]=1;
break;
}
next++;
if(poly.charAt(next)=='^'){
start=next+1;
next=start;
operator=poly.charAt(next);
for(;operator!='+'&&operator!='-'&&next!=poly.length()-2;)
operator=poly.charAt(++next);
exp[i]=Integer.parseInt(poly.substring(start,next));
}
else
exp[i]=1;
}
if(next!=-1)
poly=poly.substring(next);
start=0;
i++;
}
start=0;
next=0;
if(poly.indexOf("x",0)==-1){
operator=poly.charAt(0);
if(operator=='+')
positive=true;
else positive=false;
start++;
coef[i]=Integer.parseInt(poly.substring(start));
exp[i]=0;
if(!positive)
coef[i]=-coef[i];
}
}
public void add(Polynomial rhs){
LinkedListItr itr1,itr2,itr3;
itr1=terms.zeroth();
itr2=terms.first();
itr3=rhs.getFirst();
CoefAndExp p1=null,p2=null;
if(itr1.isPastEnd()&&!itr3.isPastEnd()){
rhs.print();
return ;
}
else
if(itr1.isPastEnd()&&itr3.isPastEnd())
System.out.println("You have input nothing.");
else
if(!itr1.isPastEnd()&&itr.isPastEnd())
print();
else{
for(;!itr2.isPastEnd()&&!itr3.isPastEnd();itr2.advance()){
p1=itr2.retrieve();
p2=itr3.retrieve();
if(p1.equal(p2)==0){
p1.add(p2.getCoef());
itr3.advance();
if(p1.getCoef()==0)
terms.remove(p1);
itr2.advance();
}
else if(p1.equal(p2)==-1){
terms.insert(p2,itr1);
itr1=itr2;
itr2.advance();
itr3.advance();
}
else{
itr1=itr2;
itr2.advance();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -