打印日历和当前时间(简单易懂)

少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述打印日历和当前时间(简单易懂)相关的知识,希望能为你提供帮助。
想打印当前时间先了解tm数据类型,懂得c结构库

头文件


#pragma once
#include< iostream>
using namespace std;
#include< string>
#include< ctime>
void show(); //打印操作步骤
int judge(int year); //判断平年(365),闰年(366)
void add(int year, int month); //从1921年开始定义,计算1921年1月到输入的日历共有多少天
void print(int year, int month); //打印日历
void printnowtime(); //打印当前的时间
#pragma warning(disable:4996); //因为使用打印当前的时间所需要用的函数
//(vs准备弃用strcpy的,安全性较低,所以微软提供了strcpy_s来代替)有报错
//若要继续使用要加上#pragma warning(disable:4996);

函数文件
【打印日历和当前时间(简单易懂)】

#include"日历.h";
int leapmonth[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 }; //闰年
int ordinarymonth[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; //平年
int year, month;
int temp;
int day;
int sum = 0;
int year2 ;
void show()//打印操作步骤
{
cout < < "**********************" < < endl;
cout < < "*****1.查询日历*******" < < endl;
cout < < "*****2.退出日历*******" < < endl;
cout < < "**********************" < < endl;
}
int judge(int year)//判断平年(365),闰年(366)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
return 2;
}
else
{
return 1;
}
}
else
{
if (year % 4 == 0)
{
return 2;
}
else
{
return 1;
}
}
}
void add(int year, int month)//从1921年开始定义,计算1921年1月到输入的日历共有多少天(sum)
{
year2 = 1921;
for (int k = 0; k < year - 1921; k++)
{
int temp2 = judge(year2);
if (temp2 == 1)
{
for (int i = 0; i < 13; i++)
{
sum = sum + ordinarymonth[i];
}
}
else
{
for (int i = 0; i < 13; i++)
{
sum = sum + leapmonth[i];
}
}
year2++;
}
temp = judge(year);
for (int i = 0; i < month; i++)
{
if (temp == 1)
{
sum = sum + ordinarymonth[i];
}
else
{
sum = sum + leapmonth[i];
}
}

}
void print(int year, int month)//打印日历
{
day = (sum + 6) % 7;
if (day == 0)
{
day = 7;
}
int a = day;
cout < < year < < "年" < < month < < "月" < < endl;
cout < < " 一 " < < \\t < < " 二 " < < \\t < < " 三 " < < \\t < < " 四 " < < \\t < < " 五 " < < \\t < < " 六 " < < \\t < < " 日 " < < endl;
for (int i = 0; i < day - 1; i++)
{
cout < < "" < < \\t;
}
if (temp == 1)
{
for (int i = 1; i < = ordinarymonth[month]; i++)
{
cout < < " " < < i < < " " < < \\t;
if (a == 7)
{
cout < < endl;
a = 0;
}
a++;
}
}
else
{
for (int i = 1; i < = leapmonth[month]; i++)
{
cout < < " " < < i < < " " < < \\t;
if (a == 7)
{
cout < < endl;
a = 1;
}
a++;
}
}
sum = 0;
cout < < endl;
}
void printnowtime()//打印当前的时间
{
string xingqi[8] = { "日" ,"一","二","三","四","五","六"};
time_t systemtime = time(NULL);
tm* nowtime = localtime(& systemtime);
cout < < "当前时间为:";
cout < < 1900 + nowtime-> tm_year < < "年" < < 1 + nowtime-> tm_mon < < "月" < < nowtime-> tm_mday < < "日" < < endl;
cout < < "星期" < < xingqi[nowtime-> tm_wday] < < endl;
cout < < nowtime-> tm_hour < < ":" < < nowtime-> tm_min < < ":" < < nowtime-> tm_sec < < endl;
cout < < "----------------------------------------------------" < < endl;
}

主文件


#include< iostream>
using namespace std;
#include< string>
#include"日历.h"
int main()
{
int sum = 0;
int year, month;
int n = 0;
show(); //打印操作步骤
cout < < "请输入数字进行操作:";
cin > > n;
if (n < = 100000 & & n > = -1000000)
{
while (true)
{
sum = 0;
if (n == 1 || n == 2)
{
if (n == 1)
{
cout < < "请输入年份:";
cin > > year;
cout < < "请输入月份:";
cin > > month;
printnowtime(); //打印当前的时间
judge(year); //判断平年(365),闰年(366)
add(year, month); //从1921年开始定义,计算1921年1月到输入的日历共有多少天
print(year, month); //打印日历

}
else
{
cout < < "欢迎下次使用!" < < endl;
return 0;
}

}
else
{
cout < < "输入错误,请重新输入!" < < endl;
}
show();
cout < < "请输入数字进行操作:";
cin > > n;
system("cls"); //清屏
}
}
else
{
cout < < "输入错误,请退出重试!!!" < < endl;
return 0;
}
return 0;
}

效果

    推荐阅读