多功能计算器

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:
        return x / y
    else:
        return "Error: Division by zero is not allowed"
def square(x):
    return x ** 2

def square_root(x):
    if x >= 0:
        return x ** 0.5
    else:
        return "Error: Cannot calculate square root of a negative number"
def reciprocal(x):
    if x != 0:
        return 1 / x
    else:
        return "Error: Cannot calculate reciprocal of zero"

def remainder(x, y):
    if y != 0:
        return x % y
    else:
        return "Error: Division by zero is not allowed"
def save_history(filename):
    with open(filename, 'w') as f:
        for item in history:
            f.write(item + '\n')

def store_variable(name, value):
    variables[name] = value

def retrieve_variable(name):
    return variables.get(name, "Error: Variable not found")

variables = {}

def save_history(filename):
    with open(filename, 'w') as f:
        for item in history:
            f.write(item + '\n')

def load_history(filename):
    global history
    history.clear()  
    try:
        with open(filename, 'r') as f:
            history = f.readlines()
    except FileNotFoundError:
        print("Error: History file not found.")

password='2012'
while True:
    password_user=input("请输入密码:")
    if password!=password_user:
        print("密码错误,请重新输入")
    
    else:
        print("密码正确")
        break

    



history = []



while True:
    print("选择操作。")
    print("1. 加法")
    print("2. 减法")
    print("3. 乘法")
    print("4. 除法")
    print("5. 平方")
    print("6. 平方根")
    print("7. 求余数")
    print("8. 计算倒数")
    print("9. 查看历史")
    print("10. 清除历史")
    print("11. 存储变量")
    print("12. 检索变量")
    print("13. 导入历史")
    print("14. 导出历史")
    print("15. 退出")

    choice = input("输入你的选择(1-15): ")

    

    if choice in ('1', '2', '3', '4', '5', '6'):
        num1 = float(input("输入第一个数字: "))

        if choice in ('1', '2', '3', '4'):
            num2 = float(input("输入第二个数字: "))

            if choice == '1':
                result = add(num1, num2)
                operation = "+"
            elif choice == '2':
                result = subtract(num1, num2)
                operation = "-"
            elif choice == '3':
                result = multiply(num1, num2)
                operation = "*"
            elif choice == '4':
                result = divide(num1, num2)
                operation = "/"

            # 将操作添加到历史记录中
            history.append(f"{num1} {operation} {num2} = {result}")

        elif choice == '5':
            result = square(num1)
            # 将操作添加到历史记录中
            history.append(f"Square of {num1} = {result}")

        elif choice == '6':
            result = square_root(num1)
            # 将操作添加到历史记录中
            history.append(f"Square root of {num1} = {result}")

        print(result)

    elif choice == '9':
        if history:
            print("历史记录:")
            for idx, item in enumerate(history, 1):
                print(f"{idx}. {item}")
        else:
            print("没有历史记录。")

    elif choice == '10':
        history.clear()
        print("历史记录已清除。")

    elif choice == '15':
        print("退出计算器程序。")
        break

    else:
        print("无效的选择,请重新输入。")

  

    if choice == '7':
        num1 = float(input("输入被除数: "))
        num2 = float(input("输入除数: "))
        result = remainder(num1, num2)
        print(result)
        history.append(f"{num1} % {num2} = {result}")

    elif choice == '8':
        num = float(input("输入一个数字: "))
        result = reciprocal(num)
        print(result)
        history.append(f"Reciprocal of {num} = {result}")

    elif choice == '11':
        var_name = input("输入变量名: ")
        var_value = float(input("输入变量值: "))
        store_variable(var_name, var_value)
        print(f"变量 {var_name} 存储了值 {var_value}")

    elif choice == '12':
        var_name = input("输入要检索的变量名: ")
        result = retrieve_variable(var_name)
        print(result)

    elif choice == '13':
        filename = input("输入要导入的历史文件名: ")
        load_history(filename)
        print("历史记录已导入。")

    elif choice == '14':
        filename = input("输入要导出的历史文件名: ")
        save_history(filename)
        print("历史记录已导出。")

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值