1. Getting atom values with gets()

Sorry for the slew of questions lately.. I'm working on a big project.
Details will be out in a week or so once more work on it is complete.
  My question is about the gets() or get() commands.  I need to be able to
input data as an atom...ie if I type 100 gets() will return 100, not
{XX,YY,YY,10}.  Or I need to take the latter and convert it to 100.  This is
probably as easy as tying my shoes, but I'm stumped.  I'll probably kick
myself when I see how easy it is ; )

Thanks,
Derek Brown

new topic     » topic index » view message » categorize

2. Re: Getting atom values with gets()

EU>  Sorry for the slew of questions lately.. I'm working on a big project.
EU>Details will be out in a week or so once more work on it is complete.
EU>  My question is about the gets() or get() commands.  I need to be able to
EU>input data as an atom...ie if I type 100 gets() will return 100, not
EU>{XX,YY,YY,10}.  Or I need to take the latter and convert it to 100.  This is
EU>probably as easy as tying my shoes, but I'm stumped.  I'll probably kick
EU>myself when I see how easy it is ; )

EU>Thanks,
EU>Derek Brown

Use get. Example:

include get.e
sequence s
puts(1,"Enter a number: ")
s = get(0)
if s[1] != GET_SUCCESS then
        puts(1,"You were supposed to enter a number!\n")
elsif not atom(s[2]) then
        puts(1,"A number. Not a sequence.\n")
else
        printf(1,"\nThe number you entered was %f\n",s[2])
end if

Jeffrey Fielding
JJProg at cyberbury.net
http://members.tripod.com/~JJProg/

new topic     » goto parent     » topic index » view message » categorize

3. Re: Getting atom values with gets()

>From: Derek Brown <Cyrusbrown at AOL.COM>
>Reply-To: Euphoria Programming for MS-DOS <EUPHORIA at LISTSERV.MUOHIO.EDU>
>To: EUPHORIA at LISTSERV.MUOHIO.EDU
>Subject: Getting atom values with gets()
>Date: Wed, 9 Jun 1999 21:23:22 EDT
>
>   Sorry for the slew of questions lately.. I'm working on a big project.
>Details will be out in a week or so once more work on it is complete.
>   My question is about the gets() or get() commands.  I need to be able to
>input data as an atom...ie if I type 100 gets() will return 100, not
>{XX,YY,YY,10}.  Or I need to take the latter and convert it to 100.  This
>is
>probably as easy as tying my shoes, but I'm stumped.  I'll probably kick
>myself when I see how easy it is ; )
>
>Thanks,
>Derek Brown

You could also use gets() instead of get(),
you can return a sequence if it's a sequence,
and an atom if the sequence is an atom.

Use the source beneath...
E.g.
---------
include seq2int.e

object input
input=gets(0)         -- Get input
if isAtom(input) then -- If the sequence is really an atom
input=seq2int(input) -- Sequence becomes an atom...
end if
---------

[SEQ2INT.E]
global function isAtom(sequence input)
object returnment,tmp
tmp=0
for a=1 to length(input) do
  if input[a]>='0' and input[a]<='9' then
   tmp+=1
  elsif input[a]='.' or input[a]='-' then
   tmp+=1
  end if
end for
if tmp=length(input) then
  returnment=0
else
  returnment=-1
end if
return returnment
end function

-- Works till "9999999999", try "1.5" or "-1.5" it works --
global function seq2int(sequence str)
object result,tmp1,tmp2,divide
divide=1
tmp1=find('.',str)
tmp2=find('-',str)
if tmp1 !=0 then
  divide=power(10,length(str)-tmp1)
  str=str[1..tmp1-1]&str[tmp1+1..length(str)]
end if
if tmp2 !=0 then
  divide*=-1
  str=str[1..tmp2-1]&str[tmp2+1..length(str)]
end if
result=str[length(str)]-48
for a=1 to length(str)-1 do
  result+=power(10,length(str)-a)*(str[a]-48)
end for
result/=divide
return result
end function
[EOF]


Bye,
PQ
QC


______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

4. Re: Getting atom values with gets()

It's overkill time: :)

My entire Euphoria life (up til around late april) is available for
download (5 Megs) from:

    http://www.bigfoot.com/~cyrek/public/euphoria/crweuph.zip

My mathematics libraries are still available from my Euphoria page...

This being the one that pertains to this thread...:

--- basebag.e --- Converts strings of didgits in bases 2 .. 36 into atoms
--                ...and back again.

type numbase(integer x)
    return x >= 2 and x <= 36
end type

type char(integer ch)
    return ch >= 0 and ch <= 255
end type

function isdigit(char ch)
    return (ch >= '0' and ch <= '9')
end function

function isalphanumeric(char ch)
    return isdigit(ch)
        or (ch >= 'A' and ch <= 'Z')
end function

function char2digit(char ch, numbase base)
    integer out
    out = -1
    if isalphanumeric(ch) then
        out = ch - '0' - 7 * (ch >= 'A')
        if out >= base then
            out = -1
        end if
    end if
    return out
end function

function digit2char(integer digit, numbase base)
    char out
    out = ' '
    if digit >= 0 and digit < base then
        out = digit + 48 + 7 * (digit > 9)
    end if
    return out
end function

global function seq2atom(sequence s, numbase base)
    integer sign, digit
    atom int, frac, multiplier
    integer fp

    fp = 0
    int = 0
    frac = 0
    multiplier = 1
    sign = 1
    digit = -1

    for i = 1 to length(s) do
        digit = char2digit(s[i], base)

        if s[i] <= 32 or s[i] = ',' then
            -- do nothing
        elsif s[i] = '-' and sign != -1 then
            sign = -1
        elsif digit != -1 then
            if fp then
                multiplier = multiplier / base
                frac = frac + multiplier * digit
            else
                int = base * int + digit
            end if
        elsif s[i] = '.' and not fp then
            fp = 1
        else
            exit
        end if
    end for

    return sign*(int+frac)
end function

global function atom2seq(atom a, numbase base)
    sequence out, front
    integer digit, maxfpp, fppcount
    atom int, frac
    char ch

    maxfpp = 14 -- max possible without accidentally hitting 15 :)
    maxfpp = floor(maxfpp/(log(base)/log(10))+.5)
    fppcount = 0
    -- Base precision on the fact that most computers can cope with
    -- [see first definition of maxfpp] digits after the _decimal_
    -- (i.e. base *10*) point.
    -- This is then scaled to the current base
    -- e.g. base 10, 12 digits -> base 2, 40 digits -> base 16, 10 digits etc.

    out = {}
    front = {}
    frac = a

    if a < 0 then
        front = "-"
        frac = -a
    end if

    int = floor(frac)
    frac = frac - int

    if int = 0 then
        front = front & '0'
    end if

    while int > 0 do
        digit = remainder(int, base)
        int = floor(int / base)

        ch = digit2char(digit, base)
        if ch = 32 then
            return front & out
        end if

        fppcount = fppcount + 1
        out = ch & out
    end while

    if frac = 0 then
        return front & out
    end if

    out = out & '.'

    while frac < 1 and fppcount < maxfpp do
        frac = frac * base
        digit = floor(frac)
        frac = frac - digit

        ch = digit2char(digit,base)
        if ch = 32 then
            return front & out
        end if

        fppcount = fppcount + 1
        out = out & ch
    end while

    while out[length(out)] = '0' do
        out = out[1..length(out)-1]
    end while

    return front & out
end function

global function baseconv(numbase from, numbase to_, sequence x)
    return atom2seq(seq2atom(x, from), to_)
end function

global function base27toalpha(sequence a)
    return a + 47*(a='0') + 16*(a>='1'and a<='9') + 9*(a>='A'and a<='R')
end function

global function alphatobase27(sequence a)
    return a - 47*(a='_') - 16*(a>='A'and a<='I') - 9*(a>='J'and a<='Z')
end function


--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

5. Re: Getting atom values with gets()

Carl
I tried to get your library, but your site wouldn't let me in.
Bye
Martin

new topic     » goto parent     » topic index » view message » categorize

6. Re: Getting atom values with gets()

On Fri, 11 Jun 1999, simulat wrote:

] Carl
] I tried to get your library, but your site wouldn't let me in.

Which page/link exactly were you having trouble with?

I've just had the 5Mb zip down for checking, but it's back up now...

Any other links?

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

7. Re: Getting atom values with gets()

Carl
I tried the link again and it worked fine. I guess I just happened to hit it
while you had the file down.
Thanks again
Martin

----- Original Message -----
From: Carl R. White <C.R.White at SCM.BRAD.AC.UK>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Friday, June 11, 1999 7:44 AM
Subject: Re: Getting atom values with gets()


> On Fri, 11 Jun 1999, simulat wrote:
>
> ] Carl
> ] I tried to get your library, but your site wouldn't let me in.
>
> Which page/link exactly were you having trouble with?
>
> I've just had the 5Mb zip down for checking, but it's back up now...
>
> Any other links?
>
> --
> Carl R White -- Final Year Computer Science at the University of Bradford
> E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
> URL...........: http://www.bigfoot.com/~cyrek/
> Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"
>

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu