# Chemical Equation Balacer

12 replies to this topic

### #1 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 02 January 2007 - 01:09 PM

Copy this code to the CPLua Emulator, then send it to your CP:

`require("string","cas","table") function ucln(a,b) if(b%a==0) then return a else return ucln(b%a,a) end end function del1(a) if(a==1) then return "" else return a end end function duavedangcoban(s) i=1 s=string.gsub(s,"%+","%)%+%(") s="val_1*("..s s=string.gsub(s,"%+",function (s) i=i+1 return "+val_"..i.."*" end) s=string.gsub(s,"%=","%)%=%(") s=string.gsub(s,"%=",function (s) i=i+1 return "=val_"..i.."*" end) s=string.gsub(s,"(%u)([0-9])","%1*%2") s=string.gsub(s,"(%u+%l)([0-9])","%1*%2") s=string.gsub(s,"(%u)","+%1") s=string.gsub(s,"%(","+%1") s=string.gsub(s,"%(%+","%(") s=string.gsub(s,",%+",",") s=string.gsub(s,"(%))([0-9])","%1*%2") s=string.gsub(s,",%*%+","%*") s=tostring(cas.expand(s)) return s,i end function getval(sf) local val,s,i={},sf,1 for w in string.gmatch(s,"%u%l") do val[i]=w s=string.gsub(s,val[i],"") i=i+1 end for w in string.gmatch(s,"%u") do val[i]=w i=i+1 end i=1 table.sort(val) while(i<#val) do if(val[i]==val[i+1]) then table.remove(val,i) else i=i+1 end end return(val) end function canbang(ss) s,numval=duavedangcoban(ss) gv=getval(s) strgv="|"..table.concat(gv,"=0|").."=0" func="solve({" for i=1,#gv,1 do func=func..cas(s.."|"..tostring(gv[i]).."=1"..strgv) if i~=#gv then func=func.."," else func=func.."}," end end func=func.."{" for i=1,#gv,1 do func=func.."val_"..i if i~=#gv then func=func.."," else func=func.."})" end end if(#gv<numval) then for i=#gv+1,numval,1 do func=func.."|val_"..i.."=87297210" end end func=string.gsub(func,"=0","=87297210") kq=tostring(cas.getright(func)) dostring("skq="..kq) uc=del1(skq[1]) for i=2,#skq,1 do uc=ucln(uc,skq[i]) end ss=string.gsub(ss,"0","??") ss=string.gsub(ss,"1","??") ss=string.gsub(ss,"2","??") ss=string.gsub(ss,"3","??") ss=string.gsub(ss,"4","??") ss=string.gsub(ss,"5","??") ss=string.gsub(ss,"6","??") ss=string.gsub(ss,"7","??") ss=string.gsub(ss,"8","?_") ss=string.gsub(ss,"9","??") i=1 ss=skq[1]/uc..ss ss=string.gsub(ss,"%+",function (s) i=i+1 if (i>#skq) then return "+"..del1(87297210/uc) else return "+"..del1(skq[i]/uc) end end ) ss=string.gsub(ss,"%=",function (s) i=i+1 if (i>#skq) then return "="..del1(87297210/uc) else return "="..del1(skq[i]/uc) end end ) return(ss) end export{balace=canbang}`

Usage: balace(string)

some example:

--It may be any name, base on your choice
require "balace"
print(balace("H2+O2=H2O")) ------------------> "2H2+O2=2H2O"

--It may be any name, base on your choice
require "balace"
print(balace( " H2+Ca(CN)2+NaAlF4+FeSO4+MgSiO3+KI+H3PO4+PbCrO4+BrCl+CF2Cl2+SO2=PbBr2+CrCl3+MgCO3
+KAl(OH)4+Fe(SCN)3+PI3+NaSiO3+CaF2+H2O")) ------------------> (Try it yourself , it's a great equation)

### #2 girdeux

girdeux

• Members
• 88 posts
• Location:Spain / Castell?n

• Calculators:
casio fx-115ms;

Posted 02 January 2007 - 08:34 PM

```ss=string.gsub(ss,"0","??")
ss=string.gsub(ss,"1","??") ss=string.gsub(ss,"2","??") ss=string.gsub(ss,"3","??") ss=string.gsub(ss,"4","??") ss=string.gsub(ss,"5","??") ss=string.gsub(ss,"6","??") ss=string.gsub(ss,"7","??") ss=string.gsub(ss,"8","?_")
ss=string.gsub(ss,"9","? ")```

UUUUaaaauuuu I'm surprised .... please can you upload the file... I think that when you put the code some simbols are be changed, like this: ?? ?? ?? ?? ?? ?? ...etc. Thanks.

### #3 Lovecasio

Lovecasio

Casio Freak

• Members
• 242 posts
• Location:Hochiminh city Vietnam
• Interests:Organic chemistry.<br />Pharmacy

• Calculators:
fx 570 MS, Casio AFX 2.0+, ClassPad 300

Posted 03 January 2007 - 01:15 AM

Copy this code to the CPLua Emulator, then send it to your CP:

`require("string","cas","table") function ucln(a,b) if(b%a==0) then return a else return ucln(b%a,a) end end function del1(a) if(a==1) then return "" else return a end end function duavedangcoban(s) i=1 s=string.gsub(s,"%+","%)%+%(") s="val_1*("..s s=string.gsub(s,"%+",function (s) i=i+1 return "+val_"..i.."*" end) s=string.gsub(s,"%=","%)%=%(") s=string.gsub(s,"%=",function (s) i=i+1 return "=val_"..i.."*" end) s=string.gsub(s,"(%u)([0-9])","%1*%2") s=string.gsub(s,"(%u+%l)([0-9])","%1*%2") s=string.gsub(s,"(%u)","+%1") s=string.gsub(s,"%(","+%1") s=string.gsub(s,"%(%+","%(") s=string.gsub(s,",%+",",") s=string.gsub(s,"(%))([0-9])","%1*%2") s=string.gsub(s,",%*%+","%*") s=tostring(cas.expand(s)) return s,i end function getval(sf) local val,s,i={},sf,1 for w in string.gmatch(s,"%u%l") do val[i]=w s=string.gsub(s,val[i],"") i=i+1 end for w in string.gmatch(s,"%u") do val[i]=w i=i+1 end i=1 table.sort(val) while(i<#val) do if(val[i]==val[i+1]) then table.remove(val,i) else i=i+1 end end return(val) end function canbang(ss) s,numval=duavedangcoban(ss) gv=getval(s) strgv="|"..table.concat(gv,"=0|").."=0" func="solve({" for i=1,#gv,1 do func=func..cas(s.."|"..tostring(gv[i]).."=1"..strgv) if i~=#gv then func=func.."," else func=func.."}," end end func=func.."{" for i=1,#gv,1 do func=func.."val_"..i if i~=#gv then func=func.."," else func=func.."})" end end if(#gv<numval) then for i=#gv+1,numval,1 do func=func.."|val_"..i.."=87297210" end end func=string.gsub(func,"=0","=87297210") kq=tostring(cas.getright(func)) dostring("skq="..kq) uc=del1(skq[1]) for i=2,#skq,1 do uc=ucln(uc,skq[i]) end ss=string.gsub(ss,"0","??") ss=string.gsub(ss,"1","??") ss=string.gsub(ss,"2","??") ss=string.gsub(ss,"3","??") ss=string.gsub(ss,"4","??") ss=string.gsub(ss,"5","??") ss=string.gsub(ss,"6","??") ss=string.gsub(ss,"7","??") ss=string.gsub(ss,"8","?_") ss=string.gsub(ss,"9","??") i=1 ss=skq[1]/uc..ss ss=string.gsub(ss,"%+",function (s) i=i+1 if (i>#skq) then return "+"..del1(87297210/uc) else return "+"..del1(skq[i]/uc) end end ) ss=string.gsub(ss,"%=",function (s) i=i+1 if (i>#skq) then return "="..del1(87297210/uc) else return "="..del1(skq[i]/uc) end end ) return(ss) end export{balace=canbang}`

Usage: balace(string)

some example:

--It may be any name, base on your choice
require "balace"
print(balace("H2+O2=H2O")) ------------------> "2H2+O2=2H2O"

--It may be any name, base on your choice
require "balace"
print(balace( " H2+Ca(CN)2+NaAlF4+FeSO4+MgSiO3+KI+H3PO4+PbCrO4+BrCl+CF2Cl2+SO2=PbBr2+CrCl3+MgCO3
+KAl(OH)4+Fe(SCN)3+PI3+NaSiO3+CaF2+H2O")) ------------------> (Try it yourself , it's a great equation)

Cũng hay đấy, nhưng em c? chắc l? n? c?n bằng được mọi phương tr?nh kh?ng?
(again) Trong đời anh chưa từng thấy phương tr?nh n?o khủng hoảng đến như vậy M? anh cũng chưa tin về t?nh đ?ng đằn của n?. ở vế phải của phương tr?nh, PI3 c? thể kết hợp với nước t?i tạo lại HI. C?n nữa, CrBr3, PbCl2 cũng l? chất khả dĩ tạo th?nh (PbCl2 l? chất kết tủa,lưu ? l? nếu [Cl-] c?n dư v? đun th? rất dẽ tạo th?nh phức [PbCl4]- , Al thường tạo số phối tr? l? 6 n?n c? thể sẽ kh?ng tạo th?nh KAl(OH)4 đ?u. Anh nhấn mạnh từ "c? thể", bởi trong phản ứng c? qu? nhiều chất tham gia như tr?n th? rất kh? viết ch?nh x?c được những sản phẩm tạo th?nh.

### #4 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 03 January 2007 - 03:02 AM

```ss=string.gsub(ss,"0","??")
ss=string.gsub(ss,"1","??") ss=string.gsub(ss,"2","??") ss=string.gsub(ss,"3","??") ss=string.gsub(ss,"4","??") ss=string.gsub(ss,"5","??") ss=string.gsub(ss,"6","??") ss=string.gsub(ss,"7","??") ss=string.gsub(ss,"8","?_")
ss=string.gsub(ss,"9","? ")```

UUUUaaaauuuu I'm surprised .... please can you upload the file... I think that when you put the code some simbols are be changed, like this: ?? ?? ?? ?? ?? ?? ...etc. Thanks.

You can copy them to the emulator as well, but if you want, you can download it here

Screen shot:

### #5 Kilburn

Kilburn

Casio Technician

• Members
• 491 posts
• Gender:Male
• Location:France
• Interests:Blah

• Calculators:
FX-7500 G

Posted 03 January 2007 - 08:53 AM

Btw, I think you mean "balance", not "balace"

### #6 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 03 January 2007 - 11:09 AM

### #7 girdeux

girdeux

• Members
• 88 posts
• Location:Spain / Castell?n

• Calculators:
casio fx-115ms;

Posted 03 January 2007 - 12:22 PM

You can copy them to the emulator as well, but if you want, you can download it here

I download this file and when I open the program canbang appears: error incorrect file type.

### #8 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 03 January 2007 - 12:52 PM

What ver of CPLua are u using? Mine is 09D

### #9 girdeux

girdeux

• Members
• 88 posts
• Location:Spain / Castell?n

• Calculators:
casio fx-115ms;

Posted 03 January 2007 - 09:54 PM

I have 0.8, I didn't know that CPlua 0.9 was available now... where can I get it??

Vanhoa do you left the forum until the summer?? why??

### #10 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 04 January 2007 - 02:26 AM

http://orwell01.free...ua/CPLua09D.zip

Sorry, I have to study very hard for 6 months, so i dont think i can online always as before,

Newbie

• Members
• 10 posts
• Location:Sweden (Vietnamese)

• Calculators:
CASIO CFX-9850GB PLUS

Posted 05 January 2007 - 11:08 PM

Very nice program Vanhoa!!,
hint, you should add so the program could solve plus "^+" and minus "^-"
for example:
Zn + NO(3^-) + H(^+) = Zn(^2+) + NH4+H2O
-------->
4Zn + NO(3^-) + 10H(^+) = 4Zn(^2+) + NH4 + 3H2O

Try to input
Zn+NO3+H=Zn+NH4+H2O to the program,,
and you will get the answer
3Zn+10NO3+H=3Zn+NH4+3H2O
4Zn+10NO3+H=4Zn+NH4+3H2O
Maybe some wrong??

And if you could add an arrow ( ) insted of ( = )

I tried to add "ss=string.gsub(ss,"=","") to the source code, but then the the program calculates wrong ...

Whatever, nice program!!!!!!!!!!

### #12 vanhoa

vanhoa

Casio Overlord

• Members
• 854 posts
• Gender:Male
• Location:Vietnam

• Calculators:
AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 06 January 2007 - 11:28 AM

Screen shot:

Newbie

• Members
• 10 posts
• Location:Sweden (Vietnamese)

• Calculators: