Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> preg_match_all(), i rozbicie rownania
Bastion
post 22.02.2005, 11:55:29
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
hwao
post 22.02.2005, 13:22:42
Post #2


Developer


Grupa: Moderatorzy
Postów: 2 844
Pomógł: 20
Dołączył: 25.11.2003
Skąd: Olkusz




Tak kombinuj
Kod
([a-z]{1,3})\(([0-9]+)\)

Swoja droga tangens to tg a nie tan :-)
Go to the top of the page
+Quote Post
Bastion
post 22.02.2005, 14:51:18
Post #3





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

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


fatamorgana ? http://pl2.php.net/manual/pl/function.tan.php

i cos nie funga twoja regula : Warning: preg_match_all() [function.preg-match-all]: Unknown modifier '\'

smile.gif no i chyba 1,3 to nie bardzo , bo na przyklad mamy log() i log10()

Ten post edytował Bastion 22.02.2005, 14:53:34


--------------------
Go to the top of the page
+Quote Post
hwao
post 22.02.2005, 15:06:51
Post #4


Developer


Grupa: Moderatorzy
Postów: 2 844
Pomógł: 20
Dołączył: 25.11.2003
Skąd: Olkusz




  1. <?php
  2.  
  3. $rownanie = &#092;"sin(40) + cos(30) * tan(20)\";
  4.  
  5. preg_match_all( '#([a-z]{1,3})(([0-9]+))#si', $rownanie, $matches );
  6.  
  7. print_r( $matches );
  8.  
  9. for ($i=0; $i< count($matches[0]); $i++) {
  10.  print_r( $matches[0][$i], true );
  11.  
  12. }
  13. ?>


Kod
Array
(
    [0] => Array
        (
            [0] => sin(40)
            [1] => cos(30)
            [2] => tan(20)
        )

    [1] => Array
        (
            [0] => sin
            [1] => cos
            [2] => tan
        )

    [2] => Array
        (
            [0] => 40
            [1] => 30
            [2] => 20
        )

)


Ruszyc glowa :-)

A co do tego tan mysalem ze chcesz to tak jak w szkole sie zapisuje ;-)
Btw:
Moze uda Ci sie to szybciej rozwiazac
eval" title="Zobacz w manualu PHP" target="_manual()
Go to the top of the page
+Quote Post
Bastion
post 22.02.2005, 15:12:07
Post #5





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

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


bóg zaplac dobry czlowieku 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 smile.gif

jeszcze raz thx

CHANGED :

hawao jeszcze tylko dodam obsluge bledow 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

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

 



RSS Wersja Lo-Fi Aktualny czas: 15.07.2025 - 18:15