Problem 126 -- The Errant Physicist

read problem

Tips

This problem is straight forward but complicated. You must do all the dirty works of algebra behind the scene. Steps include:

  1. Parse the input. Find out how many terms in the two polynomials and what values they are. Put them in two arrays. Be aware of coefficient might be 0 or 1, power might be 0 or 1, y might come before x, "+" sign might optionally occur at the front, etc.
  2. Do the multiplication. Put the results in another array. I used malloc() to prepare just enough spaces to hold all output terms.
  3. Sum up similar terms in the result array. Some terms might zero out.
  4. Sort the terms according to the requirement in the problem.
  5. Print out the terms according to the specifications. Pay attention to spacing, string length, occasion of coefficient = 0 or 1, occasion of power = 0 or 1, etc.

The prasing and print-out steps involve many if or case statements.

 

Test Cases

Input

-yx8+9x3-1+y
x5y+1+x3
1
1
0
0
0
1
-x+0
0-x
-0+x
1
+0x1y10-0+3x0y9-x8y8-12x1y+3
x3y4+x2y5-1y4x9+x2y2-8-y
#

Output

  13 2    11      8      6    5     5 2     3    3         
-x  y  - x  y + 8x y + 9x  - x y + x y  + 8x  + x y - 1 + y
 
1

0

0
 2
x 
 
x
 17 12    11 12      10 5    10 10     10 13    10 14     9 4     8 8    8 9      4 5     4 13    4 14      3 3     3 4      3 6     3 11    3 12     3 14    3 15     2 2     2 5              2       9       10     11          
x  y   - x  y   + 12x  y  - x  y   - 4x  y   - x  y   - 3x y  + 8x y  + x y  - 12x y  + 3x y   + x y   - 12x y  + 3x y  - 12x y  + 3x y   + x y   + 3x y   + x y   + 3x y  + 3x y  + 96xy + 12xy  - 24xy  - 11xy   - xy   - 24 - 3y