home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c495 / watcm951.arj / CPPSRC.WPK / CALENDAR.CPP next >
Encoding:
C/C++ Source or Header  |  1993-02-16  |  4.4 KB  |  186 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <dos.h>
  7.  
  8.  
  9. #define FEBRUARY    1
  10. #define NARROW        3
  11. #define WIDE        4
  12.  
  13.  
  14. static int   Jump[ 12 ] =    { 1, 4, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6 };
  15. static int   MonthDays[ 12 ] =    { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  16. static char  *MonthName[ 12 ] = { "January", "February", "March", "April",
  17.                   "May", "June", "July", "August",
  18.                   "September", "October", "November", "December" };
  19. static char  *WideTitle =    { "Sun Mon Tue Wed Thu Fri Sat" };
  20. static char  *NarrowTitle =    { "Su Mo Tu We Th Fr Sa" };
  21.  
  22.  
  23.  
  24. void PosCursor( int row, int col )
  25. /********************************/
  26. {
  27. #ifdef __OS2__
  28.     extern int far pascal   VioSetCurPos( unsigned short, unsigned short,
  29.                             unsigned short );
  30.  
  31.     VioSetCurPos( row, col, 0 );
  32. #else                             
  33.     union REGS            regs;
  34.  
  35.     regs.w.dx = (unsigned short)(( row << 8 ) + col - 0x0101);
  36.     regs.h.bh = 0;
  37.     regs.h.ah = 2;
  38. #ifdef __386__
  39.     int386( 0x10, ®s, ®s );
  40. #else
  41.     int86( 0x10, ®s, ®s );
  42. #endif
  43. #endif
  44. }
  45.  
  46.  
  47. void Line( int row, int col, int width, char left, char centre, char right )
  48. /**************************************************************************/
  49. {
  50.     char            buffer[ 80 ];
  51.  
  52.     buffer[ 0 ] = left;
  53.     memset( &buffer[ 1 ], centre, width );
  54.     buffer[ width + 1 ] = right;
  55.     buffer[ width + 2 ] = '\0';
  56.     PosCursor( row, col );
  57.     printf( buffer );
  58.     fflush( stdout );
  59. }
  60.  
  61.  
  62. void Box( int row, int col, int width, int height )
  63. /*************************************************/
  64. {
  65.     register int        i;
  66.  
  67.     Line( row, col, width, '┌', '─', '┐' );
  68.     Line( row + 1, col, width, '│', ' ', '│' );
  69.     Line( row + 2, col, width, '├', '─', '┤' );
  70.     for( i = 3; i <= height; ++i ) {
  71.     Line( row + i, col, width, '│', ' ', '│' );
  72.     }
  73.     Line( row + height + 1, col, width, '└', '─', '┘' );
  74. }
  75.  
  76. void Calendar( int month, int year, int row, int col, int width, char *title )
  77. /****************************************************************************/
  78. {
  79.     register int        start;
  80.     register int        days;
  81.     register int        box_width;
  82.     register char        *str;
  83.     register int        i;
  84.  
  85.     box_width = 7 * width - 1;
  86.     Box( row, col, box_width, 8 );
  87.     str = MonthName[ month ];
  88.     PosCursor( row - 1, col + 1 + ( box_width - strlen( str ) - 5 ) / 2 );
  89.     printf( "%s 19%d\n", str, year );
  90.     fflush( stdout );
  91.     PosCursor( row + 1, col + 1 );
  92.     printf( title );
  93.     fflush( stdout );
  94.  
  95.     start = year + year / 4 + Jump[ month ];
  96.     if( ( year % 4 == 0 ) && ( month <= FEBRUARY ) ) {
  97.     --start;
  98.     }
  99.     start = start % 7 + 1;
  100.     if( ( year % 4 == 0 ) && ( month == FEBRUARY ) ) {
  101.     days = 29;
  102.     } else {
  103.     days = MonthDays[ month ];
  104.     }
  105.     row += 3;
  106.     for( i = 1; i <= days; ++i ) {
  107.     PosCursor( row, col + width * start - 2 );
  108.     printf( "%2d", i );
  109.     fflush( stdout );
  110.     if( start == 7 ) {
  111.         printf( "\n" );
  112.         fflush( stdout );
  113.         ++row;
  114.         start = 1;
  115.     } else {
  116.         ++start;
  117.     }
  118.     }
  119. }
  120.  
  121.  
  122. void ClearScreen()
  123. /****************/
  124.  
  125. {
  126. #ifdef __OS2__
  127.     extern int far pascal   VioWrtNChar( char far *, unsigned short,
  128.                             unsigned short, unsigned short, unsigned short );
  129.  
  130.     VioWrtNChar( " ", 2000, 0, 0, 0 );                        
  131. #else
  132.     union REGS            regs;
  133.  
  134.     regs.w.cx = 0;
  135.     regs.w.dx = 0x1850;
  136.     regs.h.bh = 7;
  137.     regs.w.ax = 0x0600;
  138. #ifdef __386__
  139.     int386( 0x10, ®s, ®s );
  140. #else
  141.     int86( 0x10, ®s, ®s );
  142. #endif
  143. #endif
  144. }
  145.  
  146. int main()
  147. /********/
  148.  
  149. {
  150.     time_t            curr_time;
  151.     register struct tm        *tyme;
  152.  
  153.     ClearScreen();
  154.  
  155.     /* get today's date */
  156.  
  157.     curr_time = time( NULL );
  158.     tyme = localtime( &curr_time );
  159.  
  160.     /* draw calendar for this month */
  161.  
  162.     Calendar( tyme->tm_mon, tyme->tm_year, 10, 26, WIDE, WideTitle );
  163.  
  164.     /* draw calendar for last month */
  165.  
  166.     tyme->tm_mon--;
  167.     if( tyme->tm_mon < 0 ) {
  168.     tyme->tm_mon = 11;
  169.     tyme->tm_year--;
  170.     }
  171.     Calendar( tyme->tm_mon, tyme->tm_year, 5, 3, NARROW, NarrowTitle );
  172.  
  173.     /* draw calendar for next month */
  174.  
  175.     tyme->tm_mon += 2;
  176.     if( tyme->tm_mon > 11 ) {
  177.     tyme->tm_mon -= 12;
  178.     tyme->tm_year++;
  179.     }
  180.     Calendar( tyme->tm_mon, tyme->tm_year, 5, 56, NARROW, NarrowTitle );
  181.  
  182.     PosCursor( 20, 1 );
  183.     return( 0 );
  184. }
  185.  
  186.