C++输入年份月份输出日历,键盘控制月份增减

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
void print(int,int);
int main()
{	
	int year,month,ch1=0,ch2=0;
	cin>>year>>month;
	print(year,month);
	//识别键盘输入的“上、下”,据此调节year和month
	while(1)
    {
		if(ch1=getch())
        { 
            ch2=getch();
			if(ch2==72)
			{
			    if(month<12)
					month++;
				else
				{
					year++;
					month=1;
				}
				cout<<year<<' '<<month<<endl;
			    print(year,month);
			}
			else if(ch2==80)
			{
				if(month>=2)
					month--;
				else
				{
					year--;
					month=12;
				}
				cout<<year<<' '<<month<<endl;
				print(year,month);
            }
          }  
     }
	getch();
	return 0;
}
void print(int year,int month)
{
	int c,y,week,i,col=1,num;
	cout<<"日\t一\t二\t三\t四\t五\t六\n";
	if(month==1||month==3||month==5||month==7|month==8||month==10||month==12)
		num=31;
	else if(month==4||month==6||month==9||month==11)
		num=30;
	else if(month==2&&year%4==0&&year%100!=0)
		num=29;
	else
		num=28;
	if(month==1)
	{
		year=year-1;
		month=13;
	}
	if(month==2)
	{
		year=year-1;
		month=14;
	}
    c=year/100;
	y=year%100;
	//求当月的第一天是星期几
	week = ((c/4)-2*c+y+(y/4)+(26*(month+1)/10)) % 7;
	for(i=1;i<=week;i++,col++)
		cout<<'\t';
	col--;
	for(i=1;i<=num;i++)
	{
		if(col==7)
		{
			cout<<'\n';
			col=1;
		}
		else
			col++;
		cout<<i<<'\t';
	}
	cout<<'\n';
	cout<<'\n';
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值