-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Andrei Trukhan #37
base: master
Are you sure you want to change the base?
Andrei Trukhan #37
Changes from all commits
ce183f0
423dcd0
063e5c2
2e39d83
7d05a51
abb2cd1
03ff6cc
ecccb49
3039a52
b1b7255
1af2856
bd14636
f0b180e
343ee6b
19d8b5a
a19e84b
1677f79
64ef8ce
972a9db
8743ec1
d2d457f
1807f5a
121f868
690c0e8
9b2c0be
ee72ea2
8413fb6
c4fdce8
adccf54
51fc48c
8d70965
73b5be7
a6f66f1
ad9ffe6
27d0a1f
a6b659f
63a3c3b
0e7fc07
ea8c2c6
42d53e2
76b31ed
527ee21
8b2a42d
dc82757
eaa17b7
9ca56bf
d7652fd
8c2c009
8ff0bbe
b5735e4
c2c5444
656a841
d723055
38d6481
fd06e50
6e6b475
c647660
2a24d89
89624e4
61c8a11
1161036
9d29022
22c1374
df2c3ed
45813ed
dd25125
214cdac
cd0a9e1
80d1571
f5a28ef
86faab3
752abc8
cee6243
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Metadata-Version: 1.0 | ||
Name: pycalc | ||
Version: 0.1 | ||
Summary: Alpha version pure-python command-line calculator | ||
Home-page: UNKNOWN | ||
Author: Andrei Trukhan | ||
Author-email: [email protected] | ||
License: free | ||
Description: UNKNOWN | ||
Platform: UNKNOWN |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
setup.py | ||
pycalc/main.py | ||
pycalc/stack.py | ||
pycalc/start.py | ||
pycalc.egg-info/PKG-INFO | ||
pycalc.egg-info/SOURCES.txt | ||
pycalc.egg-info/dependency_links.txt | ||
pycalc.egg-info/entry_points.txt | ||
pycalc.egg-info/not-zip-safe | ||
pycalc.egg-info/top_level.txt |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[console_scripts] | ||
pycalc = pycalc.start:main | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pycalc |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from pycalc.main import Stack | ||
from pycalc.consts import * | ||
|
||
|
||
def check_mistakes(expression, functions={}, mode=0): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Крайне не рекомендуется использование изменяемых типов данных в качестве аргумента по умолчанию в функциях. Такие объекты создаются лишь единожды при создании объека функции, а не каждый раз при ее выполнении. |
||
|
||
def is_number(part): | ||
for char in part: | ||
if not char.isnumeric() and char != '.': | ||
return False | ||
return True | ||
|
||
if len(expression) == 0: | ||
if mode == 0: | ||
return "ERROR: empty expression" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Для этих целей нужно использовать исключения, а не возвращаемые значения |
||
elif mode == 1: | ||
return "ERROR: empty brackets" | ||
elif mode == 2: | ||
return '' | ||
|
||
begin = -1 | ||
brackets_stack = Stack.Stack() | ||
|
||
for index, element in enumerate(expression): | ||
if element == '(': | ||
brackets_stack.push('(') | ||
if begin == -1: | ||
begin = index | ||
elif element == ')': | ||
if brackets_stack.is_empty(): | ||
return "ERROR: brackets are not paired" | ||
brackets_stack.pop() | ||
if brackets_stack.is_empty(): | ||
if expression[begin - 1].isalpha() and not expression[begin - 1] in math_consts: | ||
error = check_mistakes(expression[begin + 1: index], functions, 2) | ||
else: | ||
error = check_mistakes(expression[begin + 1: index], functions, 1) | ||
begin = -1 | ||
|
||
if error != '': | ||
return error | ||
|
||
elif element == " ": | ||
expression.remove(element) | ||
if not brackets_stack.is_empty(): | ||
return "ERROR: brackets are not paired" | ||
|
||
if expression[len(expression) - 1] in signs: | ||
return "ERROR: no number after operator" | ||
|
||
main_sign_count = 0 | ||
l_sign_count = 0 | ||
number_count = 0 | ||
logical_sign_pos = 0 | ||
|
||
for index in range(len(expression)): | ||
|
||
if expression[index] in ['+', '-']: | ||
main_sign_count += 1 | ||
|
||
elif expression[index] in ['*', '/', '^', '%', '//']: | ||
if number_count == 0: | ||
return "ERROR: no numbers before sign" | ||
|
||
if index != len(expression) - 1 and expression[index + 1] in ['*', '/', '^', '%', '//']: | ||
return "ERROR: duplicate multiply or div sign" | ||
|
||
elif expression[index] in logical_signs: | ||
l_sign_count += 1 | ||
logical_sign_pos = index | ||
if l_sign_count > 1: | ||
return "ERROR: more than one logical operator" | ||
|
||
elif expression[index] == '=': | ||
return "ERROR: illegal usage '='" | ||
|
||
elif is_number(expression[index]): | ||
number_count += 1 | ||
if index != len(expression) - 1 and is_number(expression[index + 1]): | ||
return "ERROR: no sign between expression" | ||
|
||
elif expression[index].isalpha(): | ||
if expression[index] in math_consts: | ||
number_count += 1 | ||
elif expression[index] in functions: | ||
if index == len(expression) - 1 or expression[index + 1] != '(': | ||
return "ERROR: no brackets after function" | ||
|
||
else: | ||
return "ERROR: function does not exist" | ||
|
||
elif expression[index] == '(': | ||
if expression[index - 1] not in functions and expression[index - 1] not in signs \ | ||
and index != 0: | ||
return "ERROR: no sign or function before brackets" | ||
|
||
if l_sign_count == 1: | ||
if (len(expression[:logical_sign_pos])) == 0 or (len(expression[logical_sign_pos:])) == 0 \ | ||
or logical_sign_pos in [0, len(expression) - 1]: | ||
return "ERROR: one of expressions around logical operator is empty" | ||
|
||
if number_count == 0: | ||
return "ERROR: no numbers in expression" | ||
|
||
return '' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import operator | ||
import math | ||
|
||
signs = ['+', '-', '*', '/', '^', '%', '>', '<', '=', '//', '!'] | ||
|
||
logical_signs = { | ||
'>': operator.gt, | ||
'>=': operator.ge, | ||
'==': operator.eq, | ||
'!=': operator.ne, | ||
"<=": operator.le, | ||
"<": operator.le | ||
} | ||
|
||
|
||
operators_type1 = { | ||
'*': operator.mul, | ||
'/': operator.truediv, | ||
'//': operator.floordiv, | ||
'%': operator.mod | ||
} | ||
|
||
operators_main = { | ||
'+': operator.add, | ||
'-': operator.sub | ||
} | ||
|
||
math_consts = { | ||
"pi": math.pi, | ||
"e": math.e | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Импорт через звездочку -- это зло