仅具有可单击按钮作为输入的PHP代码计算器 [英] Only PHP Code Calculator with Clickable Buttons as Input

查看:58
本文介绍了仅具有可单击按钮作为输入的PHP代码计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅使用PHP和HTML代码编写一个计算器。

I am trying to code a calculator with only PHP and HTML code.

该计算器应该看起来像一个真实的计算器,并且按钮应该是可单击的。

The Calculator should look like a real one and the buttons should be clickable.

现在,我被困在组合两个数字,这意味着我按下一个按钮将显示该数字。但是在按下第二个按钮后,第一个值由于 type = submit 而消失。

Right now, I am stuck at combining 2 numbers, this means I press one button the number will be shown. But after I press the second button the first value disappears because of the type="submit".

我的问题是:如何保存值并一起显示所有按下的按钮?

My Question is: How can I save the value and show all pressed buttons together?

我知道我可以用隐藏的输入来做到这一点,但是我不知道如何使用他们正确地。

I know I can do that with a hidden input, but I don't know how to use them properly.

这样做之后,我如何计算整个表达式?

Also how am I able to calculate the whole expression after doing that?

我知道javascript或任何其他客户端语言会更好,但是我想找到一种纯粹使用PHP和HTML的方法。

I know javascript or any other client-side language would be better, but I want to find a way to do it purely with PHP and HTML.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Taschenrechner</title>
        <link href="Stylesheets/Stylesheets.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if(isset($_POST['zahl'])){
    $zahl1 = $_POST['zahl'];
}else{
    $zahl1 = 0;
}
?>

<form name="rechner" action="rechner.php" method="post">
    <input id="feld1" type="hidden" name="zahl1" value="">
    <input id="feld2" type="hidden" name="opera" value="">
    <input id="feld3" type="hidden" name="zahl2" value="">
    <input id="feld4" type="hidden" name="zwischerg" value=>
    <table align="center" style="width:300px; height:450px; border:solid thick black;">
        <tr>
            <td align="center">
                <input id="display" type="text" name="bildschirm" readonly="readonly" style="text-align: center; height: 50px; width: 216px;" value=<?php echo $zahl1; $op; $zahl2 ?>>
            </td>
        </tr>
        <tr>
            <td>
                <table align="center">
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="1" >
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="2">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="3">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="+">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="4">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="5">
                        </td>
                        <td >
                            <input class="taste" type="submit" name="zahl" value="6">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="-">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="7">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="8">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="9">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="*">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input class="taste" type="submit" name="clear" value="C">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value="0">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="zahl" value=".">
                        </td>
                        <td>
                            <input class="taste" type="submit" name="operator" value="/">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <input type="submit" name="gleich" value="=" style=" width: 215px; height: 50px;">
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</form>
</body>
</html>


推荐答案

这是我真正的基础,几乎没有样式,纯PHP计算器形式:

This is my truly basic, almost style-less, pure-PHP calculator form:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic PHP Calculator</title>
</head>
<body>
<?php
//var_export($_POST);
//echo "<br>";
$buttons=[1,2,3,'+',4,5,6,'-',7,8,9,'*','C',0,'.','/','='];
$pressed='';
if(isset($_POST['pressed']) && in_array($_POST['pressed'],$buttons)){
    $pressed=$_POST['pressed'];
}
$stored='';
if(isset($_POST['stored']) && preg_match('~^(?:[\d.]+[*/+-]?)+$~',$_POST['stored'],$out)){
    $stored=$out[0];    
}
$display=$stored.$pressed;
//echo "$pressed & $stored & $display<br>";
if($pressed=='C'){
    $display='';
}elseif($pressed=='=' && preg_match('~^\d*\.?\d+(?:[*/+-]\d*\.?\d+)*$~',$stored)){
    $display.=eval("return $stored;");
}

echo "<form action=\"\" method=\"POST\">";
    echo "<table style=\"width:300px;border:solid thick black;\">";
        echo "<tr>";
            echo "<td colspan=\"4\">$display</td>";
        echo "</tr>";
        foreach(array_chunk($buttons,4) as $chunk){
            echo "<tr>";
                foreach($chunk as $button){
                    echo "<td",(sizeof($chunk)!=4?" colspan=\"4\"":""),"><button name=\"pressed\" value=\"$button\">$button</button></td>";
                }
            echo "</tr>";
        }
    echo "</table>";
    echo "<input type=\"hidden\" name=\"stored\" value=\"$display\">";
echo "</form>";
?>
</body>
</html>

以下是我测试时的屏幕截图:

Here is a screenshot while I was testing:

您可以看到,我已将每个按钮设为具有相同名称但不同的提交按钮价值观。我使用一个隐藏的输入来保留生成的表达式。除了此演示之外,还有很多增强功能和注意事项,这取决于您如何深入研究。

You can see that I've made every button a submit button with the same name, but different values. I use a single hidden input to preserve built expression. There will be many enhancements and considerations beyond this demo, it is up to you how far to go down this rabbit hole.

PS对于只是不使用侧面武器,one起眼睛并严厉地说 嘿的任何人,我们都不喜欢像您这样称呼人们的 eval()在这些部分中! 。好吧,我已努力充分验证要评估的字符串。如果有已知的孔,请告诉我,我将尝试修复它。另外,这是我尝试重新设计 eval()流程时牢记BOMDAS:

P.S. For anyone who just unholstered their sidearm, squinted one eye, and sternly uttered "Hey, we don't take kindly to folks like you callin' eval() in these here parts!". Well, I've endeavored to adequately validate the string to be evaluated. If there is a known hole, please let me know and I'll try to fix it. Alternatively, this is my attempt to re-invent the eval() process with BOMDAS in mind:

代码: (演示

$stored="2+3*4/6";
$components=preg_split('~([*/+-])~',$stored,NULL,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
while(($index=array_search('*',$components))!==false){
    array_splice($components,$index-1,3,$components[$index-1]*$components[$index+1]);
}
while(($index=array_search('/',$components))!==false){
    array_splice($components,$index-1,3,$components[$index-1]/$components[$index+1]);
}
while(($index=array_search('+',$components))!==false){
    array_splice($components,$index-1,3,$components[$index-1]+$components[$index+1]);
}
while(($index=array_search('-',$components))!==false){
    array_splice($components,$index-1,3,$components[$index-1]-$components[$index+1]);
}
echo current($components);  // 4

这篇关于仅具有可单击按钮作为输入的PHP代码计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆