四柱推算小程序(绿皮编程生动案例)

参考了种种算法,最后的成品能推算1950~2050年间所有的年月日时干支,但是在春节前后可能不准。
NOIP赛前写下的小程序,用以放松心情,大家如果春节前后需要排盘还是百度一下比较好。
耿直的我直接先上代码好了:

/*
	Name: 四柱推算小程序
	Copyright: JLZ from CDQZ 
	Author: 怀尘先森 
	Date: 05/11/18 15:37
	Description: 请保留此文件头 
*/
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath> 
#include<ctime>
#include<Windows.h>
#include<WINBASE.H>
using namespace std;
const char *cTianGan[] = {"庚","辛","壬","癸","甲","乙","丙","丁","戊","己",};
const char *cDiZhi[] = {"申","酉","戌","亥","子","丑","寅","卯","辰","巳","午","未"};
const int yearfixday[101]={31,36,42,47,52,57,3,8,13,18,24,29,34,39,45,50,55,0,6,11,16,21,27,32,37,42,48,53,58,3,9,14,19,24,30,35,40,45,51,56,1,6,12,17,22,27,33,38,43,48,54,59,4,9,15,20,25,30,36,41,46,51,57,2,7,12,18,23,28,33,39,44,49,54,0,5,10,15,21,26,31,36,42,47,52,57,3,8,13,18,24,29,34,39,45,50,55,0,6,11};
const int monfixday[13]={0,6,37,0,31,1,32,2,33,4,34,5,35};
int main()
{
	int year=0,month=0,monTianGan=0,day=0,dayGanZhi=0,hour,hourTianGan=0;
	float fhour=0;
	printf("格式:如2018-11-4-18.50\n年-月-日-时\n");
	scanf("%d-%d-%d-%f",&year,&month,&day,&fhour);
	if (fhour>=23)
	{
		cout<<"请注意:晚上超过23点应该记为下一天\n";
		scanf("%d-%d-%d-%f",&year,&month,&day,&fhour);
	}
	if (month<=2) year--;
	cout<<cTianGan[year%10]<<cDiZhi[year%12]<<"年"<<endl;
	switch  (year%10)
	{
		case 4:case 9:monTianGan=5;break;
		case 5:case 0:monTianGan=7;break;
		case 6:case 1:monTianGan=-1;break;
		case 2:case 7:monTianGan=1;break;
		case 3:case 8:monTianGan=3;break;
		default: cout<<"erro code:2\n";break;
	}
	monTianGan+=month-1;
	cout<<cTianGan[monTianGan%10]<<cDiZhi[(month+4)%12]<<"月"<<endl;
	dayGanZhi=yearfixday[year-1950]+monfixday[month]+day;
	cout<<cTianGan[(dayGanZhi+3)%10]<<cDiZhi[(dayGanZhi+3)%12]<<"日"<<endl;
	hour=(fhour+1)/2;
	switch ((dayGanZhi+3)%10)
	{
		case 4:case 9:hourTianGan=4;break;
		case 5:case 0:hourTianGan=6;break;
		case 6:case 1:hourTianGan=8;break;
		case 2:case 7:hourTianGan=0;break;
		case 3:case 8:hourTianGan=2;break;
		default: cout<<"erro code:4\n";break;
	}
	hourTianGan+=hour; 
	cout<<cTianGan[hourTianGan%10]<<cDiZhi[(hour+4)%12]<<"时"<<endl;
	getchar();
	return 0;
}

以及第一版exe,微云链接:https://share.weiyun.com/5T5rgAs
完成于2018年11月4至五日。
写于NOIP考前第五天。
更新:修改了一个月份算不准的BUG,新增了两个BUG(元旦至春节年份算不准的BUG以及getchar()失效的BUG(最后用了While(1)死循环)),引用了破阵子_如是我闻的一个公农历转换函数。(https://www.cnblogs.com/liuzhong/archive/2012/04/05/2433519.html)
新增功能:输入0-0-0-0可以直接调用Windows的系统时间。

/*
	Name: 四柱推算小程序
	Copyright: JLZ from CDQZ 
	Author: 怀尘先森 
	Date: 05/11/18 15:37
	Description: 请保留此文件头 
*/
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath> 
#include<ctime>
#include<Windows.h>
#include<WINBASE.H>
using namespace std;
int year=0,month=0,nmonth=0,monTianGan=0,day=0,dayGanZhi=0,hour,hourTianGan=0;
float fhour=0;
const char *cTianGan[] = {"庚","辛","壬","癸","甲","乙","丙","丁","戊","己",};
const char *cDiZhi[] = {"申","酉","戌","亥","子","丑","寅","卯","辰","巳","午","未"};
const int yearfixday[101]={31,36,42,47,52,57,3,8,13,18,24,29,34,39,45,50,55,0,6,11,16,21,27,32,37,42,48,53,58,3,9,14,19,24,30,35,40,45,51,56,1,6,12,17,22,27,33,38,43,48,54,59,4,9,15,20,25,30,36,41,46,51,57,2,7,12,18,23,28,33,39,44,49,54,0,5,10,15,21,26,31,36,42,47,52,57,3,8,13,18,24,29,34,39,45,50,55,0,6,11};
const int monfixday[13]={0,6,37,0,31,1,32,2,33,4,34,5,35};
int GetDayOf(SYSTEMTIME pSt)
{
 /*农历日期名*/
 const char *cDayName[] = {"*","初一","初二","初三","初四","初五",
       "初六","初七","初八","初九","初十",
       "十一","十二","十三","十四","十五",
       "十六","十七","十八","十九","二十",
       "廿一","廿二","廿三","廿四","廿五",      
       "廿六","廿七","廿八","廿九","三十"};
 /*农历月份名*/
 const char *cMonName[] = {"*","正","二","三","四","五","六",
       "七","八","九","十","十一","腊"};
 /*公历每月前面的天数*/
 const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
 /*农历数据*/
 const int wNongliData[100] = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438
       ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
       ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
       ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
       ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
       ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
       ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
       ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
       ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
       ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};
 static int wCurYear,wCurMonth,wCurDay;
 static int nTheDate,nIsEnd,m,k,n,i,nBit;
 TCHAR szNongli[30], szNongliDay[10],szShuXiang[10];
 /*---取当前公历年、月、日---*/
 wCurYear = pSt.wYear;
 wCurMonth = pSt.wMonth;
 wCurDay = pSt.wDay;
 /*---计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)---*/
 nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd[wCurMonth - 1] - 38;
 if((!(wCurYear % 4)) && (wCurMonth > 2))
  nTheDate = nTheDate + 1;
 /*--计算农历天干、地支、月、日---*/
 nIsEnd = 0;
 m = 0;
 while(nIsEnd != 1)
 {
  if(wNongliData[m] < 4095)
   k = 11;
  else
   k = 12;
  n = k;
  while(n>=0)
  {
   //获取wNongliData(m)的第n个二进制位的值
   nBit = wNongliData[m];
   for(i=1;i<n+1;i++)
    nBit = nBit/2;
   nBit = nBit % 2;
   if (nTheDate <= (29 + nBit))
   {
    nIsEnd = 1;
    break;
   }
   nTheDate = nTheDate - 29 - nBit;
   n = n - 1;
  }
  if(nIsEnd)
   break;
  m = m + 1;
 }
 wCurYear = 1921 + m;
 wCurMonth = k - n + 1;
 wCurDay = nTheDate;
 if (k == 12)
 {
  if (wCurMonth == wNongliData[m] / 65536 + 1)
   wCurMonth = 1 - wCurMonth;
  else if (wCurMonth > wNongliData[m] / 65536 + 1)
   wCurMonth = wCurMonth - 1;
 }
 return wCurMonth<1?-1 * wCurMonth:wCurMonth;
}
int main()
{
	SYSTEMTIME sysTime;
	printf("格式:如2018-11-4-18.50\n年-月-日-时\n");
	scanf("%d-%d-%d-%f",&year,&month,&day,&fhour);
	if (fhour>=23)
	{
		cout<<"请注意:晚上超过23点应该记为下一天\n";
		scanf("%d-%d-%d-%f",&year,&month,&day,&fhour);
	}
	sysTime.wDay=day;
	sysTime.wMonth=month;
	sysTime.wYear=year;
	sysTime.wHour=float(fhour);
	if (year==0)
	{
		GetLocalTime(&sysTime);
		day=sysTime.wDay;
		month=sysTime.wMonth;
		year=sysTime.wYear;
		hour=sysTime.wHour;
		fhour=sysTime.wHour+sysTime.wMinute/60.00;
	}
	nmonth=GetDayOf(sysTime);
	cout<<cTianGan[year%10]<<cDiZhi[year%12]<<"年"<<endl;
	switch  (year%10)
	{
		case 4:case 9:monTianGan=5;break;
		case 5:case 0:monTianGan=7;break;
		case 6:case 1:monTianGan=-1;break;
		case 2:case 7:monTianGan=1;break;
		case 3:case 8:monTianGan=3;break;
		default: cout<<"erro code:2\n";break;
	} 
	monTianGan+=nmonth;
	cout<<cTianGan[monTianGan%10]<<cDiZhi[(nmonth+5)%12]<<"月"<<endl;
	dayGanZhi=yearfixday[year-1950]+monfixday[month]+day;
	cout<<cTianGan[(dayGanZhi+3)%10]<<cDiZhi[(dayGanZhi+3)%12]<<"日"<<endl;
	hour=(fhour+1)/2;
	switch ((dayGanZhi+3)%10)
	{
		case 4:case 9:hourTianGan=4;break;
		case 5:case 0:hourTianGan=6;break;
		case 6:case 1:hourTianGan=8;break;
		case 2:case 7:hourTianGan=0;break;
		case 3:case 8:hourTianGan=2;break;
		default: cout<<"erro code:4\n";break;
	}
	hourTianGan+=hour; 
	cout<<cTianGan[hourTianGan%10]<<cDiZhi[(hour+4)%12]<<"时"<<endl;
	while (1);
	return 0;
}

绿皮干支2.0的exe链接:https://share.weiyun.com/5bBPD1C
就是这样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值