C++公历年显示算法

C++ gregorian calendar year display algorithm

本文关键字:显示算法 C++      更新时间:2023-10-16

我想制作一个程序,从给定的一天开始显示一年的整个日历。 我"觉得"这个算法可以比我做得更好。

如果有人能指出我在这个算法中做错了什么,我将不胜感激。

#include <iostream>
#include <iomanip>
const int COLUMN_IN_WEEK = 7;
const int ROWS_IN_WEEK = 6;
const int DAYS_IN_FEBRUARY = 28;
const int DAYS_IN_MONTH = 31;
const int MONTH_IN_YEAR = 12;
enum days {Sunday, Monday, Tuesday, Wednesdey, Thursday, Friday, Saturday};
enum months{Januay, February, March, April, May, June, July, Agost, September, October, November, December};
int PrintWeek(int startDay, int totalDays, int monthNum);
void PrintYear(std::string months[],int startDay, int monthNum);
int main()
{
std::string months[MONTH_IN_YEAR] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dec"};
PrintYear(months, Tuesday, Januay);
return EXIT_SUCCESS;
}
void PrintYear(std::string months[],int startDay, int monthNum)
{
for (int currentMonth = monthNum; currentMonth < MONTH_IN_YEAR; currentMonth++)
{
int totalDays;
if (currentMonth != February)
{
if (currentMonth <= July)
{
if (currentMonth % 2)
totalDays = DAYS_IN_MONTH - 1;
else
totalDays = DAYS_IN_MONTH;
}
else
{
if (currentMonth % 2)
totalDays = DAYS_IN_MONTH;
else
totalDays = DAYS_IN_MONTH - 1;
}
}
else
{
totalDays = DAYS_IN_FEBRUARY;
}
std::cout << std::left << months[currentMonth] << std::endl;
startDay = PrintWeek(startDay, totalDays, currentMonth);
std::cout << std::endl;
}
}
int PrintWeek(int startDay, int totalDays, int monthNum)
{
int dayCounter = 1;
int rowDay = 0;
for (int row = 0; row < ROWS_IN_WEEK; row++)
{
for (int column = 0; column < COLUMN_IN_WEEK; column++)
{    
if (column >= startDay)
{
std::cout << std::right << std::setfill(' ') << std::setw(2)  << dayCounter << " ";
startDay++;
dayCounter++;
if (dayCounter > totalDays)
{
row = ROWS_IN_WEEK;
std::cout << "n";
return startDay;
}   
}
else
std::cout << "   ";
}
if (startDay < COLUMN_IN_WEEK)
startDay++;
else
{
startDay = COLUMN_IN_WEEK - startDay;
rowDay++;
}   
std::cout << "n";
}
return startDay;
}

好吧,我终于写了我能写的。 该程序询问并向控制台打印给定日期(0-6(和虚幻年份的日历。 对不起,@Paul是对的。 这是我回答(错误(问题的方式:

#include <iostream>
#include <iomanip>
#include <string>
const int DAYS_IN_WEEK = 7;
const int ROWS_IN_WEEK = 6;
const int DAYS_IN_FEBRUARY = 28;
const int DAYS_IN_MONTH = 31;
const int MONTH_IN_YEAR = 12;
std::string days[DAYS_IN_WEEK] = {"Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
std::string months[MONTH_IN_YEAR] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
int biggestDay = 0, oldBiggestDay;
int monthWidth;
int montWidthes[MONTH_IN_YEAR];
enum days {Sunday, Monday, Tuesday, Wednesdey, Thursday, Friday, Saturday};
enum months{Januay, February, March, April, May, June, July, August, September, October, November, December};
int PrintWeek(int startDay, int totalDays, int monthNum);
void PrintYear(std::string months[],int startDay, int monthNum, int year);
bool CheckLeapYear(int year);
int main()
{
for (int day = 0; day < DAYS_IN_WEEK; day++)
{
oldBiggestDay = days[day].length() +1;
if (oldBiggestDay > biggestDay)
biggestDay = oldBiggestDay;
}
monthWidth = biggestDay * DAYS_IN_WEEK / 2;
for (int i = 0; i < MONTH_IN_YEAR; i++)
montWidthes[i] = monthWidth + months[i].length() / 2;
std::cout << "Enter the day in number (0-6): ";
std::string strDay, strMonth, strYear;
std::cin >> strDay;
std::cout << "Enter the year: ";
std::cin >> strYear;
int day = std::stoi(strDay, 0, 10);
std::cout << "The calendar for " << strYear << " is:" << std::endl;
PrintYear(months, day, Januay, std::stoi(strYear));
return EXIT_SUCCESS;
}
void PrintYear(std::string months[],int startDay, int monthNum, int year)
{
for (int currentMonth = monthNum; currentMonth < MONTH_IN_YEAR; currentMonth++)
{
int totalDays;
if (currentMonth != February)
{
if (currentMonth <= July)
{
if (currentMonth % 2)
totalDays = DAYS_IN_MONTH - 1;
else
totalDays = DAYS_IN_MONTH;
}
else
{
if (currentMonth % 2)
totalDays = DAYS_IN_MONTH;
else
totalDays = DAYS_IN_MONTH - 1;
}
}
else
{
totalDays = DAYS_IN_FEBRUARY;
CheckLeapYear(year) ? ++totalDays : totalDays;
}
std::cout << std::setw(montWidthes[currentMonth])  << months[currentMonth] << std::endl;
startDay = PrintWeek(startDay, totalDays, currentMonth);
std::cout << std::endl;
}
}
int PrintWeek(int startDay, int totalDays, int monthNum)
{
int dayCounter = 1;
int rowDay = 0;
std::string blankSpaces = "";
for (int i = 0; i < biggestDay; i++)
blankSpaces += " ";
for (int column = 0; column < DAYS_IN_WEEK; column++)
std::cout << std::right << std::setw(biggestDay) << days[column];
std::cout << std::endl;
for (int row = 0; row < ROWS_IN_WEEK; row++)
{
for (int column = 0; column < DAYS_IN_WEEK; column++)
{    
if (column >= startDay)
{
std::cout << std::right << std::setfill(' ') << std::setw(biggestDay) << dayCounter;
startDay++;
dayCounter++;
if (dayCounter > totalDays)
{
row = ROWS_IN_WEEK;
std::cout << "n";
return startDay;
}   
}
else
std::cout << blankSpaces;
}
if (startDay < DAYS_IN_WEEK)
startDay++;
else
{
startDay = DAYS_IN_WEEK - startDay;
rowDay++;
}   
std::cout << "n";
}
return startDay;
}
bool CheckLeapYear(int year)  
{  
// If a year is multiple of 400,  
// then it is a leap year  
if (year % 400 == 0)  
return true;  
// Else If a year is muliplt of 100,  
// then it is not a leap year  
if (year % 100 == 0)  
return false;  
// Else If a year is muliplt of 4,  
// then it is a leap year  
if (year % 4 == 0)  
return true;  
return false;  
}