https://www98.zippyshare.com/v/dyPJAeHF/file.html
https://www98.zippyshare.com/v/mG8vx3EN/file.html
SOLUTION FOR THE ABOVE QUESTION
In this solution we will develop a python program that takes an expression as an input and prints its results. The expression should consist of only four operations i.e. addition(+),
subtraction(-), multiplication(*) and exponential(^). It does not obey the BODMAS except for the brackets i.e. (). as per the question requirements.
SOLUTION CODE
#This program has only four possible operations, i.e addition subraction, multiplication
# and exponential
#lets define the methods for all these
import math
#defining a method for addition
def addition(first_number, second_number):
return first_number+second_number;
#defining a method for subtraction
def subtraction(first_number, second_number):
return first_number-second_number;
#defining a method for multiplication
def multiplication(first_number, second_number):
return first_number*second_number;
#defining a method for power
def power_function(number, power_raise):
return pow(number,power_raise);
#prompt the user to enter the expression
my_expression = input("\nEnter your expression: ")
my_expression_length = len(my_expression)
while_terminate_condition = 0;
my_new_expression = ""
while while_terminate_condition < my_expression_length:
if(my_expression[while_terminate_condition]=="("):
while_terminate_condition = while_terminate_condition + 1;
variable_1 = int(my_expression[while_terminate_condition])
while_terminate_condition = while_terminate_condition + 1;
my_operator = my_expression[while_terminate_condition]
while_terminate_condition = while_terminate_condition + 1;
variable_2 = int(my_expression[while_terminate_condition])
while_terminate_condition = while_terminate_condition + 2;
if my_operator == "+":
my_new_expression = my_new_expression+str(addition(variable_1,variable_2))
elif my_operator == "-":
my_new_expression = my_new_expression + str(subtraction(variable_1, variable_2))
elif my_operator == "*":
my_new_expression = my_new_expression + str(multiplication(variable_1, variable_2))
else:
my_new_expression = my_new_expression + str(power_function(variable_1, variable_2))
else:
my_new_expression = my_new_expression + my_expression[while_terminate_condition]
while_terminate_condition = while_terminate_condition + 1;
#Now we have our new expression with brackets taken care of
#lets evaluate the expression now
my_new_expression_length = len(my_new_expression)
new_terminate_condition = 0
#declare the final result variable and initialize it to the fisrt element of
# the new exepression so that it can be accesible outside the while block
final_result = int(my_new_expression[new_terminate_condition]);
new_terminate_condition = new_terminate_condition + 1;
while new_terminate_condition<my_new_expression_length:
#get the operator
my_operator = my_new_expression[new_terminate_condition]
new_terminate_condition = new_terminate_condition + 1;
#get the operand
my_operand = int(my_new_expression[new_terminate_condition])
new_terminate_condition = new_terminate_condition + 1
if my_operator == "+":
final_result = addition(final_result,my_operand)
elif my_operator == "-":
final_result = subtraction(final_result,my_operand)
elif my_operator == "*":
final_result = multiplication(final_result, my_operand)
else:
final_result = power_function(final_result,my_operand)
#print your final result
print("Your Expression evaluates to: "+str(final_result))
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment