-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
executable file
·170 lines (152 loc) · 6.42 KB
/
console.py
File metadata and controls
executable file
·170 lines (152 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File name: processing.py
"""
This module contains classes responsible for controlling and managing our Text-user
console application.
It uses other classes from different modules to run our console app.
It provides some backend methods and also uses other backend methods allowing text console
users a high degree of flexibility and control over the application.
"""
from operators import *
from create_func import *
from grid import Grid
from expression_tree import *
__author__ = 'Molinge'
class Console:
"""
This classes contains methods[mostly static] for our console application (Non-GUI).
It allows the user to carry out main functionality of the GUI in a console app(terminal
based).
"""
def __init__(self):
"""Creates our grid class and runs our application"""
self.app_grid = Grid() # create a grid object
self.interface()
def interface(self):
"""
This controls the Console application and use's most of the other static
methods in order to do so.
"""
ops = ['', 'add', 'subtract', 'multiply', 'divide', 'substitute', 'compose', 'cos', 'sin', 'tan']
operator = ops[1] # default operator
print("*********************************\n"
"OPERATOR AND EXPRESSION GENERATOR\n"
"*********************************\n")
choice = self.menu()
while choice != 6:
if choice == 1:
self.app_grid.display_grid()
elif choice == 2:
n = input("Enter function's name: ")
t = input("Enter it's type(Function/Constant): ")
a = int(input("Enter Number of arguments: "))
s = input("Enter section: ")
f = self.create_func(n, t, a, s)
if s == 'Vertical':
self.app_grid.add_row(f.get_function())
elif s == 'Horizontal':
self.app_grid.add_column(f.get_function())
elif choice == 3:
# Get the operator number from user and assign the corresponding operator in the ops list
# into the operator variable
operator = ops[self.display_operators()]
elif choice == 4:
# Display the grid and get the two functions to apply from the user
func1 = tuple(map(int, input("Enter a function position from at vertical location on the grid: ").
split()))
func2 = tuple(map(int, input("Enter a function position from the horizontal location on the grid: ").
split()))
print(func1, func2)
print("The resulting computation is as follows:")
item = self.calculate(operator, self.app_grid.get_item(func1[0], func1[1]),
self.app_grid.get_item(func2[0], func2[1]))
self.app_grid.add_item(item, func1[0], func2[1])
elif choice == 5:
try:
cord = tuple(map(int, input("Enter location of expression(3 digits): ").split()))
exp = self.app_grid.get_item(cord[0], cord[1])[cord[2]]
self.expression_notations(exp)
except IndexError as e:
print(e)
print()
choice = self.menu()
@staticmethod
def calculate(operator, string1, string2):
"""
This does the calculations by performing the operations on its arguments.
And returning the corresponding result.
"""
if operator == 'add':
return Arithmetic.add(string1, string2)
elif operator == 'subtract':
return Arithmetic.substract(string1, string2)
elif operator == 'multiply':
return Arithmetic.multiply(string2)
elif operator == 'divide':
return Arithmetic.divide(string2)
elif operator == 'substitute':
return Algebraic.substitute(string2)
elif operator == 'compose':
return Algebraic.compose(string1, string2)
@staticmethod
def create_func(func_name, func_type, num_args, section):
"""This method returns an object of a created function."""
func = CreateFunc()
func.set_name(func_name)
func.set_type(func_type)
func.set_args(num_args)
func.set_section(section)
func.create_function()
return func
@staticmethod
def expression_notations_choices():
print("\nWhich format do you want")
print("-----------------------------")
print("1. Infix notation")
print("2. Prefix notation")
print("3. Postfix(or reverse polish) notation")
choice = int(input("Enter choice: "))
return choice
def expression_notations(self, e):
try:
c = self.expression_notations_choices()
my_file = input("Enter name of text file to save: ")
file = open(my_file, "w")
if c == 1:
file.write(expression_formats(e, 'inorder'))
elif c == 2:
file.write(expression_formats(e, 'preorder'))
elif c == 3:
file.write(expression_formats(e, 'postorder'))
file.close()
except IOError as e:
print("File name doesn't exist")
@staticmethod
def menu():
"""
This displays the main menu of the console app.
It then collects and returns the choice of the user.
"""
print("\nChoose which operation to carryout")
print("----------------------------------\n")
print("1. Display Grid")
print("2. Create Function")
print("3. Choose operator")
print("4. Perform operation")
print("5. Export expression ")
print("6. Exit\n")
choice = int(input("Enter your choice: "))
return choice
@staticmethod
def display_operators():
"""
This displays the list of available operators.
It then collects and returns the choice of the user.
"""
print("Available operators are")
print("Arithmetic\n\t1.Addition\n\t2.Subtract\n\t3.Multiplication\n\t4.Division\n")
print("Algebraic\n\t5.Substitution\n\t6.Composition")
print("Trigonometric\n\t7.cos\n\t8.sin\n\t9.tan")
choice = int(input("Enter an Operator number to choose: "))
return choice
if __name__ == '__main__':
Console()