Re: Tutorial of sorts

new topic     » goto parent     » topic index » view thread      » older message » newer message

Irv wrote:
>So we have assignment 1.1
>Write a *validate* function, to take input, return a number
>within a specified range, or print an error message and
>abort.
>Hint: see functions, abort() in the docs.
>Suggested syntax:
>hours =3D validate(cmd[4],{1,80},"Hours")
>payrate =3D validate(cmd[5],{5.50,35.00},"Payrate")

>Suggested output:
 >* ERROR in Hours: should be between 1 and 80 *

Here is my entry, I hope that it is not to long (>100 lines)
I use the function accept, developed by me because I was not satisfied wi=
th
the various input routines I used before. Especially the processing of
single ENTER or ESC keys. This one only accepts what you want it to accep=
t,
and the Backspace key.
Only problem is the CTRL-C or CTRL-Break checking. Under pure DOS this
works, but under WIN95 the program hangs. Does any1 have a solution to
this?
A more sophisticated version of accept.e can also accept function keys, t=
o
display help or popup a lookup table, for instance.

-- validate.ex
include file.e
include get.e
include graphics.e

global function equal(object o1, object o2) -- David Cuny
    return not compare(o1, o2)
end function    -- equal
-------------------------------------------------------------------------=
--
----
global procedure burp()    -- Jiri Babor
    atom tim
    sound(250)
    tim =3D time()
    while time() - tim < .15 do
    end while
    sound(0)
end procedure -- burp
-------------------------------------------------------------------------=
--
----
global procedure backspace()
sequence pos
    pos =3D get_position()
    if pos[2] >=3D 1 then
        position(pos[1], pos[2] - 1)
        puts(1, ' ')
        position(pos[1], pos[2] - 1)
    else
        burp()
    end if
end procedure   -- backspace
-------------------------------------------------------------------------=
--
----
global function accept(sequence prompt, sequence default, sequence
accepted)
-- version 0.02 -- October 23, 1998
-- Ad_Rienks at compuserve.com
-- returns a string, if a single enter is pressed default is returned
-- only characters you give in 'accepted' are accepted
-- so it can be used like this:
--  question =3D accept("Do you want to go on? (Y/n) ", "y", "YyNn")
--  if lower(question[1]) =3D 'y' then go_on() else exit end if
integer key
sequence string, pos
    allow_break(0)  -- user can't escape with CTRL-C or CTRL-Break
    -- CAUTION: this does not work under WIN95!!!
    key =3D -1
    string =3D ""
    puts(1, prompt & default)
    pos =3D get_position()
    position(pos[1], pos[2] - length(default))  =

    while key !=3D 13 do
        if key !=3D -1 then
            if key =3D 8 and length(string) then      -- backspace
                -- remove last element
                string =3D string[1..length(string) - 1]
                -- update display too
                backspace()
            elsif find(key, accepted) then
                if not length(string) then
                    puts(1, repeat(' ', length(default)))
                    position(pos[1], pos[2] - length(default))  =

                end if
                string =3D append(string, key)
                puts(1, string[length(string)])
            else
                burp()
            end if
        end if
        key =3D get_key()
    end while
    if equal(string, "") then
        string =3D default
    end if
    allow_break(1)  -- set breaking ability back on
    return string
end function    -- accept
-------------------------------------------------------------------------=
--
-----
global function validate(sequence prompt,   -- the input prompt
                         sequence default,  -- possible default value
                         sequence accepted, -- characters accepted
                         sequence bounds)   -- possible bounds, if {}, no=

bounds checking.
object result    =

    while 1 do
        clear_screen()
        result =3D value(accept(prompt, default, accepted))
        if result[1] =3D GET_SUCCESS then
            result =3D result[2]
        else
            result =3D -1
        end if
        if length(bounds) then  -- bounds checking
            if result < bounds[1] or result > bounds[2] then
                -- not within bounds
                burp()
                position(25, 1)
                printf(1, "Give a number between %.2f and %.2f.", bounds)=

                cursor(NO_CURSOR)
                result =3D wait_key()
                cursor(UNDERLINE_CURSOR)
            else-- within bounds
                exit
            end if
        else                    -- no bounds checking
            exit
        end if
    end while
    return result
end function    -- validate

constant INTEGER =3D "0123456789"
constant DECIMAL =3D INTEGER & '.'
integer hours
atom payrate
 =

hours =3D validate("Hours worked: ", "", INTEGER, {1, 80})
payrate =3D validate("Payment/hour: ", "", DECIMAL, {5.5, 35})

clear_screen()
puts(1, "Hours Payment     Tax Net Pay")
printf(1, "\n%5d %7.2f %7.2f %7.2f",
            {hours, payrate, hours*payrate*.25, hours*payrate*.75})

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu