Add parsing the function and fix some var name

This commit is contained in:
Sirin Puenggun 2022-12-01 11:24:07 +07:00
parent 70f0136298
commit 45ddc904af
7 changed files with 33 additions and 113 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -19,13 +19,13 @@ def is_neg(expr, i) -> bool:
i -= 1
return neg_sign%2 != 0
def is_num(char: str):
def is_num(char: str) -> bool:
"""
Check if char agrument is number or not.
"""
return char in ['1','2','3','4','5','6','7','8','9','0']
def is_op(char: str):
def is_op(char: str) -> bool:
"""
Check if char agrument is operator or not.
"""
@ -33,25 +33,27 @@ def is_op(char: str):
return True
return False
def is_letter(char: str):
def is_letter(char: str) -> bool:
"""
Check if char agrument is string character or not.
"""
return "a" <= char <= "z"
def is_func(expr: str, i: int, result: str):
def is_func(expr: str, i: int, result: str) -> bool:
"""
Check if expression is a function or not.
"""
result = ""
for i in range(len(expr)):
while i < len(expr):
if not is_letter(expr[i]):
i -= 1
break
result += expr[i]
return len(result) > 1
i += 1
new_index = i
return len(result) > 1, result, new_index
def priority(char: str):
def priority(char: str) -> int:
"""
set precedence for each operator.
This program use PEMDAS RULE as main rule
@ -89,8 +91,8 @@ def make_postfix(expr: str):
>>> make_postfix(" 3*4 + 2*5")
3 4 * 2 5 * +
>>> make_postfix("(2-3+4)*(5+6*7)
2 3 - 4 5 6 7 * + ( * + (
>>> make_postfix("(2-3+4)*(5+6*7)")
2 3 - 4 + 5 6 7 * + *
"""
@ -104,7 +106,8 @@ def make_postfix(expr: str):
i = 0
while i < len(expr):
char = expr[i]
if char == " ":
func_check, temp, last_index = is_func(expr, i, temp)
if char == " " or char == "," or char == ":":
i += 1
continue
@ -119,31 +122,32 @@ def make_postfix(expr: str):
elif char == ")":
found = False
while len(op_stack) != 0:
head = op_stack.pop()
if head == "(":
top = op_stack.pop()
if top == "(":
found = True
break
else:
result += head
result += top
result += " "
i += 1
if not found:
raise ValueError("Parentheses Error!")
elif is_op(char) or is_func(expr, i, temp):
elif is_op(char) or func_check:
isfunc = len(temp) > 1
if (not isfunc) and (char == "-"):
if i == 0 or is_op(expr[i-1]):
neg = is_neg(expr, i)
i += 1
continue
while len(op_stack) != 0:
head = op_stack[-1]
priority1 = (priority(head) < priority(char))
priority2 = (priority(head) == priority(char))
if char == "(" or head == "(" or priority1 or (priority2 and char == "^"):
top = op_stack[-1]
priority1 = (priority(top) < priority(char))
priority2 = (priority(top) == priority(char))
if char == "(" or top == "(" or priority1 or (priority2 and char == "^"):
break
result += head
result += top
op_stack.pop()
result += " "
@ -151,6 +155,7 @@ def make_postfix(expr: str):
op_stack.append(add_neg_sign(temp, neg))
neg = False
temp = ""
i = last_index
else:
op_stack.append(char)
i += 1
@ -166,8 +171,8 @@ def make_postfix(expr: str):
raise ValueError("Can't Parse this Letter!")
while len(op_stack) != 0:
head = op_stack.pop()
result += head
top = op_stack.pop()
result += top
result += " "
i += 1
return result[:len(result)-1]

View File

@ -1,91 +0,0 @@
#VARIABLE AND NUMBER
VAR = 0
NUM = 1
POINT = 2
# OPERATOR
PLUS_S = 3
MINUS_S = 4
MUL_S = 5
DIV_S = 6
EXP_S = 7
# BRACKET AND PARENTHESES
LEFT_BRAC = 11
RIGHT_BRAC = 12
LEFT_PAR = 10
RIGHT_PAR = 11
#FUNCTION
SQRT_FUNC = 100
SIN_FUNC = 101
COS_FUNC = 102
TAN_FUNC = 103
SEC_FUNC = 104
COSEC_FUNC = 105
COT_FUNC = 106
ASIN_FUNC = 107
ACOS_FUNC = 108
ATAN_FUNC = 109
ASEC_FUNC = 110
ACOSEC_FUNC = 111
ACOT_FUNC = 112
FACTORIAL = 113
LOG = 114
LN = 115
ALPHABET_LIST = ['a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's'
, 't', 'u', 'v', 'w', 'x', 'y', 'z']
NUM_LIST = ['1','2','3','4','5','6','7','8','9','0']
def general_parse(expr):
result_list = []
for value in expr:
if value == "+":
result_list.append(PLUS_S)
elif value == "-":
result_list.append(MINUS_S)
elif value == "*":
result_list.append(MUL_S)
elif value == "/":
result_list.append(DIV_S)
elif (value == "^") or (value == "*"):
result_list.append(EXP_S)
elif value == ".":
result_list.append(POINT)
elif value == "(":
result_list.append(LEFT_PAR)
elif value == ")":
result_list.append(RIGHT_PAR)
elif value == "[":
result_list.append(LEFT_BRAC)
elif value == "]":
result_list.append(RIGHT_BRAC)
elif value in ALPHABET_LIST:
result_list.append(VAR)
elif value in NUM_LIST:
result_list.append(NUM)
"""
MEANING OF EACH PATTERN EXPRESSION.
RESULT_LIST MEANING
[1,1,1,...] 1231..
[0,0,0] xyz
[1,1,2,1,1] 12.32
[1,1,0] 15x
[10,11] ()
[12,13] []
[114,10,,11] log()
[115,10,,11] ln()
[1,1,113] 31!
[101,10,,11] sin()
[1,0,3,10,1,2,1,
101,10,1,1,1,11,11] 5x+(4.2sin(324))
"""

6
nessesary/polynomial.py Normal file
View File

@ -0,0 +1,6 @@
from parser.parser import polynomial_parse
class Polynomial:
def __init__(self, poly):
self.coeff = polynomial_parse(poly)
self.degree = len(polynomial_parse(poly)) - 1