0

我正在为我的 c++ 课程制作一个日历程序,我只是想知道是否有办法改变程序中某些日子的颜色(例如,圣诞节、新年、周末,都是不同的颜色),如果它是不可能,我至少可以大胆一些……

这是我的代码

char *month_name[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char *month_underline[12] = {"-------", "--------", "-----", "-----", "---", "----", "----", "------", "---------", "-------", "--------", "--------"};
char *week_name[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int days = 1;
int days_max;
int i, j, k;

for(i = 0; i < 12; i++)
{
    cout << "--------------------------------------------------------------------------------";

    cout << '\n' << month_name[i] << '\n';
    cout << month_underline[i] << "\n\n";

    for (j = 0; j < 7; j++)
    {
        cout << week_name[j] << "     ";
    }



    cout << "\n\n";

    if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) //i is the number of the month minus 1 (jan = 0, feb = 1, etc.)
        days_max = 32;
    else if (i == 3 || i == 5 || i == 8 || i == 10)
        days_max = 31;
    else if (i == 1)
        days_max = 29;

    for (days = 1; days < days_max; days++)
    {
        if (i == 0 || i == 9)
        {
            if (days == 1)
            {
                cout << "                        ";
                cout << setw(4) << days << "        ";
            }
            else if (days == 6 || days == 13 || days == 20 || days == 27)
            {    
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }


        if (i == 1 || i == 2 || i == 10)
        {
            if (days == 1)
            {
                cout << "                                                            ";
                cout << setw(4) << days << "        ";
            }                
            else if (days == 3 || days == 10 || days == 17 || days == 24 || days == 31)
            {
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }

        if (i == 3 || i == 6)
        {
            if (days == 1)
            {
                cout << "            ";
                cout << setw(4) << days << "        ";
            }                
            else if (days == 7 || days == 14 || days == 21 || days == 28)
            {
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }

        if (i == 4)
        {
            if (days == 1)
            {
                cout << "                                    ";
                cout << setw(4) << days << "        ";
            }                
            else if (days == 5 || days == 12 || days == 19 || days == 26)
            {
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }

        if (i == 5)
        {
            if (days == 1)
            {
                cout << "                                                                        ";
                cout << setw(4) << days << "        ";
            }                
            else if (days == 2 || days == 9 || days == 16 || days == 23 || days == 30)
            {
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }

        if (i == 7)
        {
            if (days == 1)
            {
                cout << "                                                ";
                cout << setw(4) << days << "        ";
            }                
            else if (days == 4 || days == 11 || days == 18 || days == 25)
            {
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }

        if (i == 8 || i == 11)
        {           
            if (days == 8 || days == 15 || days == 22 || days == 29)
            {
                cout << "\n\n";
                cout << setw(4) << days << "        ";
            }
            else 
                cout << setw(4) << days << "        ";
        }


    }
        cout << "\n";
}

cout << "--------------------------------------------------------------------------------";


system("PAUSE");
return EXIT_SUCCESS;
}
4

2 回答 2

4

在文本模式下,您有一些限制。但是有一些便携式库可以处理您的目的,例如着色。

阅读这个问题:Portable text based console manipulator并点击链接。它至少在(支持 Windows 和 ANSI-Escape 的操作系统)之间是可移植的

您可以像下面的代码一样更改颜色:

#include <iostream>
#include "rlutil.h"  // <-- Get it from the link
int main()
{
    rlutil::setColor(2); // 2 for Green

    std::cout << "Hello! I am green!" << std::endl;

    return 0;
}
于 2013-01-13T14:53:52.230 回答
0

没有 C++“标准”方法可以做到这一点。您可以按照 Masoud 的建议找到一个库 - 这将使您的代码可以移植到其他系统。或者您可以直接使用例如 ANSI Escape 序列[这可能是便携式解决方案在幕后所做的,但还有其他方法]。

该页面提供了一些信息: http ://en.wikipedia.org/wiki/ANSI_escape_code

但是请记住,这是不可移植的,并且不适用于所有平台。

于 2013-01-13T14:56:25.357 回答