diff --git a/nessesary/__pycache__/fraction.cpython-310.pyc b/nessesary/__pycache__/fraction.cpython-310.pyc index 54e7f9e..34e85a6 100644 Binary files a/nessesary/__pycache__/fraction.cpython-310.pyc and b/nessesary/__pycache__/fraction.cpython-310.pyc differ diff --git a/nessesary/__pycache__/matrix.cpython-310.pyc b/nessesary/__pycache__/matrix.cpython-310.pyc new file mode 100644 index 0000000..88f4b5a Binary files /dev/null and b/nessesary/__pycache__/matrix.cpython-310.pyc differ diff --git a/nessesary/parser/__pycache__/parser.cpython-310.pyc b/nessesary/parser/__pycache__/parser.cpython-310.pyc new file mode 100644 index 0000000..fbb28a2 Binary files /dev/null and b/nessesary/parser/__pycache__/parser.cpython-310.pyc differ diff --git a/nessesary/parser/__pycache__/structure.cpython-310.pyc b/nessesary/parser/__pycache__/structure.cpython-310.pyc new file mode 100644 index 0000000..af13776 Binary files /dev/null and b/nessesary/parser/__pycache__/structure.cpython-310.pyc differ diff --git a/nessesary/parser/parser.py b/nessesary/parser/parser.py index fe8223b..da79169 100644 --- a/nessesary/parser/parser.py +++ b/nessesary/parser/parser.py @@ -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] \ No newline at end of file + return result[:len(result)-1] diff --git a/nessesary/parser/structure.py b/nessesary/parser/structure.py deleted file mode 100644 index 8040514..0000000 --- a/nessesary/parser/structure.py +++ /dev/null @@ -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)) - """ - diff --git a/nessesary/polynomial.py b/nessesary/polynomial.py new file mode 100644 index 0000000..4860cc0 --- /dev/null +++ b/nessesary/polynomial.py @@ -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 \ No newline at end of file