我创建了一个多项式类而不使用多项式,我使用我自己的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 方法吗?