PDA

View Full Version : کمک فرمها در php



nazanin@
جمعه 16 اردیبهشت 1390, 21:58 عصر
من دارم روی یه برنامه phpکار میکنم که باید ریشه های معادله درجه 2رو بدست بیاره وباید ضرایب این معادله رو از طریق جعبه متن از کاربر بگیره ،
موقعی که action فرم رو صفحه ای که قرار پردازش بشه ،وتابع بدست آوردن ریشه ها رو در اون صفحه قرار میدم، برنامهerrorمیگره ....نمی دونم اون تابع رو کجا قرار بدم...:متفکر::متفکر:
کسی میتونه در این زمینه من رو راهنمای کنه.:ناراحت::ناراحت::افسرده:: افسرده:

MSN_Issue
جمعه 16 اردیبهشت 1390, 22:42 عصر
اگه کدتونو به همراه ارورش بذارید راحتتر میشه راهنمایی کرد !

Mahdi.Spirit
جمعه 16 اردیبهشت 1390, 23:20 عصر
شما كه كدي ندادين ولي يه كلاس براي اينكار پيدا كردم ميتونين تست كنين:



<?php
/**
* Quadratic Equation Solver
*
* @package com.cisneiros.math
* @version 1.0
* @author Cisneiros <hide@address.com>
* @copyright 2009 - 2010 (C) by Cisneiros
* @license http://creativecommons.org/licenses/by/2.5/br/ Creative Commons Attribution 2.5 Brazil License
*/

/**
* Quadratic equation to be solved by Bhaskara's method, like "ax^2 + bx + c = 0".
*
* @package com.cisneiros.math
* @version 1.0
* @property float $a The "a" value of the equation. Cannot be zero.
* @property float $b The "b" value of the equation.
* @property float $c The "c" value of the equation.
* @property int $precision The number of decimal places of the roots.
* @property float $delta The discriminant of the equation, calculated based on a, b and c.
* @property array $roots The array containing the equation's roots.
*/
class QuadraticEquation
{
private $a;
private $b;
private $c;
private $precision;
private $delta;
private $roots;

/**
* Constructs the class, optionally setting $a, $b, $c and $precision.
*
* @version 1.0
* @since 1.0
* @param float $a The "a" value of the equation.
* @param float $b The "b" value of the equation.
* @param float $c The "c" value of the equation.
* @param int $precision The number of decimal places of the roots.
*/
public function __construct ($a = NULL, $b = NULL, $c = NULL, $precision = NULL)
{
if ($a)
{
$this->setA($a);
$this->setB($b);
$this->setC($c);
}
if ($precision)
{
$this->setPrecision($precision);
}
}

/**
* Return the root(s) as a string. CANNOT BE USED if the equation is unsolvable!
*
* @version 1.0
* @since 1.0
* @return string Root(s)
* @throws Exception On unsolvable equation (delta < 0).
*/
public function __toString()
{
$roots = $this->getRoots();
$string = "{$roots[0]}";

if (isset($roots[1]))
{
$string .= "; {$roots[1]}";
}

return $string;
}

/**
* Returns the value of "a".
*
* @version 1.0
* @since 1.0
* @return float Value of "a".
*/
public function getA()
{
return $this->a;
}

/**
* Returns the value of "b".
*
* @version 1.0
* @since 1.0
* @return float Value of "b".
*/
public function getB()
{
return $this->b;
}

/**
* Returns the value of "c".
*
* @version 1.0
* @since 1.0
* @return float Value of "c".
*/
public function getC()
{
return $this->c;
}

/**
* Returns the precision.
*
* @version 1.0
* @since 1.0
* @return int Precision.
*/
public function getPrecision()
{
if (!isset($this->precision))
{
$this->setPrecision(2);
}

return $this->precision;
}

/**
* Returns the discriminant (delta).
*
* @version 1.0
* @since 1.0
* @return float Discriminant (delta).
*/
public function getDelta()
{
if (!isset($this->delta))
{
$this->delta = pow($this->getB(), 2) - (4 * $this->getA() * $this->getC());
}

return $this->delta;
}

/**
* Calculates the roots and returns them as an array.
*
* @version 1.0
* @since 1.0
* @return array Roots.
* @throws Exception On unsolvable equation (delta < 0).
*/
public function getRoots()
{
if (!isset($this->roots))
{
if ($this->getDelta() < 0)
{
throw new Exception ('This equation has no real roots.');
}
elseif ($this->getDelta() == 0)
{
$x = (0 - $this->getB()) / 2;
$this->roots = array(round($x, $this->getPrecision()));
}
else
{
$x1 = (0 - $this->getB() + sqrt($this->getDelta())) / (2 * $this->getA());
$x2 = (0 - $this->getB() - sqrt($this->getDelta())) / (2 * $this->getA());

$this->roots = array(round($x1, $this->getPrecision()), round($x2, $this->getPrecision()));
}
}

return $this->roots;
}

/**
* Sets the value of "a". Also unsets the delta and the roots for recalculation.
*
* @version 1.0
* @since 1.0
* @throws Exception On setting a "zero" value.
*/
public function setA($a)
{
if ($a == 0)
{
throw new Exception('The value of "a" cannot be zero.');
}
else
{
$this->a = (float) $a;
unset($this->delta);
unset($this->roots);
}
}

/**
* Sets the value of "b". Also unsets the delta and the roots for recalculation.
*
* @version 1.0
* @since 1.0
*/
public function setB($b)
{
$this->b = (float) $b;
unset($this->delta);
unset($this->roots);
}

/**
* Sets the value of "c". Also unsets the delta and the roots for recalculation.
*
* @version 1.0
* @since 1.0
*/
public function setC($c)
{
$this->c = (float) $c;
unset($this->delta);
unset($this->roots);
}

/**
* Sets the precision. Also unsets the roots for rerouding.
*
* @version 1.0
* @since 1.0
* @throws Exception On precision smaller than zero.
*/
public function setPrecision($precision)
{
if ($precision < 0)
{
throw new Exception('The number of decimal places may not be smaller then zero.');
}
else
{
$this->precision = (int) $precision;
unset($this->roots);
}
}
}


اينطوري فراخواني كنين:


QuadraticEquation(2, -4, 1)


منبع:
http://www.phpkode.com/source/s/quadratic-equation-solver/quadratic-equation-solver/class.quadraticequation.php

nazanin@
شنبه 17 اردیبهشت 1390, 09:25 صبح
میشه برا من یه برنامه مثال بزنید که از جهبه متن ورودی بگیرد ویه تابع نیز در آن به کار رفته باشد .
ممنون

nazanin@
شنبه 17 اردیبهشت 1390, 09:33 صبح
ممنون از راهنمایتون ...

Mahdi.Spirit
شنبه 17 اردیبهشت 1390, 11:46 صبح
من اولین بارم که دارم با phpبرنامه می نویسم دارم برنامه بدست آوردن ریشه های معادله درجه دو رو مینویسم.
این کد رو نوشتم .:متفکر::ناراحت:


<html>
<!-- WEB.php COMP519 -->
<body>
<?php
function sloving_form($a, $b, $c)
{ $t=0;
$x=0;
$y=o;
$t=pow($b,2)-4*$a*$c;
if($t>0)
{
$u=sqrt($t);
$x=($-b+$u)/(2*$a);
$y=($-b-$u)/(2*$a)
echo 'second degree equation has tow roots '$x 'and'$y' are <br/>';
}
else if($t=0)
{
$u=Math.sqrt($t);
$x=$y= ($-b)/(2*$a);
echo 'second degree equation has one root' $i '<br/>';
}
else if($t<o)
{
echo'second degree equation has no root <br/>'
}
}
?>
.<form action=" " method="POST" >
</br> </br> Enter a number a: <input name="a" type="a" />
</br> </br> Enter a number b:<input name="b" type="b" />
</br> </br> Enter a number c:<input name="c" type="c"/>
<br> <br> <input name="submit" value="root" type="submit"> <input type="reset">
</form>
<?php
echo slovin($a,$b,$c);
?>
</body>
</html>

واین error میده
( ! ) Parse error: syntax error, unexpected '-', expecting T_VARIABLE or '$' in C:\Program Files (x86)\EasyPHP-5.3.5.0\www\WEB.php on line 14
اگه میشه من رو کمک کنید.


اونقدر اين كد خطا داشت كه قابل توضيح نيست ،‌ مقايسه كنيد با اين معلومه :


<html>
<!-- WEB.php COMP519 -->
<HEAD>
<?PHP
import_request_variables("p","f_");
?>
</HEAD>
<body>

<form action="" method="POST" >
</br> </br> Enter a number a: <input name="a" value="" type="text" />
</br> </br> Enter a number b:<input name="b" value="" type="text" />
</br> </br> Enter a number c:<input name="c" value="" type="text"/>
<br> <br> <input name="submit" value="root" type="submit"> <input type="reset">
</form>

<?php
function sloving_form($a, $b, $c)
{
$t=0;
$x=0;
$y=0;
$t=pow($b,2)-4*$a*$c;

if($t>0)
{
$u=sqrt($t);
$x=(-$b+$u)/(2*$a);
$y=(-$b-$u)/(2*$a);
echo ("second degree equation has tow roots $x and $y are <br/>");
}
else if($t==0)
{

$u=sqrt($t);
$x= (-$b)/(2*$a);
echo ("second degree equation has one root $x <br/>");
}
else if($t<0)
{

echo("second degree equation has no root <br/>");
}
}

if (isset($f_submit) && $f_submit=="root"){
sloving_form($f_a,$f_b,$f_c);
}
?>


</body>
</html>