Make a python program which can 1) Understand a string of polynomial forms and c
ID: 3599751 • Letter: M
Question
Make a python program which can
1) Understand a string of polynomial forms and change to the array.
2) Addition, subtraction, Multiplication of polynomials with one variable x.
3) Evaluate the value of f(a) for a given value a.
Create a class name Polynomial, and write addition, subtraction, Multiplication and evaluate method inside class. The class should also include __init__ and __str__ method.
sample OUTPUT;
First, user can type a command. For example, type a for addition.
Users’s polynomial calculator
>>>Type a for addition
>>>Type s for subtraction
>>>Type m for multiplication
>>>Type e for evaluation
>>>Type command: a #(a is user input)
Second, type two polynomials. Then return the their sum.
>>>Type first polynomial: x^2 - 2x + 3
>>>Type second polynomial: x^2 + 3x + 1
>>>Their sum is x + 4
Ask command again until user types q.
User's polynomial calculator
>>>Type a for addition
>>>Type s for subtraction
>>>Type m for multiplication
>>>Type e for evaluation
>>>Type your command: e #(e is user input)
Type first polynomial: -x^2 + 1
>>>Choose value of x to evaluate: -1
>>>The value of your polynomial at x = -1 is 0
include the screenshot of the program & output.
Explanation / Answer
# This function adds two numbers
def addition(x, y):
return x + y
# This function subtracts two numbers
def subtraction(x, y):
return x - y
# This function multiplies two numbers
def multiplication(x, y):
return x * y
# This function divides two numbers
def division(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
ch = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if ch == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif ch == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif ch == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif ch == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.