AI编写代码(个性计算器)

分享一个个性计算器,由吾爱大神用AI写的计算器,因为有些时候计算的结果需要记录,使用用ai写了一个自动记录计算过程的计算器。

这是HTML和js写的,必须下载附件!必须下载附件!必须下载附件!重要的事情说三遍
如果你要是喜欢自行下载

夸克链接:夸克网盘分享,百度链接:https://pan.baidu.com/s/14D7UfmnFUV_BChU59uxMHQ 提取码:6666

{JavaScript}以下为代码

// 获取DOM元素
const display = document.getElementById('display');
const equation = document.getElementById('equation');
const buttons = document.querySelectorAll('.btn');
const historyTable = document.getElementById('historyTable').querySelector('tbody');
const clearBtn = document.getElementById('clearBtn');
const downloadBtn = document.getElementById('downloadBtn');
const calculationType = document.getElementById('calculationType');
const dimensionInputs = document.querySelectorAll('.dimension-input');
 
// 键盘按键映射
const keyMap = {
    '0': '0', '1': '1', '2': '2', '3': '3', '4': '4',
    '5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
    '.': '.', '+': '+', '-': '-', '*': '*', '/': '/',
    'Enter': '=', '=': '=', 'Backspace': '⌫', 'Escape': 'C',
    '%': '%'
};
 
// 添加键盘事件监听
document.addEventListener('keydown', (e) => {
    // 如果当前焦点在输入框上,则不处理键盘事件
    if (e.target.tagName === 'INPUT' && e.target.type === 'text') {
        return;
    }
     
    const key = keyMap[e.key];
    if (key) {
        const button = document.querySelector(`.btn[value="${key}"]`);
        if (button) {
            button.click();
        }
    }
});
 
// 计算器状态
let currentInput = '0';
let equationStr = '';
let result = null;
 
// 更新显示
function updateDisplay() {
    display.textContent = currentInput;
    equation.textContent = equationStr;
}
 
// 处理数字输入
function handleNumber(number) {
    if (currentInput === '0' && number !== '.') {
        currentInput = number;
    } else {
        currentInput += number;
    }
    updateDisplay();
}
 
// 处理运算符
function handleOperator(operator) {
    if (operator === 'C') {
        currentInput = '0';
        equationStr = '';
        result = null;
    } else if (operator === '⌫') {
        currentInput = currentInput.slice(0, -1) || '0';
    } else if (operator === '+/-') {
        currentInput = (parseFloat(currentInput) * -1).toString();
    } else if (operator === '%') {
        currentInput = (parseFloat(currentInput) / 100).toString();
    } else {
        equationStr += currentInput + operator;
        currentInput = '0';
    }
    updateDisplay();
}
 
// 计算等式
function calculate() {
    try {
        equationStr += currentInput;
        result = eval(equationStr);
        currentInput = result.toString();
         
        // 添加到历史记录
        history.push({
            equation: equationStr,
            result: result
        });
        updateHistoryTable();
         
        equationStr = '';
        updateDisplay();
    } catch (e) {
        currentInput = 'Error';
        updateDisplay();
    }
}
 
// 更新历史记录表格
// 初始化时加载历史记录
let history = JSON.parse(localStorage.getItem('calcHistory') || '[]');
updateHistoryTable();
 
// 更新历史记录时保存到localStorage
function updateHistoryTable() {
    localStorage.setItem('calcHistory', JSON.stringify(history));
    historyTable.innerHTML = '';
    history.forEach((item, index) => {
        const row = document.createElement('tr');
        row.innerHTML = `
            <td>${index + 1}</td>
            <td>${item.equation}</td>
            <td>${item.result}</td>
        `;
        historyTable.appendChild(row);
    });
}
 
// 修改清除历史函数
function clearHistory() {
    history = [];
    localStorage.removeItem('calcHistory');
    updateHistoryTable();
}
 
// 修改下载功能读取localStorage
function downloadHistory() {
    const savedHistory = JSON.parse(localStorage.getItem('calcHistory') || '[]');
    const wb = XLSX.utils.book_new();
    const ws = XLSX.utils.json_to_sheet(savedHistory.map((item, index) => ({
        '序号': index + 1,
        '计算式': item.equation,
        '结果': item.result
    })));
    XLSX.utils.book_append_sheet(wb, ws, '计算历史');
    XLSX.writeFile(wb, '计算历史.xlsx');
}
 
// 绑定按钮事件
buttons.forEach(button => {
    button.addEventListener('click', () => {
        const value = button.value;
         
        if (button.classList.contains('number')) {
            handleNumber(value);
        } else if (button.classList.contains('operator')) {
            if (value === '=') {
                calculate();
            } else {
                handleOperator(value);
            }
        }
    });
});
 
// 绑定功能按钮事件
clearBtn.addEventListener('click', clearHistory);
downloadBtn.addEventListener('click', downloadHistory);
 
// 初始化显示
updateDisplay();
 
// 监听计算类型变化
calculationType.addEventListener('change', function() {
    // 获取所有输入框
    const inputGroups = document.querySelectorAll('.input-group');
     
    // 默认隐藏所有输入框(从第二个开始,第一个是计算类型选择)
    for (let i = 1; i < inputGroups.length; i++) {
        inputGroups[i].style.display = 'none';
    }
     
    // 根据选择的值显示对应的输入框
    switch(this.value) {
        case 'area': // 长方形面积
            document.getElementById('lengthGroup').style.display = 'flex';
            document.getElementById('widthGroup').style.display = 'flex';
            break;
        case 'cylinder_area': // 圆柱体面积
            document.getElementById('radiusGroup').style.display = 'flex';
            document.getElementById('heightGroup').style.display = 'flex';
            break;
        case 'cone_area': // 圆锥体面积
            document.getElementById('radiusGroup').style.display = 'flex';
            document.getElementById('generatrixGroup').style.display = 'flex';
            break;
        case 'trapezoid_area': // 梯形面积
            document.getElementById('upperBaseGroup').style.display = 'flex';
            document.getElementById('lowerBaseGroup').style.display = 'flex';
            document.getElementById('heightGroup').style.display = 'flex';
            break;
        case 'rectangle_perimeter': // 长方形周长
            document.getElementById('lengthGroup').style.display = 'flex';
            document.getElementById('widthGroup').style.display = 'flex';
            break;
        case 'circle_perimeter': // 圆形周长
            document.getElementById('radiusGroup').style.display = 'flex';
            break;
        case 'cube_volume': // 立方体体积
            document.getElementById('lengthGroup').style.display = 'flex';
            document.getElementById('widthGroup').style.display = 'flex';
            document.getElementById('heightGroup').style.display = 'flex';
            break;
        case 'cylinder_volume': // 圆柱体体积
            document.getElementById('radiusGroup').style.display = 'flex';
            document.getElementById('heightGroup').style.display = 'flex';
            break;
        case 'triangle_area': // 三角形面积
            document.getElementById('baseGroup').style.display = 'flex';
            document.getElementById('height1Group').style.display = 'flex';
            break;
    }
});
 
// 初始隐藏所有输入框(从第二个开始)
const inputGroups = document.querySelectorAll('.input-group');
for (let i = 1; i < inputGroups.length; i++) {
    inputGroups[i].style.display = 'none';
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值