将二维数组集成到日历打印程序中
Integrating a 2D Array into a Calendar Printing Program
我决定开始自己学习 C++ 教科书的编码,其中一个挑战是创建一个程序来打印给定年份的日历,每个月都采用这种风格:
-------------January-------------
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
我已经在 Microsoft Visual Studio 中编写了整个程序,但另一个挑战是将来自 getMonthName 和 dayNumber 函数的两个一维数组的数据转换为一个具有二维数组的函数使用此数组收集月份名称和日期编号的数组:
// the first number is the month and second number is the last day of the month.
int yearly[12][2] =
{{1,31},{2,28},{3,31},{4,30},{5,31},{6,30},{7,31},{8,31},{9,30},{10,31},{11,30},{12,31}};
教科书对二维数组的解释不是很好,但我知道基础知识。我只是不知道如何去引用数组的内容。我可以将数组的所有“月份编号”值分配给一个变量,就像我在这里为变量 monthNumber 分配的那样,然后从那里开始吗?如果有人可以提供一个关于如何做到这一点的例子,我将不胜感激。我正在努力吸收尽可能多的信息,所以如果你能解释你的例子是如何工作的,我会欣喜若狂。
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
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;
}
string getMonthName(int monthNumber)
{
string months[] = { "January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
return (months[monthNumber]);
}
//number of days in month
int numberOfDays(int monthNumber, int year)
{
// january
if (monthNumber == 0)
return (31);
// february
if (monthNumber == 1)
return (28);
// march
if (monthNumber == 2)
return (31);
// april
if (monthNumber == 3)
return (30);
// may
if (monthNumber == 4)
return (31);
// june
if (monthNumber == 5)
return (30);
// july
if (monthNumber == 6)
return (31);
// august
if (monthNumber == 7)
return (31);
// september
if (monthNumber == 8)
return (30);
// october
if (monthNumber == 9)
return (31);
// november
if (monthNumber == 10)
return (30);
// december
if (monthNumber == 11)
return (31);
}
// display calendar function
void printCalendar(int year)
{
printf (" Calendar - 2021", year);
int days;
// day from 0 - 6
int current = dayNumber(1, 1, year);
for (int i = 0; i < 12; i++)
{
days = numberOfDays(i, year);
// current month display
printf("\n ------------%s-------------\n",
getMonthName(i).c_str());
//columns
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
// spaces
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++)
{
printf("%5d", j);
if (++k > 6)
{
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
// main function
int main()
{
int year = 2021;
printCalendar(year);
return (0);
}
如果您需要更多信息,请告诉我,我很乐意提供更多信息。
我声明了一个全局变量int yearly[12][2];
代码段第 90 行:
for (int i = 0; i < 12; i++)
{
days = numberOfDays(i, year);
yearly[i][0] = i; //month
yearly[i][1] = days;//days
... ...
for循环执行12次,每次i对应一个月
每年 [i][0] 是月份,每年 [i][1] 是每月的第几天。
二维数组是什么样的:
column
Row 0 [0][1]
1 [0][1]
2 [0][1]
... ...
11[0][1]
二维数组打印示例:
for (int Row= 0; Row< 12; Row++)
{
std::cout << "{";
for (int column= 0; column< 2; column++)
{
std::cout << yearly[Row][column]<<",";
}
std::cout << "}"<<std::endl;
}
我决定开始自己学习 C++ 教科书的编码,其中一个挑战是创建一个程序来打印给定年份的日历,每个月都采用这种风格:
-------------January-------------
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
我已经在 Microsoft Visual Studio 中编写了整个程序,但另一个挑战是将来自 getMonthName 和 dayNumber 函数的两个一维数组的数据转换为一个具有二维数组的函数使用此数组收集月份名称和日期编号的数组:
// the first number is the month and second number is the last day of the month.
int yearly[12][2] =
{{1,31},{2,28},{3,31},{4,30},{5,31},{6,30},{7,31},{8,31},{9,30},{10,31},{11,30},{12,31}};
教科书对二维数组的解释不是很好,但我知道基础知识。我只是不知道如何去引用数组的内容。我可以将数组的所有“月份编号”值分配给一个变量,就像我在这里为变量 monthNumber 分配的那样,然后从那里开始吗?如果有人可以提供一个关于如何做到这一点的例子,我将不胜感激。我正在努力吸收尽可能多的信息,所以如果你能解释你的例子是如何工作的,我会欣喜若狂。
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
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;
}
string getMonthName(int monthNumber)
{
string months[] = { "January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
return (months[monthNumber]);
}
//number of days in month
int numberOfDays(int monthNumber, int year)
{
// january
if (monthNumber == 0)
return (31);
// february
if (monthNumber == 1)
return (28);
// march
if (monthNumber == 2)
return (31);
// april
if (monthNumber == 3)
return (30);
// may
if (monthNumber == 4)
return (31);
// june
if (monthNumber == 5)
return (30);
// july
if (monthNumber == 6)
return (31);
// august
if (monthNumber == 7)
return (31);
// september
if (monthNumber == 8)
return (30);
// october
if (monthNumber == 9)
return (31);
// november
if (monthNumber == 10)
return (30);
// december
if (monthNumber == 11)
return (31);
}
// display calendar function
void printCalendar(int year)
{
printf (" Calendar - 2021", year);
int days;
// day from 0 - 6
int current = dayNumber(1, 1, year);
for (int i = 0; i < 12; i++)
{
days = numberOfDays(i, year);
// current month display
printf("\n ------------%s-------------\n",
getMonthName(i).c_str());
//columns
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
// spaces
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++)
{
printf("%5d", j);
if (++k > 6)
{
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
// main function
int main()
{
int year = 2021;
printCalendar(year);
return (0);
}
如果您需要更多信息,请告诉我,我很乐意提供更多信息。
我声明了一个全局变量int yearly[12][2];
代码段第 90 行:
for (int i = 0; i < 12; i++)
{
days = numberOfDays(i, year);
yearly[i][0] = i; //month
yearly[i][1] = days;//days
... ...
for循环执行12次,每次i对应一个月
每年 [i][0] 是月份,每年 [i][1] 是每月的第几天。
二维数组是什么样的:
column
Row 0 [0][1]
1 [0][1]
2 [0][1]
... ...
11[0][1]
二维数组打印示例:
for (int Row= 0; Row< 12; Row++)
{
std::cout << "{";
for (int column= 0; column< 2; column++)
{
std::cout << yearly[Row][column]<<",";
}
std::cout << "}"<<std::endl;
}