improve printing ofr FormalExp
This commit is contained in:
parent
1e376787aa
commit
33220c2a3c
22
formal.py
22
formal.py
@ -134,13 +134,27 @@ class FormalExp(object):
|
|||||||
return len(list(self._coef.keys()))
|
return len(list(self._coef.keys()))
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return " + ".join([str(v) + str(k) for k,v in self._coef.items()])
|
ans = ""
|
||||||
|
for k,v in self._coef.items():
|
||||||
|
if v < 0:
|
||||||
|
ans += "-"
|
||||||
|
else:
|
||||||
|
ans += "+"
|
||||||
|
|
||||||
|
if abs(v) == 1:
|
||||||
|
ans += str(k)
|
||||||
|
else:
|
||||||
|
ans += str(abs(v)) + str(k)
|
||||||
|
if ans[0] == "+":
|
||||||
|
return ans[1:]
|
||||||
|
else:
|
||||||
|
return ans
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
fe1 = FormalExp({"x": 1, "":2})
|
fe1 = FormalExp({"x": -1, "":-2})
|
||||||
print(fe1.get_postfix())
|
print(fe1)
|
||||||
fe2 = FormalExp({"x^12": 5, "":2})
|
fe2 = FormalExp({"x^12": 5, "":2})
|
||||||
print(fe2.get_postfix())
|
print(fe2)
|
||||||
fe3 = fe1 * fe2
|
fe3 = fe1 * fe2
|
||||||
for s in fe3:
|
for s in fe3:
|
||||||
print(s)
|
print(s)
|
||||||
|
Loading…
Reference in New Issue
Block a user