1. #1

    Testing an Online Calculator

    Im trying to make a calculator, so i'll just upload it here to test.


    PHP Code:
    $opera1 $_GET['operando1'];
    $opera2 $_GET['operando2'];
    $result NULL;
    $erro NULL;
    /* switch ($operador) {
            case "+":
                $result = $opera1 + $opera2;
                break;
            case "-":
                $result = $opera1 - $opera2;
                break;
            case "*":
                $result = $opera1 * $opera2;
                break;
            case "/":
                $result = $opera1 / $opera2;
                break;    
        } */
    if (isset($_GET['operador+'])){
        
    $result $opera1 $opera2;
    } elseif (isset(
    $_GET['operador-'])){
        
    $result $opera1 $opera2;
    } elseif (isset(
    $_GET['operador*'])){
        
    $result $opera1 $opera2;
    } elseif (isset(
    $_GET['operador/'])){
        
    $result $opera1 $opera2;
        if (
    $opera2 === "0"){
            
    $erro "Não se pode dividir algo por zero";
        }
    } elseif (isset(
    $_GET['limpa'])){
        
    $result NULL;
        
    $opera1 NULL;
        
    $opera2 NULL;

    HTML Code:
    <frameset cols="80%, *" frameborder="1">
    <frame>
    <table border="2">
    <form action="#" method="GET" >
    	
    <tr> <td> Somar <input type="submit" value="+" name="operador+"/></td>
    	<td> Subtrair	<input type="submit" value="-" name="operador-"  /> </td>
    	<td> Multiplicar	<input type="submit" value="*" name="operador*"  /> </td>
    	<td> Dividir	<input type="submit" value="/" name="operador/"  /> </td>
    	<td> Limpar	<input type="submit" value ="C" name="limpa"     /> </td> </tr>
    		
    	<tr> <td> 	<input type="text" size="10" maxlength="8" 	name="operando1" value="[PHP] echo $opera1 [/PHP]"		 /> </td> </tr>
    	<tr><td> 	<input type="text" size="10" maxlength="8" name="operando2"		value="[PHP] echo $opera2 [/PHP]"	 /> </td> </tr>
    	<tr><td> 	<input type="text" size="10" maxlength="8" name="resultado"	readonly value=" [PHP]echo $result[/PHP] " /> </td> </tr> 
    </form>
    </table>
    </frame>
    <frame>
    Barra Lateral 
    </frame>
    </frameset>

  2. #2
    Brewmaster Zangeiti's Avatar
    10+ Year Old Account
    Join Date
    Mar 2013
    Location
    The Grilled Cheese Factory
    Posts
    1,299
    Im no coder hear but it looks fine to me because you can pop up your computer calculator and inspect element and the coding looks the same also this should probably be in computer section.

  3. #3
    Is the goal here to restrict user input such that they can only enter a left value, right value, and binary operator?

    If not, you're going to need significantly more logic to handle different kinds of input. An easy solution would be to parse the input into postfix notation (Dijsktra's Shunting-Yard is a good choice) and then use a stack to evaluate postfix representation of the expression.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •