简单的万年历(C++)

这篇博客分享了作者初次尝试编写万年历程序的心得,通过C++实现了功能,虽然不完美,但能帮助初学者理解编程思路。
摘要由CSDN通过智能技术生成

这个也算是一个小项目了,第一次接触的时候一头雾水,完全不知道怎么去写,看到源码后感觉也没太难,只是没想到方法,下面我放上代码,不是很完美,看看就行。

#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;

void OutPut(int,int,int);  /*输出*/
int DayNum(int,int);  /*计算从公元1月1日到所输入的时间的前一个月的最后一天的总天数*/
int Distinguish(int,int);/*判断月份的类型,确定此月的天数*/
char Content();/*输出一目录,提示用户选择后序的操作,并返回用户的选择*/

int main()
{
	char r; //用于记录用户的选择。
	int year,month; 
	cout << "请输入年份:";
	cin >> year;
	cout << "请输入月份:";
	cin >> month;
	do
	{   
		system("cls"); //清屏
		cout << year << "年" << month << "月:" << endl;		
		OutPut(DayNum(year,month),year,month);
		r=Content();
		if(r=='>')              /*根据用户的选择进行相应的处理*/
		{
	        month=month+1; 
			if(month>=
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单C++ 万年历程序,可以输入年份和月份,输出该月份的日历: ``` #include <iostream> using namespace std; // 判断是否是闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } // 获取某年某月的天数 int getDaysOfMonth(int year, int month) { if (month == 2) { return isLeapYear(year) ? 29 : 28; } else if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else { return 31; } } // 获取某年某月第一天是星期几 int getFirstDayOfWeek(int year, int month) { int y = year; int m = month; if (m == 1 || m == 2) { m += 12; y--; } int d = 1; int w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7; return w; } // 输出日历 void printCalendar(int year, int month) { cout << " " << year << "年" << month << "月" << endl; cout << "日 一 二 三 四 五 六" << endl; int firstDayOfWeek = getFirstDayOfWeek(year, month); int daysOfMonth = getDaysOfMonth(year, month); int day = 1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < firstDayOfWeek) { cout << " "; } else if (day > daysOfMonth) { break; } else { printf("%2d ", day++); } } cout << endl; if (day > daysOfMonth) { break; } } cout << endl; } int main() { int year, month; cout << "请输入年份和月份:" << endl; cin >> year >> month; printCalendar(year, month); return 0; } ``` 注意:这只是一个简单的实现,还有很多细节需要考虑,比如输入的年份和月份是否合法,输出的格式是否美观等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值