Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.