home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c500 / 1.ddi / SRC386.WPK / CALENDAR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-28  |  4.5 KB  |  207 lines

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