Polycalc

This assignment is to produce a calculator that can take polynomials and perform
arithmetic operations on them. Only non negative exponents are to be considered.

anxn + an-1xn-1 + an-2xn-2 + … + a2x2 + a1x + a0

ai is coefficient.

if an is not 0 then degree is n

Degree is defined as max i with ai is not 0.

Ex: x2 + 2x + 1 degree is 2, and 3x3 + 2 degree is 3

Addition:

(anxn + an-1xn-1 + … + a1x + a0) + (bnxn + bn-1xn-1 + … + b1x + b0) =

(an + bn) xn + (an-1 + bn-1) xn-1) + … + (a1 + b1) x + (a0 + b0)

Subtraction:

(anxn + an-1xn-1 + … + a1x + a0) – (bnxn + bn-1xn-1 + … + b1x + b0) =

(an – bn) xn + (an-1 – bn-1) xn-1) + … + (a1 – b1) x + (a0 – b0)

Multiplication:

(anxn + an-1xn-1 + … + a1x + a0) * (bnxn + bn-1xn-1 + … + b1x + b0) =

cn+mxn+m + cn-1+m-1 xn-1+m-1 + {… + c2x2 + c1 x + c0

Evaluation:

Let f(x) = anxn + an-1xn-1 + … + a1x + a0

Then f(a) = (anan + an-1an-1 + … + a1a + a0)

Ex: f(x) = -2x2 + 2x + 1

f(3) = -2 x (3)2 + 2 x 3 + 1 = -11

Requirements:

  1. Convert a polynomial string into an instanceo of a polynomial
  2. Addition, Subtract, Multiple of 2 polynomials
  3. Evaluation of a polynomial

Create a class Polynomial and add overloaded methods for adding,
subtracting, multiplying, and converting to a string, add a
member function to evaluate the polynomial with a particular value
for x.

The output should look like the following.

Guozhen An's polynomial calculator
Type a for addition
Type s for subtraction
Type m for multiplcation
Type e for evaluation

Type your command: a
Tyoe your first polynomial: x^2-2x+3
Tyoe your second polynomial: x^2+3x+1
Their sum is 2x^2+x+4

Type your command: e
Tyoe your first polynomial: -x^2+1
Choose value of x to evaluate: -1
The value of the polynomial at x = -1 is 0

Type your command: m
Tyoe your first polynomial: x+1
Tyoe your second polynomial: x+1
Their product is x^2+2x+1

Type your command: q
>>>

Solution:

polycalc.py

class Polynomial:
    def __init__(self):
        self.value = 0
        self.result = ''

    #convert String to Array
    def toArr(self, x):
        arr = [0] * 10 #create an array to store polynomial values 
        poly = ''
        sign = '?'
        deg = 0
        s = 1
        #add a symbol to the front of the string
        if x[0] != '-' or x[0] != '+':
            x = "+" + x
        x = x+" + 0"
        for i in x: #go through the string and convert polynomial values to array
            if i == '+' or i == '-':
                if sign == '?':
                    sign = i
                    if sign == '-':
                        s = -1
                    else:
                        s = 1
                    poly += str(i)
                    continue
                poly = poly.strip()
                val = poly.split("x^")
                if sign == '-':
                    s = -1
                else:
                    s = 1
                #print(poly, "-- ", s)
                if(len(val) == 1 ):
                    if(val[0][len(val[0])-1] == 'x'):
                        val[0] = val[0][:-1]
                        if(len(val[0]) == 0):
                            val[0] = '1'
                        elif val[0] == '+':
                            val[0] = 1
                        elif val[0] == '-':
                            val[0] = -1
                        arr[1] = int(val[0]) * s
                    else:
                        if val[0] == '+':
                            val[0] = 1
                        elif val[0] == '-':
                            val[0] = -1
                        arr[0] = int(val[0]) * s
                else:
                    #print(val)
                    val[0] = val[0][1:]
                    if(len(val[1]) == 0):
                        val[1] = '1'                
                    if(len(val[0]) == 0):
                        val[0] = '1'
                    arr[int(val[1])] = int(val[0]) * s
                poly = ''
                sign = i
            else:
                poly += str(i)
        if sign == '-':
            s = -1
        else:
            s = 1
        poly = poly.strip()
        #remove unwanted string
        if poly[0] > '9' or poly[0] < '0':
            poly = poly[1:]
        arr[0] += int(poly) * s
        return arr

    #Display the menu
    def menu(self):
        print("Guozhen An's polynomial calculator\nType a for addition\n"
            +"Type s for subtraction\n"
            +"Type m for multiplication\n"
            +"Type e for evaluation\n")

    #add two polynomial
    def add(self):
        x1 = str(input("Type first polynomial: "))
        x2 = str(input("Type second polynomial: "))
        a1 = self.toArr(x1)
        a2 = self.toArr(x2)
        for i in range(len(a1)):
            a1[i] += a2[i]
        #convert array to polynomial
        self.conv(a1)
        print("Their sum is", self.result)

    #substract two polynomial
    def sub(self):
        x1 = str(input("Type first polynomial: "))
        x2 = str(input("Type second polynomial: "))
        a1 = self.toArr(x1)
        a2 = self.toArr(x2)
        for i in range(len(a1)):
            a1[i] -= a2[i]
        self.conv(a1)
        print("Their difference is", self.result)

    #multiply two polynomial
    def mul(self):
        x1 = str(input("Type first polynomial: "))
        x2 = str(input("Type second polynomial: "))
        a1 = self.toArr(x1)
        a2 = self.toArr(x2)
        #store the product of polynomials
        mul = [0] * 2000
        for i in range(len(a1)):
            for j in range(len(a2)):
                mul[i+j] += a1[i]*a2[j]
        self.conv(mul)
        print("Their product is", self.result)

    #Evaluate the polynomial for a given X value    
    def evaluate(self):
        x1 = input("Type first polynomial: ")
        val = int(input("Choose value of x to evaluate: "))
        a1 = self.toArr(x1)
        ans = 0
        for i in range(len(a1)):
            if i == 0:
                ans += a1[i]
            else:
                ans += a1[i]*(val**i)
        print("The value of your polynomial at x =",val,"is", ans)

    #Run the program
    def run(self):
        inp = '' #input user enters
        while(inp != 'q'):
            self.menu()
            self.result = ''
            inp = input("Type your command: ")
            if inp == 'a':
                self.add() 
            elif inp == 's':
                self.sub() 
            elif inp == 'm':
                self.mul()
            elif inp == 'e':
                self.evaluate()

    #display the array in polynomial form
    def conv(self, arr):
        flag = 0
        result = ''
        for i in range(len(arr) - 1, -1 , -1):
            if arr[i] == 0:
                continue
            if(arr[i] > 0) and flag == 1:
                    result += '+'
            if i > 1:
                if(arr[i] != 1):
                    result += str(arr[i])+'x^'+str(i)
                else:
                    result += 'x^'+str(i)

            elif i == 1:
                if(arr[i] != 1):
                    result += str(arr[i])+'x'
                else:
                    result += 'x'
            elif i == 0:
                result += str(arr[i])
            flag = 1
        if len(result) == 0:
            result = "0"
        self.result = result

    def __str__(self):
        return str(self.result)

a = Polynomial()
a.run()

So if you decide that you require help with Python homework then I’m sure we can deliver.