home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX05004.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  1.4 KB  |  60 lines

  1. // ex05004.cpp
  2. // Two structures with the same function name
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6.  
  7. // ------  date structure with a function
  8. struct date {
  9.     int month, day, year;
  10.     void display(void);       // a function to display the date
  11. };
  12.  
  13. void date::display()
  14. {
  15.     static char *mon[] = {
  16.         "January","February","March","April","May","June",
  17.         "July","August","September","October","November",
  18.         "December"};
  19.     cout << mon[month] << " " << day << ", " << year;
  20. }
  21.  
  22. // ------  time structure with a function
  23. struct Time {
  24.     int hour, minute, second;
  25.     void display(void);      // a function to display the clock
  26. };
  27.  
  28. void Time::display()
  29. {
  30.     char tmsg[15];
  31.     sprintf(tmsg, "%d:%02d:%02d %s",
  32.         (hour > 12 ? hour - 12 : (hour == 0 ? 12 : hour)),
  33.         minute, second,
  34.         hour < 12 ? "am" : "pm");
  35.     cout << tmsg;
  36. }
  37.  
  38. main()
  39. {
  40.     // -------- get the current time from the OS
  41.     time_t curtime = time((time_t *)NULL);
  42.     struct tm tim = *localtime(&curtime);
  43.     // --------- clock and date structures
  44.     Time now;
  45.     date today;
  46.     // --------- initialize the structures
  47.     now.hour = tim.tm_hour;
  48.     now.minute = tim.tm_min;
  49.     now.second = tim.tm_sec;
  50.     today.month = tim.tm_mon;
  51.     today.day = tim.tm_mday;
  52.     today.year = tim.tm_year+1900;
  53.     // ---------- display the date and time
  54.     cout << "At the tone it will be ";
  55.     now.display();
  56.     cout << " on ";
  57.     today.display();
  58.     cout << '\a';
  59. }
  60.