c++ - 制作日历 c++(初级)
问题描述
我正在制作日历,并且已经设法显示日历的标题,但我不知道如何使正文或月份的实际日期/数字与每个月保持一致以及如何使它们显示。该计划是让用户输入他们想要查看日历的年份。我已经设置了一个函数来获取用户希望的年份并检查它是否是闰年,并检查一年中第一天的日期。 我正在以这种格式制作日历
#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]);
}
推荐阅读
- android - 当我在 android 中应用安全 args 插件(应用插件:“androidx.navigation.safeargs.kotlin”)时,显示以下错误
- go - windows机器上设置GOPATH值的问题
- javascript - 使用 6 个条件对对象数组进行排序
- python - 如何在同一个 repl.it 项目中运行两个 Flask discord.py 机器人?
- postgresql - 使用 Docker 创建 postgres 容器时出错
- python - IPTCInfo 关键字错误 - 未显示所有关键字
- node.js - 如何在不使用 child_process 的情况下使用 nodejs 运行 shell 命令?
- javascript - Express JS - 在服务器中添加路由来处理 POST 请求
- .htaccess - 当文件夹与 .html 文件同名时,删除 URL 中的 .html 扩展名
- spring - Spring boot Admin Spring cloud Kubernetes,更改默认角色和角色绑定