【发布时间】:2021-10-21 05:57:18
【问题描述】:
我有一个 C++ 日历,我想在每个不同月份的特定日期放置某些颜色,但我不知道如何,所以我尝试至少为每个月放置某种颜色,但我无法做到它。我在主代码的末尾使用了这段代码,但我试着把它放在任何地方: 系统(“颜色 A”); std::cout
这是完整的代码:
#include <stdio.h>
#include <string>
#include <windows.h>
#include <conio.h>
#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)
// returns the names of the months in the order i wrote them to be
{
string months[] = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
return (months[monthNumber]);
}
int numberOfDays(int monthNumber, int year)
// returns the number of days each month has, and instead of having numbers from 28 to 31 i
put 30 for each month there is
{
// January
if (monthNumber == 0)
return (30);
// February
if (monthNumber == 1)
return (30);
// March
if (monthNumber == 2)
return (30);
// April
if (monthNumber == 3)
return (30);
// May
if (monthNumber == 4)
return (30);
// June
if (monthNumber == 5)
return (30);
// July
if (monthNumber == 6)
return (30);
// August
if (monthNumber == 7)
return (30);
// September
if (monthNumber == 8)
return (30);
// October
if (monthNumber == 9)
return (30);
// November
if (monthNumber == 10)
return (30);
// December
if (monthNumber == 11)
return (30);
}
void printCalendar(int year)
// making a calendar for the months and days to be in order and nice looking
{
printf(" Calendar - %d\n\n", year);
int days;
int current = dayNumber (1, 1, year);
// i is for iterating months
// j is for iterating days of the months
for (int i = 0; i < 12; i++)
{
days = numberOfDays(i, year);
//The name of the current month
printf("\n -------------%s------------- \n",
getMonthName(i).c_str());
//The columns for each day in the week so they dont get mixed together
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
/* blank spaces for the spaces in the calendar that dont regard the days in the exact
month, for example if 30 was on monday,
we would see the next 6 days as blank, as they are not part of this month, but are part of
the other month */
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;
}
// here we put what year we want, but it doesnt matter as the months have all 30 days,
//therefor whatever year we put we still have the same amounts of days
void Color(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
int main()
{
system("color A");
std::cout<<"hi"<<std::endl;
int year = 2020;
printCalendar(year);
return 0;
}
【问题讨论】:
-
如果您在
main的开头设置了一次颜色并且从不重置它,那么它将保持这种状态。您可以在打印彩色文本之前更改颜色,并在打印彩色文本后将其更改回非彩色 -
彩色控制台输出取决于控制台。存在(存在?)终端只支持整个物理线的颜色(想想光栅)。还有其他终端可以支持单个字符的颜色(实际上是屏幕上的单元格)。现在让我们谈谈 GUI 控制台窗口,它可能能够支持彩色图形,您可以在其中使用单独的彩色像素绘制自己的字符。
-
您的
getMonthName函数采用int参数。int类型可以为负数。给它一个-20,看看会发生什么。另一个测试用例是我们最喜欢的数字 0 和 13。 -
由于您有月份名称的数组,因此您还可以有一个每月天数的数组。这会从您的程序中删除大量代码。
-
您的
numberOfDays函数不使用year参数。通常year参数用于处理闰年计算。