« Event 3: Instant (Runoff) Winner | Main | Event 5: You Call that a Strong Password? »

Event 4: Image is Everything

Here is my submission for the Advanced Event 4 in the Microsoft Scripting Games 2008.

   1: '*********************************************************************

   2: ' Script Name: Event4.vbs

   3: '     Version: 1.1

   4: '      Author: Perry Harris (PHactotum)

   5: '     Updated: 1:30 AM Friday, February 22, 2008

   6: '     Purpose: Solves the 2008 winter Scripting Games Advance Event 4: Image is Everything

   7: '              

   8: '       Usage: cscript Event4.vbs

   9: '       Notes:

  10: '    Keywords: 

  11: '  versioning: 1.0  Original release

  12: '              1.1  Modified to put lines in our calendar using + - and |

  13: '*********************************************************************

  14: Option Explicit

  15: Dim intYear, i, intMonth

  16: Dim strText

  17: Dim StartDate, EndDate, CurrDate

  18: Dim intLeft, intRight

  19: Dim a

  20:  

  21: 'Get the desired Month and Year for the Calendar

  22: WScript.StdOut.Write "Please enter Month/Year using numeric digits: "

  23: strText = WScript.StdIn.ReadLine

  24:  

  25: 'Assuming the user used a / as the separator

  26: a = split(strText,"/")

  27: intMonth = a(0)

  28: intYear = a(1)

  29:  

  30: 'Start our Calendar on the first day of the given month and year

  31: StartDate = DateSerial(intYear,intMonth,1)

  32: EndDate = DateSerial(Year(DateAdd("m",1,StartDate)),Month(DateAdd("m",1,StartDate)),1)

  33:  

  34: 'Write Top border

  35: WScript.StdOut.Write chr(43)

  36: WScript.StdOut.Write string(41,chr(45))

  37: WScript.StdOut.WriteLine chr(43)

  38:  

  39: 'Write out our Month and Year

  40: WScript.StdOut.Write chr(124)

  41: intLeft = (41 - Len(MonthName(intMonth) & " " & Year(StartDate))) /2

  42: intRight = 41 - Len(MonthName(intMonth) & " " & Year(StartDate)) - intLeft

  43: WScript.StdOut.Write space(intLeft)

  44: WScript.StdOut.Write MonthName(intMonth) & " " & Year(StartDate)

  45: WScript.StdOut.Write space(intRight)

  46: WScript.StdOut.WriteLine chr(124)

  47: WScript.StdOut.WriteLine chr(124) & space(41) & chr(124)

  48:  

  49: 'Yes, we could have used a function for these three lines seeing as we use them repeatedly

  50: WScript.StdOut.Write chr(43)

  51: WScript.StdOut.Write string(41,chr(45))

  52: WScript.StdOut.WriteLine chr(43)

  53:  

  54:  

  55: 'Write out our days of the week tab separated and in abbreviated form

  56: For i = 1 to 7

  57:     WScript.StdOut.Write chr(124) & space(1) & WeekDayName(i,True) & space(1)

  58: next

  59: WScript.StdOut.WriteLine chr(124)

  60: 'And again these three lines

  61: WScript.StdOut.Write chr(43)

  62: WScript.StdOut.Write string(41,chr(45))

  63: WScript.StdOut.WriteLine chr(43)

  64:  

  65:  

  66:  

  67: 'Pad the first week

  68: i = 0

  69: While i < Weekday(StartDate) - 1

  70:     WScript.StdOut.Write chr(124) & space(5)

  71:     i = i + 1

  72: wend

  73:  

  74: 'Print The Days

  75: CurrDate = StartDate

  76: While CurrDate < EndDate    

  77:     If Weekday(CurrDate) = 7 then 

  78:         WScript.StdOut.WriteLine Chr(124) & LeftPad(Day(CurrDate),4) & space(1) & chr(124)

  79:     else

  80:         WScript.StdOut.Write chr(124) & LeftPad(day(CurrDate),4) & space(1)

  81:     end if

  82:     CurrDate = DateAdd("d",1,CurrDate)

  83: Wend

  84:  

  85: 'Pad the last week

  86: If Weekday(CurrDate) > 1 then ' If we are at 1, we don't want to print a blank row

  87: While Weekday(CurrDate) < 7

  88:         WScript.StdOut.Write chr(124) & space(5)

  89:     CurrDate = DateAdd("d",1,CurrDate)

  90: wend

  91: WScript.StdOut.WriteLine chr(124) & space(5) & chr(124)

  92: end if

  93:  

  94: 'One more time

  95: WScript.StdOut.Write chr(43)

  96: WScript.StdOut.Write string(41,chr(45))

  97: WScript.StdOut.WriteLine chr(43)

  98:  

  99:  

 100:  

 101: 'This is used to right justify the dates under the three letter abbreviations for the days of the week.

 102: Function LeftPad(strText,intWidth)

 103:     LeftPad = Space(intWidth - Len(strText)) & strText

 104: end function


 

I spent a little extra time on this one.  I had a calendar that looked just like their example:



C:\scripts>cscript Event4-1.vbs
Please enter Month/Year using numeric digits: 2/2008
February 2008

Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29
C:\scripts>


 

But I thought I'd like to take it up a notch.  I like calendars with grid lines:

C:\scripts>cscript Event4.vbs
Please enter Month/Year using numeric digits: 2/2008
+-----------------------------------------+
|              February 2008              |
|                                         |
+-----------------------------------------+
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
+-----------------------------------------+
|     |     |     |     |     |   1 |   2 |
|   3 |   4 |   5 |   6 |   7 |   8 |   9 |
|  10 |  11 |  12 |  13 |  14 |  15 |  16 |
|  17 |  18 |  19 |  20 |  21 |  22 |  23 |
|  24 |  25 |  26 |  27 |  28 |  29 |     |
+-----------------------------------------+

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on February 22, 2008 9:52 AM.

The previous post in this blog was Event 3: Instant (Runoff) Winner.

The next post in this blog is Event 5: You Call that a Strong Password?.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 4.1