html+JavaScript+css 24点计算器

源代码       采用穷举计算方法

讲人话:根据四个数随机列算式,算出来是24就显示在列表里。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>24 Point Game</title>
<style>
    body {
        background: #f0f0f0;
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
    }
    .game-container {
        background: #fff;
        border-radius: 10px;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        padding: 20px;
        width: 400px;
    }
    h1 {
        text-align: center;
        color: #333;
    }
    input[type="number"] {
        width: 100%;
        padding: 10px;
        margin-bottom: 10px;
        border: none;
        border-radius: 5px;
        box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
    }
    button {
        width: 100%;
        padding: 10px;
        background: #007bff;
        color: #fff;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        transition: background 0.3s;
    }
    button:hover {
        background: #0056b3;
    }
    .results {
        margin-top: 20px;
        color: #333;
    }
    .results p {
        margin: 5px 0;
    }
</style>
</head>
<body>
<div class="game-container">
    <h1>24 Point Game</h1>
    <input type="number" id="num1" placeholder="Enter Number 1">
    <input type="number" id="num2" placeholder="Enter Number 2">
    <input type="number" id="num3" placeholder="Enter Number 3">
    <input type="number" id="num4" placeholder="Enter Number 4">
    <button οnclick="solve24Point()">Calculate</button>
    <div class="results" id="results"></div>
</div>

<script>
function solve24Point() {
    const numbers = [
        parseFloat(document.getElementById('num1').value),
        parseFloat(document.getElementById('num2').value),
        parseFloat(document.getElementById('num3').value),
        parseFloat(document.getElementById('num4').value)
    ];
    const solutions = findSolutions(numbers);
    displaySolutions(solutions);
}

function findSolutions(nums) {
    const ops = ['+', '-', '*', '/'];
    const solutions = [];
    const permutations = getPermutations(nums);
    
    for (let perm of permutations) {
        for (let op1 of ops) {
            for (let op2 of ops) {
                for (let op3 of ops) {
                    let expr = `(${perm[0]} ${op1} ${perm[1]}) ${op2} (${perm[2]} ${op3} ${perm[3]})`;
                    let result = evalExpression(expr);
                    if (Math.abs(result - 24) < 0.01) {
                        solutions.push(`(${perm[0]} ${op1} ${perm[1]}) ${op2} (${perm[2]} ${op3} ${perm[3]}) = ${result.toFixed(2)}`);
                    }
                }
            }
        }
    }
    return solutions;
}

function getPermutations(arr) {
    if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
    return arr.reduce((acc, item, i) => acc.concat(
        getPermutations(arr.slice(0, i).concat(arr.slice(i + 1)))
            .map(x => [item].concat(x))
    ), []);
}

function evalExpression(expression) {
    try {
        return eval(expression);
    } catch (e) {
        return NaN;
    }
}

function displaySolutions(solutions) {
    const resultsDiv = document.getElementById('results');
    resultsDiv.innerHTML = '';
    if (solutions.length > 0) {
        solutions.forEach(solution => {
            resultsDiv.innerHTML += `<p>${solution}</p>`;
        });
    } else {
        resultsDiv.innerHTML = '<p>No solution found.</p>';
    }
}
</script>
</body>
</html>

 

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的 HTMLCSSJavaScript 实现的计算器HTML 代码: ``` <!DOCTYPE html> <html> <head> <title>计算器</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> </head> <body> <div class="calculator"> <div class="display"> <input type="text" id="result" disabled> </div> <div class="buttons"> <button onclick="clearResult()">C</button> <button onclick="backspace()">←</button> <button onclick="insertValue('%')">%</button> <button onclick="insertValue('/')">÷</button> <button onclick="insertValue('7')">7</button> <button onclick="insertValue('8')">8</button> <button onclick="insertValue('9')">9</button> <button onclick="insertValue('*')">x</button> <button onclick="insertValue('4')">4</button> <button onclick="insertValue('5')">5</button> <button onclick="insertValue('6')">6</button> <button onclick="insertValue('-')">-</button> <button onclick="insertValue('1')">1</button> <button onclick="insertValue('2')">2</button> <button onclick="insertValue('3')">3</button> <button onclick="insertValue('+')">+</button> <button onclick="insertValue('0')">0</button> <button onclick="insertValue('.')">.</button> <button onclick="calculate()">=</button> </div> </div> <script src="script.js"></script> </body> </html> ``` CSS 代码: ``` * { margin: 0; padding: 0; box-sizing: border-box; } body { background-color: #f2f2f2; font-size: 16px; font-family: Arial, sans-serif; } .calculator { margin: 50px auto; width: 300px; background-color: #fff; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2); } .display { padding: 10px; border-bottom: 1px solid #ccc; } .display input { width: 100%; border: none; font-size: 20px; text-align: right; } .buttons { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 1px; } .buttons button { padding: 10px; font-size: 20px; background-color: #f2f2f2; border: none; cursor: pointer; } .buttons button:hover { background-color: #ddd; } ``` JavaScript 代码: ``` let result = document.getElementById('result'); function insertValue(val) { result.value += val; } function clearResult() { result.value = ''; } function backspace() { result.value = result.value.slice(0, -1); } function calculate() { let expression = result.value; if (expression) { result.value = eval(expression); } } ``` 该计算器实现了加减乘除四则运算、百分比计算、清除、删除、等于号计算和小数点输入的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值