View Full Version : Calculator script.
Independent
09-07-2008, 08:54 PM
This script was purely made out of boredom, but it was made by Independent of Habboxforum,
I hope this script comes useful in some way.
Newbies guide to installation..
You can just extract the files and it will work straight away.
http://www.calonuk.net/SCRIPTS/calculator.zip - Download it.
http://www.calonuk.net/calculator/ - Sort of demoish.
I know it's badly coded, but I was bored and I think someone could make good use of it.
Invent
09-07-2008, 08:57 PM
Why have you used several files for a simple task?
Independent
09-07-2008, 08:59 PM
Why have you used several files for a simple task?
Because it's craply coded.
Dentafrice
09-07-2008, 08:59 PM
<?php
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
$type = $_POST["type"];
if ( $type == "+" ) {
echo $number1 + $number2;
}
else {
if ( $type == "-" ) {
echo $number1 - $number2;
}
else {
if ( $type == "*" ) {
echo $number1 * $number2;
}
else {
if ( $type == "/" ) {
echo $number1 / $number2;
}
else {
exit( "idiot" );
}
}
}
}
?>
could do it with a lot less coding though.
Independent
09-07-2008, 09:01 PM
<?php
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
$type = $_POST["type"];
if ( $type == "+" ) {
echo $number1 + $number2;
}
else {
if ( $type == "-" ) {
echo $number1 - $number2;
}
else {
if ( $type == "*" ) {
echo $number1 * $number2;
}
else {
if ( $type == "/" ) {
echo $number1 / $number2;
}
else {
exit( "idiot" );
}
}
}
}
?>
could do it with a lot less coding though.
Lmao, everyone corrects my code.
Thanks though, I may learn something from that code ^_^
Dentafrice
09-07-2008, 09:02 PM
You could do it with a switch and case also, as well as many other ways, I'm half buggered at the moment anyway, 36 hours with no sleep, and enough caffeine in me to last years.
Independent
09-07-2008, 09:03 PM
You could do it with a switch and case also, as well as many other ways, I'm half buggered at the moment anyway, 36 hours with no sleep, and enough caffeine in me to last years.
Ouch. :D
Dentafrice
09-07-2008, 09:04 PM
Yeah, had a couple of friends over, beat GTA IV again. Swam a little in my pool, my eyes are about as big as golf balls.
Invent
09-07-2008, 09:04 PM
<?php
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
$type = $_POST["type"];
switch( $type )
{
case "+":
echo ( $number1 + $number2 );
break;
case "-":
echo ( $number1 - $number2 );
break;
case "*":
echo ( $number1 * $number2 );
break;
case "/":
echo ( $number1 / $number2 );
break;
default:
echo 'Please go back and choose a valid option from the drop-down menu';
break;
}
?>
Switches ftw (I just read that Caleb suggested using them, I find them much easier to use for a script like this).
Independent
09-07-2008, 09:05 PM
<?php
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
$type = $_POST["type"];
switch( $type )
{
case "+":
echo ( $number1 + $number2 );
break;
case "-":
echo ( $number1 - $number2 );
break;
case "*":
echo ( $number1 * $number2 );
break;
case "/":
echo ( $number1 / $number2 );
break;
default:
echo 'Please go back and choose a valid option from the drop-down menu';
break;
}
?>
Switches ftw.
Thanks again!
I'm off to bed now, thanks for your code corrections Caleb/Simon.
Dentafrice
09-07-2008, 09:12 PM
Switches ftw (I just read that Caleb suggested using them, I find them much easier to use for a script like this).
I think they are a better alternative when parsing types, EX in this script would be the method that you are using to perform the calculation.
Or verifying that an option in a submission form is actually an option provided, and not someone submitting something that is either malicious, or just not acceptable.
:)
Protege
09-07-2008, 09:20 PM
Could of at least put the buttons on the page and made it abit interesting, but celebs right with the switch & case method, its how I'd do it and many others.
Jackboy
10-07-2008, 02:49 PM
I think you should make it into an actual calculator shape :P wud be a good idea
Excellent
10-07-2008, 03:02 PM
Just echo what they put in.. say the fields were call blah 1 and blah 2.
elseif($submit){
$sum=$blah+$blah1;
echo "$blah+$blah1=$sum";
Dentafrice
10-07-2008, 03:10 PM
Even easier:
<?php
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
$type = $_POST["type"];
switch( $type )
{
case "+": $calculation = $number1 + $number2; break;
case "-": $calculation = $number1 - $number2; break;
case "/": $calculation = $number1 / $number2; break;
case "*": $calculation = $number1 * $number2; break;
default: "You ain't doin math boy.";
}
echo $number1 . " " . $type . " " . $number2 . " = " . $calculation;
?>
Independent
10-07-2008, 03:15 PM
Even easier:
<?php
$number1 = $_POST["number1"];
$number2 = $_POST["number2"];
$type = $_POST["type"];
switch( $type )
{
case "+": $calculation = $number1 + $number2; break;
case "-": $calculation = $number1 - $number2; break;
case "/": $calculation = $number1 / $number2; break;
case "*": $calculation = $number1 * $number2; break;
default: "You ain't doin math boy.";
}
echo $number1 . " " . $type . " " . $number2 . " = " . $calculation;
?>
Haha, love it.
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.