پیمان غفاری پور

ماشین حساب ( فقط توسط php)

شنبه, ۲۳ آذر ۱۳۹۲، ۱۲:۴۵ ق.ظ

هدف نوشتن یک ماشین حساب بدون استفاده از کد های جاوا اسکریپت و فقط با استفاده از php و css و html

 

توجه: نحوه ی ارزیابی این ماشین حساب با استفاده از تابع eval هست

و فقط برای اینکه مثال آموزشیه از این تابع استفاده شده و اکیدا توصیه می شه که از این تابع استفاده نکنید، 

چون این تابع دستورات php را اجرا می کنه.

همچنین می تونین از این لینک  هم سورس برنامه رو کپی کنید

یا به این لینک هم برید که یه ماشین حساب دیگه هست و از تبدیل عبارت به postfix و ارزیابی آن برای محاسبه استفاده شده

 

 

<?php
$result = 0;
if (isset($_GET['result'])) {
    $result = $_GET['result'];
}
$result = calculator();

function calculator()
{
    global $result;
    $equal = '';
    try {
        if (isset($_GET['pin'])) {
            if ($_GET['pin'] === 'c') {
                $result = 0;
            } elseif ($_GET['pin'] === '=') {
                eval("\$result = " . $result . ";");
            } elseif ($result == 0) {
                $result = $_GET['pin'];
            } else {
                $result .= $_GET['pin'];
            }
        }
    } catch (Exception $e) {
        $result = "Can't divide by zero";
    }
    return $result;
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        body {
            margin: 3%;
        }

        input[type=submit] {
            width: 100px;
            height: 45px;
            border-radius: 2px;
            border-style: none;
            background-color: #0782C1;
            color: #dddddd;
            transition: background-color 1s, color 500ms;
        }

        input[type=submit]:hover {
            color: #555555;
            background-color: #05B2D2;
        }

        input[type=text] {
            width: 100%;
            text-align: center;
            font-size: 34px;
            font-family: sans-serif;
            border: #aaaaaa solid 1px;
            border-radius: 3px;
            color: #999999;
        }

        table {
            margin: 0 auto;
        }

    </style>
</head>
<body>
<form action="" method="get">
    <table>
        <tr>
            <td>
                <label>
                    <input type="text" name="result" value="<?php echo $result ?>"/>
                </label>
            </td>
        </tr>
        <tr>
            <td>
                <input name="pin" type="submit" value="1">
                <input name="pin" type="submit" value="2">
                <input name="pin" type="submit" value="3">
                <input name="pin" type="submit" value="/">
            </td>
        </tr>
        <tr>
            <td>
                <input name="pin" type="submit" value="4">
                <input name="pin" type="submit" value="5">
                <input name="pin" type="submit" value="6">
                <input name="pin" type="submit" value="*">
            </td>
        </tr>
        <tr>
            <td>
                <input name="pin" type="submit" value="7">
                <input name="pin" type="submit" value="8">
                <input name="pin" type="submit" value="9"">
                <input name="pin" type="submit" value="-">

            </td>
        </tr>
        <tr>
            <td>
                <input name="pin" type="submit" value="0"">
                <input name="pin" type="submit" value="c">
                <input name="pin" type="submit" value="="">
                <input name="pin" type="submit" value="+"">

            </td>
        </tr>
    </table>
</form>
</body>
</html>

 

 

نظرات  (۱)

  • محد و ناصر
  • سلام
    پیمان جان
    پروژه خوبی نوشتید من هم از بخشی از اون استفاده کردم امیدوارم شاهد رشد و تعالی شما در این زمینه باشم
    با آرزوی سرافرازی روز افزون
    پاسخ:
    ممنون، خوشحال شدم کدم به یه دردی خورد :)

    ارسال نظر

    ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
    شما میتوانید از این تگهای html استفاده کنید:
    <b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
    تجدید کد امنیتی