我正在制作日历,并且我已设法显示日历的标题,但我不知道如何使正文或月份的实际日期/数字与每个月对齐以及如何使它们显示。该计划是让用户输入他们想要查看日历的年份。我已经设置了一个函数来获取用户希望的年份,并检查它是否是闰年,并检查该年第一天的日期。 我正在以这种格式制作日历
#include <iostream>
#include <iomanip>
using namespace std;
int GetFirstDay(int year);
void main()
{
int year, numdays;
int month = 1;
// getting the user to give the year
cout << "Enter year: ";
cin >> year;
cout << "\n\n";
// getting first day of given year
int First_Day = GetFirstDay(year);
while (month <= 12) {
switch (month) {
//Body
case 1:
numdays = 31;
cout << "January" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 2:
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { numdays = 29; }
else { numdays = 28; }
cout << "Febrary" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 3:
numdays = 31;
cout << "March" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 4:
numdays = 30;
cout << "April"<< endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 5:
numdays = 31;
cout << "May" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 6:
numdays = 30;
cout << "June" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 7:
numdays = 31;
cout << "July" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 8:
numdays = 31;
cout << "August" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 9:
numdays = 30;
cout << "September" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 10:
numdays = 31;
cout << "October" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 11:
numdays = 31;
cout << "November" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
case 12:
numdays = 31;
cout << "December" << endl;
cout << "S M T W T F S" << endl;
cout << "-------------------" << endl << endl;
break;
}
month++;
}
}
int GetFirstDay(int year) {
int century = (year - 1) / 100;
int y = (year - 1) % 100;
int weekday = (((29 - (2 * century) + y + (y / 4) + (century / 4)) % 7) + 7) % 7;
// 0 would be sunday and 6 would be Saturday
return weekday;
}
首先,您需要一个函数来告诉您今天是哪一天,给定日、月和年。
Index Day
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
找到每个月的开始日期,并相应地打印制表符空格。例如,如果开始日是星期三,则根据您的格式打印 3 个制表符空格或任意数量的空格。每个月都这样做,你就会得到你想要的对齐方式
//A Function that returns the index of the day of the date - day/month/year
int dayNumber(int day, int month, int year)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1,
4, 6, 2, 4 };
year -= month < 3;
return ( year + year/4 - year/100 +
year/400 + t[month-1] + day) % 7;
}
提示:不要对月份进行硬编码,而是使用一个函数或数组将月份名称作为字符串返回,以便您可以循环 12 个月
string getMonthName(int monthNumber)
{
string months[] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (months[monthNumber]);
}
使用高级文本格式制作带有假期列表的商业风格日历转到https://codingbigdataalgorithm.com/