php乘法函数,PHP: Math 函数 - Manual

For people interest in Differential Equations, I've done a function that receive a string like: x^2+x^3 and put it in

2x+3x^2 witch is the differantial of the previous equation.

In the code there is one thing missing: the $string{$i} is often going outOfBound (Uninitialized string offset: 6 in...)

if your error setting is set a little too high... I just dont know how to fix this.

So there is the code for differential equation with (+ and -) only:

function differentiel($equa)

{

$equa = strtolower($equa);

echo "Equation de depart: ".$equa."
";

$final = "";

for($i = 0; $i < strlen($equa); $i++)

{

//Make a new string from the receive $equa

if($equa{$i} == "x" && $equa{$i+1} == "^")

{

$final .= $equa{$i+2};

$final .= "x^";

$final .= $equa{$i+2}-1;

}

elseif($equa{$i} == "+" || $equa{$i} == "-")

{

$final .= $equa{$i};

}

elseif(is_numeric($equa{$i}) && $i == 0)

{

//gerer parenthese et autre terme generaux + gerer ^apres: 2^2

$final .= $equa{$i}."*";

}

elseif(is_numeric($equa{$i}) && $i > 0 && $equa{$i-1} != "^")

{

//gerer ^apres: 2^2

$final .= $equa{$i}."*";

}

elseif($equa{$i} == "^")

{

continue;

}

elseif(is_numeric($equa{$i}) && $equa{$i-1} == "^")

{

continue;

}

else

{

if($equa{$i} == "x")

{

$final .= 1;

}

else

{

$final .= $equa{$i};

}

}

}

//

//Manage multiplication add in the previous string $final

//

$finalMul = "";

for($i = 0; $i < strlen($final); $i++)

{

if(is_numeric($final{$i}) && $final{$i+1} == "*" && is_numeric($final{$i+2}))

{

$finalMul .= $final{$i}*$final{$i+2};

}

elseif($final{$i} == "*")

{

continue;

}

elseif(is_numeric($final{$i}) && $final{$i+1} != "*" && $final{$i-1} == "*")

{

continue;

}

else

{

$finalMul .= $final{$i};

}

}

echo "equa final: ".$finalMul;

}

?>

I know this is not optimal but i've done this quick :)

If you guys have any comment just email me.

I also want to do this fonction In C to add to phpCore maybe soon...

Patoff

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值