Jump to content

Adding


zheka007

Recommended Posts

Sounds like homework to me.  If it is, people on forums generally prefer for people to be upfront about something being homework.

 

 

Anyway, the goal of this forum is to help people, not do things for people.  (Well, the freelancing section is all about doing things for people, but chances are, you don't want to pay someone for something this simple.)

 

 

You will want to read a tutorial on $_POST and basic PHP syntax.

 

 

Here are a couple of links:

 

 

http://www.tizag.com/phpT/

http://www.tizag.com/phpT/postget.php

http://www.w3schools.com/php/php_post.asp

Link to comment
https://forums.phpfreaks.com/topic/156358-adding/#findComment-823246
Share on other sites

zheka007 - I hope you realize that this is for *HELP* not requests.

 

Here's a basic outline (not all complete).

 

something.html

<!-- some HTML -->

<form method="post" action="something.php">
    Number 1: <input type="text" name="num1" /><br />
    Number 2: <input type="text" name="num2" />
</form>

<!-- some more HTML -->

 

something.php

<?php
// check PHP version if it has filter_input() function. If not, use superglobals.
$num1 = filter_input(INPUT_POST, 'num1', FILTER_VALIDATE_INT);
$num2 = filter_input(INPUT_POST, 'num2', FILTER_VALIDATE_INT);
$num1 = empty($num1)? filter_input(INPUT_POST, 'num1', FILTER_VALIDATE_FLOAT);
$num2 = empty($num2)? filter_input(INPUT_POST, 'num2', FILTER_VALIDATE_FLOAT);
if (empty($num1)) die("The first input value is not a number.");
if (empty($num2)) die("The second input value is not a number.");
echo $num1 + $num2;

 

Again, finish the rest yourself.

Link to comment
https://forums.phpfreaks.com/topic/156358-adding/#findComment-823248
Share on other sites

$left = $leftOrig = (int) $_POST['num1'];
$right = $rightOrig = (int) $_POST['num2'];

$c = $r = 0;

for ($t = PHP_INT_MAX; (bool) $t; $t >>= 1) 
{
$r <<= 1;
$r |= ($left ^ $right ^ $c) & 1;
$c = (($left | $right) & $c | $left & $right) & 1;
$left >>= 1;
$right >>= 1;
}

for ($t = PHP_INT_MAX, $c = ~$t; (bool) $t; $t >>= 1)
{
$c <<= 1;
$c |= $r & 1;
$r >>= 1;
}

echo "{$leftOrig} + {$rightOrig} = {$c}";

Link to comment
https://forums.phpfreaks.com/topic/156358-adding/#findComment-823385
Share on other sites

And just for luck, subtraction using Daniel's method:

 

$left = $leftOrig = (int) $_POST['num1'];
$right = $rightOrig = (int) $_POST['num2'];

$right = PHP_INT_MAX ^ $right -1;

$c = $r = 0;

for ($t = PHP_INT_MAX; (bool) $t; $t >>= 1)
{
$r <<= 1;
$r |= ($left ^ $right ^ $c) & 1;
$c = (($left | $right) & $c | $left & $right) & 1;
$left >>= 1;
$right >>= 1;
}

for ($t = PHP_INT_MAX, $c = ~$t; (bool) $t; $t >>= 1)
{
$c <<= 1;
$c |= $r & 1;
$r >>= 1;
}

echo "{$leftOrig} - {$rightOrig} = {$c}";

 

Some of us remember using two's complement

Link to comment
https://forums.phpfreaks.com/topic/156358-adding/#findComment-823601
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.