home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l196 / 3.ddi / CAL.BA$ / CAL.bin
Encoding:
Text File  |  1990-06-24  |  5.0 KB  |  175 lines

  1. DEFINT A-Z      ' Default variable type is integer.
  2.  
  3. ' Define a data type for the names of the months and the
  4. ' number of days in each:
  5. TYPE MonthType
  6.     Number AS INTEGER  ' Number of days in the month
  7.     MName AS STRING * 9   ' Name  of the month
  8. END TYPE
  9.  
  10. ' Declare procedures used:
  11. DECLARE FUNCTION IsLeapYear% (N%)
  12. DECLARE FUNCTION GetInput% (Prompt$, Row%, LowVal%, HighVal%)
  13.  
  14. DECLARE SUB PrintCalendar (Year%, Month%)
  15. DECLARE SUB ComputeMonth (Year%, Month%, StartDay%, TotalDays%)
  16.  
  17. DIM MonthData(1 TO 12)   AS MonthType
  18.  
  19. ' Initialize month definitions from DATA statements below:
  20. FOR I = 1 TO 12
  21.     READ MonthData(I).MName, MonthData(I).Number
  22. NEXT
  23.  
  24. ' Main loop, repeat for as many months as desired:
  25. DO
  26.     CLS
  27.  
  28.     ' Get year and month as input:
  29.     Year = GetInput("Year (1899 to 2099): ", 1, 1899, 2099)
  30.     Month = GetInput("Month (1 to 12): ", 2, 1, 12)
  31.  
  32.     ' Print the calendar:
  33.     PrintCalendar Year, Month
  34. ' Another Date?
  35.     LOCATE 13, 1      ' Locate in 13th row, 1st column.
  36.     PRINT "New Date? ";  ' Keep cursor on same line.
  37.     LOCATE , , 1, 0, 13  ' Turn cursor on and make it one
  38.             ' character high.
  39.     Resp$ = INPUT$(1) ' Wait for a key press.
  40.     PRINT Resp$    ' Print  the key  pressed.
  41.  
  42. LOOP WHILE UCASE$(Resp$) = "Y"
  43. END
  44.  
  45. ' Data for the months of a year:
  46. DATA January, 31, February, 28,  March, 31
  47. DATA April, 30,   May, 31, June, 30, July, 31, August, 31
  48. DATA September,   30, October, 31, November, 30, December, 31
  49.  
  50. ' ====================== COMPUTEMONTH =====================
  51. '  Computes the first day and the total days in a month
  52. ' =========================================================
  53. '
  54. SUB ComputeMonth (Year, Month, StartDay, TotalDays) STATIC
  55.     SHARED MonthData() AS MonthType
  56.  
  57.     CONST LEAP = 366 MOD 7
  58.     CONST NORMAL = 365 MOD 7
  59.  
  60.     ' Calculate total number of days (NumDays) since 1/1/1899:
  61.  
  62.     ' Start with whole years:
  63.     NumDays = 0
  64.     FOR I = 1899 TO Year - 1
  65.         IF IsLeapYear(I) THEN              ' If leap year,
  66.             NumDays = NumDays + LEAP   ' add 366 MOD 7.
  67.         ELSE                               ' If normal year,
  68.             NumDays = NumDays + NORMAL ' add 365 MOD 7.
  69.         END IF
  70.     NEXT
  71.  
  72.     ' Next, add in days from whole months:
  73.     FOR I = 1 TO Month - 1
  74.         NumDays = NumDays + MonthData(I).Number
  75.     NEXT
  76.  
  77.     ' Set the number of days in the requested month:
  78.     TotalDays = MonthData(Month).Number
  79.  
  80.     ' Compensate if requested year is a leap year:
  81.     IF IsLeapYear(Year) THEN
  82.  
  83.         ' If after February, add one to total days:
  84.         IF Month > 2 THEN
  85.             NumDays = NumDays + 1
  86.  
  87.         ' If February, add one to the month's days:
  88.         ELSEIF Month = 2 THEN
  89.             TotalDays = TotalDays + 1
  90.         END IF
  91.     END IF
  92.  
  93.     ' 1/1/1899 was a Sunday, so calculating "NumDays MOD 7"
  94.     ' gives the day of week (Sunday = 0, Monday = 1, Tuesday
  95.     ' = 2, and so on) for the first day of the input month:
  96.     StartDay = NumDays MOD 7
  97. END SUB
  98.  
  99. ' ======================== GETINPUT =======================
  100. '  Prompts for input, then tests for a valid range
  101. ' =========================================================
  102. '
  103. FUNCTION GetInput (Prompt$, Row, LowVal, HighVal) STATIC
  104.  
  105.     ' Locate prompt at specified row, turn cursor on and
  106.     ' make it one character high:
  107.     LOCATE Row, 1, 1, 0, 13
  108.     PRINT Prompt$;
  109.  
  110.     ' Save column position:
  111.     Column = POS(0)
  112.  
  113.     ' Input value until it's within range:
  114.     DO
  115.         LOCATE Row, Column   ' Locate cursor at end of prompt.
  116.         PRINT SPACE$(10)     ' Erase anything already there.
  117.         LOCATE Row, Column   ' Relocate cursor at end of prompt.
  118.         INPUT "", Value      ' Input value with no prompt.
  119.     LOOP WHILE (Value < LowVal OR Value > HighVal)
  120.  
  121.     ' Return valid input as value of function:
  122.     GetInput = Value
  123.  
  124. END FUNCTION
  125.  
  126. ' ====================== ISLEAPYEAR =======================
  127. '   Determines if a year is a leap year or not
  128. ' =========================================================
  129. '
  130. FUNCTION IsLeapYear (N) STATIC
  131.  
  132.     ' If the year is evenly divisible by 4 and not divisible
  133.     ' by 100, or if the year is evenly divisible by 400,
  134.     ' then it's a leap year:
  135.     IsLeapYear = (N MOD 4 = 0 AND N MOD 100 <> 0) OR (N MOD 400 = 0)
  136. END FUNCTION
  137.  
  138. ' ===================== PRINTCALENDAR =====================
  139. '   Prints a formatted calendar given the year and month
  140. ' =========================================================
  141. '
  142. SUB PrintCalendar (Year, Month) STATIC
  143. SHARED MonthData() AS MonthType
  144.  
  145.     ' Compute starting day (Su M Tu ...)
  146.     ' and total days for the month:
  147.     ComputeMonth Year, Month, StartDay, TotalDays
  148.     CLS
  149.     Header$ = RTRIM$(MonthData(Month).MName) + "," + STR$(Year)
  150.  
  151.     ' Calculate location for centering month and year:
  152.     LeftMargin = (35 - LEN(Header$)) \ 2
  153. ' Print header:
  154.     PRINT TAB(LeftMargin); Header$
  155.     PRINT
  156.     PRINT "Su    M   Tu    W   Th    F   Sa"
  157.     PRINT
  158.  
  159.     ' Recalculate and print tab
  160.     ' to the first day of the month (Su M Tu ...):
  161.     LeftMargin = 5 * StartDay + 1
  162.     PRINT TAB(LeftMargin);
  163.  
  164.     ' Print out the days of the month:
  165.     FOR I = 1 TO TotalDays
  166.         PRINT USING "##_   "; I;
  167.  
  168.         ' Advance to the next line
  169.         ' when the cursor is past column 32:
  170.         IF POS(0) > 32 THEN PRINT
  171.     NEXT
  172.  
  173. END SUB
  174.  
  175.