Hi-
I'm trying to write a PHP program that is like a supercalculator. What
it does is take the input strings and then convert the strings into an
array, one digit per space in the array. For example if '549' was
passed it would be put in an array called digit and
digit[0] would be equal to 9,
digit[1] would be equal to 4,
digit[2] would be equal to 5 and so on. (Notice the ones digit is 0,
tens digit is 1 and so on)
Now I am simply having a problem printing out my digit array for a
defined object. I know the strings are being placed in the array
correctly, because I print it out immediately afterewards (right in
the constructor!) But then
the next call to that object using the 'justprint' function doesn't
show
anything. And it should.
Please help me correct this so I can properly print each object's
digit arrays and then I'll be able to do the addition and other
features I want to add.
Also note that the only type that works now is Addition (by work I
mean will show something that doesn't work) ;)
Here is my code:
<?php
class bignum
{
var $digit; //this is going to be an array that holds all the
digits
//***** START bignum constructor******//
function bignum()
{
if (func_num_args() == 1) //we were passed a string, so convert it
to a bignum array
{
$string = func_get_arg(0); //get the argument using a special
function
$len = strlen($string);
for ($i=0; $i<$len; $i+=1) //use string to fill in array
{
$int = intval( $string[$len-$i-1] ); //convert to integer for
math later on
$digit[$i]=$int; //place digit in the array
}
}
echo 'start test print out<br>';
$thecount = count($digit);
for ($i=$thecount; $i>=0; $i-=1)
echo $digit[$i];
echo '<BR>end test print out<BR>';
}
//***** END bignum constructor******//
//***** START justprint******//
function justprint()
{
$thecount = count($digit); //count the total number of digits
for ($i=$thecount; $i>=0; $i-=1) //print them out (remember the
ones column is digit[0] so print backwards)
echo $digit[$i];
}
//***** END justprint ******//
//***** START print_it ******//
function print_it()
{
$thecount = count($digit); //count the total number of digits
for ($i=$thecount; $i>=0; $i-=1) //print them out (remember the
ones column is digit[0] so print backwards)
echo $digit[$i];
echo '<form action="calculator.php" method=post><table><tbody>';
echo '<tr><td> A= <td><input name="a" type=text size=100 value="';
for ($i=$thecount; $i>=0; $i-=1) //print out the answer again as a
value in the top field
echo $digit[$i];
echo '">'; //close value
echo "<TR><TD><TD><INPUT type=radio value=+ name=option> Plus ";
echo "<TR><TD><TD><INPUT type=radio value=- name=option> Minus ";
echo "<TR><TD><TD><INPUT type=radio value=* name=option> Times ";
echo "<TR><TD><TD><INPUT type=radio value='d' name=option> Divided
by ";
echo "<TR><TD><TD><INPUT type=radio value='p' name=option> To the
Power of ";
echo "<TR><TD><TD><INPUT type=radio value='f' name=option>
Factorial ";
echo '<TR><TD>B= <TD><INPUT size=100 name="b"> <TR><TD>';
echo '<TD><INPUT type=submit value="CALCULATE">
</TR></TBODY></TABLE></FORM>';
}
//***** END print_it ******//
//***** START add******//
function add($b) //b is a bignum object
{
$carry = 0;
$thecount = count($digit); //count the total number of digits
for ($i=0; $i<$thecount; $i+=1)
{
$sum = $digit[$i] + $b->digit[$i] + $carry;
$carry = $sum/10;
$sum = $sum - $carry*10;
$digit[$i] = $sum;
}
}
//***** END add ******//
} //***** END bignum class ******//
//***** START MAIN PROGRAM
************************************************************
//check to see if variables have been passed
if( !($_REQUEST[a]) ) //is a doesn't exist set to 0
$a = 0;
if( !($_REQUEST[b]) ) //is b doesn't exist set to 0
$b = 0;
if($_REQUEST[option] == "+") //if they're adding
{
$aa = new bignum($a); //turn the passed strings into bignum objects
$bb = new bignum($b);
echo 'digit zero printout ' . $aa->digit[0];
$aa->justprint(); //this is where it isn't working
echo "<br>"; //Show what was done on webpage.
echo "+ <br>";
$bb->justprint();
echo "<br>=";
echo "<br><br>";
$aa->add($bb);
$aa->print_it();
}
else //nothing to add/subtract/multi.... yet, so just list form
{
$aa = new bignum($a);
$aa->print_it();
}
//***** END MAIN PROGRAM
************************************************************
?>
Here is a link to it, if you would prefer that. (Not sure is the
syntax gets messed up by posting here on google.)
http://www.reidsremodeling.com/calc/calculator.txt
Thanks for any help! I know this is something stupid I'm doing wrong
so please someone enlighten me. |