Re: My First Program

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

Joseph Martin wrote:
>
>---------------------- Information from the mail header -----------------------
>Sender:       Euphoria Programming for MS-DOS <EUPHORIA at
>MIAMIU.ACS.MUOHIO.EDU>
>Poster:       Joseph Martin <jam at EXIS.NET>
>Subject:      My First Program
>-------------------------------------------------------------------------------
>
>Okay, Okay, maybe it's not that much to get excited over, but it IS
>my first one. You're probably asking: "So what is it?" My reply is
>
>Star Date Converter 1.0
>
>It is a program to take TNG, DS9, and VOY stardates and turn them
>into self-respecting Julian dates. If anyone would like to see it let
>me know and I'll post it.
>
>I do have one minor question about one of my routines. It recieves a
>value of the number of days since the first of the year and
>calculates the month and day. Right now it works, but is a bit longer
>than I would like. If anyone has any ideas I how to make it smaller I
>would appreciate it.
>
great program! also, I caught a bug that you should be aware of.  Did you know
that your day2month function returns the same day/month even if its a leap year?
try year = 1996 and day = 130.  the answer should be may 9, but your code
returns
may 10.  I have put comments into your code where it should be fixed.

>--Begin Code Cut--
>
>global function day2month(atom day, atom year)
>
>sequence month
>atom leap
>leap = remainder(year,4) --check to see if it's a leap year
>
>if leap = 0 then --if yes then
>  if compare(day, 1) > -1 and compare( day,  31) < 1 then
>   month = "January"
>   return{month,day}
>  elsif compare(day, 32) > -1 and compare( day,  60) < 1 then
>   month = "February"
>   day = day - 31
>   return{month,day}
>  elsif compare(day, 60) > -1 and compare( day,  91) < 1 then
             ^^^^^^^  should be compare(day, 61)
>   month = "March"
>   day = day - 59
                ^^^^^ should be -60
>   return{month,day}
>  elsif compare(day, 91) > -1 and compare( day,  121) < 1 then
                ^^^^^   compare(day, 92)
>   month = "April"
>   day = day - 90
                ^^^^^ - 91
>   return{month,day}
>  elsif compare(day, 121) > -1 and compare( day,  152) < 1 then
>   month = "May"
>   day = day - 120
>   return{month,day}
<snip>

anyway, for the very first if section (that is, the isleap section), the first
compare(day, X) should be compare(day, X+1) for all months after feb.
and the day=day - X should be day=day - X+1 for all months after feb.
the rest of the code (that is, the standard year if part) is ok.

anyway, as far as a smaller routine, you dont have to perform all those if's
twice!
just check for the standard year, and get your day & month. then, if it is a
leap year
decrease the day by 1. if the day is then = to 0, decrease the month by 1,
and set the
day = to the max days in that month!  simple, yes?  heres some sample code

<Begin code>
constant maxdays = {{"January", 31}, {"February", 29}, {"March", 31}, {"April",
30}, {"May", 31}, {"June", 30}, {"July", 31}, {"August", 31}, {"September", 30},
{"October", 31}, {"November", 30}, {"December", 31}}

global function day2month(atom day, atom year)
atom leap
object month

leap = remainder(year, 4) -- check to see if it is a leap year

if day < 32 then
    month = 1
elsif day < 60 then
    month = 2
    day = day - 31
elsif day < 91 then
    month = 3
    day = day - 59
elsif day < 121 then
    month = 4
    day = day - 90
elsif day < 152 then
    month = 5
    day = day - 120
elsif day < 182 then
    month = 6
    day = day - 151
elsif day < 213 then
    month = 7
    day = day - 181
elsif day < 244 then
    month = 8
    day = day - 212
elsif day < 274 then
    month = 9
    day = day - 243
elsif day < 305 then
    month = 10
    day = day - 273
elsif day < 335 then
    month = 11
    day = day - 304
elsif day < 366 then
    month = 12
    day = day - 334
end if

if leap = 0 then
    if month > 2 then
        day = day - 1
    end if
    if day = 0 then
        month = month - 1
        day = maxdays[month][2]
    end if
end if
month = maxdays[month][1]
return {month, day}
end function

<END CODE>

as you can see, this is about half the size of your original function.  as
for speed,
it's about the same.
good luck in all your programming efforts, and please do share your code
when finished, ok?
James Powell

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

Search



Quick Links

User menu

Not signed in.

Misc Menu