一步步实现一个完整的万年历

一、2015年的NOIP普及组初赛有一道代码补全的题目,很有意思,功能是实现一个当年任意月的月历显示。

为了让代码更有趣,我这里把它改为针对2022年的任意月的月历的显示。代码如下:

// 2022年,任一月份的月历 
#include<iostream>
using namespace std;
const int dayNum[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
	int m;
	cin >> m;
	cout << "S\tM\tT\tW\tT\tF\tS" << endl; 
	int offset=6;
	for (int i=1; i<m; i++) {
		offset= (offset+ dayNum[i])%7;
	}
	for (int i=0; i<offset; i++){
		cout << "\t";
	}
	for (int i=1; i<= dayNum[m]; i++){
		cout << i ;
		if (i==dayNum[m] || (i+offset)%7==0) {
			cout << endl;
		} else {
			cout << "\t";
		}
	}
	return 0;
}

我们学习软件的一个非常好的方式就是给现有的代码增加新的功能。从这个最基础的功能开始,我们逐步增加新的功能进来。

二、首先,这里是周日起点,我们想把它改为更符合中国人习惯的周一起点,并且使用中文。

 代码只须修改一点点:

#include<iostream>
using namespace std;
const int dayNum[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
	int m;
	cin >> m;
	cout << "一\t二\t三\t四\t五\t六\t日" << endl; 
	int offset=6;
	for (int i=1; i<m; i++) {
		offset= (offset+ dayNum[i])%7;
	}
	offset= (offset+6)%7;
	for (int i=0; i<offset; i++){
		cout << "\t";
	}
	for (int i=1; i<= dayNum[m]; i++){
		cout << i ;
		if (i==dayNum[m] || (i+offset)%7==0) {
			cout << endl;
		} else {
			cout << "\t";
		}
	}
	return 0;
}

 三、常见的月历中,周六周日用红色显示,我们可以使用windows的库实现这个功能。

注意设置演示的代码setColor的实现:

#include<iostream>
#include"windows.h"
using namespace std;
const int dayNum[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
void setColor(int ftColor, int bkColor=0){
	int aColor= bkColor*16+ ftColor;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), aColor);
}
int main(){
	int m;
	cin >> m;
	cout << "一\t二\t三\t四\t五\t六\t日" << endl; 
	int offset=6;
	for (int i=1; i<m; i++) {
		offset= (offset+ dayNum[i])%7;
	}
	offset= (offset+6)%7;
	for (int i=0; i<offset; i++){
		cout << "\t";
	}
	for (int i=1; i<= dayNum[m]; i++){
		int a= (i+offset)%7;
		if (a==0 || a==6) {
			setColor(12);
		} else {
			setColor(15);
		}
		cout << i ;
		if (i==dayNum[m] || a==0) {
			cout << endl;
		} else {
			cout << "\t";
		}
	}
	return 0;
}

四、下一步,本月的月历中经常会把上个月和下个月的结尾和开头部分带上,怎样实现这个功能呢?

 代码如下:

#include<iostream>
#include"windows.h"
using namespace std;
const int dayNum[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
void setColor(int ftColor, int bkColor=0){
	int aColor= bkColor*16+ ftColor;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), aColor);
}
int main(){
	int m;
	cin >> m;
	setColor(11);
	cout << "一\t二\t三\t四\t五\t六\t日" << endl; 
	int offset=6;
	for (int i=1; i<m; i++) {
		offset= (offset+ dayNum[i])%7;
	}
	offset= (offset+6)%7;
	// 前面补 
	setColor(8); 
	for (int i=0; i<offset; i++){
		cout << (dayNum[m-1]-offset+1)+i << "\t";
	}
	int a;
	setColor(15); 
	for (int i=1; i<= dayNum[m]; i++){
		a= (i+offset)%7;
		if (a==0 || a==6) {
			setColor(12);
		} else {
			setColor(15);
		}
		cout << i ;
		if (a==0) {
			cout << endl;
		} else {
			cout << "\t";
		}
	}
	/*
	后面补空 
	0 0
	6 1
	5 2
	4 3
	*/
	if (true){
		setColor(8);
		for (int i=1; i<= 7-a; i++){
			cout << i << "\t";
		} 
	}
	setColor(15); 
	return 0;
}

五、既然月历功能已经实现得比较完整了,我们的目标向年历进发

 为了实现年历,应对支持在任意位置打印的功能,注意这里的gotoxy函数。另外,由于月历功能已经完善,我们把它移入函数中。

#include<iostream>
#include"windows.h"
#include<iomanip> 
using namespace std;
const int dayNum[]={31,31,28,31,30,31,30,31,31,30,31,30,31};
void setColor(int ftColor, int bkColor=0){
	int aColor= bkColor*16+ ftColor;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), aColor);
}
void gotoxy(unsigned char x,unsigned char y){
    COORD cor;
    HANDLE hout;
    cor.X = x;
    cor.Y = y;
    hout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, cor);
}

void showMonth(int m, int x, int y){
	setColor(15);
	gotoxy(x,y);
	y++;
	cout << "          " << setw(2)<< m << "月"; 
	gotoxy(x,y);
	y++;
	cout << "一  二  三  四  五  六  日" << endl; 
	int offset=6;
	for (int i=1; i<m; i++) {
		offset= (offset+ dayNum[i])%7;
	}
	offset= (offset+6)%7;
	setColor(8);
	gotoxy(x,y);
	for (int i=0; i<offset; i++){
		cout << setw(2)<< (dayNum[m-1]-offset+1)+i << "  ";
	}
	int a;
	setColor(15); 
	for (int i=1; i<= dayNum[m]; i++){
		a= (i+offset)%7;
		if (a==0 || a==6) {
			setColor(12);
		} else {
			setColor(15);
		}
		cout << setw(2)<< i ;
		if (a==0) {
			cout << endl;
			y++;
			gotoxy(x,y);
		} else {
			cout << "  ";
		}
	}
	/*
	后面补空 
	0 0
	6 1
	5 2
	4 3
	*/
	if (true){
		setColor(8);
		for (int i=1; i<= 7-a; i++){
			cout << setw(2)<< i << "  ";
		} 
	}
	setColor(15); 
} 
int main(){
	for (int i=0; i<12; i++){
		showMonth(i+1, i%4* 30, i/4* 10);
	} 
	gotoxy(0, 30);
	return 0;
}

六、我们希望将年份信息以较大的字体显示出来,让年历效果更美观。这里简单使用了点阵字库。并且对代码进行扩展,支持任意年的年历。

 字库的使用,详见代码,字库文件的下载,参加文章末尾的下载链接。

#include<iostream>
#include"windows.h"
#include<iomanip> 
#include<fstream> 
using namespace std;
int dayNum[]={31,31,28,31,30,31,30,31,31,30,31,30,31};
void setColor(int ftColor, int bkColor=0){
	int aColor= bkColor*16+ ftColor;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), aColor);
}
void gotoxy(unsigned char x,unsigned char y){
    COORD cor;
    HANDLE hout;
    cor.X = x;
    cor.Y = y;
    hout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hout, cor);
}
struct Han{
	// 汉字本身 
	string han;
	// 用16个整数代表它的点阵表示 
	int arr[16];
}zi[7000];
int maxZi;
// 载入字库,用了fstream的方式 
void loadHan(){
	ifstream fin("点阵字库.txt");
	string s1;
	while (fin >> s1){
		zi[maxZi].han= s1;
		for (int i=0; i<16; i++){
			fin >> zi[maxZi].arr[i];
		}
		maxZi++;
	}
	fin.close(); 
}
string int2bit(int a){
	string ret="";
	for (int i=0; i<16; i++){
		ret= (a%2==0?" ":"*") + ret;
		a/=2;
	}
	return ret;
}
// 显示一个点阵汉字,参数为点阵数组 
void showHan(int dot[], int x, int y){
	for (int i=0; i<16; i++){
		gotoxy(x, y+i);
		cout << int2bit(dot[i]);
	}
}
// 查找一个汉字,返回它的数组编号,如果没有则返回-1
int findHan(string s1){
	s1= s1.substr(0,2);
	for (int i=0; i<maxZi; i++){
		if (zi[i].han==s1){
			return i;
		}
	}
	return -1;
} 
void showZi(string s1, int x, int y){
	int a= findHan(s1);
	if (a>=0){
		showHan(zi[a].arr, x, y);
	}
}
bool isLeapYear(int y){
	return (y%400==0 || y%4==0 && y%100!=0);
}
int calcFirstDay(int y){
	// 1900年1月1日,周一
	int offset=1;
	for (int i=1900; i<y; i++){
		offset= (offset+ (isLeapYear(i)?366:365))%7;
	} 
	return offset;
}
void showMonth(int year, int m, int x, int y){
	setColor(15);
	gotoxy(x,y);
	y++;
	cout << "          " << setw(2)<< m << "月"; 
	gotoxy(x,y);
	y++;
	cout << "一  二  三  四  五  六  日" << endl; 
	int offset= calcFirstDay(year);
	if (isLeapYear(year)){
		dayNum[2]=29;
	} else {
		dayNum[2]=28;
	}
	for (int i=1; i<m; i++) {
		offset= (offset+ dayNum[i])%7;
	}
	offset= (offset+6)%7;
	setColor(8);
	gotoxy(x,y);
	for (int i=0; i<offset; i++){
		cout << setw(2)<< (dayNum[m-1]-offset+1)+i << "  ";
	}
	int a;
	setColor(15); 
	for (int i=1; i<= dayNum[m]; i++){
		a= (i+offset)%7;
		if (a==0 || a==6) {
			setColor(12);
		} else {
			setColor(15);
		}
		cout << setw(2)<< i ;
		if (a==0) {
			cout << endl;
			y++;
			gotoxy(x,y);
		} else {
			cout << "  ";
		}
	}
	/*
	后面补空 
	0 0
	6 1
	5 2
	4 3
	*/
	if (true){
		setColor(8);
		for (int i=1; i<= 7-a; i++){
			cout << setw(2)<< i << "  ";
		} 
	}
	setColor(15); 
} 
void showYear(int y){
	int year=y; 
	string cn[10]={"零","一","二","三","四","五","六","七","八","九"};
	for (int i=0; i<4; i++){
		int a= y%10;
		showZi(cn[a], 10+18*(3-i), 2);
		y/=10;
	} 
	showZi("年", 82, 2);
	for (int i=0; i<12; i++){
		showMonth(year, i+1, i%4* 30, i/4* 10+20);
	} 
} 
int main(){
	loadHan();
	int y;
	cin >> y;
	//cout << calcFirstDay(y);
	//return 0;
	showYear(y);
	gotoxy(0, 50);
	return 0;
}

相关各个版本的源代码和字库文件的下载地址:

c++入门代码完整的改进过程趣味年历制作-C++文档类资源-CSDN下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

圣手书生肖让

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值