Source section

This example shows the source section in a C++ compiler listing.

Table 1. Source section in a C++ listing
 
15650ZOS V2.3 z/OS XL C++         //'CBC.SCCNSAM(CCNUBRC)'                    02/23/2017 19:58:18                                    
                                                                                                           
                                      * * * * *   S O U R C E   * * * * *                                                                                                                                                                                         
            1 | //                                                                                                                   
            2 | // Sample Program: Biorhythm                                                                                         
            3 | // Description   : Calculates biorhythm based on the current                                                         
            4 | //                 system date and birth date entered                                                                
            5 | //                                                                                                                   
            6 | // File 2 of 2-other file is CCNUBRH                                                                                 
            7 |                                                                                                                      
            8 | #include <stdio.h>                                                                                                   
            9 | #include <string.h>                                                                                                  
           10 | #include <math.h>                                                                                                    
           11 | #include <time.h>                                                                                                    
           12 | #include <iostream>                                                                                                  
           13 | #include <iomanip>                                                                                                   
           14 |                                                                                                                      
           15 | #include "ccnubrh.h"  //BioRhythm class and Date class                                                               
           16 | using namespace std;                                                                                                 
           17 | static ostream& operator << (ostream&, BioRhythm&);                                                                  
           18 |                                                                                                                      
           19 |                                                                                                                      
           20 | int main(void) {                                                                                                     
           21 |                                                                                                                      
           22 |   BioRhythm bio;                                                                                                     
           23 |   int code;                                                                                                          
           24 |                                                                                                                      
           25 |   if (!bio.ok()) {                                                                                                   
           26 |     cerr << "Error in birthdate specification - format is yyyy/mm/dd";                                               
           27 |     code = 8;                                                                                                        
           28 |   }                                                                                                                  
           29 |   else {                                                                                                             
           30 |     cout << bio;  // write out birthdate for bio                                                                     
           31 |     code = 0;                                                                                                        
           32 |   }                                                                                                                  
           33 |   return(code);                                                                                                      
           34 | }                                                                                                                    
           35 |                                                                                                                      
           36 | const int Date::dateLen ;                                                                                            
           37 | const int Date::numMonths;                                                                                           
           38 | const int Date::numDays[Date::numMonths] = {                                                                         
           39 |   31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31                                                                     
           40 |     };                                                                                                               
           41 |                                                                                                                      
           42 | const int BioRhythm::pCycle;                                                                                         
           43 | const int BioRhythm::eCycle;                                                                                         
           44 | const int BioRhythm::iCycle;                                                                                         
           45 |                                                                                                                      
           46 | ostream& operator<<(ostream& os, BioRhythm& bio) {                                                                   
           47 |   os << "Total Days  : " << bio.AgeInDays() << "\n";                                                                 
           48 |   os << "Physical    : " << bio.Physical() << "\n";                                                                  
           49 |   os << "Emotional   : " << bio.Emotional() << "\n";                                                                 
           50 |   os << "Intellectual: " << bio.Intellectual() << "\n";                                                              
           51 |                                                                                                                      
           52 | return(os);                                                                                                          
           53 | }                                                                                                                    
           54 |                                                                                                                      
           55 | Date::Date() {                                                                                                       
           56 |   time_t lTime;                                                                                                      
           57 |   struct tm *newTime;                                                                                                
           58 |                                                                                                                      
           59 |   time(&lTime);                                                                                                      
           60 |   newTime = localtime(&lTime);                                                                                       
           61 |   cout << "local time is " << asctime(newTime) << endl;                                                              
           62 |                                                                                                                      
           63 |   curYear = newTime->tm_year + 1900;                                                                                 
           64 |   curDay  = newTime->tm_yday + 1;                                                                                    
           65 | }                                                                                                                    
           66 |                                                                                                                      
           67 | BirthDate::BirthDate(const char *birthText) {                                                                        
           68 |   strcpy(text, birthText);                                                                                           
           69 | }                                                                                                                    
           70 |                                                                                                                      
           71 | BirthDate::BirthDate() {                                                                                             
           72 |   cout << "Please enter your birthdate in the form yyyy/mm/dd\n";                                                    
           73 |   cin >> setw(dateLen+1) >> text;                                                                                    
           74 | }                                                                                                                    
           
           75 |                                                                                                                      
           76 | Date::DaysSince(const char *text) {                                                                                  
           77 |                                                                                                                      
           78 |   int year, month, day, totDays, delim;                                                                              
           79 |   int daysInYear = 0;                                                                                                
           80 |   int i;                                                                                                             
           81 |   int leap = 0;                                                                                                      
           82 |                                                                                                                      
           83 |   int rc = sscanf(text, "%4d%c%2d%c%2d",                                                                             
           84 |                   &year, &delim, &month, &delim, &day);                                                              
           85 |   --month;                                                                                                           
           86 |   if (rc != 5 || year  < 0 || year  > 9999 ||                                                                        
           87 |                  month < 0 || month >   11 ||                                                                        
           88 |                  day   < 1 || day   >   31 ||                                                                        
           89 |                 (day   > numDays[month]&& month != 1)) {                                                             
           90 |     return(-1);                                                                                                      
           91 |   }                                                                                                                  
           92 |   if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)                                                         
           93 |      leap = 1;                                                                                                       
           94 |                                                                                                                      
           95 |   if (month == 1 && day > numDays[month]) {                                                                          
           96 |     if (day > 29)                                                                                                    
           97 |       return(-1);                                                                                                    
           98 |     else if (!leap)                                                                                                  
           99 |       return (-1);                                                                                                   
          100 |   }                                                                                                                  
          101 |                                                                                                                      
          102 |   for (i=0;i<month;++i) {                                                                                            
          103 |     daysInYear += numDays[i];                                                                                        
          104 |   }                                                                                                                  
          105 |   daysInYear += day;                                                                                                 
          106 |                                                                                                                      
          107 |   // correct for leap year                                                                                           
          108 |   if (leap == 1 &&                                                                                                   
          109 |      (month > 1 || (month == 1 && day == 29)))                                                                       
          110 |      ++daysInYear;                                                                                                   
          111 |                                                                                                                      
          112 |   totDays = (curDay - daysInYear) + (curYear - year)*365;                                                            
          113 |                                                                                                                      
          114 |   // now, correct for leap year                                                                                      
          115 |   for (i=year+1; i < curYear; ++i) {                                                                                 
          116 |     if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {                                                              
          117 |       ++totDays;                                                                                                     
          118 |     }                                                                                                                
          119 |   }                                                                                                                  
          120 |   return(totDays);                                                                                                   
          121 | }                                                                                                                    
                                                                                                                                     
                                   * * * * *   E N D   O F   S O U R C E   * * * * *