吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3350|回复: 36
收起左侧

[其他原创] AI写的《个性计算器》

  [复制链接]
快乐的小驹 发表于 2025-5-1 06:18
本帖最后由 快乐的小驹 于 2025-5-10 08:56 编辑

用AI写的计算器,因为有些时候计算的结果需要记录,使用用ai写了一个自动记录计算过程的计算器。

这是HTML和js写的,必须下载附件!
必须下载附件!必须下载附件!
如果你要是喜欢

免费的评分给点吧。

微信图片_2025-05-01_061417_787.png

[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';
}


个性计算器.zip (315.99 KB, 下载次数: 192)


《Excel文件比较器v1.3》
https://www.52pojie.cn/thread-2020893-1-1.html

没啥用软件 - 《Python库的批量安装》
https://www.52pojie.cn/thread-2030214-1-1.html

没啥用软件 - 《本地测试服务器》
https://www.52pojie.cn/thread-2030222-1-1.html

免费评分

参与人数 9吾爱币 +12 热心值 +8 收起 理由
sunmou + 1 + 1 我很赞同!
nywthy + 2 + 1 谢谢@Thanks!
mohan007 + 1 + 1 我很赞同!
wwr21 + 1 + 1 热心回复!
lovepluto6 + 1 + 1 我很赞同!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
yingzichuanshuo + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
pyjiujiu + 1 热心回复!
shisoro + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

mujita666 发表于 2025-5-1 07:29
界面看着还不错,新手学习
shutiao88888 发表于 2025-5-1 15:01
安逸zzy 发表于 2025-5-1 06:32
 楼主| 快乐的小驹 发表于 2025-5-1 06:33
安逸zzy 发表于 2025-5-1 06:32
大佬,怎么运行的啊

在这个是html的~要下载的!
scxclzyzb 发表于 2025-5-1 07:43
确实有个性,第一次用这种形式的。
52soft 发表于 2025-5-1 07:54
AI无所不能
那些年打的飞机 发表于 2025-5-1 08:11
只给结果不说过程的吗?用啥AI写的,教一下呗……
那些年打的飞机 发表于 2025-5-1 08:12
只给结果不说过程的吗?用啥AI写的,教一下呗……
 楼主| 快乐的小驹 发表于 2025-5-1 08:16
那些年打的飞机 发表于 2025-5-1 08:12
只给结果不说过程的吗?用啥AI写的,教一下呗……

用Trae CN 写的!
walykyy 发表于 2025-5-1 08:55
这个真不错,感谢分享,现在ai越来越强大了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-3-26 02:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表