-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerm.py
57 lines (49 loc) · 1.79 KB
/
Term.py
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
class Term:
sign = ""
operation = ""
exponent = 1
coefficient = 1
def __init__(self, sign, operation, exponent, coefficient):
if sign == 1:
self.sign = "-"
else:
self.sign = "+"
self.operation = operation
self.exponent = exponent
self.coefficient = coefficient
self.pre_string = ""
self.post_string = ""
self.update_strings()
def build_term(self, x):
index = self.operation.find("(")
if index != -1:
expresion = self.pre_string + str(x) + self.post_string
else:
index = self.operation.find("[")
if index != -1:
expresion = self.pre_string + str(x) + self.post_string
else:
expresion = self.pre_string + str(x) + self.post_string
return expresion
def update_strings(self):
index = self.operation.find("(")
if index != -1:
self.pre_string = self.sign + str(self.coefficient) + "*(" + self.operation[:(index + 1)]
self.post_string = "**" + str(self.exponent) + self.operation[(index + 1):] + ")"
else:
index = self.operation.find("[")
if index != -1:
self.pre_string = self.sign + str(self.coefficient) + "*" + self.operation[:index] + "**("
self.post_string = "*" + str(self.exponent) + ")"
else:
self.pre_string = self.sign + str(self.coefficient) + "*("
self.post_string = "**" + str(self.exponent) + ")"
def flip_sign(self):
if self.sign == "":
self.sign = "-"
else:
self.sign = "+"
def set_exponent(self, x):
self.exponent = x
def set_coefficient(self, x):
self.coefficient = x