C语言编写一个带农历的万年历

C语言编写一个带农历的万年历,第1张

农历算法太复杂了,我查了一下,羡誉有个人写出来了,但是我怎么看也看不懂,不知道你能不能看懂! (只是探讨一下,我也想写个农历算法)

首先,我们要确定一个时刻,作为一天的起点(包括这个时刻)。然后我们以月亮通过朔望交界点的那一

天作为每个月的第一天,以太阳通过雨水点的那个月作为正月,依次的,以太阳通过各中气点的那个月作为

二月、三月等等。 经过长期的观察之后,我们就会发现每个月不是29天就是30天,每年不是12个月就是13

个月,每个中气点唯一对应一个月 ,但是有的月却没有中气。为了制定我们的历法,我们至少要知道足够

精度的朔望月长度, 记为ML,和两个中气的间隔时间记为YL,这两个值经过前人长期不懈的观测和计算,

我们知道分别为29.5306日和30.4377日。 接下来,我们要随便找到某一年,以太阳通过雨水点的时刻到

那一年开始的那个时刻的时间,记为亮派滚DY,以日为单位, 还要找出这一年正月里,月亮通过朔望交界点的时

刻到那一月开始的那个时刻的时间,记为DM,以日为单位。这两个值是要通过实地观测得出来的。 这个时候

,我们就可以通过递推来制订我们的历法,以预测未来月球和太阳的运行情况,并进一步预测月相的变化和

气候的变化。

在本算法中我们使用了三个近似处理:

每天的长度总是一样的;

每个朔望月的长度总是一样的;

每两个相邻中气的时间总是一样的。

接下来,我们就逐月推算每月的大小:

Select Case Fix(DM + ML)

Case is = 29

The month have 29 days.

The DM of next month is DM + ML - 29.

Case is = 30

The month have 30 days.

The DM of next month is DM + ML - 30.

End Select

每年中各月的月份、是否为闰月

For i = 1 to 12

This month has A days.

If DY>=A Then

This month is an Extra Month.

DY = DY - A

Else

This month is the i Month.

DY = DY + YL - A

i = i + 1

End If

Next

这就是我们的核心算法。通过它,我们可以从任何一个包含中气的月份开始向后推算各月各年的情况。

由于我们所用的DM、DY、ML、YL都是近似值,若干年后,我们的历法必有偏差,我们只要重新观测,

更新DM、DY、ML、YL的值就可敬余以了。事实上,诸朝气数最多不过400年,在这些值精确到万分位的时候,

通行一朝是完全可以的。

下面对大小月的排列和闰月的出现作一些定性分析

显然0≤DM<1,那么

DM<0.4694时,当月为小月,下月为大月

DM≥0.4694时,当月为大月

DM<0.4694 * 2时,下月为小月,下下月为大月

DM≥0.4694 * 2时,下月亦为大月,下下月为小月

所以,大小月一般是交替出现,有时会出现连续两个月是大月的情况,但不会出现连续三个

月或更多的是大月的情况,也不会出现连续两个月更多的是小月的情况。也就是说,一般是小月大

月相继出现一段时间后,接着就出现一个大月,然后又是小月大月相继出现,具体的间隔也不定,

大家可以通过计算机推算后,找点规律出来。

一年中最少有十二个月,因为YL * 12 (代表十二个中气循环一次的时间)> ML * 12

(代表十二个朔望月的时间) + 1 (第一个月的DM总小于1),最多有十三个月,因为30

(第一个月的DY总小于30) + YL * 12 < ML * 14。两个相邻中气的间隔时间大于30,

所以一个月不可能有两个中气,那么一年有十二个月的时候,12个中气必然刚好依次各居一个月,

这一年就称为平年,一年有十三个月的时候,必然有且仅有一个月没有中气,那个月就时闰月,

这一年就称为闰年。平年的时候,根据大小月的排列规则,一年最多有8个大月,最少有6个大月,

因为ML * 12 + 1 < 30 * 8 + 29 * (12 - 8),所以不可能有8个大月,最多为7个大月,

那么一年就为354或355天。闰年的时候,根据大小月的排列规则,一年最多有9个大月,

最少有6个大月,因为ML * 13 + 1 < 30 * 8 + 29 * (13 - 8),所以不可能有8个或更多的大月,

最多为7个大月,那么一年就为383或384天。

因为每月的天数不固定,所以闰月的出现规律也不好讨论,连续十二个月的天数也不固定,

所以闰年的出现规律也不好讨论。不过可以肯定是,每年的第一个月肯定不是闰月,因为我们

是以包含中气雨水的月份作为正月,也就是每年的第一个月的。所以,闰月必定出现在某个有

名字的月份后面,出现在哪个后面就叫闰哪月。

自己整理的,专业分享学习高质量代码,每周期布置学习任务,激发学习C#兴趣!

用法:

DateTime dt = mc.SelectionStart

c = new EcanChineseCalendar(dt)

StringBuilder dayInfo = new StringBuilder()

dayInfo.Append("阳历:" + c.DateString + "r ")//闭陪汪阳历日期

dayInfo.Append("农历:" + c.ChineseDateString + "r ")//农历日期

dayInfo.Append("星期:" + c.WeekDayStr)//星期

dayInfo.Append("时辰:" + c.ChineseHour + "r ")//时辰

dayInfo.Append("属相:" + c.AnimalString + "r ")//属相

dayInfo.Append("节气:" + c.ChineseTwentyFourDay + "r ")//节气

dayInfo.Append("前一个节气:" + c.ChineseTwentyFourPrevDay + "r ")//前一个节气

dayInfo.Append("下一个节气:" + c.ChineseTwentyFourNextDay + "r ")//下一个节气

dayInfo.Append("节日:" + c.DateHoliday + "r ")//节日

dayInfo.Append("干支:" + c.GanZhiDateString + "r ")//干支

dayInfo.Append("星宿:" + c.ChineseConstellation + "r ")//星宿

dayInfo.Append(" 星座 :" + c.Constellation + "r ")/乱雀/ 星座

using System

using System.Collections.Generic

using System.Text

namespace AH.BI.MWeb.Service

{

#region ChineseCalendarException

///

public class EcanChineseCalendar

{

#region 内部结构

private struct SolarHolidayStruct//阳历

{

public int Month

public int Day

public int Recess//假期长度

public string HolidayName

public SolarHolidayStruct(int month, int day, int recess, string name)

{

Month = month

Day = day

Recess = recess

HolidayName = name

}

}

private struct LunarHolidayStruct//轿仔农历

{

public int Month

public int Day

public int Recess

public string HolidayName

public LunarHolidayStruct(int month, int day, int recess, string name)

{

Month = month

Day = day

Recess = recess

HolidayName = name

}

}

private struct WeekHolidayStruct

{

public int Month

public int WeekAtMonth

public int WeekDay

public string HolidayName

public WeekHolidayStruct(int month, int weekAtMonth, int weekDay, string name)

{

Month = month

WeekAtMonth = weekAtMonth

WeekDay = weekDay

HolidayName = name

}

}

#endregion

#region 内部变量

private DateTime _date

private DateTime _datetime

private int _cYear

private int _cMonth

private int _cDay

private bool _cIsLeapMonth//当月是否闰月

private bool _cIsLeapYear//当年是否有闰月

#endregion

#region 基础数据

#region 基本常量

private const int MinYear = 1900

private const int MaxYear = 2050

private static DateTime MinDay = new DateTime(1900, 1, 30)

private static DateTime MaxDay = new DateTime(2049, 12, 31)

private const int GanZhiStartYear = 1864//干支计算起始年

private static DateTime GanZhiStartDay = new DateTime(1899, 12, 22)//起始日

private const string HZNum = "零一二三四五六七八九"

private const int AnimalStartYear = 1900//1900年为鼠年

private static DateTime ChineseConstellationReferDay = new DateTime(2007, 9, 13)//28星宿参考值,本期为角

#endregion

#region 阴历数据

///

#endregion

#region 星座 名称

private static string[] _constellationName =

{

"白羊座", "金牛座", "双子座",

"巨蟹座", "狮子座", "处女座",

"天秤座", "天蝎座", "射手座",

"摩羯座", "水瓶座", "双鱼座"

}

#endregion

#region 二十四节气

private static string[] _lunarHolidayName =

{

"小寒", "大寒", "立春", "雨水",

"惊蛰", "春分", "清明", "谷雨",

"立夏", "小满", "芒种", "夏至",

"小暑", "大暑", "立秋", "处暑",

"白露", "秋分", "寒露", "霜降",

"立冬", "小雪", "大雪", "冬至"

}

#endregion

#region 二十八星宿

private static string[] _chineseConstellationName =

{

//四 五 六 日 一 二 三

"角木蛟","亢金龙","女土蝠","房日兔","心月狐","尾火虎","箕水豹",

"斗木獬","牛金牛","氐土貉","虚日鼠","危月燕","室火猪","壁水獝",

"奎木狼","娄金狗","胃土彘","昴日鸡","毕月乌","觜火猴","参水猿",

"井木犴","鬼金羊","柳土獐","星日马","张月鹿","翼火蛇","猎水蚓"

}

#endregion

#region 节气数据

private static string[] SolarTerm = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" }

private static int[] sTermInfo = new int[] { 0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758 }

#endregion

#region 农历相关数据

private static string ganStr = "甲乙丙丁戊己庚辛壬癸"

private static string zhiStr = "子丑寅卯辰巳午未申酉戌亥"

private static string animalStr = "鼠牛虎兔龙蛇马羊猴鸡狗猪"

private static string nStr1 = "日一二三四五六七八九"

private static string nStr2 = "初十廿卅"

private static string[] _monthString =

{

"出错","正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","腊月"

}

#endregion

#region 按公历计算的节日

private static SolarHolidayStruct[] sHolidayInfo = new SolarHolidayStruct[]{

new SolarHolidayStruct(1, 1, 1, "元旦"),

new SolarHolidayStruct(2, 2, 0, "世界湿地日"),

new SolarHolidayStruct(2, 10, 0, "国际气象节"),

new SolarHolidayStruct(2, 14, 0, "情人节"),

new SolarHolidayStruct(3, 1, 0, "国际海豹日"),

new SolarHolidayStruct(3, 5, 0, "学雷锋纪念日"),

new SolarHolidayStruct(3, 8, 0, "妇女节"),

new SolarHolidayStruct(3, 12, 0, "植树节 孙中山逝世纪念日"),

new SolarHolidayStruct(3, 14, 0, "国际警察日"),

new SolarHolidayStruct(3, 15, 0, "消费者权益日"),

new SolarHolidayStruct(3, 17, 0, "中国国医节 国际航海日"),

new SolarHolidayStruct(3, 21, 0, "世界森林日 消除种族歧视国际日 世界儿歌日"),

new SolarHolidayStruct(3, 22, 0, "世界水日"),

new SolarHolidayStruct(3, 24, 0, "世界防治结核病日"),

new SolarHolidayStruct(4, 1, 0, "愚人节"),

new SolarHolidayStruct(4, 7, 0, "世界卫生日"),

new SolarHolidayStruct(4, 22, 0, "世界地球日"),

new SolarHolidayStruct(5, 1, 1, "劳动节"),

new SolarHolidayStruct(5, 2, 1, "劳动节假日"),

new SolarHolidayStruct(5, 3, 1, "劳动节假日"),

new SolarHolidayStruct(5, 4, 0, "青年节"),

new SolarHolidayStruct(5, 8, 0, "世界红十字日"),

new SolarHolidayStruct(5, 12, 0, "国际护士节"),

new SolarHolidayStruct(5, 31, 0, "世界无烟日"),

new SolarHolidayStruct(6, 1, 0, "国际儿童节"),

new SolarHolidayStruct(6, 5, 0, "世界环境保护日"),

new SolarHolidayStruct(6, 26, 0, "国际禁毒日"),

new SolarHolidayStruct(7, 1, 0, "建党节 香港回归纪念 世界建筑日"),

new SolarHolidayStruct(7, 11, 0, "世界人口日"),

new SolarHolidayStruct(8, 1, 0, "建军节"),

new SolarHolidayStruct(8, 8, 0, "中国男子节 父亲节"),

new SolarHolidayStruct(8, 15, 0, "抗日战争胜利纪念"),

new SolarHolidayStruct(9, 9, 0, "毛主席逝世纪念"),

new SolarHolidayStruct(9, 10, 0, "教师节"),

new SolarHolidayStruct(9, 18, 0, "九·一八事变纪念日"),

new SolarHolidayStruct(9, 20, 0, "国际爱牙日"),

new SolarHolidayStruct(9, 27, 0, "世界 旅游 日"),

new SolarHolidayStruct(9, 28, 0, "孔子诞辰"),

new SolarHolidayStruct(10, 1, 1, "国庆节 国际音乐日"),

new SolarHolidayStruct(10, 2, 1, "国庆节假日"),

new SolarHolidayStruct(10, 3, 1, "国庆节假日"),

new SolarHolidayStruct(10, 6, 0, "老人节"),

new SolarHolidayStruct(10, 24, 0, "联合国日"),

new SolarHolidayStruct(11, 10, 0, "世界青年节"),

new SolarHolidayStruct(11, 12, 0, "孙中山诞辰纪念"),

new SolarHolidayStruct(12, 1, 0, "世界艾滋病日"),

new SolarHolidayStruct(12, 3, 0, "世界残疾人日"),

new SolarHolidayStruct(12, 20, 0, "澳门回归纪念"),

new SolarHolidayStruct(12, 24, 0, "平安夜"),

new SolarHolidayStruct(12, 25, 0, "圣诞节"),

new SolarHolidayStruct(12, 26, 0, "毛主席诞辰纪念")

}

#endregion

#region 按农历计算的节日

private static LunarHolidayStruct[] lHolidayInfo = new LunarHolidayStruct[]{

new LunarHolidayStruct(1, 1, 1, "春节"),

new LunarHolidayStruct(1, 15, 0, "元宵节"),

new LunarHolidayStruct(5, 5, 0, "端午节"),

new LunarHolidayStruct(7, 7, 0, "七夕情人节"),

new LunarHolidayStruct(7, 15, 0, "中元节 盂兰盆节"),

new LunarHolidayStruct(8, 15, 0, "中秋节"),

new LunarHolidayStruct(9, 9, 0, "重阳节"),

new LunarHolidayStruct(12, 8, 0, "腊八节"),

new LunarHolidayStruct(12, 23, 0, "北方小年(扫房)"),

new LunarHolidayStruct(12, 24, 0, "南方小年(掸尘)"),

//new LunarHolidayStruct(12, 30, 0, "除夕") //注意除夕需要其它方法进行计算

}

#endregion

#region 按某月第几个星期几

private static WeekHolidayStruct[] wHolidayInfo = new WeekHolidayStruct[]{

new WeekHolidayStruct(5, 2, 1, "母亲节"),

new WeekHolidayStruct(5, 3, 1, "全国助残日"),

new WeekHolidayStruct(6, 3, 1, "父亲节"),

new WeekHolidayStruct(9, 3, 3, "国际和平日"),

new WeekHolidayStruct(9, 4, 1, "国际聋人节"),

new WeekHolidayStruct(10, 1, 2, "国际住房日"),

new WeekHolidayStruct(10, 1, 4, "国际减轻自然灾害日"),

new WeekHolidayStruct(11, 4, 5, "感恩节")

}

#endregion

#endregion

#region 构造函数

#region ChinaCalendar

///

CheckDateLimit(dt)

_date = dt.Date

_datetime = dt

//农历日期计算部分

leap = 0

temp = 0

TimeSpan ts = _date - EcanChineseCalendar.MinDay//计算两天的基本差距

offset = ts.Days

for (i = MinYeari 0)

{

_cIsLeapYear = true

}

else

{

_cIsLeapYear = false

}

_cIsLeapMonth = false

for (i = 1i 0) &&(i == leap + 1) &&(_cIsLeapMonth == false))

{

_cIsLeapMonth = true

i = i - 1

temp = GetChineseLeapMonthDays(_cYear)//计算闰月天数

}

else

{

_cIsLeapMonth = false

temp = GetChineseMonthDays(_cYear, i)//计算非闰月天数

}

offset = offset - temp

if (offset GetChineseMonthDays(cy, cm))

{

throw new newCalendarException("不合法的农历日期")

}

offset = offset + cd//加上当月的天数

#endregion

}

else //是闰年,且计算月份大于或等于闰月

{

#region ...

for (i = 1i <cmi++)

{

Temp = GetChineseMonthDays(cy, i)//计算非闰月天数

offset = offset + Temp

}

if (cm >leap) //计算月大于闰月

{

Temp = GetChineseLeapMonthDays(cy)//计算闰月天数

offset = offset + Temp//加上闰月天数

if (cd >GetChineseMonthDays(cy, cm))

{

throw new newCalendarException("不合法的农历日期")

}

offset = offset + cd

}

else //计算月等于闰月

{

//如果需要计算的是闰月,则应首先加上与闰月对应的普通月的天数

if (this._cIsLeapMonth == true) //计算月为闰月

{

Temp = GetChineseMonthDays(cy, cm)//计算非闰月天数

offset = offset + Temp

}

if (cd >GetChineseLeapMonthDays(cy))

{

throw new newCalendarException("不合法的农历日期")

}

offset = offset + cd

}

#endregion

}

_date = MinDay.AddDays(offset)

}

#endregion

#endregion

#region 私有函数

#region GetChineseMonthDays

//传回农历 y年m月的总天数

private int GetChineseMonthDays(int year, int month)

{

if (BitTest32((LunarDateArray[year - MinYear] &0x0000FFFF), (16 - month)))

{

return 30

}

else

{

return 29

}

}

#endregion

#region GetChineseLeapMonth

//传回农历 y年闰哪个月 1-12 , 没闰传回 0

private int GetChineseLeapMonth(int year)

{

return LunarDateArray[year - MinYear] &0xF

}

#endregion

#region GetChineseLeapMonthDays

//传回农历 y年闰月的天数

private int GetChineseLeapMonthDays(int year)

{

if (GetChineseLeapMonth(year) != 0)

{

if ((LunarDateArray[year - MinYear] &0x10000) != 0)

{

return 30

}

else

{

return 29

}

}

else

{

return 0

}

}

#endregion

#region GetChineseYearDays

///

sumDay = 348//29天 X 12个月

i = 0x8000

info = LunarDateArray[year - MinYear] &0x0FFFF

//计算12个月中有多少天为30天

for (int m = 0m <12m++)

{

f = info &i

if (f != 0)

{

sumDay++

}

i = i >>1

}

return sumDay + GetChineseLeapMonthDays(year)

}

#endregion

#region GetChineseHour

///

int _hour, _minute, offset, i

int indexGan

string ganHour, zhiHour

string tmpGan

//计算时辰的地支

_hour = dt.Hour//获得当前时间小时

_minute = dt.Minute//获得当前时间分钟

if (_minute != 0) _hour += 1

offset = _hour / 2

if (offset >= 12) offset = 0

//zhiHour = zhiStr[offset].ToString()

//计算天干

TimeSpan ts = this._date - GanZhiStartDay

i = ts.Days % 60

indexGan = ((i % 10 + 1) * 2 - 1) % 10 - 1//ganStr[i % 10] 为日的天干,(n*2-1) %10得出地支对应,n从1开始

tmpGan = ganStr.Substring(indexGan) + ganStr.Substring(0, indexGan + 2)//凑齐12位

//ganHour = ganStr[((i % 10 + 1) * 2 - 1) % 10 - 1].ToString()

return tmpGan[offset].ToString() + zhiStr[offset].ToString()

}

#endregion

#region CheckDateLimit

///

#region CheckChineseDateLimit

///

int leap = GetChineseLeapMonth(year)// 计算该年应该闰哪个月

if ((leapMonth == true) &&(month != leap))

{

throw new newCalendarException("非法农历日期")

}

}

#endregion

#region ConvertNumToChineseNum

///

#region BitTest32

///

if ((bitpostion >31) || (bitpostion <0))

throw new Exception("Error Param: bitpostion[0-31]:" + bitpostion.ToString())

int bit = 1 day)

{

if ((week - 1) * 7 + day + firWeekDays == date.Day)

{

ret = true

}

}

else

{

if (day + firWeekDays + (week - 2) * 7 == date.Day)

{

ret = true

}

}

}

}

return ret

}

#endregion

#endregion

#region 属性

#region 节日

#region newCalendarHoliday

///

tempStr = lh.HolidayName

break

}

}

//对除夕进行特别处理

if (this._cMonth == 12)

{

int i = GetChineseMonthDays(this._cYear, 12)//计算当年农历12月的总天数

if (this._cDay == i) //如果为最后一天

{

tempStr = "除夕"

}

}

}

return tempStr

}

}

#endregion

#region WeekDayHoliday

///

#region DateHoliday

///

foreach (SolarHolidayStruct sh in sHolidayInfo)

{

if ((sh.Month == _date.Month) &&(sh.Day == _date.Day))

{

tempStr = sh.HolidayName

break

}

}

return tempStr

}

}

#endregion

#endregion

#region 公历日期

#region Date

///

#region WeekDay

///

#region WeekDayStr

///

#region DateString

///

#region IsLeapYear

///

#region ChineseConstellation

///

TimeSpan ts = this._date - ChineseConstellationReferDay

offset = ts.Days

modStarDay = offset % 28

return (modStarDay >= 0 ? _chineseConstellationName[modStarDay] : _chineseConstellationName[27 + modStarDay])

}

}

#endregion

#region ChineseHour

///

#endregion

#region 农历日期

#region IsChineseLeapMonth

///

#region IsChineseLeapYear

///

#region ChineseDay

///

#region ChineseDayString

///

}

}

}

#endregion

#region ChineseMonth

///

#region ChineseMonthString

///

#region ChineseYear

///

#region ChineseYearString

///

#region ChineseDateString

///

#region ChineseTwentyFourDay

///

y = this._date.Year

for (int i = 1i = 1i--)

{

num = 525948.76 * (y - 1900) + sTermInfo[i - 1]

newDate = baseDateAndTime.AddMinutes(num)//按分钟计算

if (newDate.DayOfYear <_date.DayOfYear)

{

tempStr = string.Format("{0}[{1}]", SolarTerm[i - 1], newDate.ToString("yyyy-MM-dd"))

break

}

}

return tempStr

}

}

//当前日期后一个最近节气

public string ChineseTwentyFourNextDay

{

get

{

DateTime baseDateAndTime = new DateTime(1900, 1, 6, 2, 5, 0)//#1/6/1900 2:05:00 AM#

DateTime newDate

double num

int y

string tempStr = ""

y = this._date.Year

for (int i = 1i _date.DayOfYear)

{

tempStr = string.Format("{0}[{1}]", SolarTerm[i - 1], newDate.ToString("yyyy-MM-dd"))

break

}

}

return tempStr

}

}

#endregion

#endregion

#region 星座

#region Constellation

///

if (((y >= 321) &&(y = 420) &&(y = 521) &&(y = 621) &&(y = 723) &&(y = 823) &&(y = 923) &&(y = 1023) &&(y = 1122) &&(y = 1222) || (y = 120) &&(y = 219) &&(y

#include <windows.h>棚芹

#include <winnt.h>

#include<iostream>

#include<iomanip>

using namespace std

int week(int,int,int) //根据年月日判断星期几

int leap_year(int)//判断闰年

void display_year(int ) //显示某年岁粗日历

void demand_day(int,int,int) //查询某天

int main()

{

int y,m,d,es=1

while(es)

{

HANDLE consolehwnd

consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE)

SetConsoleTextAttribute(consolehwnd,12)

cout<<"请选择 *** 作:\n1→显示某年日历\

\n2→查询某天\n0→退出"<<endl

char tp[20]cin>>tp

if(tp[1]!='\0'||tp[0]>'2'||tp[0]<'0'){cout<<链雀毕"输入有误"<<endlcontinue}

switch(tp[0]-48)

{

case 1:{cout<<"请输入年份:"cin>>ysystem("cls")display_year(y)break}

case 2:{cout<<"请输入年、月、日,以空格分开:"cin>>y>>m>>dsystem("cls")

demand_day(y,m,d)break}

case 0:{es=0break}

}

}

return 0

}

//-----根据年月日判断星期几-------------------------

int week(int y,int m, int d)

{

int week1,yy=y

if(m==1) {m=13yy--}

if(m==2) {m=14yy--}

week1=(d+2*m+3*(m+1)/5+yy+yy/4-yy/100+yy/400)%7

int s

switch (week1)

{

case 0: s=1break

case 1: s=2break

case 2: s=3break

case 3: s=4break

case 4: s=5break

case 5: s=6break

case 6: s=0break

}

return s

}

//----判断闰年-------------------------------------

int leap_year(int y)

{

int i

if((y%4==0&&y%100!=0)||y%400==0)i=1

else i=0

return i

}

//--------显示某年日历------------------------

void display_year(int y)

{

int n1,n2,i,j,a[13],c,d

HANDLE consolehwnd

consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE)

SetConsoleTextAttribute(consolehwnd,5)

cout<<setw(38)<<y<<"年"<<endl

cout<<setw(28)<<"*********"

for(i=1i<=27i++)cout<<'*'

cout<<endl

a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31//

a[4]=a[6]=a[9]=a[11]=30 //确定每月天数

if(leap_year(y))a[2]=29

else a[2]=28 //

for(i=1i<=11i+=2) //六次循环

{

SetConsoleTextAttribute(consolehwnd,1)

cout<<setw(14)<<i<<"月"<<setw(42)<<i+1<<"月"<<endl

SetConsoleTextAttribute(consolehwnd,2)

cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\

<<"四"<<setw(4)<<"五"<<setw(4)<<"六"

cout<<setw(16)<<' '

cout<<setw(4)<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)\

<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl

SetConsoleTextAttribute(consolehwnd,7)

n1=week(y,i,1)n2=week(y,i+1,1)

if(n1)//-----------

{

for(j=1j<=n1j++)//

cout<<setw(4)<<' '

}

for(j=1j<=7-n1j++)

cout<<setw(4)<<j

cout<<setw(16)<<' '

if(n2)

{//-----输出每次循环的第一行---

for(j=1j<=n2j++)

cout<<setw(4)<<' '

}

for(j=1j<=7-n2j++)

cout<<setw(4)<<j

cout<<endl //--------------

c=8-n1d=8-n2

for(int m=1m<6m++) //每月日历最多占六行

{

if(c>a[i])cout<<setw(4*7)<<' '//若c>a[i],则该月的这一行全部输出空格

for(j=cj<=a[i]j++)

{

cout<<setw(4)<<j

if((j-c+1)%7==0){c=j+1break}

if(j==a[i]){cout<<setw((6-week(y,i,a[i]))*4)<<' 'c=j+1break}

//如果j是该月最后一天,该行剩下的全部补空格

}

cout<<setw(16)<<' '

if(d>a[i+1])cout<<setw(4*7)<<' '

for(j=dj<=a[i+1]j++)

{ //

cout<<setw(4)<<j

if((j-d+1)%7==0){d=j+1break}

if(j==a[i+1]){cout<<setw((6-week(y,i+6,a[i+1]))*4)<<' 'd=j+1break}

}

cout<<endl

}

cout<<endl

}

cout<<endl

}

//--------查询某天------------

void demand_day(int y,int m,int d)

{

int n

HANDLE consolehwnd

consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE)

SetConsoleTextAttribute(consolehwnd,5)

n=week(y,m,d)

switch(n)

{

case 1:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期一"<<endlbreak

case 2:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期二"<<endlbreak

case 3:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期三"<<endlbreak

case 4:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期四"<<endlbreak

case 5:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期五"<<endlbreak

case 6:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期六"<<endlbreak

case 0:cout<<y<<"年"<<m<<"月"<<d<<"日"<<","<<"星期日"<<endlbreak

default:break

}

cout<<endl

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8243013.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存