fork(1) download
  1. class c(complex):__mul__=lambda s,o:c(s.real*o.real+s.imag*o.imag,s.real*o.imag+s.imag*o.real);__add__=lambda s,o:c(sum(map(complex,[s,o])))
  2. import re
  3.  
  4. tests = [
  5. "(2+j*3)+(4+j*7)",
  6. "(2+j*3)*(4+j*7)",
  7. "(-5+j*1+j*2+2)*(4+j*7)",
  8. "(1+j*-1)*(1+j*1)",
  9. "j*((j*-1)+2)",
  10. "(2+(5+-1*(j*1))+2)",
  11. ]
  12.  
  13. for i in tests:
  14. r=eval(re.sub("j","c(0,1)",re.sub(r"(-?\d+)",r"c(\1)",i)))
  15. print`int(r.real)`+"+j*"+`int(r.imag)`
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
6+j*10
29+j*26
9+j*-9
0+j*0
-1+j*2
9+j*-1