运用Linux嵌入式Python语言实现科学计算器 具有以下功能: 功能1:二进制,八进制,十进制,十六进制,四种格 式之间可以相互切换 功能2:加减乘除基本运算,平方、开方、x^y、x^3 等 功能3:tan、sin、cos、n!、log等请帮我编写具体的代码
时间: 2023-07-10 12:06:51 浏览: 170
以下是一个简单的例子,实现一个基本的科学计算器,包括二进制,八进制,十进制和十六进制之间的转换以及基本运算、平方、开方、x^y、x^3、tan、sin、cos、n!和log等功能。
```python
import math
def binary_to_decimal(binary):
return int(binary, 2)
def octal_to_decimal(octal):
return int(octal, 8)
def hex_to_decimal(hex):
return int(hex, 16)
def decimal_to_binary(decimal):
return bin(decimal)[2:]
def decimal_to_octal(decimal):
return oct(decimal)[2:]
def decimal_to_hex(decimal):
return hex(decimal)[2:]
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def square(x):
return x ** 2
def sqrt(x):
return math.sqrt(x)
def power(x, y):
return x ** y
def cube(x):
return x ** 3
def tan(x):
return math.tan(x)
def sin(x):
return math.sin(x)
def cos(x):
return math.cos(x)
def factorial(x):
return math.factorial(x)
def log(x, base=10):
return math.log(x, base)
while True:
print("Select operation:")
print("1. Convert from Binary/Octal/Decimal/Hexadecimal")
print("2. Addition")
print("3. Subtraction")
print("4. Multiplication")
print("5. Division")
print("6. Square")
print("7. Square Root")
print("8. Power")
print("9. Cube")
print("10. Tangent")
print("11. Sine")
print("12. Cosine")
print("13. Factorial")
print("14. Logarithm")
print("0. Exit")
choice = input("Enter choice: ")
if choice == '0':
break
elif choice == '1':
num = input("Enter number: ")
base = input("Enter base (2, 8, 10, 16): ")
if base == '2':
print("Decimal: " + str(binary_to_decimal(num)))
print("Octal: " + decimal_to_octal(binary_to_decimal(num)))
print("Hexadecimal: " + decimal_to_hex(binary_to_decimal(num)))
elif base == '8':
print("Decimal: " + str(octal_to_decimal(num)))
print("Binary: " + decimal_to_binary(octal_to_decimal(num)))
print("Hexadecimal: " + decimal_to_hex(octal_to_decimal(num)))
elif base == '10':
print("Binary: " + decimal_to_binary(int(num)))
print("Octal: " + decimal_to_octal(int(num)))
print("Hexadecimal: " + decimal_to_hex(int(num)))
elif base == '16':
print("Decimal: " + str(hex_to_decimal(num)))
print("Binary: " + decimal_to_binary(hex_to_decimal(num)))
print("Octal: " + decimal_to_octal(hex_to_decimal(num)))
elif choice in ('2', '3', '4', '5'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '2':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '3':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '4':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '5':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == '6':
num = float(input("Enter number: "))
print("Square of", num, "is", square(num))
elif choice == '7':
num = float(input("Enter number: "))
print("Square root of", num, "is", sqrt(num))
elif choice == '8':
num1 = float(input("Enter base: "))
num2 = float(input("Enter exponent: "))
print(num1, "^", num2, "=", power(num1, num2))
elif choice == '9':
num = float(input("Enter number: "))
print("Cube of", num, "is", cube(num))
elif choice == '10':
num = float(input("Enter number: "))
print("Tangent of", num, "is", tan(num))
elif choice == '11':
num = float(input("Enter number: "))
print("Sine of", num, "is", sin(num))
elif choice == '12':
num = float(input("Enter number: "))
print("Cosine of", num, "is", cos(num))
elif choice == '13':
num = int(input("Enter number: "))
print("Factorial of", num, "is", factorial(num))
elif choice == '14':
num = float(input("Enter number: "))
base = float(input("Enter base: "))
print("Logarithm of", num, "with base", base, "is", log(num, base))
else:
print("Invalid input")
```
注意:此代码仅为示例,可能需要根据具体需求进行修改。
阅读全文