webrekenmachine

Gesponsorde koppelingen

PHP script bestanden

  1. webrekenmachine

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
error_reporting(E_ALL);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>PHP rekenmachine</title>
<link rel="stylesheet" type="text/css" href="rekenmachine.css" />
</head>
<body>
<?php
$calc
= new Calculator();
$calc->checkAction();
$calc->display();

?>

</body>
</html>
<?php


class Calculator {
    protected $buttons = array ();
    protected $totals;
    protected $ops = array ('div' => '/', 'mult' => '*', 'sub' => '-', 'add' => '+');

    public function __construct() {
        $this->totals = new Totals();
        reset($this->ops);
        for ($i = 1; $i < 10; $i ++) {
            $this->buttons[] = new NumberButton("b$i", "$i");
            if ($i % 3 == 0) {
                list ($key, $val) = each($this->ops);
                $this->buttons[] = new OpsButton("$key", "$val");
            }
        }

        $this->buttons[] = new NumberButton("b0", "0");
        $this->buttons[] = new NumberButton("dot", ".");
        $this->buttons[] = new EqualsButton("eq", "=");
        list ($key, $val) = each($this->ops);
        $this->buttons[] = new OpsButton("$key", "$val");
        $this->buttons[] = new ClearButton("clear", "clear");
    }

    
    public function display() {
        $srv = $_SERVER['PHP_SELF'];
        echo "<div class='rekenmachine''>\n";
        echo '<br/><br/>';
        echo "<form action='$srv' method='post'>\n";
        echo $this->totals->displayTotals();
        echo '<br/><table><tr>';
        $i=0;
        foreach ($this->buttons as $button) {
            echo $button->displayButton();
            $i++;
             if ($i % 4 == 0) {
                echo '</tr></tr>'; // start a new row after 4 buttons
            }
        }

        echo '</tr></table></div>';
    }

    
    public function checkAction() {
        foreach ($this->buttons as $button) {
            if ($button->keyIsset()) {
                $button->handleIsset($this->totals);
                break;
            }
        }
    }

}


class Totals {
    public $display = "";
    public $expression = "";
    
    public function __construct() {
        $this->display = $_POST['display'];
        $this->expression = $_POST['totval'];
    }

    
    public function clear() {
        $this->display = "";
        $this->expression = "";
    }

    
    public function addNumber($num) {
        $this->display = $this->display.$num;
    }


    public function addOps($ops) {
        $this->expression = $this->expression.$this->display.$ops;
        $this->display = "";
    }

    
    public function evalTotals() {
        $this->expression = $this->expression.$this->display;
        $last = substr($this->expression, -1, 1); // find the last character
        if (is_numeric($last)) {
            eval ("\$this->display = $this->expression;");
            $this->expression = "";
        }
    }

    
    public function displayTotals() {
        $dval = $this->display;
        $disp = "<input type='text' name='display' class='display'".
            "value='$dval' size='14' maxlength='40' readonly />";
        $expression = $this->expression;
        $hidden = "<input type='hidden' name='totval' value='$expression' />";
        return "$disp\n$hidden<br/>";
    }
}


abstract class CalcButton {
    protected $buttonName;
    protected $buttonValue;
    
    public function __construct($name, $value) {
        $this->buttonName = $name;
        $this->buttonValue = $value;
    }


    public function displayButton() {
        $buttonValue = $this->buttonValue;
        $buttonName = $this->buttonName;
        return "<td><input type='submit' name='$buttonName' value='$buttonValue'/></td>\n";
    }

    
    public function keyIsset() {
        if (isset($_POST["$this->buttonName"])) return TRUE;
        else return FALSE;
    }

    
    abstract public function handleIsset(Totals $totals);
}


class NumberButton extends CalcButton {
    public function handleIsset(Totals $totals) {
        $totals->addNumber($this->buttonValue);
    }

}


class OpsButton extends CalcButton {
    public function handleIsset(Totals $totals) {
        $totals->addOps($this->buttonValue);
    }
}


class EqualsButton extends CalcButton {
 
    public function handleIsset(Totals $totals) {
        $totals->evalTotals();
    }
}


class ClearButton extends CalcButton {
    public function displayButton() {
        $buttonValue = $this->buttonValue;
        $buttonName = $this->buttonName;
        return "<td colspan='4'><input class='clear' type='submit' name='$buttonName' value='$buttonValue'/></td>\n";
    }


    public function handleIsset(Totals $totals) {
        $totals->clear();
}
    
}

?>









Nu de CSS :
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
body {background-color: white; text-align: center; }
input { font-family: "Courier New"; font-size: 30px}
table {text-align: center; margin-left: auto; margin-right: auto;}
div.rekenmachine {
    width: 250px;  border-style: solid; border-width: 1px; padding: 10px;
}

.display {text-align: right; font-size: 20px }
.clear { font-size: 20px }






Ik hoop dat jullie het een erg goede eerste script vinden..
Bedankt

Ohja editje het script werkt volledig er zitten geen bugs meer in..
Als ik tijd genoeg heb post ik ook nog een soort van imagelibary .!
Edit: Verplaatst naar andere server (@Boaz)

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.