用python写一个高难度的科学专业计算器
时间: 2023-07-01 16:11:28 浏览: 157
好的,我可以为您提供一个基于Python的科学专业计算器的框架,您可以根据自己的需求进行修改和扩展。
首先,您需要安装Python的科学计算库numpy和绘图库matplotlib。可以通过以下命令进行安装:
```
pip install numpy
pip install matplotlib
```
接下来,您可以使用Python的Tkinter库创建一个用户界面,并添加计算器的各种功能。以下是一个示例代码,包括基本的数学函数和常量:
```python
import tkinter as tk
import numpy as np
import matplotlib.pyplot as plt
class Calculator:
def __init__(self, master):
self.master = master
master.title("Science Calculator")
# Create input field
self.input_field = tk.Entry(master, width=50, borderwidth=5)
self.input_field.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# Create buttons
self.create_button("sin", lambda: self.operation(np.sin))
self.create_button("cos", lambda: self.operation(np.cos))
self.create_button("tan", lambda: self.operation(np.tan))
self.create_button("^", lambda: self.operation(np.power))
self.create_button("log", lambda: self.operation(np.log10))
self.create_button("ln", lambda: self.operation(np.log))
self.create_button("e", lambda: self.add_to_input(np.e))
self.create_button("pi", lambda: self.add_to_input(np.pi))
self.create_button("7", lambda: self.add_to_input("7"))
self.create_button("8", lambda: self.add_to_input("8"))
self.create_button("9", lambda: self.add_to_input("9"))
self.create_button("/", lambda: self.add_to_input("/"))
self.create_button("4", lambda: self.add_to_input("4"))
self.create_button("5", lambda: self.add_to_input("5"))
self.create_button("6", lambda: self.add_to_input("6"))
self.create_button("*", lambda: self.add_to_input("*"))
self.create_button("1", lambda: self.add_to_input("1"))
self.create_button("2", lambda: self.add_to_input("2"))
self.create_button("3", lambda: self.add_to_input("3"))
self.create_button("-", lambda: self.add_to_input("-"))
self.create_button("0", lambda: self.add_to_input("0"))
self.create_button(".", lambda: self.add_to_input("."))
self.create_button("=", lambda: self.calculate())
self.create_button("+", lambda: self.add_to_input("+"))
self.create_button("Clear", lambda: self.clear_input())
self.create_button("Plot", lambda: self.plot())
def create_button(self, text, command):
button = tk.Button(self.master, text=text, padx=40, pady=20, command=command)
button.grid(row=self.get_row(text), column=self.get_column(text))
def get_row(self, text):
if text in ["sin", "cos", "tan", "^", "log", "ln", "e", "pi"]:
return 1
elif text in ["7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"]:
if text in ["0", ".", "="]:
return 5
elif text in ["+", "-", "*", "/"]:
return self.get_row("0") + self.get_column(text) // 2
else:
return self.get_row("0") + self.get_column(text) // 3
else:
return 0
def get_column(self, text):
if text in ["sin", "7", "4", "1"]:
return 0
elif text in ["cos", "8", "5", "2", "0", ".", "Clear"]:
return 1
elif text in ["tan", "9", "6", "3", "=", "+", "-", "*", "/"]:
return 2
elif text in ["^", "log", "ln", "e", "pi", "Plot"]:
return 3
def add_to_input(self, value):
self.input_field.insert(tk.END, value)
def clear_input(self):
self.input_field.delete(0, tk.END)
def operation(self, func):
x = float(self.input_field.get())
self.input_field.delete(0, tk.END)
self.input_field.insert(0, str(func(x)))
def calculate(self):
try:
result = eval(self.input_field.get())
self.input_field.delete(0, tk.END)
self.input_field.insert(0, str(result))
except:
self.input_field.delete(0, tk.END)
self.input_field.insert(0, "Error")
def plot(self):
try:
x = np.linspace(-10, 10, 100)
y = eval(self.input_field.get())
plt.plot(x, y)
plt.show()
except:
self.input_field.delete(0, tk.END)
self.input_field.insert(0, "Error")
root = tk.Tk()
my_calculator = Calculator(root)
root.mainloop()
```
此代码创建了一个包含各种数字和运算符的简单计算器,以及一些科学函数和常量。它还包括绘图功能,可以将用户输入的函数绘制为图形。
您可以使用此框架扩展和修改以满足您的需求,例如添加更多的科学函数和常量,或者使用不同的GUI库创建更复杂的用户界面。
阅读全文