Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> preg_match_all(), i rozbicie rownania
Bastion
post
Post #1





Grupa: Zarejestrowani
Postów: 505
Pomógł: 0
Dołączył: 8.01.2005

Ostrzeżenie: (0%)
-----


Przypuscmy ze mam $rownanie = "sin(40) + cos(30) * tan(20)"

jak powinna wyglada formula preg_math_all() aby uzyskac tablice :

[0][0] = sin
[0][1] = 40
[1][0] = cos
[1][1] = 30
[2][0] = tan
[2][1] = 20

?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Bastion
post
Post #2





Grupa: Zarejestrowani
Postów: 505
Pomógł: 0
Dołączył: 8.01.2005

Ostrzeżenie: (0%)
-----


bóg zaplac dobry czlowieku (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)
wlasnie chodzi o to ze chce dodac nowe funkcje jak np. dsin(x) == sin(deg2rad(x))
czyli zamienic nowe dsin(x) na sin(deg2rad(x)) i dopiero wykonac to w eval by otrzymac
wynik. dlaczego tak ? gdyz rownanie bedzie wprowadzane jako string (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

jeszcze raz thx

CHANGED :

hawao jeszcze tylko dodam obsluge bledow (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) i git

  1. <?php
  2. function kameleon_math($equation)
  3. {
  4. $allowed = Array('sin', 'cos', 'tan', 'pi', 'log', 'pow', 'sqrt', 'min', 'max', 'round', 'floor', 'abs');
  5. $special = Array('ctg', 'dsin', 'dcos', 'dtan', 'dtg', 'tg');
  6.  
  7. preg_match_all('#([a-zA-Z0-9]*)(([0-9]*))#', $equation, $matches );
  8.  
  9. foreach($matches[1] as $key => $current)
  10. {
  11. if (!in_array($current,$allowed))
  12. {
  13.  if (!in_array($current,$special))
  14.  {
  15.  return &#092;"Nieznane wyrazenie : $current\";
  16.  } else
  17.  {
  18.  $from = $matches[0][$key];
  19.  $value = $matches[2][$key];
  20.  switch($current)
  21.  {
  22.  case 'ctg' : $to = &#092;"1/tan($value)\"; break;
  23.  case 'dsin' : $to = &#092;"sin(deg2rad($value))\"; break;
  24.  case 'dcos' : $to = &#092;"cos(deg2rad($value))\"; break;
  25.  case 'dtan' : $to = &#092;"tan(deg2rad($value))\"; break;
  26.  case 'dctg' : $to = &#092;"1/tan(deg2rad($value))\"; break;
  27.  case 'tg'  : $to = &#092;"tan($vakue)\"; break;
  28.  case 'dtg' : $to = &#092;"tan(deg2rad($value))\"; break;
  29.  }
  30.  $equation = str_replace($from,$to,$equation);
  31.  }
  32. }
  33. }
  34. eval(&#092;"$math_result = \".$equation.\";\");
  35. return $math_result;
  36. }
  37. ?>


echo kameleon_math("dsin(30) + dsin(30)" => 1

Ten post edytował Bastion 22.02.2005, 16:13:25
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 06:17