|
|
Line 5: |
Line 5: |
| ATOMS = {"C", "H", "Cl", "N", "O", "P", "S"}; | | ATOMS = {"C", "H", "Cl", "N", "O", "P", "S"}; |
| MASS = {12, 1, 35, 14, 16, 31, 32}; | | MASS = {12, 1, 35, 14, 16, 31, 32}; |
− | MASS2={C=12,H=1,Cl=35,N=14,O=16,P=31,S=32};
| |
− |
| |
− | function map(f, list)
| |
− | local ret = {}
| |
− | for _, v in pairs(list) do
| |
− | table.insert(ret, f(v))
| |
− | end
| |
− | return ret
| |
− | end
| |
− |
| |
− | function pairlis(x,y)
| |
− | local ret = {}
| |
− | for i,v in pairs(x) do
| |
− | table.insert(ret, {v, y[i]})
| |
− | end
| |
− | return ret
| |
− | end
| |
| | | |
| function toFormula(t) | | function toFormula(t) |
Line 43: |
Line 26: |
| end | | end |
| return ret; | | return ret; |
− | end
| |
− |
| |
− | function atomicNum(atom, str)
| |
− | if (str == "") then return 0 end
| |
− | if (MASS2[str] ~= nil) then return 1 end
| |
− | if (str == atom) then return 1 end
| |
− | if (string.len(str) > 2 and MASS2[string.sub(str,1,2)] ~= nil)
| |
− | then return tonumber(string.sub(str,3));
| |
− | else return tonumber(string.sub(str,2));
| |
− | end
| |
| end | | end |
| | | |
Line 68: |
Line 41: |
| end | | end |
| return ret | | return ret |
− | --[[
| |
− | local c1,h1,cl1,n1,o1,p1,s1=string.match(str1,FORMULA_PAT);
| |
− | local c2,h2,cl2,n2,o2,p2,s2=string.match(str2,FORMULA_PAT);
| |
− | c1 = atomicNum("C", c1);
| |
− | h1 = atomicNum("H", h1);
| |
− | cl1= atomicNum("Cl",cl1);
| |
− | n1 = atomicNum("N", n1);
| |
− | o1 = atomicNum("O", o1);
| |
− | p1 = atomicNum("P", p1);
| |
− | s1 = atomicNum("S", s1);
| |
− | c2 = atomicNum("C", c2);
| |
− | h2 = atomicNum("H", h2);
| |
− | cl2= atomicNum("Cl",cl2);
| |
− | n2 = atomicNum("N", n2);
| |
− | o2 = atomicNum("O", o2);
| |
− | p2 = atomicNum("P", p2);
| |
− | s2 = atomicNum("S", s2);
| |
− | if (c2 > c1) then return nil; else c1 = c1 - c2; end
| |
− | if (h2 > h1) then return nil; else h1 = h1 - h2; end
| |
− | if (cl2>cl1) then return nil; else cl1 = cl1 - cl2; end
| |
− | if (n2 > n1) then return nil; else n1 = n1 - n2; end
| |
− | if (o2 > o1) then return nil; else o1 = o1 - o2; end
| |
− | if (p2 > p1) then return nil; else p1 = p1 - p2; end
| |
− | if (s2 > s1) then return nil; else s1 = s1 - s2; end
| |
− | local ret = "";
| |
− | if (c1 > 1) then ret = ret .. "C" .. c1;
| |
− | else if (c1 == 1) then ret = ret .. "C"; end end
| |
− | if (h1 > 1) then ret = ret .. "H" .. h1;
| |
− | else if (h1 == 1) then ret = ret .. "H"; end end
| |
− | if (cl1 > 1) then ret = ret .. "Cl" .. cl1;
| |
− | else if (cl1 == 1) then ret = ret .. "Cl"; end end
| |
− | if (n1 > 1) then ret = ret .. "N" .. n1;
| |
− | else if (n1 == 1) then ret = ret .. "N"; end end
| |
− | if (o1 > 1) then ret = ret .. "O" .. o1;
| |
− | else if (o1 == 1) then ret = ret .. "O"; end end
| |
− | if (p1 > 1) then ret = ret .. "P" .. p1;
| |
− | else if (p1 == 1) then ret = ret .. "P"; end end
| |
− | if (s1 > 1) then ret = ret .. "S" .. s1;
| |
− | else if (s1 == 1) then ret = ret .. "S"; end end
| |
− | return ret;
| |
− | ]]--
| |
| end | | end |
| | | |