四阶行列式计算器

这是一个计算器来计算四阶行列式,并且将计算结果显示在桌面上。

实用JavaScript实现的行列式计算器。并且用html渲染出来

代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4x4 行列式计算</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table th,
        table td {
            border: 1px solid #dee2e6;
            padding: 10px;
            text-align: center;
        }

        button {
            padding: 10px 20px;
            border-radius: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #0056b3;
        }

        #result {
            font-size: 18px;
            font-weight: bold;
            margin-top: 20px;
            color: #333;
            text-align: center;
        }
    </style>
</head>

<body>
<table border="1">
    <tr>
        <td><input type="number" id="cell00"></td>
        <td><input type="number" id="cell01"></td>
        <td><input type="number" id="cell02"></td>
        <td><input type="number" id="cell03"></td>
    </tr>
    <tr>
        <td><input type="number" id="cell10"></td>
        <td><input type="number" id="cell11"></td>
        <td><input type="number" id="cell12"></td>
        <td><input type="number" id="cell13"></td>
    </tr>
    <tr>
        <td><input type="number" id="cell20"></td>
        <td><input type="number" id="cell21"></td>
        <td><input type="number" id="cell22"></td>
        <td><input type="number" id="cell23"></td>
    </tr>
    <tr>
        <td><input type="number" id="cell30"></td>
        <td><input type="number" id="cell31"></td>
        <td><input type="number" id="cell32"></td>
        <td><input type="number" id="cell33"></td>
    </tr>
</table>
<button onclick="calculateDeterminant()">计算</button>
<p id="result"></p>

<script>
    function calculateDeterminant() {
        let cells = [];
        for (let i = 0; i < 4; i++) {
            cells[i] = [];
            for (let j = 0; j < 4; j++) {
                cells[i][j] = parseInt(document.getElementById(`cell${i}${j}`).value);
            }
        }

        let sign = 1;
        let result = 0;

        for (let i0 = 0; i0 < 4; i0++) {
            for (let i1 = 0; i1 < 4; i1++) {
                if (i1 === i0) continue;
                for (let i2 = 0; i2 < 4; i2++) {
                    if (i2 === i0 || i2 === i1) continue;
                    for (let i3 = 0; i3 < 4; i3++) {
                        if (i3 === i0 || i3 === i1 || i3 === i2) continue;

                        let permutation = [i0, i1, i2, i3];
                        let invCount = 0;

                        for (let i = 0; i < 4; i++) {
                            for (let j = i + 1; j < 4; j++) {
                                if (permutation[i] > permutation[j]) invCount++;
                            }
                        }

                        let term = sign * cells[i0][0] * cells[i1][1] * cells[i2][2] * cells[i3][3];
                        if (invCount % 2 === 1) sign = -1;
                        else sign = 1;
                        result += term;
                    }
                }
            }
        }

        document.getElementById('result').innerHTML = `行列式的结果: ${result}`;
    }
</script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值