Skip to content

Commit

Permalink
Merge pull request #5 from maddypie/refactoring
Browse files Browse the repository at this point in the history
Refactoring done 🏆
  • Loading branch information
tsadarsh committed Aug 1, 2020
2 parents e531335 + d9233a7 commit 81e0d63
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 533 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Currently the program supports 'Addition :heavy_plus_sign:', 'Subtraction :heavy

#### User interface of PyCalc *v0.1-beta* :desktop_computer:

![As of v0.1-alpha](https://github.com/maddypie/PyCalc/blob/master/PyCalcDemo.png)
![As of v0.1-beta](https://github.com/maddypie/PyCalc/blob/master/PyCalcDemo.png)

#### Tasks List :writing_hand:
- [ ] **Refactor source.py**
- [x] **Refactor source.py**
- [x] Add 'Copy' functionality, to copy the result. Currently 'Copy' button does not do anything.
- [x] Bind 'Number-pad' keys to enter input.
- [ ] Option to switch between 'Simple' to 'Advanced' mode. Advanced mode should have more funtionalities.
Expand Down
278 changes: 0 additions & 278 deletions WIP refactoring source.py

This file was deleted.

36 changes: 36 additions & 0 deletions core_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Calculate():
ans: str
_get_value = {
'/': lambda x, y: x/y,
'*': lambda x, y: x*y,
'+': lambda x, y: x+y,
'-': lambda x, y: x-y
}

def __init__(self, expr_as_list, operators):
self.expr_as_list = expr_as_list
self.operators = operators

def calculate(self) -> str:
while self.operators[0] in self.expr_as_list:
index = self.expr_as_list.index(self.operators[0])
self.__partial_calculate(index)
while self.operators[1] in self.expr_as_list:
index = self.expr_as_list.index(self.operators[1])
self.__partial_calculate(index)
while self.operators[2] in self.expr_as_list:
index = self.expr_as_list.index(self.operators[2])
self.__partial_calculate(index)
while self.operators[3] in self.expr_as_list:
index = self.expr_as_list.index(self.operators[3])
self.__partial_calculate(index)
self.ans = self.expr_as_list[0]
return self.ans

def __partial_calculate(self, index) -> None:
operator = self.expr_as_list[index]
left_operand = float(self.expr_as_list[index-1])
right_operand = float(self.expr_as_list[index+1])
sub_result = self._get_value[operator](left_operand, right_operand)
self.expr_as_list[index-1:index+2] = [str(sub_result)]

Loading

0 comments on commit 81e0d63

Please sign in to comment.