PHP implements simple calculator program code

Source: Internet
Author: User

Php calculator

The code is as follows: Copy code

<Html>
<Head>
<Title> PHP implements a simple calculator </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<? Php
$ Num1 = true;
$ Num2 = true;
$ Numa = true;
$ Numb = true;
$ Message = "";
// Single Branch
If (isset ($ _ GET ["sub"]) {
If ($ _ GET ["num1"] = ""){
$ Num1 = false;
$ Message. = "The first number cannot be blank ";

        }
If (! Is_numeric ($ _ GET ["num1"]) {
$ Numa = false;
$ Message. = "The first number is not a number ";
        }

If ($ _ GET ["num2"] = ""){
$ Num2 = false;
$ Message. = "The second number cannot be blank ";

        }
If (! Is_numeric ($ _ GET ["num2"]) {
$ Numb = false;
$ Message. = "The second number is not a number ";
        }

If ($ num1 & $ num2 & $ numa & $ numb ){
$ Sum = 0;
// Multiple branch switches
Switch ($ _ GET ["ysf"]) {
Case "+ ":
$ Sum = $ _ GET ["num1"] + $ _ GET ["num2"];
Break;
Case "-":
$ Sum = $ _ GET ["num1"]-$ _ GET ["num2"];
Break;
Case "x ":
$ Sum = $ _ GET ["num1"] * $ _ GET ["num2"];
Break;
Case "/":
$ Sum = $ _ GET ["num1"]/$ _ GET ["num2"];
Break;
Case "% ":
$ Sum = $ _ GET ["num1"] % $ _ GET ["num2"];
Break;
            }
        }
    }
?>
<Br/> <B> Notice </B>: Undefined index: num1 in <B>
D: \ php \ PHPnow1.5.5 \ Package \ vhosts \ 127.0.0.1 \ ceshi \ jsq. php </B>
On line <B> 64 </B> <br/>
<Body>
<Table align = "center" border = "1" width = "500">
<Caption> <Form action = "jsq. php">
<Tr>
<Td>
<Input type = "text" size = "5" name = "num1" value = "<? Php echo $ _ GET ["num1"]?> ">
</Td>
<Td>
<Select name = "ysf">
<Option value = "+" <? Php if ($ _ GET ["ysf"] = "+") echo "selected"?> + </Option>
<Option value = "-" <? Php if ($ _ GET ["ysf"] = "-") echo "selected"?> -</Option>
<Option value = "x" <? Php echo $ _ GET ["ysf"] = "x "? "Selected": ""?> X </option>
<Option value = "/" <? Php echo $ _ GET ["ysf"] = "/"? "Selected": ""?> /</Option>
<Option value = "%" <? Php echo $ _ GET ["ysf"] = "% "? "Selected": ""?> % </Option>
</Select>
</Td>
<Td>
<Input type = "text" size = "5" name = "num2" value = "<? Php echo $ _ GET ["num2"]?> ">
</Td>
<Td>
<Input type = "submit" name = "sub" value = "computing">
</Td>
</Tr>

<? Php
If (isset ($ _ GET ["sub"]) {

Echo '<tr> <td colspan = "5"> ';
If ($ num1 & $ num1 & $ numa & $ numb ){
Echo "result :". $ _ GET ["num1"]. "". $ _ GET ["ysf"]. "". $ _ GET ["num2"]. "= ". $ sum;
} Else {
Echo $ message;
                    }
Echo '</td> </tr> ';
                }
?>
</Form>
</Table>

</Body>
</Html>

Js calculator

The code is as follows: Copy code

<Html>
<Head>
<Title> calculator </title>
<Script>
<! --
Willclear = false // This variable is used as the status marker of whether to clear the input box for the next input.
Function backspace () {// return
Calc. Input. value = Calc. Input. value. substring (0, Calc. Input. value. length-1)
Calc. Input. title = Calc. Input. value. substring (0, Calc. Input. title. length-1)
} // Take the string whose length is equal to or less than one of the original length to return the lattice.
Function addoperator () {// add an operator
If (willclear) {// if you need to clear the input box before this input
Willclear = false // clear the flag first to avoid being mistakenly cleared again next time
Clearinput () // clear the input content
}
Calc. Input. value + = event. srcElement. innerText // add the newly entered operator to the display.
Calc. Input. title + = event. srcElement. name // add the newly entered operator to the expression area for calculation.
}

Function clearinput () {// clear input
Calc. Input. value = ''// clear the display area
Calc. Input. title = ''// clear expression
}

Function result () {// used for calculation results
Calc. Input. value = eval (Calc. Input. title) // executes the expression in the expression area and returns the result to the display area.
Willclear = true // set the next button to clear the calculation result first.
}
// -->
</Script>

<Style>
Button {width: 40; border: 1 solid #808080; background-color: # FFFFFF}
</Style>
</Head>
<Body>
<Form name = "Calc">
<Table border = 4 bordercolorlight = "#808080" bordercolor = "#808080" width = "250" height = "38"> <TR> <TD width = "291" height = "39" bgcolor = "# C0C0C0">
& Nbsp;
<Input type = "text" NAME = "Input" readonly Size = "21" style = "background-color: # F5F5F5; font-family: Arial; border: 1 solid #000000 "> & nbsp
<Button NAME = "back" OnClick = "backspace ()"> & larr; </button>
</TD> </TR> <TD width = "291" height = "38">
<Table border = "0" width = "100%" height = "105" cellpadding = "0">
<Tr>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "1" OnClick = "addoperator ()"> 1 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "2" OnClick = "addoperator ()"> 2 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "3" OnClick = "addoperator ()"> 3 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "+" OnClick = "addoperator ()"> + </BUTTON>
</Td>
</Tr>
<Tr>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "4" OnClick = "addoperator ()"> 4 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "5" OnClick = "addoperator ()"> 5 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "6" OnClick = "addoperator ()"> 6 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "-" OnClick = "addoperator ()">-</BUTTON>
</Td>
</Tr>
<Tr>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "7" OnClick = "addoperator ()"> 7 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "8" OnClick = "addoperator ()"> 8 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "9" OnClick = "addoperator ()"> 9 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "*" OnClick = "addoperator ()"> × </BUTTON>
</Td>
</Tr>
<Tr>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "clear" OnClick = "clearinput ()"> C </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "0" OnClick = "addoperator ()"> 0 </BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "=" OnClick = "result ()" >=</BUTTON>
</Td>
<Td width = "25%" height = "31" align = "center" bgcolor = "# C0C0C0">
<Button name = "/" OnClick = "addoperator ()"> Events </BUTTON>
</Td>
</Tr>
</Table>
</TD> </TR>
</TABLE>
</FORM>
</Body>
</Html>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.