i am having problem with this code plz help.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

zionsoumik
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Mar 28, 2013 7:36 am

i am having problem with this code plz help.

Postby zionsoumik » Thu Mar 28, 2013 7:40 am

<html>
<body>
<?php
class Complex{
public $real;
public $imag;



public function _construct($r, $i) {


$this->real=$r;
$this->imag=$i;

}




public function getreal()
{
return $this->real;
}


public function getimag()
{
return $this->imag;
}

public function mag($c)
{
$mag=($c->getreal())*($c->getreal())+($c->getimag())*($c->getimag());

return sqrt($mag);

}

public function arg($c)
{

$arg=($c->getimag())/($c->getreal());
return atan($arg);
}

public function argandize($mag,$arg)
{
$this->real=(float) (($mag)*cos($arg));
$this->imag=(float) (($mag)*sin($arg));
}



public function printd()
{
if($this->imag>0)
{

echo $this->real;
echo "+";
echo $this->imag;
echo "i";}
else{
echo $this->real;
echo $this->imag;
echo "i";}
}
}

class SoumikCalculator{



public function add($c1, $c2)
{
$real=$c1->getreal()+$c2->getreal();
$imag=$c1->getimag()+$c2->getimag();
$mycomplexresult=new Complex($real, $imag);
return $mycomplexresult;
}

public function sub($c1, $c2)
{
$real=$c1->getreal()-$c2->getreal();
$imag=$c1->getimag()-$c2->getimag();
$mycomplexresult= new Complex($real, $imag);
return $mycomplexresult;
}

public function mul($c1, $c2)
{
$mag=(float) (($c1->mag($c1))*($c2->mag($c2)));
$arg=(float) (($c1->arg($c1))+($c2->arg($c2)));
$mycomplexresult=new Complex();
$mycomplexresult->argandize($mag,$arg);
return $mycomplexresult;
}

public function div($c1, $c2)
{
$mag=(float) (($c1->mag($c1))/($c2->mag($c2)));
$arg=(float) (($c1->arg($c1))-($c2->arg($c2)));
$mycomplexresult=new Complex();
$mycomplexresult->argandize($mag,$arg);
return $mycomplexresult;
}

}

/* @var $_POST type */
$real1=(float)$_POST["real1"] ;
/* @var $imag1 type */
$imag1=(float)$_POST["imag1"] ;
/* @var $real2 type */
$real2=(float)$_POST["real2"] ;
/* @var $imag2 type */
$imag2=(float)$_POST["imag2"] ;
$com1=new Complex($real1,$imag1);
$com2=new Complex($real2,$imag2);
$com3=new Complex();
$mycalculator=new SoumikCalculator();
$op=htmlspecialchars($_POST["op"]);
switch($op)
{
case '+':$com3=$mycalculator->add($com1, $com2);break;
case '-':$com3=$mycalculator->sub($com1, $com2);break;
case '*':$com3=$mycalculator->mul($com1, $com2);break;
case '/':$com3=$mycalculator->div($com1, $com2);break;
}
$com3->printd();

?>
</body>
</html>



//MY HTML FILE IS:
/*
<html>
<body>

<form action="complexf.php" method="post">
REAL1: <input type="text" name="real1">
IMAG1: <input type="text" name="imag1">
REAL2: <input type="text" name="real2">
IMAG2: <input type="text" name="imag2">
OPERATOR: <input type="text" name="op">
<input type="submit">
</form>

</body>
</html>*/

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: i am having problem with this code plz help.

Postby seandisanti » Thu Mar 28, 2013 10:29 am

what is the problem?

zionsoumik
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Mar 28, 2013 7:36 am

Re: i am having problem with this code plz help.

Postby zionsoumik » Mon Apr 01, 2013 6:43 pm

seandisanti wrote:what is the problem?


the problem is it is not printing the value of the new complex com3.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: i am having problem with this code plz help.

Postby seandisanti » Mon Apr 01, 2013 10:39 pm

is it giving you an error? if so, what is the error? if not, what's it doing? what output are you getting and what output are you expecting? It's great that you showed your code (although [ code] blocks would make it better), but you have to let us know what's going on with it too.

zionsoumik
New php-forum User
New php-forum User
Posts: 3
Joined: Thu Mar 28, 2013 7:36 am

Re: i am having problem with this code plz help.

Postby zionsoumik » Tue Apr 02, 2013 1:20 am

seandisanti wrote:is it giving you an error? if so, what is the error? if not, what's it doing? what output are you getting and what output are you expecting? It's great that you showed your code (although [ code] blocks would make it better), but you have to let us know what's going on with it too.


for com1(1,2) and com2(3,4) and op='+' it should show 4+6i but it is only showing i


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests