0

我正在尝试在 php 中进行矩阵加法。在这里,我试图从用户那里获取输入并制作要添加的数组。我试过这个。我这里还有一些错误。任何人都可以为此提供解决方案...在此先感谢...

<html>
<body>
<form name="form1" action="matrixaddjs.php" method="post">
Enter the number of rows for matrix : <input type="text" name="ar">
Enter the number of columns for matrix : <input type="text" name="ac">
<input type="submit" name="submit" value="submit">
<script>
var row=document.getElementById('ar').value;
var col=document.getElementById('ac').value;
var i;
var j;
var k;
var l;
var amatrix= new array();
document.write('<table>');
for(i=0;i<row;i++)
{
    amatrix[i]=new array(j);
            document.write('Enter the A matrix :'); 
    document.write('<tr>');
    for(j=0;j<col;j++)
    {
        document.write('<td>');
        document.write('<input type="text" name="amatrix[i][j]">');
        document.write('</td>');
    }
document.write('</tr>');
}
document.write('</table>');


var bmatrix= new array();
    document.write('Enter the B matrix :');
document.write('<table>');
for(i=0;i<row;i++)
{
    bmatrix[i]=new array(j);
    document.write('<tr>');
    for(j=0;j<col;j++)
    {
        document.write('<td>');
        document.write('<input type="text" name="bmatrix[i][j]">');
        document.write('</td>');
    }
document.write('</tr>');
}
document.write('</table>');
</script>
</form>
<?php
if($_POST['submit']=== 'submit')
{
$amatrix=$_POST['amatrix'];
$bmatrix=$_POST['bmatrix'];
echo "<table>";
    echo "The resultant matrix is :";
for($m=0;$m<$ar;$m++)
{
    echo "<tr>";
    for($n=0;$n<$ac;$n++)
    {
        $cmatrix[$m][$n]=$amatrix[$m][$n]+$bmatrix[$m][$n];
        echo "<td>";
        echo $cmatrix[$m][$n];
        echo "</td>";
    }
    echo "</tr>";

}
echo "</table>";
}
?>

</body>
</html>

如果用户给出:

输入矩阵的行数:2

输入矩阵的列数:2

输入 A 矩阵:

2 4

3 5

输入 B 矩阵:

3 4

5 6

结果矩阵为:

5 8

8 11

这是我的预期结果。

4

2 回答 2

2

我不知道你的 php 代码逻辑。但是此代码将根据您的意愿创建一个矩阵,并进行一些基本验证。要在实际应用中使用,请根据您的可行性。 只需将此代码复制并粘贴到空白文件并在本地主机上运行。

<body>
    <form name="form1" action="matrixaddjs.php" method="post" onsubmit="return checkMatrix()">
        Enter the number of rows for matrix :
        <input type="text" name="ar" id="ar">
        Enter the number of columns for matrix :
        <input type="text" name="ac" id="ac">
        <input type="button" name="create" value="Create Matrix" onclick="createMatrix()">
        <div id="matrixA">
        </div>
        <div id="matrixB">
        </div>
        <input type="submit" name="submit" value="submit">
        <script>
            function createMatrix() {
                var row = document.getElementById('ar').value;
                var col = document.getElementById('ac').value;
                var i;
                var j;
                var k;
                var l;
                if (row == '') {
                    alert('Please Enter Number Of Rows!');
                    return false;
                }
                if (col == '') {
                    alert('Please Enter Number Of Columns !');
                    return false;
                }

                var amatrix = new Array();

                var htmlA = 'Enter the A matrix :';
                htmlA += '<table>';

                for (i = 0; i < row; i++) {
                    amatrix[i] = new Array();

                    htmlA += '<tr>';
                    for (j = 0; j < col; j++) {
                        htmlA += '<td>';
                        htmlA += '<input type="text" name="amatrix[' + i + '][' + j + ']">';
                        htmlA += '</td>';
                    }
                    htmlA += '</tr>';
                }
                htmlA += '</table>';
                document.getElementById('matrixA').innerHTML = htmlA;

                var bmatrix = new Array();
                var htmlB = 'Enter the B matrix :';
                htmlB += '<table>';
                for (i = 0; i < row; i++) {
                    bmatrix[i] = new Array();
                    htmlB += '<tr>';
                    for (j = 0; j < col; j++) {
                        htmlB += '<td>';
                        htmlB += '<input type="text" name="bmatrix[' + i + '][' + j + ']">';
                        htmlB += '</td>';
                    }
                    htmlB += '</tr>';
                }
                htmlB += '</table>';
                document.getElementById('matrixB').innerHTML = htmlB;
            }

            function checkMatrix() {
                var row = document.getElementById('ar').value;
                var col = document.getElementById('ac').value;

                if (row == '' || col == '') {
                    alert('Please Create Matrix First!');
                    return false;
                }
                else {
                    return true;
                }
            }
        </script>
    </form>
     <?php
if ($_POST['submit'] === 'submit') {
$amatrix = $_POST['amatrix'];
$bmatrix = $_POST['bmatrix'];
if (!is_array($amatrix) && !is_array($bmatrix)) {
    echo "Please Create Matrix First by Clicking on Create Matrix!";
    exit;
}
echo "<table>";
echo "The resultant matrix is :";
for ($m = 0; $m < $ar; $m++) {
    echo "<tr>";
    for ($n = 0; $n < $ac; $n++) {
        $cmatrix[$m][$n] = $amatrix[$m][$n] + $bmatrix[$m][$n];
        echo "<td>";
        echo $cmatrix[$m][$n];
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";
}
?>
</body>

于 2013-08-07T12:36:51.400 回答
1

#1

如果您使用 document.getElementById,则必须有一个 ID 而不是 NAME

<input type="text" name="ar" id="ar">

#2

将生成的 A 和 B 矩阵的代码放到一个函数中。并将它们放在一个 DIV 中

<a href="javascript:renderMatrix();">Render</a>
<div id="target"></div>
<script>
  function renderMatrix() {
    var html = '';
    html += '<table>';  // use a variable instead of document.write
    ...
    html += '<input type="text" name="amatrix['+i+']['+j+']">';  // !!!
    ...
    document.getElementById('target').innerHTML = html;
  }
</script>
于 2013-08-07T11:04:07.960 回答