CalendarAssistant.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:4k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- // Function prototypes
- int GetYear();
- int GetMonth();
- int GetDay(int year, int month);
- void DisplayDate(int year, int month, int day);
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- Console::WriteLine(S"Welcome to your calendar assistant");
-
- for (int count = 1; count <= 5; count++)
- {
- Console::Write(S"nPlease enter date ");
- Console::WriteLine(count);
- int year = GetYear();
- int month = GetMonth();
- int day = GetDay(year, month);
- Console::Write(S"Press B (break), C (continue), or anything else to display date ");
- String * input = Console::ReadLine();
- if (input->Equals(S"B"))
- {
- break;
- }
- else if (input->Equals(S"C"))
- {
- continue;
- }
- DisplayDate(year, month, day);
- }
- return 0;
- }
- // Ask the user to enter a year
- int GetYear()
- {
- Console::Write(S"Year? ");
- String * input = Console::ReadLine();
- int year = input->ToInt32(0);
- return year;
- }
- // Ask the user to enter a month
- int GetMonth()
- {
- int month = 0;
- do
- {
- Console::Write(S"Month [1 to 12]? ");
- String * input = Console::ReadLine();
- month = input->ToInt32(0);
- }
- while (month < 1 || month > 12);
- return month;
- }
- // Ask the user to enter a day
- int GetDay(int year, int month)
- {
- int day = 0;
-
- int maxDay;
- if (month == 4 || month == 6 || month == 9 || month == 11)
- {
- maxDay = 30;
- }
- else if (month == 2)
- {
- bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- if (isLeapYear)
- {
- maxDay = 29;
- }
- else
- {
- maxDay = 28;
- }
- }
- else
- {
- maxDay = 31;
- }
- do
- {
- Console::Write(S"Day [1 to ");
- Console::Write(maxDay);
- Console::Write(S"]? ");
-
- String * input = Console::ReadLine();
- day = input->ToInt32(0);
- }
- while (day < 1 || day > maxDay);
- return day;
- }
- // Display the date
- void DisplayDate(int year, int month, int day)
- {
- Console::WriteLine(S"nThis is the date you entered:");
-
- Console::Write(year);
- switch (month)
- {
- case 1: Console::Write(S" January "); break;
- case 2: Console::Write(S" February "); break;
- case 3: Console::Write(S" March "); break;
- case 4: Console::Write(S" April "); break;
- case 5: Console::Write(S" May "); break;
- case 6: Console::Write(S" June "); break;
- case 7: Console::Write(S" July "); break;
- case 8: Console::Write(S" August "); break;
- case 9: Console::Write(S" September "); break;
- case 10: Console::Write(S" October "); break;
- case 11: Console::Write(S" November "); break;
- case 12: Console::Write(S" December "); break;
- default: Console::Write(S" Unknown "); break;
- }
- Console::Write(day);
- switch (month)
- {
- case 12:
- case 1:
- case 2: Console::WriteLine(S" [Winter]"); break;
- case 3:
- case 4:
- case 5: Console::WriteLine(S" [Spring]"); break;
- case 6:
- case 7:
- case 8: Console::WriteLine(S" [Summer]"); break;
- case 9:
- case 10:
- case 11: Console::WriteLine(S" [Fall]"); break;
- }
- Console::WriteLine();
- }