C/C++教程

C++日历:不同颜色打印出给定年月的日历,若为本月可高亮标记出当前日期(附源码)

本文主要是介绍C++日历:不同颜色打印出给定年月的日历,若为本月可高亮标记出当前日期(附源码),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

效果图:
在这里插入图片描述

功能:

  1. 周六周日显示为红色
  2. 工作日显示为蓝色
  3. 若查询的是本月,则用绿色标记

更改颜色(以绿色为例):

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);

注意:改变颜色的效果会持续,如果只想改变某一句的颜色,改色后还需要改回去

GetStdHandle(STD_OUTPUT_HANDLE) 获取句柄(不用改)
FOREGROUND_INTENSITY / BACKGROUND_INTENSITY 选择更改前景色还是背景色
FOREGROUND_xxx 有RED、BLUE、GREEN可选,如果需要其他颜色需要用 | 连接,组合成不同颜色,背景色同理

下面是源码,有启发的话可以点个赞支持下~

#include<iostream>
#include<iomanip>
#include "windows.h"
SYSTEMTIME sys; //获取系统时间
using namespace std;

bool Judge_LeapYear(int year);  //判断是否为闰年
int Get_DaysOfMonth(int year, int month);   //获取这个月共有几天
int Get_AllDays(int year, int month);   //获取所有天数
int Get_startDay(int year, int month);  //获取首日是星期几
void Print_MonthHead(int year, int month);
void Print_MonthBody(int year, int month);
bool Juge_WeekDay(int year, int month, int day);    //判断是否为周末

int Is_Today(int year, int month); //附加功能,如果查询的是本月就把今天标记出来


int main() {
    int year,month;
    cout << "请输入要查询的年份:";
    cin >> year;
    cout << "请输入要查询的月份:";
    cin >> month;
    Print_MonthHead(year, month);
    Print_MonthBody(year, month);
}

bool Judge_LeapYear(int year) {
    return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}

int Get_DaysOfMonth(int year, int month) {

    if (month == 2) {
        //闰年的2月有29天
        return Judge_LeapYear(year) ? 29 : 28;
    }
    else {
        //大小月天数判断
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        else {
            return 31;
        }
    }

}

int Get_AllDays(int year, int month){
    int sum = 0;
    for (int i = 1800; i < year; i++) {
        if (Judge_LeapYear(i)) {
            sum = sum + 366;
        }
        else{
            sum = sum + 365;
        }
    }
    for (int j = 1; j < month; j++) {
        sum = sum + Get_DaysOfMonth(year, j);
    }
    return sum;
}

int Get_startDay(int year, int month){
    int firstday = 3;
    int alldays = Get_AllDays(year, month);
    return (firstday + alldays) % 7;
}

void Print_MonthHead(int year, int month){
    cout << endl<<"\t";
    switch (month) {
        case 1:
            cout << "January";
            break;
        case 2:
            cout << "February";
            break;
        case 3:
            cout << "March";
            break;
        case 4:
            cout << "April";
            break;
        case 5:
            cout << "May";
            break;
        case 6:
            cout << "June";
            break;
        case 7:
            cout << "July";
            break;
        case 8:
            cout << "August";
            break;
        case 9:
            cout << "September";
            break;
        case 10:
            cout << "October";
            break;
        case 11:
            cout << "November";
            break;
        case 12:
            cout << "December";

    }
    cout << " " << year << endl;
    cout<<"------------------------------"<<endl;
    //周六周日为红色显示,工作日为蓝色显示
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
    cout << " Sun";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
    cout << " Mon Tue Wed Thu Fri";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
    cout << " Sat"<<endl;
}

void Print_MonthBody(int year, int month){
    int startday = Get_startDay(year, month);   //startday也是该月1号对应的星期几
    int monthday = Get_DaysOfMonth(year, month);
    //打印第一天之前的空格,使日期与星期对应
    for (int i = 0; i < startday; i++) {
        cout << "    ";
    }

    for (int i = 1; i <= monthday; i++) {
        int today = 0;
        //如果查询的是本月就把今天标记出来
        if(Is_Today(year,month)){
            GetLocalTime( &sys );
            today = sys.wDay;
        }
        //如果日期对应周六周日就显示红色,其他显示蓝色
        if (Juge_WeekDay(year, month, i)) {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
        }
        else{
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
        }

        if (today == i) {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
        }
        //打印日期
        cout << setw(4) << i;

        if ((i + startday) % 7 == 0) {
            cout<<endl;
        }
    }
}

bool Juge_WeekDay(int year, int month, int day){
    if(month==1||month==2) //把一月和二月换算成上一年的十三月和十四月
    {
        month+=12;
        year--;
    }
    //使用吉姆拉尔森公式判断是否为周末
    int Week=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7;
    if (Week == 5 || Week == 6) {
        return true;
    }
    return false;
}

int Is_Today(int year, int month){
    GetLocalTime( &sys );
    if(year==sys.wYear&&month==sys.wMonth) return sys.wDay;
    return 0;
}
这篇关于C++日历:不同颜色打印出给定年月的日历,若为本月可高亮标记出当前日期(附源码)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!