多项式项的Java-toString方法
我创建了一个没有使用多项式的多项式类,我使用自己的多项式项的Java-toString方法,java,tostring,polynomial-math,Java,Tostring,Polynomial Math,我创建了一个没有使用多项式的多项式类,我使用自己的项(系数、指数)来创建多项式表达式 我有以下几个条件: coefficient = 0 -> Term(0,2) -> 0x^2 -> "0" coefficient = 1 -> Term(1,2) -> 1x^2 -> "x^2" coefficient = -1 -> Term(-1,2) -> -1x^2 -> "-x^2" exponent = 1 = -> Term(5,1)
项(系数、指数)
来创建多项式表达式
我有以下几个条件:
coefficient = 0 -> Term(0,2) -> 0x^2 -> "0"
coefficient = 1 -> Term(1,2) -> 1x^2 -> "x^2"
coefficient = -1 -> Term(-1,2) -> -1x^2 -> "-x^2"
exponent = 1 = -> Term(5,1) -> 5x^1 -> "5x"
exponent = 0 = -> Term(5,0) -> 5x^0 -> "5"
但是实现所有这些功能并使其相互作用会让我非常头疼,例如,如果我有
术语(-1,1)
,我希望出现“-x”
,对于术语(1,1)
,则出现“x”
。有人能帮我想出一些逻辑,把所有这些“规则”组合在一起,形成一个toString方法吗?这里的顺序很重要。如果你仔细考虑一下,你会发现系数=0应该是第一位的,因为当它为零时,其他的一切都无关紧要。接下来是指数等于1
或0
时的特殊情况。当系数为-1
时,就有了。然后剩下的是除-1
之外的负系数
或正系数
的默认情况。因此,if语句应该如下所示:
public String toString() {
if(coefficient == 0){
return "0";
} else if ( coefficient == 1 && exponent != 0){
return "x^"+exponent;
} else if ( exponent == 1){
return coefficient+"x";
} else if ( exponent == 0){
return ""+coefficient;
} else if ( coefficient == -1){
return "-x^"+exponent;
} else {
return coefficient+"x^"+exponent;
}
}
嗯,你真的没有什么办法让它变得更容易。您基本上有以下情况:
系数=0-->显示0(指数无关)
系数=+/-1-->显示-if-1(特例为(-1,0)),否则不显示任何内容
其他系数-->显示为已存储
指数=0-->不显示任何内容
否则,请显示x^指数
所以
public String toString() {
String term = "";
if (coefficient == 0) {
return "0";
} elseif (coefficient == -1) {
if (exponent == 0) {
return "-1";
} else {
term += "-";
}
} elseif (coefficient != 1) {
term += String.valueOf(coefficient);
} else {
if (exponent == 0) {
return "1";
}
}
if (exponent != 0) {
if (exponent == 1) {
term += "x";
} else {
term += "x^" + String.valueOf(exponent);
}
}
return term;
}
我想这就够了吧?我没有通过UnitTest来真正确定它。您可以将第一个特殊情况与最后一个特殊情况结合起来。您还可以通过查看系数的abs
值来组合第二种和第三种情况
// If the coefficient is zero or the exponent is zero,
// the result is simply the coefficient:
if (c == 0 || e == 0) {
return ""+c;
}
StringBuilder res = new StringBuilder();
// Do not print the number for the coefficient of +/- 1
if (Math.abs(c) == 1) {
// For +1 do not print the sign either
if (c == -1) {
res.append("-");
}
} else {
res.append(c);
}
res.append("x");
// For exponent of 1, do not print ^1
if (e != 1) {
res.append("^");
res.append(e);
}
return res.toString();
这是我想说的最接近的了
public class Term {
private final int coefficient;
private final int exponent;
public Term (final int coefficient,final int exponent) {
this.coefficient = coefficient;
this.exponent = exponent;
}
@Override
public String toString() {
final String sign = getSign (coefficient);
final String number = getNumber (coefficient);
final String exponentStr = getExponentStr (coefficient, exponent);
return String.format ("%s%s%s",sign, number, exponentStr);
}
private String getExponentStr(final int coefficient, final int exponent) {
if (coefficient == 0 || exponent == 0) {
return "";
}
if (exponent == 1) {
return "x";
}
return "x^" + exponent;
}
private String getNumber(final int value) {
final int absValue = Math.abs(value);
return absValue == 1 ? "" : Integer.toString (absValue);
}
private String getSign(final int value) {
return value < 0 ? "-" : "";
}
public static void main(String[] args) throws Exception {
System.out.println(new Term (0, 2));
System.out.println(new Term (1, 2));
System.out.println(new Term (-1, 2));
System.out.println(new Term (5, 1));
System.out.println(new Term (5, 0));
}
}
公共类术语{
私有最终整数系数;
私有最终整数指数;
公共术语(最终整数系数、最终整数指数){
这个系数=系数;
这个指数=指数;
}
@凌驾
公共字符串toString(){
最终字符串符号=getSign(系数);
最终字符串编号=getNumber(系数);
最终字符串exponentStr=getExponentStr(系数,指数);
返回String.format(“%s%s%s”,符号,数字,指数str);
}
私有字符串getExponentStr(最终整数系数,最终整数指数){
如果(系数=0 | |指数=0){
返回“”;
}
如果(指数=1){
返回“x”;
}
返回“x^”+指数;
}
私有字符串getNumber(最终整数值){
最终int abs值=数学abs(值);
返回absValue==1?“:Integer.toString(absValue);
}
私有字符串getSign(最终整数值){
返回值<0?”-“:”;
}
公共静态void main(字符串[]args)引发异常{
系统输出println(新术语(0,2));
系统输出打印LN(新术语(1,2));
系统输出println(新术语(-1,2));
系统输出打印LN(新术语(5,1));
系统输出println(新术语(5,0));
}
}
我想,你不知道世博会不会是负面的。尝试以下方法:
@Override
public String toString() {
if(coef ==0){
return "0";
}else if(expo ==0){
return ""+coef;
}else{
String pref = coef==1? "": coef==-1?"-":""+coef;
String suff = expo>1? "^"+expo:"";
return pref+"x"+suff;
}
}
编辑:若要使用StringBuilder
,请按如下所示更改最后一条语句(但我看不出有什么好处)
这将为术语(5,0)
返回1
5
是正确的值,因为5*x^0
是5*1
是5
,而不是1
@dasblinkenlight:我被x和简单幂弄糊涂了。更新了答案。请检查并让我知道,如果还有任何差距。在我看来,它工作得很好。5x^0
代表5*x^0
,所以虽然x^0
实际上是1,5*1=5
@user1828314:是的,我明白了。我已经更新了答案。请检查并让我知道,如果还有任何差距。在我看来,它工作正常。@YogendraSingh是的,这会给出错误,例如,测试编号1是(-7,0),它应该打印“-7”,但您的是打印“1”。我喜欢使用StringBuilder进行此操作的想法,在逻辑上考虑不同的规则时,这是一个构建一个字符串来表示多项式表达式的例子,这就是您所做的。我不喜欢有成百上千条if语句的想法,因为它看起来很混乱,所以谢谢你。这里我得到的唯一错误是(-5,1)
它应该打印“-5x”
,但它打印“-5x^1”
对不起,我完全跳过了指数部分。我来修理
return new StringBuilder(pref).append("x").append(suff).toString();