Solution

ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2017/imperative-programming-resit/4/e/solution

The coefficient type is not assumed to be ordered, so do not require a sign or absolute-value operation beyond the supplied output routine. Parenthesize coefficients to keep negative or compound values unambiguous:
OutPoly(p):
    first = true
    while p != NIL:
        if p.coeff != 0:
            if not first: Out.String("+")
            Out.String("("); OutT(p.coeff); Out.String(")")
            if p.power != 0:
                Out.String("*x")
                if p.power != 1:
                    Out.String("^"); Out.Int(p.power)
            first = false
        p = p.next
    if first: OutT(0)
For example, the representation in the question can print (-1)+(1)*x^2, which denotes the same polynomial. If additionally provides ordered-real sign operations, cosmetic suppression of +(-...) and unit coefficients can produce -1+x^2. The generic routine already handles the zero polynomial and preserves the represented value without introducing those extra assumptions.

New to topics? Read the docs here!