jesobreira

Calcular peso atômico

May 14th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ERROR);
  3. /**
  4.     @@description Calcular peso atomico
  5.     @@author Jefrey S. Santos
  6. */
  7. /*
  8. vetor: 3 dimensoes, index pascal-like
  9. ELEMENTO[Z] (
  10.     [1] (
  11.         [mass]=X
  12.         [abund]=Y
  13.     )
  14.     [2] (
  15.         [mass]=X
  16.         [abund]=Y
  17.     )
  18. )
  19.  
  20. flat database:
  21. Z|1|x|Y
  22. Z|A|X|Y
  23. */
  24.  
  25. $z_this = 0;
  26. function calc($vetor) {
  27.     global $z_this;
  28.     $j = sizeof($vetor);
  29.     $sup = 0;
  30.     #for($i=1; $i<=$j; $i++) {
  31.     foreach($vetor as $array) {
  32.         #print_r($vetor);
  33.         #$array = $vetor[$i];
  34.         #print_r($vetor);
  35.         $cache = $array['mass']*$array['abund']; // vai calculando a parte superior da fração
  36.         $sup += $cache;
  37.     }
  38.     $result = $sup/100;
  39.     $z_this++;
  40.     echo "\n\nElemento Z = $z_this\n$sup/100 = $result";
  41.     return true;
  42. }
  43.  
  44. function _main($table) {
  45.     $table = str_replace(",", ".", $table);
  46.     $mainarr = array();
  47.     $elements = explode("\n", $table); // linha a linha
  48.     foreach($elements as $elemento) {
  49.         $elem = explode("|", $elemento); // explodir pra vetor
  50.         $z = $elem[0];
  51.         $a = $elem[1];
  52.         $x = $elem[2];
  53.         $y = $elem[3];
  54.         #$mainarr[$z] = array(); NAO MUDAR! SE MUDAR EXPLODE! FUNCIONANDO!
  55.         $mainarr[$z][$a] = array(
  56.                 'mass' => $x,
  57.                 'abund' => $y // PoG - Programacao Orientada a Gambiarra
  58.         );
  59.         #print_r($mainarr[$z]);
  60.     }
  61.     #print_r($mainarr);
  62.     return $mainarr; // nao sei como, mas esta funcionando
  63. }
  64.  
  65. function gasolina($todo_o_vetor) { // joga cada array dentro da func calc
  66.     $j = sizeof($todo_o_vetor); $i = 1;
  67.     #print_r($todo_o_vetor);
  68.     #while($i<=$j) {
  69.     foreach($todo_o_vetor as $esse_vetor_aqui) {
  70.         calc($esse_vetor_aqui);
  71.         $i++;
  72.     }
  73.     return true;
  74. }
  75.  
  76. $database = file_get_contents("database.txt");
  77. $myarr = _main($database);
  78. ob_start();
  79. gasolina($myarr);
  80.  
  81. $buffer = ob_get_clean();
  82.  
  83. echo '<pre>'.str_replace(".", ",", $buffer).'</pre>';
  84. exit;
  85. ?>
Add Comment
Please, Sign In to add comment