1. 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.

--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
   month = "March"
   day = day - 59
   return{month,day}
  elsif compare(day, 91) > -1 and compare( day,  121) < 1 then
   month = "April"
   day = day - 90
   return{month,day}
  elsif compare(day, 121) > -1 and compare( day,  152) < 1 then
   month = "May"
   day = day - 120
   return{month,day}
  elsif compare(day, 152) > -1 and compare( day,  182) < 1 then
   month = "June"
   day = day - 151
   return{month,day}
  elsif compare(day, 182) > -1 and compare( day,  213) < 1 then
   month = "July"
   day = day - 181
   return{month,day}
  elsif compare(day, 213) > -1 and compare( day,  244) < 1 then
   month = "August"
   day = day - 212
   return{month,day}
  elsif compare(day, 244) > -1 and compare( day,  274) < 1 then
   month = "September"
   day = day - 243
   return{month,day}
  elsif compare(day, 274) > -1 and compare( day,  305) < 1 then
   month = "October"
   day = day - 273
   return{month,day}
  elsif compare(day, 305) > -1 and compare( day,  335) < 1 then
   month = "November"
   day = day - 304
   return{month,day}
  elsif compare(day, 335) > -1 and compare( day,  366) < 1 then
   month = "December"
   day = day - 334
   return{month,day}
 end if
else --otherwise
  if compare(day, 1) > -1 and compare( day,  31) < 1 then
   month = "January"
   return{month,day}
  elsif compare(day, 32) > -1 and compare( day,  59) < 1 then
   month = "February"
   day = day - 31
   return{month,day}
  elsif compare(day, 60) > -1 and compare( day,  90) < 1 then
   month = "March"
   day = day - 59
   return{month,day}
  elsif compare(day, 91) > -1 and compare( day,  120) < 1 then
   month = "April"
   day = day - 90
   return{month,day}
  elsif compare(day, 121) > -1 and compare( day,  151) < 1 then
   month = "May"
   day = day - 120
   return{month,day}
  elsif compare(day, 152) > -1 and compare( day,  181) < 1 then
   month = "June"
   day = day - 151
   return{month,day}
  elsif compare(day, 182) > -1 and compare( day,  212) < 1 then
   month = "July"
   day = day - 181
   return{month,day}
  elsif compare(day, 213) > -1 and compare( day,  243) < 1 then
   month = "August"
   day = day - 212
   return{month,day}
  elsif compare(day, 244) > -1 and compare( day,  273) < 1 then
   month = "September"
   day = day - 243
   return{month,day}
  elsif compare(day, 274) > -1 and compare( day,  304) < 1 then
   month = "October"
   day = day - 273
   return{month,day}
  elsif compare(day, 305) > -1 and compare( day,  334) < 1 then
   month = "November"
   day = day - 304
   return{month,day}
  elsif compare(day, 335) > -1 and compare( day,  365) < 1 then
   month = "December"
   day = day - 334
   return{month,day}
 end if
end if
end function

--End Code Cut--

Thanks for your time.


~~>Joseph Martin
~~>Personal: joe at cyber-wizard.com
~~>Web:  jam.net at poboxes.com
~~>URL: http://users.exis.net/~jam/

new topic     » topic index » view message » categorize

2. Re: My First Program

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 message » categorize

3. Re: My First Program

James Powell wrote:

> 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


Why not factor the rest of the code? This stuff:

> 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

can be replaced with a loop similar to the one you already have, and use the
array of months:

 -- START OF CODE

 -- sum of days that past, not including current month
   daysPast = 0

 -- cycle through the months
for i = 1 to length( maxdays ) do

   -- does the day fall within this month?
   if day <= daysPast + maxdays[i][2] then
      -- save the month
      month = i

      -- calculate the day
      day = day - daysPast

      -- leave the loop
      exit

   else

      -- add the total of prior days
      daysPast = daysPast + maxdays[i][2]

   end if


end for

 -- END OF CODE

I haven't bothered to test it, but you get the idea.

 -- David Cuny

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

Search



Quick Links

User menu

Not signed in.

Misc Menu