Lemick project site
 
 
 
Code examples

Example 1: A small GUI application in Lemick implementing a very simple calculator.

 

use io.console, lang.math, lgwc

class CalcFrame extends Frame

    private class CalcButton extends Button

	public const CBUT_DIGIT = 1, CBUT_OP = 2, CBUT_SPEC = 3

	public const COP_ADD = 1, COP_SUB = 2, COP_MUL = 3,
		     COP_EVL = 4, COP_NEG = 5, COP_PNT = 6,
		     COP_DIV = 7, COP_POW = 8, COP_SQR = 9,
		     COP_MOD = 10   
	
	public flag as Integer
	public opcode as Integer
	public final sub New(d as Integer) 
	    = new Button(d +> String, 35, 35)
	    opcode = d
	    flag = CBUT_DIGIT
	end sub
	public final sub New(op as String, opc as Integer) 
	    = new Button(op, 35, 35)
	    opcode = opc
	    flag = CBUT_OP
	end sub
	public final sub New(flg as Integer, opc as Integer, op as String) 
	    = new Button(op, 35, 35)
	    opcode = opc
	    flag = flg
	end sub
    end class

    xtext as TextInput
    reg() as Single
    creg as Integer
    cop as Integer
    pnt as Integer
    reset as Integer

    public sub Initialize
	reg = new(3) as Single
	creg = 0
	pnt = 0
	
	xtext = new TextInput("0")
	xtext.Width = this.Width - 4

	i = 0
	for x = 0 to 2
	    for y = 0 to 2
		i = i + 1
		addControl new CalcButton(i), y*40 + 2, x*40 + 30
	    next
	next

	addControl new CalcButton(0), 40 + 2, 120 + 30

	addControl new CalcButton(CalcButton.CBUT_SPEC, CalcButton.COP_NEG, "+/-"),  2, 120 + 30
	addControl new CalcButton(CalcButton.CBUT_SPEC, CalcButton.COP_PNT, "."),  80 + 2, 120 + 30
	
	addControl new CalcButton("+", CalcButton.COP_ADD), 3*40 + 2, 30
	addControl new CalcButton("-", CalcButton.COP_SUB), 3*40 + 2, 40  + 30
	addControl new CalcButton("*", CalcButton.COP_MUL), 3*40 + 2, 80  + 30
	addControl new CalcButton("=", CalcButton.COP_EVL), 3*40 + 2, 120 + 30
	addControl new CalcButton("/", CalcButton.COP_DIV), 4*40 + 2, 30
	addControl new CalcButton("^", CalcButton.COP_POW), 4*40 + 2, 40  + 30
	addControl new CalcButton("sqr", CalcButton.COP_SQR), 4*40 + 2, 80  + 30
	addControl new CalcButton("mod", CalcButton.COP_MOD), 4*40 + 2, 120 + 30

	this.addControl xtext, 2, 2
	
	this.Top = 300
	this.Left = 50
	this.Visible  = True

	EventsLoop
    end sub
    
    public sub EventsListener(msg as EventMessage)
	dim cbut as CalcButton

	if msg.Sender is CalcButton then
	    cbut = msg.Sender ?> CalcButton

	    select case cbut.flag
		case CalcButton.CBUT_DIGIT
		    if reset then reg(creg) = 0 : reset = 0
		    
		    if pnt then 
			reg(creg) = reg(creg) + pow(10, -pnt) * cbut.opcode
			pnt = pnt + 1
		    else
			reg(creg) = reg(creg) * 10 + cbut.opcode
		    end if
    
		    xtext.Text = reg(creg) +> String

		case CalcButton.CBUT_OP
		    creg = (creg + 1) mod 2
		    pnt = 0
		    if creg == 0 then
			select case cop
			    case CalcButton.COP_ADD
				reg(0) = reg(0) + reg(1)
			    case CalcButton.COP_SUB
				reg(0) = reg(0) - reg(1)
			    case CalcButton.COP_MUL
				reg(0) = reg(0) * reg(1)
			    case CalcButton.COP_DIV
				reg(0) = reg(0) / reg(1)
			    case CalcButton.COP_POW
				reg(0) = pow(reg(0), reg(1))
			    case CalcButton.COP_SQR
				reg(0) = pow(reg(0), 1.0/reg(1))
			    case CalcButton.COP_MOD
				reg(0) = reg(0) mod reg(1)
			end select
			reg(1) = 0
		    end if
		    cop = cbut.opcode
		    reset = 1
		    xtext.Text = reg(creg) +> String
		case CalcButton.CBUT_SPEC
		    select case cbut.opcode
			case CalcButton.COP_NEG
			    reg(creg) = -reg(creg)
			case CalcButton.COP_PNT
			    Println "POINT!"
			    if not pnt then pnt = 1
		    end select
		    xtext.Text = reg(creg) +> String
	    end select
	else
	    select case msg.MsgType
		case EventMessage.MSG_DESTROY
		    Abort
	    end select	
	end if
    end sub

end class

dim window as Frame = new CalcFrame("Lemick sample calc", 200, 188)


Example 2: Statistical problem solution and related graphics using OpenGL module.

Use io.console, lang.math, lemgl

Dim As Shared Double 
	istart(10) = { 80, 85, 90, 95, 100, 105, 110, 115, 120, 125 },
	  imid(10) = { 82.5, 87.5, 92.5, 97.5, 102.5, 107.5, 112.5, 117.5, 122.5, 127.5 },
	    sm(10) = { 7, 11, 15, 24, 49, 41, 26, 17, 7, 3 },
	    sf(10) = { 0.035, 0.055, 0.075, 0.120, 0.245, 0.205, 0.130, 0.085, 0.035, 0.015 },
	   sfa(10) = { 0.035, 0.090, 0.165, 0.285, 0.530, 0.735, 0.865, 0.950, 0.985, 1 }

Dim As Shared Double N = 200, K = 10, H = 5, Alpha = 0.05

Dim As Shared Double m_mexp, m_disp, m_disp_q, m_G


Function MExp As Double
	For i = 0 To K - 1
	    me = me + sm(i) * imid(i)
	Next    
	me = me / N
End Function

Function Disp As Double
    Dim As Double s1 = 0, s2 = 0

	For i = 0 To K - 1
	    s1 = s1 + sm(i) * imid(i) * imid(i)
	Next    
	For i = 0 To K - 1
	    s2 = s2 + sm(i) * imid(i)
	Next    
	me = (1.0 / (N - 1.0)) * (s1 - (1.0 / N) * s2 * s2)
End Function


Function FGaussDen(x As Double) As Double
	me = (1.0 / sqrt(2.0*PI)) * exp(-pow(x, 2.0)/2.0)
End Function

Function mtheo(x As Integer) As Double
	me = (N * H / m_disp) * FGaussDen((imid(x) - m_mexp) / m_disp)
End Function


Function gauss_quantil(q As Double) As Double
    Dim a As Single, t As Single
    Dim As Double c0, c1, c2, d1, d2, d3

	c0 = 2.515517: c1 = 0.8028538: c2 = 0.01032
	d1 = 1.432788: d2 = 0.189269:  d3 = 0.001308

	a = 1.0 - q
	t = sqrt(log(pow(a, -2.0)))

	me = t - (c0 + c1*t + c2*t*t)/(1 + d1*t + d2*t*t + d3*t*t*t)
End Function

Function hi_q(v As Double, q As Double) As Double
	me = v * pow(1.0D - (2.0D/(9.0D*v)) + gauss_quantil(q)*sqrt(2.0D/(9.0D*v)), 3.0)
End Function


m_mexp   = MExp()
m_disp_q = Disp()
m_disp   = sqrt(m_disp_q)

Println "Exp    = " + m_mexp
Println "Disp^2 = " + m_disp_q
Println "Disp   = " + m_disp

m_G = 0
For i = 0 To K - 1
    m_G = m_G + pow(sm(i) - mtheo(i), 2) / mtheo(i)
Next

Println "g      = " + m_G

Println "hi_q   = " + hi_q(7, 0.95)


Dim sf2(10) As Double

For i = 0 To K - 1
    sf2(i) = sf(i) / 5.0
Next


GLInit "Lab2 charts", 10, 10, 600, 400

GLLineChart "freqs hist", sf2, 10, 1
GLLineChart "freqs rel diag", sf, 10, 0
GLLineChart "freqs abs diag", sm, 10, 0

GLMainLoop

 

 

 

© Copyright 2004 Alex Iliasov (alex at iliasov dot org)
All the information from this site can be used without any restrictions provided the reference to this site is included.
Main page