Google Answers Logo
View Question
 
Q: PHP Classes Help VERY SIMPLE PROBLEM ( No Answer,   0 Comments )
Question  
Subject: PHP Classes Help VERY SIMPLE PROBLEM
Category: Computers > Programming
Asked by: lighthousedweller-ga
List Price: $2.00
Posted: 31 Aug 2003 15:34 PDT
Expires: 03 Sep 2003 14:13 PDT
Question ID: 250869
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.

Clarification of Question by lighthousedweller-ga on 31 Aug 2003 22:44 PDT
If you have any questions, please let me know, thanks!
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy