home *** CD-ROM | disk | FTP | other *** search
- /* EasyCODE(C++) V5.1 01.03.1995 08:03:12
- EasyCODE(C++) sample: Write current date, date of yesterday and of tomorrow. */
- /* EasyCODE O
- If=horizontal
- LevelNumbers=no
- LineNumbers=no
- ScreenFont=Courier,,100,9217,-13,0,400,0,0,0,0,0,0,1,2,1,49
- PrinterFont=Courier,,100,2,-42,0,400,0,0,0,0,0,0,2,1,2,49
- LastLevelId=9 */
-
- /* EasyCODE ( 1
- Date-Class Application */
- #include <iostream.h>
- #include <stdlib.h>
- #include <time.h>
-
- /* EasyCODE ( 2
- Date */
-
- /* EasyCODE C */
- class Date
- {
- short day;
- short month;
- static char *nameofmonth[];
- short year;
- void check();
- public:
- void set (short day=-1, short mon=-1, short year=-1);
-
- /* EasyCODE ( 3
- get_day */
-
- /* EasyCODE F */
- short get_day()
- {
- return day;
- }
- /* EasyCODE ) */
-
- /* EasyCODE ( 4
- get_month */
-
- /* EasyCODE F */
- const char *get_month()
- {
- return nameofmonth[month-1];
- }
- /* EasyCODE ) */
-
- /* EasyCODE ( 5
- get_year */
-
- /* EasyCODE F */
- short get_year()
- {
- return year;
- }
- /* EasyCODE ) */
- void print();
- };
- /* EasyCODE E */
- /* EasyCODE ) */
- /* EasyCODE < */
- // define and initialize global class variable
- char* Date::nameofmonth[] = {"January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December"};
- /* EasyCODE > */
-
- /* EasyCODE ( 6
- Date::set */
-
- /* EasyCODE F */
- void Date::set(short d, short m, short y)
- {
- time_t seconds = time((time_t *)0);
-
- struct tm *ptm = localtime(&seconds);
-
- year = (y > 0) ? y : ptm->tm_year + 1900;
- month = (m > -1) ? m : ptm->tm_mon + 1;
- day = (d > -1) ? d : ptm->tm_mday;
-
- check();
- }
- /* EasyCODE ) */
-
- /* EasyCODE ( 7
- Date::print */
-
- /* EasyCODE F */
- void Date::print()
- {
- cout << day << ". ";
- cout << nameofmonth[month-1] << " ";
- cout << year << "\n";
- }
- /* EasyCODE ) */
-
- /* EasyCODE ( 8
- Date::check */
-
- /* EasyCODE F */
- void Date::check() // check year, month and day and correct if necessary'
- {
- /* EasyCODE < */
- static struct tm tdata = {0};
-
- // write year, month and day into structure
- tdata.tm_year = year - 1900;
- tdata.tm_mon = month - 1;
- tdata.tm_mday = day;
- tdata.tm_hour = 12; // noon
-
- // check date, calculate day of the week
- time_t seconds = mktime (&tdata);
- /* EasyCODE > */
- if (seconds > -1)
- {
- year = tdata.tm_year + 1900;
- month = tdata.tm_mon + 1;
- day = tdata.tm_mday;
- }
- }
- /* EasyCODE ) */
-
- /* EasyCODE ( 9
- Main program */
-
- /* EasyCODE F */
- void main()
- {
- Date current_date, date_yesterday, date_tomorrow;
-
- // set current date
- current_date.set();
-
- // set yesterday's date
- date_yesterday.set(current_date.get_day() - 1);
-
- // set tomorrow's date
- date_tomorrow.set(current_date.get_day() + 1);
-
-
- cout << "Today: \d";
- current_date.print();
- cout << "Yesterday:\d";
- date_yesterday.print();
- cout << "Tomorrow: \d";
- date_tomorrow.print();
- }
- /* EasyCODE ) */
- /* EasyCODE ) */
-