📄 termnode.java
字号:
import java.util.Vector;
/**Class TermNode<br/>
*This class is data type to store the term of the polynomial
*It contians several attributes such as coefficient,veriable
*and exponent.Also it contains the mothod to analyse the expression so to ensure the coefficient and variables.
*@author zhlmmc
*@version 1.0
*/
public class TermNode
{
/**the term's simplest expression
*/
String s = "";
/**the term's coefficient
*/
private long coefficient;
private int pos;
Vector variableName = new Vector();
Vector exponent = new Vector();
/**
*constructors
*TermNode() receive no arguments
*/
public TermNode()
{
coefficient = 0;
pos = 0;
}
/**
*constructors
*TermNode(String input)
*@param input the term's expression
*/
public TermNode(String input)
{
this();
s = analyse(input);
}
/**
*method analyse(String input)
*to analyse the input
*and to determin the coefficient and the variable.
*call methods analyseCoefficient and analyseVariable
*@param input the expression of the term
*@return the simplest expression of the term
*/
public String analyse(String input)
{
//this method maby invoked by another method,so the vectors must clare and pos set to "0"
variableName.clear();
exponent.clear();
pos = 0;
String output = "";//to store the analysed data
coefficient = analyseCoefficient(input);
output += coefficient;
//analyse variable
if (pos <= input.length() - 1)
{
analyseVariable(input);
}
//set output
for (int i = 0;i <= variableName.size() - 1 ;i++ )
{
output +=variableName.get(i);
if (((Integer)exponent.get(i)).intValue() > 1)
{
output += "^" + exponent.get(i);
}
}
return output;
}
/**
*analyse coefficient
*/
private long analyseCoefficient(String input)
{
int i = 0;//i to store the index of the first variable
int j = 0;//j to store the index of the firse number
//default coefficient is 1
coefficient = 1;
//deal the negative coefficient
if (input.charAt(0) == '-')
{
coefficient *= -1;
j++;
}
//find the first letter so to find the number
while ((i < input.length()) && (!Character.isLetter(input.charAt(i))))
{
i++;
}
if (i >= 10)
{
pos = i;
return 200000001;
}
//System.out.println(j + "" +i);
if ((i - j) > 0)
{
coefficient *= (Integer.valueOf(input.substring(j,i))).intValue();
}
pos = i;
return coefficient;
}
/**
*analyse variable
*/
private void analyseVariable(String input)
{
do
{
//the current variable's name
Character tempName = new Character(input.charAt(pos));
//the current variable's exponent
int exp;
if (pos < input.length() - 1)
{
exp = getExp(input);
}
else exp = 1;
//judge if the term has already had the variablenanme add the current exponent to the original exponent
//else add the name to the term's variablename
if (variableName.contains(tempName))
{
int a = variableName.indexOf(tempName);//a to store the index of the object
exp +=((Integer)exponent.get(a)).intValue();
Integer tempInt = new Integer(exp);
exponent.set(a,tempInt);
}
else
{
variableName.add(tempName);
Integer tempInt = new Integer(exp);
exponent.add(tempInt);
}
//search the next variable
do
{
pos++;
}
while ((pos < input.length()) && !Character.isLetter(input.charAt(pos)));
}
while (pos < input.length());
//the variables have to be sort by the specified squence so that it will be easy to compare two terms
sortVariable();
}
/**method sortVariable
*sort the variable by the variableName's dictionary sort
*/
private void sortVariable()
{
for (int i = variableName.size() - 1;i > 0;i-- )
{
for (int j = 0;j < i ;j++ )
{
if (((Character)variableName.get(j)).charValue() > ((Character)variableName.get(j + 1)).charValue())
{
change(j);
}
}
}
}
/**method change
*chang the specified variableName and the corresponding exponent
*@param i the first variable's index
*/
private void change(int i)
{
char tempName = ((Character)variableName.get(i)).charValue();
int tempExp = ((Integer)exponent.get(i)).intValue();
variableName.setElementAt(new Character(((Character)variableName.get(i+1)).charValue()),i);
exponent.setElementAt(new Integer(((Integer)exponent.get(i+1)).intValue()),i);
variableName.setElementAt(new Character(tempName),i + 1);
exponent.setElementAt(new Integer(tempExp),i + 1);
}
/**method getExp
*/
private int getExp(String input)
{
String exp = "";
if (input.charAt(pos + 1) == '^')
{
int i = 2;
//get the nubmber
while ((pos + i < input.length()) && Character.isDigit(input.charAt(pos + i)))
{
exp += input.charAt(pos + i);
i++;
}
return Integer.valueOf(exp).intValue();
}
else return 1;
}
/**method getCoefficient
*@return the value of the terms coefficient
*/
public long getCoefficient()
{
return coefficient;
}
/**method setCoefficient
*this method to modify the term's coefficient
*and it changes the s
*@param m the requied coefficient
*/
public void setCoefficient(long m)
{
if (m > 200000000)
{
m = 200000001;
}
coefficient = m;
int i = 0;
if (s.charAt(0) == '-')
{
i++;
}
while ((i < s.length()) && Character.isDigit(s.charAt(i)))
{
i++;
}
s = coefficient + s.substring(i,s.length());
}
/**method clone
*copy this term and return it
*@return the same Object as this
*/
public Object clone()
{
return new TermNode(this.s);
}
/**
*method toString
*return the polynomial type of the term
@return the terms simplest expression in String type
*/
public String toString()
{
if (variableName.isEmpty())
{
if (coefficient > 0)
{
return "+" + String.valueOf(coefficient);
}
else if (coefficient < 0)
{
return String.valueOf(coefficient);
}
else return "";
}
if (coefficient == 0)
{
return "";
}
if (coefficient == 1)
{
return "+" + s.substring(1,s.length());
}
if (coefficient > 1)
{
return "+" + s;
}
if (coefficient < -1)
{
return s;
}
if (coefficient == -1)
{
return "-" + s.substring(2,s.length());
}
return "";
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -