import math 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): if y == 0: raise ValueError("除数不能为0") return x / y def power(x, y): return x ** y def sqrt(x): if x < 0: raise ValueError("负数没有平方根") return math.sqrt(x) def log(x, base=math.e): if x <= 0: raise ValueError("对数的底数必须大于0") return math.log(x, base) def sin(x): return math.sin(x) def cos(x): return math.cos(x) def tan(x): return math.tan(x) def main(): while True: print("请选择操作:") print("1.加法") print("2.减法") print("3.乘法") print("4.除法") print("5.幂运算") print("6.平方根") print("7.对数") print("8.正弦") print("9.余弦") print("10.正切") print("0.退出") choice = input("请输入操作序号:") if choice == '0': break if choice in ('1', '2', '3', '4', '5'): num1 = float(input("请输入第一个数字:")) num2 = float(input("请输入第二个数字:")) if choice == '1': print("结果:", add(num1, num2)) elif choice == '2': print("结果:", subtract(num1, num2)) elif choice == '3': print("结果:", multiply(num1, num2)) elif choice == '4': try: print("结果:", divide(num1, num2)) except ValueError as e: print(e) elif choice == '5': print("结果:", power(num1, num2)) elif choice in ('6', '7', '8', '9', '10'): num = float(input("请输入一个数字:")) if choice == '6': try: print("结果:", sqrt(num)) except ValueError as e: print(e) elif choice == '7': base = float(input("请输入对数的底数(默认为自然对数):")) or math.e try: print("结果:", log(num, base)) except ValueError as e: print(e) elif choice == '8': print("结果:", sin(num)) elif choice == '9': print("结果:", cos(num)) elif choice == '10': try: print("结果:", tan(num)) except ValueError as e: print(e) else: print("无效的输入,请重新输入") if __name__ == "__main__": main()
各位小伙伴们,我刚刚为大家分享了有关科学计算器源码的知识,希望对你们有所帮助。如果您还有其他相关问题需要解决,欢迎随时提出哦!
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1090565.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复