10783 - EEDS Polynomial Calculator   

Description

We would like to design a program that helps us in performing polynomial calculations.  We want our calculator to support the following features:

  1. A human-readable polynomial expression, e.g., x^7 +2*x^3 -x +10
  2. The four fundamental operations, +, -, *, and /.
  3. Successive operations in an formula, e.g., A * B / C - D + E, where A, B, C, D, and E are all polynomials.

 

Input

Each input line contains a polynomial or an operation. 

A polynomial is in a human-readable expression, e.g., x^7 +2*x^3 -x +10.  The coefficients are integers, and the exponents are non-negative integers.  A polynomial is in a descending order of exponents.  

An operation is one of the four character, +, -, *, and /.  

The formulas to calculate are in a "postfix" representation, which is directly evaluated from left to right.  For example, let X, Y, and Z are polynomials. 

  1. X Y +  means (X + Y)
  2. X Y + Z - means ((X + Y) - Z)
  3. X Y - Z * means ((X - Y) * Z) 

We can assume the following for simplicity:

  1. Except for the first polynomial, each polynomial is followed by exact one operation.
  2. The values of the coefficients and exponents are not excessive large; 32-bit int variables should be able to perform the calculation.

  3. When a division is performed, the dividend is a multiple of the divisor.  The results are quotient with integer coefficients and non-negative integer exponents without any remainder.

Output

The output first repeats the input.

The last line of the output contains a equation mark, =, followed by the calculation result.

Please note the conventions of expressing a polynomial.  Let us take x^5 -3*x^2 +x +7 for example.  The following variants of the polynomial are not good:
+x^5 -3*x^2 +x + 7  (we should omit the leading plus mark)
+1*x^5 -3*x^2 +x + 7  (we should omit the leading +1* coefficient)
x^5 + -3*x^2 +x + 7  (we should omit this plus mark for breavity)
x^5 +0*x^4 -3*x^2 +x + 7  (we should omit any zero term)
x^5 -3*x^2 +1*x + 7 (we should omit any 1* coefficient)
x^5 -3*x^2 +x^1 + 7 (we should omit an ^1 exponent)
x^5 -3*x^2 +x + 7x^0 (we should omit x^0)

Sample Input  Download

Sample Output  Download

Tags




Discuss