【问题标题】:Can you put color only on certain words/paragraphs/lines?您可以仅在某些单词/段落/行上添加颜色吗?
【发布时间】: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参数用于处理闰年计算。

标签: c++ colors calendar


【解决方案1】:

不要使用 system 命令,它会改变整个控制台的颜色,并且不会针对控制台上的某个文本。

改为使用 SetTextConsoleAttribute 函数,就像您在 Color 函数中所做的那样。您可以简单地调用 Color 函数,只要您希望控制台的颜色发生变化。

例如,

int main()
{
Color(7); // Use this color as default 
for (int i = 0; i < 10; i++)
{
if (i == 4)
Color(10); // Change color to 10 if i = 4, otherwise use the default color. 

cout << i << " "; 
Color(7); // Change color to default. 

}
}

我已修改您的代码以更改整个二月的颜色以及十一月的某些特定日子的颜色。以您希望为日历着色的任何方式使用相同的图案。我还添加了 cmets 以及我为更好地理解所做的所有更改。如果您仍然有任何困惑,请询问。

#include <stdio.h>
#include <string>
#include <windows.h>
#include <conio.h>
#include <iostream>
using namespace std;

void Color(int); // Make prototype of function. 
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 
{
    // 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++)
    {
        if (i == 1) // If month is Feb
            Color(10); 
        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++)
        {
            if (i == 10) // If the month is November 
            {
                switch (j)
                {
                case 5:
                case 6:
                case 15:
                    Color(12); // For the days of 5, 6, 7 => Change color to Red. 
                    break;
                default:
                    Color(7); // Otherwise, use the default color. 
                }
            }
            printf("%5d", j);
            if (++k > 6)
            {
                k = 0;
                printf("\n");
            }
        }
        if (k)
            printf("\n");

        current = k;
        Color(7); // Change color to default color 
    }
    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()
{
    Color(7); // Default Color. Use color value = 7 
    std::cout << "hi" << std::endl;

    int year = 2020;
    printCalendar(year);
    return 0;
}

【讨论】:

  • 感谢您的回答,它真的非常有帮助。我也尝试了一些其他的东西,但他们一直给我错误,我不知道如何解决它们。非常感谢。
猜你喜欢
  • 2020-11-23
  • 2017-06-12
  • 2020-05-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多