-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvance_calculator_game.py
More file actions
237 lines (196 loc) · 8.34 KB
/
advance_calculator_game.py
File metadata and controls
237 lines (196 loc) · 8.34 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import math
import cmath
class AdvancedCalculator:
def __init__(self):
self.history = [] # Stores previous calculations
self.memory = 0 # Stores the memory value
# Function to evaluate expressions with basic and scientific operations
def evaluate_expression(self, expression):
try:
# Handle functions like sin, cos, etc.
expression = expression.replace("sin", "math.sin")
expression = expression.replace("cos", "math.cos")
expression = expression.replace("tan", "math.tan")
expression = expression.replace("log", "math.log10")
expression = expression.replace("sqrt", "math.sqrt")
expression = expression.replace("exp", "math.exp")
expression = expression.replace("pi", str(math.pi))
expression = expression.replace("e", str(math.e))
# Handle complex numbers
if 'j' in expression:
expression = expression.replace("j", "1j")
result = eval(expression) # Evaluate the mathematical expression
self.history.append(f"{expression} = {result}") # Save to history
return result
except Exception as e:
return f"Error: {e}"
# Function to clear the memory
def clear_memory(self):
self.memory = 0
return "Memory Cleared!"
# Function to store a value in memory
def store_to_memory(self, value):
self.memory = value
return f"Stored {value} in memory."
# Function to recall the value from memory
def recall_memory(self):
return f"Memory: {self.memory}"
# Function to display the history
def show_history(self):
if not self.history:
return "No history available."
return "\n".join(self.history)
# Main interface to interact with the calculator
def main():
calc = AdvancedCalculator()
print("Advanced Calculator")
print("Type 'exit' to quit.")
while True:
print("\nOptions:")
print("1. Enter an expression (e.g., 2+2, sin(30), log(10), etc.)")
print("2. View History")
print("3. Memory Operations (Store, Recall, Clear)")
print("4. Exit")
option = input("Choose an option: ")
if option == "1":
expression = input("Enter the expression: ")
if expression.lower() == "exit":
break
result = calc.evaluate_expression(expression)
print(f"Result: {result}")
elif option == "2":
print("History:")
print(calc.show_history())
elif option == "3":
memory_option = input("Memory Operations:\n1. Store value\n2. Recall value\n3. Clear memory\nChoose option: ")
if memory_option == "1":
value = float(input("Enter value to store in memory: "))
print(calc.store_to_memory(value))
elif memory_option == "2":
print(calc.recall_memory())
elif memory_option == "3":
print(calc.clear_memory())
else:
print("Invalid option!")
elif option == "4":
print("Exiting...")
break
else:
print("Invalid option! Please choose again.")
if __name__ == "__main__":
main()
import math
import cmath
import sympy as sp
class AdvancedCalculator:
def __init__(self):
self.history = [] # Stores previous calculations
self.memory = 0 # Stores the memory value
# Function to evaluate expressions with basic and scientific operations
def evaluate_expression(self, expression):
try:
# Handle functions like sin, cos, etc.
expression = expression.replace("sin", "math.sin")
expression = expression.replace("cos", "math.cos")
expression = expression.replace("tan", "math.tan")
expression = expression.replace("log", "math.log10")
expression = expression.replace("sqrt", "math.sqrt")
expression = expression.replace("exp", "math.exp")
expression = expression.replace("pi", str(math.pi))
expression = expression.replace("e", str(math.e))
# Handle complex numbers
if 'j' in expression:
expression = expression.replace("j", "1j")
# Evaluate the expression
result = eval(expression) # Evaluate the mathematical expression
self.history.append(f"{expression} = {result}") # Save to history
return result
except Exception as e:
return f"Error: {e}"
# Function for symbolic differentiation
def differentiate(self, expression):
try:
# Use SymPy to parse and differentiate the expression
x = sp.symbols('x')
expr = sp.sympify(expression)
derivative = sp.diff(expr, x)
self.history.append(f"diff({expression}) = {derivative}")
return derivative
except Exception as e:
return f"Error: {e}"
# Function for symbolic integration
def integrate(self, expression):
try:
# Use SymPy to parse and integrate the expression
x = sp.symbols('x')
expr = sp.sympify(expression)
integral = sp.integrate(expr, x)
self.history.append(f"integrate({expression}) = {integral}")
return integral
except Exception as e:
return f"Error: {e}"
# Function to clear the memory
def clear_memory(self):
self.memory = 0
return "Memory Cleared!"
# Function to store a value in memory
def store_to_memory(self, value):
self.memory = value
return f"Stored {value} in memory."
# Function to recall the value from memory
def recall_memory(self):
return f"Memory: {self.memory}"
# Function to display the history
def show_history(self):
if not self.history:
return "No history available."
return "\n".join(self.history)
# Main interface to interact with the calculator
def main():
calc = AdvancedCalculator()
print("Advanced Calculator with Differentiation & Integration")
print("Type 'exit' to quit.")
while True:
print("\nOptions:")
print("1. Enter an expression (e.g., 2+2, sin(30), log(10), etc.)")
print("2. View History")
print("3. Memory Operations (Store, Recall, Clear)")
print("4. Differentiation (e.g., diff(x**2 + 3*x))")
print("5. Integration (e.g., integrate(x**2 + 3*x))")
print("6. Exit")
option = input("Choose an option: ")
if option == "1":
expression = input("Enter the expression: ")
if expression.lower() == "exit":
break
result = calc.evaluate_expression(expression)
print(f"Result: {result}")
elif option == "2":
print("History:")
print(calc.show_history())
elif option == "3":
memory_option = input("Memory Operations:\n1. Store value\n2. Recall value\n3. Clear memory\nChoose option: ")
if memory_option == "1":
value = float(input("Enter value to store in memory: "))
print(calc.store_to_memory(value))
elif memory_option == "2":
print(calc.recall_memory())
elif memory_option == "3":
print(calc.clear_memory())
else:
print("Invalid option!")
elif option == "4":
expression = input("Enter the expression to differentiate: ")
result = calc.differentiate(expression)
print(f"Result: {result}")
elif option == "5":
expression = input("Enter the expression to integrate: ")
result = calc.integrate(expression)
print(f"Result: {result}")
elif option == "6":
print("Exiting...")
break
else:
print("Invalid option! Please choose again.")
if __name__ == "__main__":
main()