mirror of
https://github.com/Sosokker/Calculator-for-Matrix-and-Algebra.git
synced 2025-12-18 12:44:05 +01:00
Add Insert_ml_sign function
This commit is contained in:
parent
45ddc904af
commit
dfdf7326a9
@ -39,7 +39,7 @@ def is_letter(char: str) -> bool:
|
||||
"""
|
||||
return "a" <= char <= "z"
|
||||
|
||||
def is_func(expr: str, i: int, result: str) -> bool:
|
||||
def is_func(expr: str, i: int, result: str) -> tuple[bool, str, int]:
|
||||
"""
|
||||
Check if expression is a function or not.
|
||||
"""
|
||||
@ -103,6 +103,7 @@ def make_postfix(expr: str):
|
||||
result = ""
|
||||
temp = ""
|
||||
neg = False
|
||||
has_func = False
|
||||
i = 0
|
||||
while i < len(expr):
|
||||
char = expr[i]
|
||||
@ -156,6 +157,7 @@ def make_postfix(expr: str):
|
||||
neg = False
|
||||
temp = ""
|
||||
i = last_index
|
||||
has_func = True
|
||||
else:
|
||||
op_stack.append(char)
|
||||
i += 1
|
||||
@ -175,4 +177,88 @@ def make_postfix(expr: str):
|
||||
result += top
|
||||
result += " "
|
||||
i += 1
|
||||
return result[:len(result)-1]
|
||||
return result[:len(result)-1], has_func
|
||||
|
||||
def insert_mul_sign(expr):
|
||||
"""
|
||||
Insert multiplication sign to expression(*)
|
||||
>>> insert_mul_sign("xx")
|
||||
x*x
|
||||
>>> insert_mul_sign("xy+xsin(xy)+12x+y")
|
||||
x*y+x*sin(x*y)+12*x+y
|
||||
"""
|
||||
function_list = [
|
||||
"sin","cos","tan", "cosec", "sec", "cot",
|
||||
"arcsin","arccos","arctan", "arcsec",
|
||||
"arccosec", "arccot", "asin", "acos",
|
||||
"atan", "asec", "acosec", "acot"
|
||||
]
|
||||
i = 0
|
||||
result = ""
|
||||
f_name = ""
|
||||
neg = False
|
||||
new = ""
|
||||
for ind in range(len(expr)):
|
||||
if expr[ind] == " ":
|
||||
continue
|
||||
new += expr[ind]
|
||||
expr = new
|
||||
|
||||
while i < len(expr):
|
||||
char = expr[i]
|
||||
check_func, f_name, last_index = is_func(expr, i, f_name)
|
||||
|
||||
if is_num(char):
|
||||
num, i = parsing_num(expr, i)
|
||||
result += add_neg_sign(num, neg)
|
||||
neg = False
|
||||
i += 1
|
||||
if expr[-1] != char:
|
||||
if expr[i] != ")" and not is_op(expr[i]):
|
||||
result += "*"
|
||||
elif char == ")":
|
||||
try:
|
||||
c = expr[i+1]
|
||||
result += char
|
||||
if c != ")":
|
||||
result += "*"
|
||||
except IndexError:
|
||||
result += char
|
||||
i += 1
|
||||
|
||||
elif is_op(char):
|
||||
result += char
|
||||
i += 1
|
||||
|
||||
elif check_func:
|
||||
check_list = [1 for n in function_list if n in f_name]
|
||||
if sum(check_list) > 0:
|
||||
result += f_name
|
||||
i = last_index
|
||||
else:
|
||||
count = len(f_name)
|
||||
for ind in range(len(f_name)+1):
|
||||
if ind % 2 != 0:
|
||||
f_name = f_name[:ind] + "*" + f_name[ind:]
|
||||
result += f_name
|
||||
i = last_index + count
|
||||
|
||||
elif is_letter(char) or char == "(":
|
||||
if result[-1] != "*":
|
||||
if (result[i] != "(") and (char != "("):
|
||||
result += "*"
|
||||
result += char
|
||||
# if not is_op(expr[i]):
|
||||
# result += "*"
|
||||
i += 1
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# print(make_postfix("(2-3+4)*(5+6*7)"))
|
||||
# print(make_postfix("12sin(x^2)+(4x+12*3)-5"))
|
||||
# print(insert_mul_sign("12x"))
|
||||
# print(insert_mul_sign("5x+12y+421abcde+1"))
|
||||
# print(insert_mul_sign("12((x+12x)x)"))
|
||||
# print(insert_mul_sign("xsin(x^2)"))
|
||||
# print(insert_mul_sign("5(1+2*3^12)(12-5(4^2)) + (1-3*4)12"))
|
||||
Loading…
Reference in New Issue
Block a user