2013-11-24 21 views
3

我創建了一個網站,其中有兩個輸入一個用於反應物,另一個用於產品。提交兩個字符串後,我想輸出反應物和產物之間的平衡化學方程式。添加和組合基於化學元素的陣列

例如,如果某人輸入設置在反應物 H2 + O2H2O的產品。我想要的結果是2H2 + O2 -> 2H2O

我這樣做是爲了提高我使用數組的技巧。但現在我停下來,不知道該怎麼做才能達到我想要的。

我已經得到了它的返回這個數組(產品):

[0] => 2 
[1] => 2 
// [0] is the H, [1] is the O. 

但是,當我嘗試輸入類似CH6 + CH12它會返回這樣說:

[0] => 1 
[1] => 6 
[2] => 12 
// [0] is the C, [1] is the H6, [2] is the H12 

我真正想要的是這樣的:

[0] => 1 
[1] => 18 
// [0] is the C, [1] is the H 

我當前的代碼是這樣的:

//returns H2 + H2O 
$reactions = $_GET['reactions']; 
//returns H2 H2O 
$reactionsnospace = str_replace('+', '', $reactions); 
//returns array(H2,H2O) 
$reactionsarray = explode("+",$reactions); 
//returns array(H2,H2,O) 
$reactionsplitarray = splitAtUpperCase($reactionsnospace); 

//returns the repeated number element H2 x 2 and O x 1 
$reactioncountrepeatedelements = array_count_values($reactionsplitarray); 
//returns the elements name with no repetition 2 and 1 (2 and ) 
$reactionkeys = array_keys($reactioncountrepeatedelements); 
foreach($reactionkeys as &$item) { 
    $item = preg_replace('/\D/', '', $item); 
    if(empty($item)) 
    {$item = "1";} 
} 
//returns the elements repetition number only 
$reactionvalues = array_values($reactioncountrepeatedelements); 


$d = array_map(function ($a1, $a2) { return $a1 * $a2; }, $reactionkeys , $reactionvalues); 
print_r($d); 

我需要改進我的代碼的建議,解決我目前的問題,以及我可以使用的方法來達到平衡化學方程式的最終目標。使用數組是一個好主意嗎?有沒有更好的辦法?

閱讀這個,如果你不喜歡讀書上述所有的:

我有此數組:

Array ([0] => C [1] => H6 [2] => C [3] => H12 [4] => Cl2) 

什麼,我想實現的是:

Array ([0] => C2 [1] => H18 [2] => Cl2) 
+0

你可以使用一個兩個維數組來表示價電子的元件的#,從而處理不同的氧化態。就CH6 + CH12的例子而言,我不太確定你們如何平衡這些因素,因爲它們是無效分子,除非你的意思是苯(CH)6 +(某些雙環分子)(CH)12。即使如此,在上面顯示的1,18結果中,你仍然只有1碳。 – WebChemist

+0

@WebChemist我只是舉一個例子,我不是在說這裏有效和無效的化學。請閱讀最後三行..因爲這是我想要實現的。 – shnisaka

回答

1

如果你只想總結原子的數量,那麼你可以使用這個函數。上半部分創建一個2維數組來存儲每個原子類型和計數,然後下半部分將它們相加並將這些字母重新加入數字。

function addAtoms($arr){ 
    $newArr = array(); 
    foreach($arr as $v){ 
     //if the entire value is alpha, assign a subscript of 1 
     if(ctype_alpha($v)){ 
      $newArr[$v][] = 1; 
     } 
     //if the first 2 letters are alpha, it is an atom with 2 letters 
     elseif(ctype_alpha(substr($v, 0, 2))){ 
      $newArr[substr($v, 0, 2)][] = substr($v, 2); 
     } 
     //atoms with only 1 letter like H, B, N, O, F, P, S etc 
     else{ 
      $newArr[substr($v, 0, 1)][] = substr($v, 1); 
     } 
    } 

    //Sum the values of the 2nd level array and concatenate to the key 
    $new2 = array(); 
    foreach($newArr as $k => $v){ 
     $new2[] = $k.array_sum($v);  
    } 
    return $new2; 
} 

輸入:

$test = array("C", "H6" ,"C" ,"H12" ,"Cl2"); 
print_r(addAtoms($test)); 

會給你的期望陣列:

Array ([0] => C2 [1] => H18 [2] => Cl2) 

編輯

,如果你有可能多於1或2個字符,則可以取代:

//if the first 2 letters are alpha, it is an atom with 2 letters 
elseif(ctype_alpha(substr($v, 0, 2))){ 
    $newArr[substr($v, 0, 2)][] = substr($v, 2); 
} 
//atoms with only 1 letter like H, B, N, O, F, P, S etc 
else{ 
    $newArr[substr($v, 0, 1)][] = substr($v, 1); 
} 

與較不具體的:

else{ 
    $newArr[str_replace(range(0,9),'',$v)][] 
     = str_ireplace(range('a','z'),'',$v); 
}