XL C++ program examples — CCNUBRH and CCNUBRC
The examples contain two files. File CCNUBRH contains the classes that are used in the main program. File CCNUBRC contains the remaining source code. The example files CCNUBRC and CCNUBRH are shipped with the z/OS® XL C++ compiler in data sets CBC.SCCNSAM(CCNUBRC) and CBC.SCCNSAM(CCNUBRH).
CCNUBRH
The CCNUBRH example shows a z/OS XL C++ program that prompts you to enter a birth date. The program output is the corresponding biorhythm chart. The following example shows the header file for the biorhythm example.
//
// Sample Program: Biorhythm
// Description : Calculates biorhythm based on the current
// system date and birth date entered
//
// File 1 of 2-other file is CCNUBRC
class Date {
public:
Date();
int DaysSince(const char *date);
protected:
int curYear, curDay;
static const int dateLen = 10;
static const int numMonths = 12;
static const int numDays[];
};
class BirthDate : public Date {
public:
BirthDate();
BirthDate(const char *birthText);
int DaysOld() { return(DaysSince(text)); }
private:
char text[Date::dateLen+1];
};
class BioRhythm {
public:
BioRhythm(char *birthText) : birthDate(birthText) {
age = birthDate.DaysOld();
}
BioRhythm() : birthDate() {
age = birthDate.DaysOld();
}
~BioRhythm() {}
int AgeInDays() {
return(age);
}
double Physical() {
return(Cycle(pCycle));
}
double Emotional() {
return(Cycle(eCycle));
}
double Intellectual() {
return(Cycle(iCycle));
}
int ok() {
return(age >= 0);
}
private:
int age;
double Cycle(int phase) {
return(sin(fmod((double)age, (double)phase) / phase * M_2PI));
}
BirthDate birthDate;
static const int pCycle=23; // Physical cycle - 23 days
static const int eCycle=28; // Emotional cycle - 28 days
static const int iCycle=33; // Intellectual cycle - 33 days
};
The program is written using an object-oriented method. A class that is called
BioRhythm is defined. It contains an object birthDate of class
BirthDate, which is derived from the class Date. An object that is
called bio of the class BioRhythm is declared.
CCNUBRC
This is the z/OS XL C++ biorhythm example program.
//
// Sample Program: Biorhythm
// Description : Calculates biorhythm based on the current
// system date and birth date entered
//
// File 2 of 2-other file is CCNUBRH
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <iomanip>
#include "ccnubrh.h" //BioRhythm class and Date class
using namespace std;
static ostream& operator << (ostream&, BioRhythm&);
int main(void) {
BioRhythm bio;
int code;
if (!bio.ok()) {
cerr << "Error in birthdate specification - format is yyyy/mm/dd";
code = 8;
}
else {
cout << bio; // write out birthdate for bio
code = 0;
}
return(code);
}
const int Date::dateLen ;
const int Date::numMonths;
const int Date::numDays[Date::numMonths] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
const int BioRhythm::pCycle;
const int BioRhythm::eCycle;
const int BioRhythm::iCycle;
ostream& operator<<(ostream& os, BioRhythm& bio) {
os << "Total Days : " << bio.AgeInDays() << "\n";
os << "Physical : " << bio.Physical() << "\n";
os << "Emotional : " << bio.Emotional() << "\n";
os << "Intellectual: " << bio.Intellectual() << "\n";
return(os);
}
Date::Date() {
time_t lTime;
struct tm *newTime;
time(&lTime);
newTime = localtime(&lTime);
cout << "local time is " << asctime(newTime) << endl;
curYear = newTime->tm_year + 1900;
curDay = newTime->tm_yday + 1;
}
BirthDate::BirthDate(const char *birthText) {
strcpy(text, birthText);
}
BirthDate::BirthDate() {
cout << "Please enter your birthdate in the form yyyy/mm/dd\n";
cin >> setw(dateLen+1) >> text;
}
Date::DaysSince(const char *text) {
int year, month, day, totDays, delim;
int daysInYear = 0;
int i;
int leap = 0;
int rc = sscanf(text, "%4d%c%2d%c%2d",
&year, &delim, &month, &delim, &day);
--month;
if (rc != 5 || year < 0 || year > 9999 ||
month < 0 || month > 11 ||
day < 1 || day > 31 ||
(day > numDays[month]&& month != 1)) {
return(-1);
}
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
leap = 1;
if (month == 1 && day > numDays[month]) {
if (day > 29)
return(-1);
else if (!leap)
return (-1);
}
for (i=0;i<month;++i) {
daysInYear += numDays[i];
}
daysInYear += day;
// correct for leap year
if (leap == 1 &&
(month > 1 || (month == 1 && day == 29)))
++daysInYear;
totDays = (curDay - daysInYear) + (curYear - year)*365;
// now, correct for leap year
for (i=year+1; i < curYear; ++i) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
++totDays;
}
}
return(totDays);
}
If you need more details on the constructs of the z/OS XL C++ language, see z/OS XL C/C++ Language Reference or z/OS XL C/C++ Runtime Library Reference.