duanjue6575 2014-06-22 11:48
浏览 42
已采纳

gcd和表格编码问题[关闭]

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title>Euclidean Algorithm</title>
    </head>
    <body>

        <form method="get" action="EuclidAlgo.php">
            <fieldset>

<label for="remainder">Euclidean Algorithm : </label>
<input type="text" name="NumberA" value="<?php echo (isset($_GET['NumberA'])) ? $_GET['NumberA'] : 5; ?>"/>
<input type="text" name="NumberB" value="<?php echo (isset($_GET['NumberB'])) ? $_GET['NumberB'] : 5; ?>"/>
<input type="submit" name='submit' value="submit" />
</fieldset>
</form>

    <?php



    function gcd($a, $b)


    {
        $a = abs($a);
        $b = abs($b);

        if ($a == 0)
        {
            return $b;
        }
        elseif ($b == 0)
        {
            return $a;
        }
        elseif ($a > $b)
        {
            return gcd($b, $a % $b);
        }
        else
        {
            return gcd($a, $b % $a);
        }
    }

    function lcm($a, $b)
    {
        $a = abs($a);
        $b = abs($b);

        if  ($a > $b)
        {
            return ($b / gcd($a, $b)) * $a;
        }
        else
        {
            return ($a / gcd($a, $b)) * $a;
        }
    }

   if (isset($_GET['submit']))
{
    $a = $_GET['NumberA'];
    $b = $_GET['NumberB'];
} else {
     $a = 5;
     $b = 5;    
}
echo gcd($a, $b);
?>
<br />
<?php
echo lcm($a, $b);



    ?>

My lcm function is not running correctly for example when i enter 100 and 150 into my program rather than it returning 50 it is returning 200 any ideas, also if i am missing anything important that i need to state just ask rather than complaining or giving my work a low mark.

  • 写回答

1条回答 默认 最新

  • duan1933 2014-06-22 11:59
    关注

    Try

    if (isset($_GET['submit']))
    

    instead of

    if (isset($_GET['gcd']))
    

    Why? Because the form doesn't have a field named gcd.


    EDIT: based on your edit, and some suggestions.

    Change your HTML (suggestion) to look like:

    <input type="text" name="NumberA" value="<?php echo (isset($_GET['NumberA'])) ? $_GET['NumberA'] : 0; ?>"/>
    <input type="text" name="NumberB" value="<?php echo (isset($_GET['NumberB'])) ? $_GET['NumberB'] : 0; ?>"/>
    <input type="submit" name='submit' value="submit" />
    

    This will prevent any 'undefined variable' errors from popping up.

    Then end your PHP with:

    if (isset($_GET['submit']))
    {
        $a = $_GET['NumberA'];
        $b = $_GET['NumberB'];
    } else {
         $a = 0;
         $b = 0;    
    }
    echo gcd($a, $b);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题