C++ Program 1

profiletheman04
program_1.zip

Program 1/~$AD FOR WHAT TO DO.docx

Program 1/cityinfo.txt

APN 45.07 83.57 E ATL 33.65 84.42 E DCA 38.85 77.03 E DEN 39.75 104.87 M DFW 32.90 97.03 C DTW 42.23 83.33 E GRR 42.88 85.52 E JFK 40.65 73.78 E LAF 40.42 86.93 E LAN 42.77 84.60 E LAX 33.93 118.40 P MBS 43.53 84.08 E MIA 25.82 80.28 E MQT 46.53 87.55 E ORD 41.98 87.90 C SSM 46.47 84.37 E TVC 44.73 85.58 E YYZ 43.67 79.63 E

Program 1/datefun.cpp

Program 1/datefun.cpp

// This file includes implementations for date functions

#include   < iostream >
#include   < string >
using   namespace  std ;

#include   "datefun.h"

//-----------------------------------------------------------------  
// This function receives an integer year and returns true if the
// year is a leap year and false otherwise.

bool  leapYear ( int  theYear )
{
   if   (  theYear  %   400   ==   0   ||
      (  theYear  %   4   ==   0   &&  theYear  %   100   !=   0   )   )
      return   true ;
   else
      return   false ;
     
}    // end function leapYear

//-----------------------------------------------------------------  
// This function receives an integer month and year and returns an 
// integer of the number of days in the month.  Leap years are
// considered.

int   DaysInMonth ( int  theMonth ,   int  theYear )
{
   int  days  =   0 ;
  
   // 31 Day theMonths
   if   ( theMonth  ==   1   ||  theMonth  ==   3   ||  theMonth  ==   5    ||
      theMonth  ==   7   ||  theMonth  ==   8   ||  theMonth  ==   10   ||
      theMonth  ==   12   )  
         days  =   31 ;
            
   // 30 Day theMonths
   else   if   ( theMonth  ==   4   ||  theMonth  ==   6   ||  
           theMonth  ==   9   ||  theMonth  ==   11   )  
         days  =   30 ;
            
   // February
   else    // theMonth == 2
      if   (  leapYear ( theYear )   )
        days  =   29 ;
      else  
        days  =   28 ;  

   return  days ;
    
}    // end function DaysInMonth

//-----------------------------------------------------------------  
// This function receives a valid calendar date and returns the Julian
// date (the day number of the date in that year).

int  julianDate ( int  theMonth ,   int  theDay ,   int  theYear )
{
   int  dayCnt  =   0 ;
   int  mon ;
   for   ( mon  =   1 ;  mon  <  theMonth ;  mon ++ )             
     dayCnt  +=   DaysInMonth ( mon , theYear );         
  dayCnt  +=  theDay ;
  
   return  dayCnt ;
  
}    // end function julianDate

//-----------------------------------------------------------------  
// This function validates a calendar date and returns 'true' if
// all three components  represent a valid date and 'false' otherwise

bool  validDate ( int  mon ,   int  day ,   int  yr )
{
    bool  valDate  =   true ;     // Assume a good date
   
    // Test for conditions that would make the date validity false
    if   ( yr  <   1900 )
      valDate  =   false ;
    if   (( mon  <   1 )   ||   ( mon  >   12 )   ||   ( day  <   1 )   ||   ( day  >   31 ))
      valDate  =   false ;
    else   if   ((( mon  ==   4 )   ||   ( mon  ==   6 )   ||   ( mon  ==   9 )   ||   ( mon  ==   11 ))   &&   ( day  ==   31 ))
      valDate  =   false ;
    else   if   (( mon  ==   2 )    &&  leapYear ( yr )   &&   ( day  >   29 ))
     valDate  =   false ;
    else   if   (( mon  ==   2 )    &&   !  leapYear ( yr )   &&   ( day  >   28 ))
     valDate  =   false ;
      
   return  valDate ;
}    // end function validDate
   
//-----------------------------------------------------------------  
// This function receives a valid date and returns a date code for the
// day of the week.  It counts the number of days since 1/1/1900
// which was on a Sunday.  Output is: 0=Sun,1=Mon, ..., 6=Sat.

int  weekDay ( int  mon ,   int  day ,   int  year )
{         
     int   DayCnt ;
     int  daynum , i ;

     DayCnt   =   ( year  -   1900 )   *   365 ;     
     DayCnt   +=   (( year  -   1900 )   /   4 )   +   1 ;
     for   ( i = 1 ; i <= mon - 1 ; i ++ )
     switch ( i )
     {
         case   2 :       DayCnt   += 28 ;   break ;
         case   4 :
         case   6 :
         case   9 :
         case   11 :       DayCnt   += 30 ;   break ;
         default :      DayCnt   += 31 ;  
     };
    
     if   ((( year  -   1900 )   %   4   ==   0 )   &&   ( mon  <=   2 ))
         DayCnt -- ;
     DayCnt   +=  day ;
    daynum  =   ( DayCnt   -   1 )   %   7 ;
    
     return  daynum ;
}   // end function weekDay 
   
//-----------------------------------------------------------------  
// This function receives a day code and returns the string (2nd
// parametere with the three-character day of the week descriptor
// (0=Sun,1=Mon, ..., 6=Sat)
void  dayCode ( int  code ,   char  descript [])
{         
      switch ( code )
      {
           case   0 :  strcpy ( descript ,   "SUN" );   break ;
           case   1 :  strcpy ( descript ,   "MON" );   break ;
           case   2 :  strcpy ( descript ,   "TUE" );   break ;
           case   3 :  strcpy ( descript ,   "WED" );   break ;
           case   4 :  strcpy ( descript ,   "THU" );   break ;
           case   5 :  strcpy ( descript ,   "FRI" );   break ;
           case   6 :  strcpy ( descript ,   "SAT" );   break ;
      };   // end switch
}

Program 1/datefun.h

// These functions include various date manipulation algorithms //----------------------------------------------------------------- // This function receives an integer year and returns true if the // year is a leap year and false otherwise. bool leapYear(int theYear); //----------------------------------------------------------------- // This function receives an integer month and year and returns an // integer of the number of days in the month. Leap years are // considered. int DaysInMonth(int theMonth, int theYear); //----------------------------------------------------------------- // This function receives a valid calendar date and returns the Julian // date (the day number of the date in that year). int julianDate(int theMonth, int theDay, int theYear); //----------------------------------------------------------------- // This method receives a calendar date and returns a boolean value // defining the validity of the date. bool validDate(int mon, int day, int yr); //----------------------------------------------------------------- // This function receives a valid date and returns a date code for the // day of the week. It counts the number of days since 1/1/1900 // which was on a Sunday. Output is: 0=Sun,1=Mon, ..., 6=Sat. int weekDay(int mon, int day, int year); //----------------------------------------------------------------- // This function receives a day code and returns the string (2nd // parametere with the three-character day of the week descriptor // (0=Sun,1=Mon, ..., 6=Sat) void dayCode(int code, char descript[]);

Program 1/READ FOR WHAT TO DO.docx

Purpose

To build a complete working C++ program to review basic C++ concepts focusing on multi-file C++ applications, input files, and strings.

Specifications

Write a program to create a system allowing a user to perform various functions relevant to a time almanac. Your program should be a basic console application that will:

1. Prompt the user to enter a month, year, and 3-character city airport identifier (i.e. 1 2010 MBS)

2. Print the monthly calendar for the month

3. Build and print a sunrise/sunset table of the for a given city for the month entered by the user

Your program should read in a list of city airport identifiers from a file (cityinfo.txt). that includes their geographical coordinates (latitude,longitude) and time zones. Your program should then allow the user to enter a three-character airport ID an integer month and a four-digit integer year.

Your program should search the list of city data you have access to and get the coordinates necessary for the sunrise/sunset calculations. If the airport ID is not in the list, provide an appropriate message. The file format has the city code on a line followed on the next line with the latitude and longitude and time zone. Along with validation of the airprt code, be sure to validate by checking that the month code is 1...12.

Sample output is provided for January 2014 for the location of MBS. Include the correct day of the week identifier for the dates you include in the report (i.e. MON, TUE, etc.).

Finally, include a feature that will give the user a Do you wish to continue? prompt that will allow them to enter another city and/or date range. Using arrays for the city information will allow you to simply enter another array/list search instead of closing and reopening the input file.

Using arrays for the city information will allow you to simply enter another array/list search instead of closing and reopening the input file.

Your program should utilize the following tools:

· cityinfo.txt: A list of city airport identifiers with geographical coordinates (latitude first, then longitude, then time zone).

· sun.h and sun.cpp: Primary function for calculating sunrise and sunset from a date and geographical coordinate.

· datefun.h and datefun.cpp: Several relevant date processing algorithms. Hint: you will require the algorithm for finding the day of the week any day is on.

Note: You have formal permission to use all instructor-provided functions provided credit is given within comments in your code as as long as the provided functions are unchanged.

Design your application using guidelines to maximize modularity, reusability, and maintainability. Utilize the "sun" and "date" functions in their current form - as external function sets that will be included into your main "driver" program for this application.

Deliverables

Deliver the following as your final product:

· All final source code files (.cpp and/or .h files; only those build/changed by you)

Program 1/Sample output.txt

CST 280 C++ Programming Program Assignment 1 (Sample Output) Given input: 1 2014 MBS Output should be something similar to: Sunrise/Sunset: MBS for January 2014 WED January 1, 2014 Rise: 8:10am Set: 5:10pm THU January 2, 2014 Rise: 8:10am Set: 5:11pm FRI January 3, 2014 Rise: 8:10am Set: 5:12pm SAT January 4, 2014 Rise: 8:10am Set: 5:13pm SUN January 5, 2014 Rise: 8:09am Set: 5:14pm MON January 6, 2014 Rise: 8:09am Set: 5:15pm TUE January 7, 2014 Rise: 8:09am Set: 5:16pm WED January 8, 2014 Rise: 8:09am Set: 5:17pm THU January 9, 2014 Rise: 8:09am Set: 5:18pm FRI January 10, 2014 Rise: 8:08am Set: 5:19pm SAT January 11, 2014 Rise: 8:08am Set: 5:21pm SUN January 12, 2014 Rise: 8:08am Set: 5:22pm MON January 13, 2014 Rise: 8:07am Set: 5:23pm TUE January 14, 2014 Rise: 8:07am Set: 5:24pm WED January 15, 2014 Rise: 8:06am Set: 5:25pm THU January 16, 2014 Rise: 8:06am Set: 5:26pm FRI January 17, 2014 Rise: 8:05am Set: 5:28pm SAT January 18, 2014 Rise: 8:05am Set: 5:29pm SUN January 19, 2014 Rise: 8:04am Set: 5:30pm MON January 20, 2014 Rise: 8:03am Set: 5:31pm TUE January 21, 2014 Rise: 8:03am Set: 5:33pm WED January 22, 2014 Rise: 8:02am Set: 5:34pm THU January 23, 2014 Rise: 8:01am Set: 5:35pm FRI January 24, 2014 Rise: 8:00am Set: 5:37pm SAT January 25, 2014 Rise: 7:59am Set: 5:38pm SUN January 26, 2014 Rise: 7:58am Set: 5:39pm MON January 27, 2014 Rise: 7:58am Set: 5:41pm TUE January 28, 2014 Rise: 7:57am Set: 5:42pm WED January 29, 2014 Rise: 7:56am Set: 5:43pm THU January 30, 2014 Rise: 7:55am Set: 5:45pm FRI January 31, 2014 Rise: 7:54am Set: 5:46pm Calendar for: January 2014 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Program 1/sun.cpp

#include <iostream> #include <string> #include <cmath> using namespace std; #include "sun.h" // Function prototype void sunriseset (double, double, int, int, int, int&, int&); void adjusttime (int, char, bool, TextTime); char DigitChar(int); void sunriseset (double lat, double lon, int mo, int da, int yr, int& RI, int& SE) { double FC,TN,X,Y,T,T9,S,A,Z,YD,WD,pi; double MA,A0,A1,ML,L0,L1,C,N,E,EO,W; double DL,TL,OB,DE,RA,S0,H,ZT; // double Q,TA,RV,JD; int sw,sw1; void f1(void); pi = 3.141592654; FC = 2 * pi;OB=0.409095; L0=4.88376619;L1=0.017202791; A0=6.23471229;A1=0.01720197; E =0.016728; EO=0.00218; TN = (lon/FC) + 0.5; X = 1; Y = 1; sw = 0;goto f1; f1_1: T9 = T; X = da; Y = mo; sw = 1;goto f1; f1_2: YD=T-T9+1; X=floor(T+1)/7;Y=floor(X); WD=floor(7*(X-Y)+0.5); T=T+3449.5+TN; X=YD-WD; goto f2; f2_1:// Q=ML-RA; X=-0.0143; goto f4; f4_1: if (fabs(Y)>=1) { RI = -9999; SE = -9999; } S0=Z;H=-S0; sw1=0;goto f5; f5_1: X=ZT+EO; sw1=0;goto f6; f6_1: RI=int(X*100+Y); H=S0; sw1=1;goto f5; f5_2: X=ZT+EO; sw1=1;goto f6; f6_2:SE=int(X*100+Y); goto f_end; // Subroutine f1 (810) f1:T = 367*(yr-1980); T=T-floor(7*(yr+floor((Y+9)/12))/4); if ((Y-9) > 0) S= 1;if((Y-9) < 0) S=-1; if ((Y-9) == 0) S= 0; A=fabs(Y-9); Z=floor((yr+S*floor(A/7))/100); T=T-floor(3*(Z+1)/4); T=T+floor(275*Y/9)+X - 0.5; // JD=T+2447689; if (sw == 0) goto f1_1; if (sw == 1) goto f1_2; // Subroutine f2 (900) f2:MA=A0+A1*T; ML=L0+L1*T; X=sin(ML);Y=cos(ML); sw = 0;goto f3; f3_1: ML=Z; DL=2*E*sin(MA)+1.25*(E*E)*sin(2*MA); // TA=MA+DL; TL=ML+DL; // RV=(1-(E*E))/(1+E*cos(TA)); X=sin(TL)*sin(OB);Y=sqrt(1-(X*X)); sw=1; goto f3; f3_2: DE=Z;if (Z>pi) Z=-FC; X=sin(TL)*cos(OB);Y=cos(TL); sw=2; goto f3; f3_3: RA=Z; goto f2_1; // Subroutine f3 (570) f3:C=0;N=0; if (Y != 0.0) Z=X/Y; else { Z=0;C=1; if (X<0) N=1; } Z=atan(Z); if (C==1) Z=pi/2-Z; if (N==1) Z=-1*Z; if (Y<0) Z=Z+pi; if (Z<0) Z=Z+2*pi; if (sw==0) goto f3_1; if (sw==1) goto f3_2; if (sw==2) goto f3_3; if (sw==3) goto f3_4; if (sw==4) goto f3_5; // Subroutine f4 (770) f4:Y=(X-sin(lat)*sin(DE))/(cos(lat)*cos(DE)); if (fabs(Y)<=1) { X=sqrt(1-(Y*Y)); sw = 3; goto f3; f3_4:sw=3; } goto f4_1; // Subroutine f5 (710) f5:ZT=H+RA+lon-ML-pi; X=sin(ZT);Y=cos(ZT); sw=4;goto f3; f3_5:ZT=Z; if (sw1 == 0) goto f5_1; if (sw1 == 1) goto f5_2; //Subroutine f6 (740) f6:W=X*24/FC;X=floor(W); Z=(W-X)*60;Y=floor(Z); Z=floor((Z-Y)*60); if (sw1 == 0) goto f6_1; if (sw1 == 1) goto f6_2; f_end:Z=1; } char DigitChar (int inDig) { char dig = inDig + 48; return dig; } void adjusttime (int time, char tzone, bool DST, TextTime timeout) { char addchar[2]; char suffix[3]; TextTime tempTime; strcpy(suffix,""); int i,j; int adjust = 0; // Assume UTC - no adjustment switch (tzone) { case 'E': if (DST) adjust = 400; else adjust = 500; break; case 'C': if (DST) adjust = 500; else adjust = 600; break; case 'M': if (DST) adjust = 600; else adjust = 700; break; case 'P': if (DST) adjust = 700; else adjust = 800; }; time = time - adjust; if (time < 0) time +=2400; if (tzone == 'U') strcpy(suffix,"Z"); else { if (time > 1200) strcpy(suffix,"pm"); else strcpy(suffix,"am"); time %= 1200; if (time < 100) time += 1200; } strcpy(tempTime,""); strcpy(addchar," "); if (time > 1000) { addchar[0] = DigitChar(time / 1000); strcpy(tempTime,addchar); } else if (tzone == 'U') strcpy(tempTime,"0"); addchar[0] = DigitChar((time % 1000) / 100); strcat(tempTime,addchar); addchar[0] = DigitChar((time % 100) / 10); strcat(tempTime,addchar); addchar[0] = DigitChar(time % 10); strcat(tempTime,addchar); strcat(tempTime,suffix); if (tzone != 'U') { if (time > 1000) i = 2; else i = 1; j = strlen(tempTime); while (j >= i) { tempTime[j+1] = tempTime[j]; j--; } tempTime[i] = ':'; } strcpy(timeout,tempTime); } void Sun_Rise_Set( double latitude, double longitude, int mon, int day, int year, char timeZone, bool DST, TextTime sun_rise, TextTime sun_set) { int rise,set; // Convert to radians latitude = latitude * pi / 180; longitude = longitude * pi / 180; sunriseset(latitude,longitude,mon,day,year,rise,set); adjusttime(rise, timeZone, DST, sun_rise); adjusttime(set, timeZone, DST, sun_set ); }

Program 1/sun.h

// Sunrise/Sunset module // //-------------------------------------------------------------- // Misc constants (as needed) const double pi = 3.1415962536; //-------------------------------------------------------------- // Time zone data type: // E Eastern time zone // C Central time zone // M Mountain time zone // P Pacific time zone // U Universal (Greenwich Mean) time //-------------------------------------------------------------- // Texttime data type (time returned as string) typedef char TextTime[8]; //--------------------------------------------------------------// // This function receives a date and geographical coordinates // // returns the sunrise and sunset for that day and location // // Input: // // latitude: Latitude of site (pos. float in degrees) // // longitude: Longitude of site (pos. float in degrees) // // mon: Month (integer 1..12) // // day: Day of month (1..31) // // year: Year (4-digit integer 19xx or 20xx) // // timeZone: Code for time zone // // DST bool indicating if daylight savings time // // is active. // // Output: // // sun_rise Sunrise value (string) // // sun_set Sunset value (string) // // Preconditions: // // * Date is valid; year is four-digit // // * Latitude and longitude is 0.0 .. 90.0 degrees // // * Time zone codes must be 'E', 'C', 'M', or 'P' only) // // Postconditions: // // * Date and geog. coords. unchanged // // // // Algorithm extracted from a BASIC program in Weatherwise // // Magazine. It was converted to Pascal in 1995 and then to // // this C++ version. // void Sun_Rise_Set( double latitude, double longitute, int mon, int day, int year, char timeZone, bool DST, TextTime sun_rise, TextTime sun_set);