Assignment – Calendar Project Technical Report

Assignment – Calendar Project Technical Report Assignment – Calendar Project Technical Reportby nathanpennyPart one: Complete Code Implementation//Calendar.h //Header guard to prevent multiple inclusions of the header file, avoiding redefinition errors #pragma once ​ //Date class encapsulates date-related data and operations class date { private: int day; //Stores the day of the date (1-31, depending on the month) int month; //Stores the month of the date (1-12) int year; //Stores the year of the date (e.g., 2024, 2025) public: //Parameterized constructor: initializes a date object with specific day, month, and year date(int d, int m, int y) : day(d), month(m), year(y) {} //Sets new values for day, month, and year of the date object void set_date(int d, int m, int y); //Prints the date in day/month/year format (const ensures no modification to object state) void print_date() const; //Getter method for day (const: does not alter object properties) int get_day() const { return day; } //Getter method for month (const: does not alter object properties) int get_month() const { return month; } //Getter method for year (const: does not alter object properties) int get_year() const { return year; } //Adds (or subtracts, if n is negative) n days to the current date and updates the date void add_days(int n); //Checks if the year of the current date is a leap year (const: no state modification) bool is_leap_year() const; //Validates if the current day/month/year combination forms a valid date (const: no state modification) bool is_valid_date() const; }; ​ //External function declaration: retrieves the systems current date (passed by reference to modify caller variables) extern void get_current_date(int day, int month, int year);//Calendar.cpp //Suppress C runtime library secure warnings (Windows-specific) #define _CRT_SECURE_NO_WARNINGS #includeiostream #includecmath #includectime using namespace std; #include Calendar.h ​ //Implementation of set_date: updates the dates day, month, and year void date::set_date(int d, int m, int y) { day d; month m; year y; } ​ //Implementation of print_date: outputs date in day/month/year format to the console void date::print_date() const { cout day / month / year endl; } ​ //Implementation of add_days: adjusts the date by n days (handles positive/negative n) void date::add_days(int n) { day n; //First adjust the day by n days, then resolve overflow/underflow ​ //Loop until the day is within the valid range for the current month while (true) { //Array storing days in each month (February is adjusted for leap years) int days_in_month[] { 31, is_leap_year() ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //Get number of days in the current month (array index starts at 0, so month-1) int current_month_days days_in_month[month - 1]; ​ //Exit loop if day is within valid range for current month if (day 1 day current_month_days) { break; } ​ //Handle case where day is less than 1 (e.g., subtracting days leads to previous month/year) if (day 1) { month--; //Move to the previous month //Wrap around to December if month underflows below 1 if (month 1) { month 12; year--; //Move to the previous year } //Add days from the new current month to resolve negative day day days_in_month[month - 1]; } ​ //Handle case where day exceeds days in current month (e.g., adding days leads to next month/year) else if (day current_month_days) { day - current_month_days; //Subtract days in current month to get day in next month month; //Move to the next month //Wrap around to January if month overflows above 12 if (month 12) { month 1; year; //Move to the next year } } } } ​ //Implementation of is_leap_year: checks leap year rules for the current dates year bool date::is_leap_year() const { //Leap year rules: //1. Divisible by 4 but not by 100, OR //2. Divisible by 400 return (year % 4 0 year % 100 ! 0) || (year % 400 0); } ​ //Implementation of is_valid_date: validates if day/month/year is a valid combination bool date::is_valid_date() const { //Invalid if month is out of 1-12 range or day is less than 1 if (month 1 || month 12 || day 1) return false; //Array of days per month (adjust February for leap years) int days_in_month[] { 31, is_leap_year() ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //Check if day is within the valid range for the current month return day days_in_month[month - 1]; } ​ //Implementation of get_current_date: retrieves systems current local date void get_current_date(int day, int month, int year) { //Get current Unix timestamp (seconds since 00:00:00 UTC, 1 Jan 1970) time_t now time(nullptr); //Convert timestamp to local time structure (tm) and dereference to avoid pointer risks tm local_time *localtime(now); ​ //tm_year stores year as (current year - 1900), so add 1900 to get actual year year local_time.tm_year 1900; //tm_mon stores month as 0-11 (0Jan, 11Dec), so add 1 to get 1-12 range month local_time.tm_mon 1; //tm_mday directly stores day of the month (1-31), no conversion needed day local_time.tm_mday; }//CalendarMain.cpp //Suppress C runtime library secure warnings (Windows-specific) #define _CRT_SECURE_NO_WARNINGS #includeiostream #includecmath #includectime #includelimits //Required for numeric_limits (input validation) using namespace std; #include Calendar.h ​ //Main function: entry point of the program, handles user interaction and date calculation int main() { int n; //Stores number of days to add/subtract (input by user) int today_day, today_month, today_year; //Stores current system date //Retrieve current local date and store in today_day/today_month/today_year get_current_date(today_day, today_month, today_year); //Create date object for todays date date today(today_day, today_month, today_year); ​ //Validate todays date (defensive check against system time errors) if (!today.is_valid_date()) { cout Error: Invalid date endl; return 1; //Exit with non-zero code to indicate error } ​ //Display todays date to the user cout Todays date is: today.get_day() / today.get_month() / today.get_year() endl; cout Please enter an integer number of days to add: ; ​ //Input validation: ensure user enters a valid integer while (!(cin n)) { cout Invalid input! Please enter an integer: ; cin.clear(); //Clear cins error state //Ignore all remaining characters in the input buffer until newline cin.ignore(numeric_limitsstreamsize::max(), \n); } ​ //Adjust todays date by n days today.add_days(n); //Display the resulting date cout The new date after adding n days is: today.get_day() / today.get_month() / today.get_year() endl; return 0; //Exit with 0 to indicate successful execution }Part two: Compilation and Execution InstructionsSystem RequirementsOperating System: Windows 11 (64-bit)Compiler: Microsoft Visual Studio 2026 (MSVC compiler)Compilation StepsOpen Visual Studio 2026 and create a new Empty Project (C).Add the three files (Calendar.h,Calendar.cpp,CalendarMain.cpp) to the project:Right-click the project → Add → Existing Item → Select the three files.Set the project to x64 platform (match Windows 11 architecture):Use the dropdown menu in the Visual Studio toolbar (default may be x86).Compile the project:PressCtrl Shift B(Build Solution) or click Build → Build Solution.Execution InstructionsAfter successful compilation, run the executable (generated in thex64/Debugorx64/Releasefolder of the project directory).The program will:Automatically retrieve and display the current system date (format:day/month/year).Prompt you to enter an integer (positive add days, negative subtract days).Calculate and display the new date after adjusting by the input number of days.Example OutputTodays date is: 17/3/2026 Please enter an integer number of days to add: 10 The new date after adding 10 days is: 27/3/2026NotesThe program runs once and exits (non-cyclic). Restart the executable to perform another date calculation.Ensure the system clock is set correctly (the program relies on system time forget_current_date).Part three: Detailed Code ExplanationOverviewThe program is modularized into three core components, following object-oriented programming (OOP) principles:Calendar.h: Header file defining thedateclass (encapsulates date data and function declarations).Calendar.cpp: Implementation file for thedateclass methods andget_current_datefunction.CalendarMain.cpp: Main program logic (user input, date validation, and result display).Key Component Breakdown1.dateClass (Calendar.h)Thedateclass encapsulates date state (day/month/year) and behaviors (date adjustment, validation, etc.), adhering to OOP encapsulation (private data public methods).Constructor: Initializes the date with user-provided day/month/year.Getter/Setter Methods:get_day(),get_month(),get_year()(read-only access to private data) andset_date()(modify date state).Core Functional Methods:is_leap_year(): Implements Gregorian calendar leap year rules (critical for February day validation).is_valid_date(): Validates if the date is logically consistent (e.g., no 30 February or 13th month).add_days(int n): Adjusts the date byndays (handles month/year wrapping for both positive/negativen).2.add_days(int n)Implementation (Calendar.cpp)This method is the core of the program, handling date arithmetic with edge cases (e.g., subtracting days from January → December of the previous year, adding days to December → January of the next year):First adjust thedayattribute byndays.Use a loop to resolve day overflow/underflow:Ifday 1: Decrement the month (wrap to December if needed), decrement the year if month underflows, and add days from the new month today.Ifday days_in_current_month: Subtract days in the current month fromday, increment the month (wrap to January if needed), and increment the year if month overflows.Exit the loop whendayis within the valid range for the current month.3.get_current_date()Implementation (Calendar.cpp)This function retrieves the system’s local date using C standard library time functions:time(nullptr): Gets the current Unix timestamp (seconds since 1970-01-01 UTC).localtime(now): Converts the timestamp to atmstructure (local time, e.g., Beijing Time).Adjusttm_year(add 1900) andtm_mon(add 1) to match human-readable date format.4. Input Validation (CalendarMain.cpp)The program includes robust input validation to handle non-integer user input:while (!(cin n)): Checks if the input is a valid integer.cin.clear(): Resets cin’s error flag (required to continue reading input).cin.ignore(...): Discards invalid characters in the input buffer to avoid infinite loops.