我创建了一个不使用多项式的多项式类,我使用自己的Term(coefficient, exponent)
来创建多项式表达式。
我有一些条件如下:
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"
但实现所有这些功能在彼此内部和周围发挥作用让我头疼不已,例如,如果我Term(-1,1)
我希望"-x"
出现,"x"
Term(1,1)
。任何人都可以帮助考虑某种逻辑,将所有这些“规则”组合在一起用于toString方法吗?
答案 0 :(得分:1)
订单在这里很重要。如果你仔细考虑,你会发现coefficient = 0
应该先行,因为当它为零时,其他任何事情都不重要。接下来是exponent
等于1
或0
时的特殊情况。然后,当coefficient
为-1
时,您就拥有了。剩下的就是除coefficient
之外的否定-1
或正coefficient
的默认情况。因此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;
}
}
答案 1 :(得分:1)
您可以将第一个特殊情况与最后一个结合起来。您还可以通过查看系数的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();
答案 2 :(得分:1)
这就像我要明确的那样接近
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));
}
}
并为它fiddle。
答案 3 :(得分:1)
我认为,你不参加世博会不会消极。尝试以下内容:
@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
,请按以下更改最后一条语句(虽然我看不到多少好处)
return new StringBuilder(pref).append("x").append(suff).toString();
答案 4 :(得分:0)
嗯,你真的没有太多可以让它变得更容易。你基本上有以下几种情况:
系数= 0 - >显示0(指数无关)
系数= +/- 1 - >显示 - 如果为-1((-1,0)的特例),否则为什么
其他系数 - &gt;显示为已存储
指数= 0 - &gt;什么都不显示 否则,显示x ^ exponent ...
所以.....
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来确定。