Jump to content



Photo

Luaconv


  • Please log in to reply
4 replies to this topic

#1 vanhoa

vanhoa

    Casio Overlord

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

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

Posted 09 January 2007 - 06:14 AM

require("ui","cas","table","io") function initvar() init={name="",cd={{name="",value1="",value2=""}}} str_input_nhom="Catelogy:" Input_unit_name="Unit name:" Input_unit_value="Unit value:" addcat="+Catelogy" addunit="+Unit" texit="Exit" strcd="Conv" end function read() fi = io.file("main/convsave") if(fi:exists()) then fi:open("r") data=fi:read(1) fi:close() else data={} data[1]=init end end function write() fi = io.file("main/convsave") if not fi:exists() then fi:create("data","ConvData") end fi:open("w") fi:write(data) fi:close() end function addnhom() ui.stop() win:show(false) cons:setposition(0,0) local ten=input(str_input_nhom) data[#data]={name=ten,cd={{name="",value1="",value2=""}}} table.sort(data,function(a,b) if a.name<b.name then return true end end) i=1 while(i<#data) do if(data[i].name==data[i+1].name) then table.remove(data,i) else i=i+1 end end data[#data+1]=init end function addft(i) ui.stop() win:show(false) cons:setposition(0,0) local ten,val1,val2=input(Input_unit_name),input(Input_unit_value.."(*)"),input(Input_unit_value.."(+)") data[i].cd[#data[i].cd]={name=ten,value1=val1,value2=val2} table.sort(data[i].cd,function(a,b) if a.name<b.name then return true end end) ki=1 while(ki<#data[i].cd) do if(data[i].cd[ki].name==data[i].cd[ki+1].name) then table.remove(data[i].cd,ki) else ki=ki+1 end end data[i].cd[#data[i].cd+1]={name="",value1="",value2=""} end function conv() initvar() read() local sel,sas,ssa=1,1,1 win = ui.window{name="main",x=0,y=0,width=160,height=100, frame="thick",scroll="v"} m = ui.menu() m:add{type="button", text=addcat, _action=function() addnhom() end} m:add{type="button", text=addunit, _action=function() if #data>1 then addft(sel) end end} m:add{type="button", text=texit, _action=function() running=false ui.stop() end} win:add(m) value=ui.textedit{x=10,y=10,width=130,height=16,frame ="thin",align="center",parent=win} local do_conv=ui.button{x=30,y=60,width=100,parent=win,text=strcd,_clicked=function() local calc=cas("a2*(x-b1)/a1+b2|x="..cas(value:gettext()).."|a1="..cas(data[sel].cd[sas].value1).."|b1="..cas(data[sel].cd[sas].value2).."|a2="..cas(data[sel].cd[ssa].value1).."|b2="..cas(data[sel].cd[ssa].value2)) print(value:gettext().." "..data[sel].cd[sas].name.." = "..calc.." "..data[sel].cd[ssa].name.."\n  ~"..cas.approx(calc).." "..data[sel].cd[ssa].name) end} running=true cons = ui.getwin("Console") while running do cons:setposition(0,105) cons:setsize(160,100)  local dfrom=ui.combobox{x=10,y=45,width=57,height=10*#data[1].cd,parent=win,scroll="auto",_selected=function(I,T) sas=T end} local dto=ui.combobox{x=67,y=45,width=57,height=10*#data[1].cd,parent=win,_selected=function(I,T) ssa=T end} local nhom=ui.combobox{x=10,y=30,width=114,height=10*#data,parent=win,_selected=functi
on(II,T) sel=T ui.stop() end} local do_delt=ui.button{x=130,y=44,width=10,parent=win,text="x",_clicked=function() if #data[sel].cd~=1 then table.remove(data[sel].cd,ssa) ssa=1 ui.stop() end end} local do_deln=ui.button{x=130,y=29,width=10,parent=win,text="x",_clicked=function() if #data~=1 then table.remove(data,sel) sel=1 ui.stop() end end} for i=1,#data-1 do nhom:addstring(data[i].name) end if #data[sel].cd~=1 then for i=1,#data[sel].cd-1 do dfrom:addstring(data[sel].cd[i].name) dto:addstring(data[sel].cd[i].name) end sas,ssa=1,1 dfrom:setselected(1) dto:setselected(1) end if #data>1 then nhom:setselected(sel) end win:show() ui.start() end win:show(false) cons:setposition(0,0) cons:setsize(160,215) write() end export{conv=conv}



use conv() to call the prog, I made this for only 7 mins and didnt test a lot, fell free to edit it.

#2 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 09 January 2007 - 11:09 AM

I've taken a very quick look to your code (sth like 7 mins :lol: ), and here are 2 remarks:

1) Remember what I said earlier in the CPLua topic:

Concerning ui.start() / ui.stop(): now ui.stop() simply raises an error, which is catched by ui.start(). That means that any instructions written after ui.stop() in an event will never be executed. Thus,

b = ui.button{...}
function b:_clicked() print("a") ui.stop() print("b") end
ui.start()
print("c")
This code will print "a c" and not "a b c" when you press the button, because ui.stop() will immediatly stop the execution of ui.start().

In your code some instructions won"t be executed correctly:
function addnhom() ui.stop() win:show(false) cons:setposition(0,0) ...

2) Just a detail:
m:add{type="button", text=addcat, _action=function() addnhom() end}
This is correct, but you may write directly this:
m:add{type="button", text=addcat, _action=addnhom}


#3 vanhoa

vanhoa

    Casio Overlord

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

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

Posted 09 January 2007 - 12:12 PM

I've taken a very quick look to your code (sth like 7 mins :lol: ), and here are 2 remarks:

1) Remember what I said earlier in the CPLua topic:
In your code some instructions won"t be executed correctly:

function addnhom() ui.stop() win:show(false) cons:setposition(0,0) ...

2) Just a detail:
m:add{type="button", text=addcat, _action=function() addnhom() end}
This is correct, but you may write directly this:
m:add{type="button", text=addcat, _action=addnhom}





:D But it works! I dont know why, as you say after we call ui.stop() it will jump to ui.start() but as i see it continue do every thing in the function before go to ui.start()

b1 = ui.button({x=10, y=10, text="btn1"})
function b1:_clicked()
 ui.stop() ui.start()
end


#4 Orwell

Orwell

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 777 posts
  • Gender:Male
  • Location:Paris - France

  • Calculators:
    Casio AFX 1.02 / Casio ClassPad 300

Posted 09 January 2007 - 10:39 PM

Your example isn't really good, the correct code should be
b1 = ui.button({x=10, y=10, text="btn1"})
function b1:_clicked()
 ui.stop() print "hello!"
end
ui.start()
If this code prints "hello" when you wlick on the button, then there is a bug somewhere :huh:

#5 vanhoa

vanhoa

    Casio Overlord

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

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

Posted 10 January 2007 - 03:29 AM

No, dont fix that bug :P




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users