突出显示当日的c++日历代码

下为突出显示输入日期的日历代码。
#include 
#include 
#include "windows.h"
using namespace std;

/**
*   此函数参考 http://blog.csdn.net/odaynot/article/details/7722240
*   设置抽屉输出颜色,不过输出颜色只有三原色及其组合:
*   FOREGROUND_GREEN FOREGROUND_BLUE FOREGROUND_RED
*/
void SetColor(unsigned short forecolor =4 ,unsigned short backgroudcolor =0)
{
  HANDLE hCon =GetStdHandle(STD_OUTPUT_HANDLE); //获取缓冲区句柄
  SetConsoleTextAttribute(hCon,forecolor|backgroudcolor); //设置文本及背景色
}

class mydate{
public:
    mydate(){
        year = 1970;
        month = 1;
        day = 1;
        first_day_week = 4;
        err = 0;
    }
    mydate(int inYear,int inMonth,int inDay){
        year = inYear;
        month = inMonth;
        day = inDay;
        first_day_week = -1;
        err = 0;
    }

    //判断是否为闰年
    int isRunNian(int yearJudge){
        /*
        if(year < 1400 || year > 2500)
            return -1;
        */
        if(yearJudge % 4 != 0)
            return 0;
        if(yearJudge % 100 == 0 && yearJudge % 400 != 0)
            return 0;
        return 1;
    }

    //计算当月的天数
    int DayInMonth(int monthJudge){
        switch(monthJudge){
        case 1:
            return 31;
        case 2:
            if(isRunNian(year))
                return 29;
            return 28;
        case 3:
            return 31;
        case 4:
            return 30;
        case 5:
            return 31;
        case 6:
            return 30;
        case 7:
            return 31;
        case 8:
            return 31;
        case 9:
            return 30;
        case 10:
            return 31;
        case 11:
            return 30;
        case 12:
            return 31;
        default :
            return -1;
        }
    }

    //根据对当年是否为闰年的判断,返回当年的总天数
    int daysForYear(int y){
        if(isRunNian(y) > 0)
            return 366;
        return 365;
    }

    //计算出的第一天是星期几
    void getFirstWeek(){
        if(year >= 1970){
            first_day_week = 4;
            //计算该年第一天是星期几
            for(int y = 1970;y < year;y++){
                int r = daysForYear(y);
                if(r < 0){
                    err = -1;
                    goto return_pos;
                }
                first_day_week += r;
            }
        }
        else{
            first_day_week = 4;

            //计算该年第一天是星期几
            for(int y = 1969;y >= year;y--){
                int r = daysForYear(y);
//                cout << y << " --- " << r << endl;
                if(r < 0){
                    err = -1;
                    goto return_pos;
                }
                first_day_week -= r;
            }
        }

        //计算该月第一天是星期几
        for(int m = 1;m < month;m++){
            int ds = DayInMonth(m);
            if(ds < 0){
                err = -2;
                goto return_pos;
            }
            first_day_week += ds;
        }
        //计算该日是星期几
        if(day < 1 || day > DayInMonth(month)){
            err = -3;
            goto return_pos;
        }
        first_day_week = first_day_week%7;
        if(first_day_week == 0)
            first_day_week = 7;
        while(first_day_week < 0)
            first_day_week += 7;


return_pos:
        return;
    }

    //错误输出
    int errorPro(){
        switch(err){
        case -1:
            cout << "Year Input Error" << endl;
            return -1;
        case -2:
            cout << "Month Input Error" << endl;
            return -1;
        case -3:
            cout << "Day Input Error" << endl;
            return -1;
        case -4:
            cout << "Week Get Error" << endl;
            return -1;
        case 0:
            return 0;
        }
    }

    //打印日历表
    void printCalendar(){
        SetColor(10,FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
        if(errorPro() < 0)
            return;
//        cout << "input date is " << first_day_week << endl;
        cout << " Sun. Mon. Tue. Wes. Ths. Fri. Sat." << endl;
        int mark = 0;
        for(int i = 0;i 6){
                mark = 0;
                cout << endl;
            }
        }
        cout << endl;
    }

private:
    int year;       //年份
    int month;      //月份
    int day;        //日期
    int first_day_week; //该月第一天的星期数
    int err;
};

int main()
{
    int year,month,day;
    mydate *date;
    SetColor(10,FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    while(1){
        cout << endl;
        cout << "Input date : ";
        cin >> year >> month >> day;
        cout << endl;
        date = new mydate(year,month,day);
        date->getFirstWeek();
        date->printCalendar();
        delete date;
    }
    return 0;
}

注:颜色控制部分是windows下的,如果在linux下执行,需要删去。下为运行结果:

突出显示当日的c++日历代码_第1张图片


控制台颜色设置相关博客:

http://blog.csdn.net/odaynot/article/details/7722240

你可能感兴趣的:(编程技术,C++,日历,编程)