dop20345 2013-08-07 10:18
浏览 45
已采纳

在php中添加矩阵

I am trying to do matrix addition in php. Here i am trying to get the input from the user and make the array for addition. I tried this. i have some more mistakes here. any one can give the solution for this... Thanks in advance...

<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>

if user gives:

Enter the number of rows for matrix : 2

Enter the number of columns for matrix : 2

Enter the A matrix :

2 4

3 5

Enter the B matrix :

3 4

5 6

The resultant matrix is:

5 8

8 11

This is my expected result.

  • 写回答

2条回答 默认 最新

  • doudang2537 2013-08-07 12:36
    关注

    I Don't Know About your php code logic. But This code will create a matrix as u wish , and having some basic validation. To Use in real App, Please according to your feasibility. Just Copy and paste this code to a blank file and run on localhost.

    <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>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算