Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
roidsaja committed Oct 14, 2019
2 parents fe7b8cd + 8f98031 commit 6abc226
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 9 deletions.
62 changes: 53 additions & 9 deletions calc_main.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
#!/usr/bin/env python2
import math
import parser as pr

def add(a, b):
return a + b
def red(a, b):
return a - b
def mul(a, b):
return a * b
def div(a, b):
return a / b
def power(a, b):
return a ** b

def statement_wrapper():
doper = {
'+' : add
,'-' : red
,'*' : mul
,'/' : div
,'^' : power
}
states=raw_input(">>")
result=pr.apply(states, darg=doper)
if(result == None):
print("<< Undefined")
else:
full = "<< ", str(result)
print(full)
return

while True:
print("MENU")
print("1 for addition :")
Expand All @@ -8,42 +39,52 @@
print("5 for Division:")
print("6 for floor division:")
print("7 for factorial:")
print("8 for Statement based:")
choice=int(input("enter any choice:"))
def additon():
a=int(input("enter 1st no to perform addition:")) #a-first input
b=int(input("enter 2nd no to perform addition:")) #b-second input
c=a+b
c=add(a, b)
print("sum is:",c)
return
def subtract():
a = int(input("enter 1st no to perform subtraction:"))
b = int(input("enter 2nd no to perform subtraction:"))
c = a - b
c = red(a, b)
print("subtraction is:", c)
return
def multiplication():
a = int(input("enter 1st no to perform multipication:"))
b = int(input("enter 2nd no to perform multiplication:"))
c = a*b
c = mul(a, b)
print("multiplication is:", c)
return
def power():
a = int(input("enter base :"))
b = int(input("enter power :"))
c = a**b
c = pow(a, b)
print("division is:", c)
return

def divide():
a = int(input("enter 1st no to perform division:"))
b = int(input("enter 2nd no to perform division:"))
c = a/b
c = div(a, b)
print("division is:", c)
return
def floor_division():


#def floor_division():

def factorial():
num = int(input("enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is:",factorial)
return

if choice==1:
additon()
Expand All @@ -59,6 +100,9 @@ def factorial():
floor_division()
elif choice==7:
factorial()
elif choice==8:
statement_wrapper()
else:
print("wrong input")
exit(0)
exit(0)

52 changes: 52 additions & 0 deletions parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#module tokenization
import re

def apply(string, sarg = {}, darg = {}):
return execute(to_postfix(tokenize(string)), sarg=sarg, darg=darg)

def execute(statement, sarg = {}, darg = {}):
stack = []
for elem in statement :
if elem[0].isdigit() :
stack.append(float(elem))
elif elem in sarg :
operand = stack.pop()
stack.append(sarg[elem](operand))
elif elem in darg :
operandb = stack.pop()
operanda = stack.pop()
stack.append(darg[elem](operanda, operandb))
if len(stack) == 1 :
return stack.pop()
else:
return None


def tokenize(strng):
return filter(None, re.split("([\+\-\/\*\!\(\)])", strng.replace(" ", "")))

def to_postfix(tokens):
output = []
operst = []
for opt in tokens:
if opt[0].isdigit():
output.append(opt)
elif opt == ")":
while True:
if len(operst) == 0 :
return []
temp = operst.pop()
if temp == "(" :
break;
else:
output.append(temp)
else:
operst.append(opt)
for something in operst :
if something == "(" :
return []
while True :
output.append(operst.pop())
if len(operst) == 0:
break;
return output

0 comments on commit 6abc226

Please sign in to comment.