小言_互联网的博客

c语言 八字排盘

416人阅读  评论(0)
#include<stdio.h>
#include<math.h>
int main()
{
	int runnian(int y);
	int tianshu(int y, int m, int d);
	int wnl(int y, int m, int d);
	int year, month, date, hour, minute,shichen;
	float x, r=0;
	long long int Days,F1,F;
	int Bazi[2][4];
	scanf("%d/%d/%d/%d/%d", &year, &month, &date, &hour, &minute);
	if (hour >= 23)
		date += 1;
	Days = wnl(year, month, date);
	Bazi[0][2] = (Days + 5) % 10;           //日元
	Bazi[1][2] = (Days + 3) % 12;            //日支
	if (hour % 2 == 0)
		shichen = hour / 2 + 1;
	else shichen = hour / 2 + 2;
	Bazi[1][3] = shichen % 12;               //时支
	Bazi[0][3] = (Bazi[0][2] * 2 + Bazi[1][3] - 2) % 10;           //时干
	F1 = tianshu(year, month, date);
	x = (month - 1) * 2;
	for (int i = 1900;i < year;i++)
		if (runnian(i) == 1)
			r += 1;
	F = (int)(0.242 * (year * 1.0 - 1900) + 6.2 + 15.22 * x - 1.9 * sin(0.262 * x) - r);
	if (F1 >= F)
		month -= 1;
	else month -= 2;
	if (month <= 0)
	{
		year -= 1;
		month += 12;
	}
	Bazi[0][0] = (year + 7) % 10;       //年干
	Bazi[1][0] = (year + 9) % 12;        //年支
	Bazi[1][1] = (month + 2) % 12;         //月令
	Bazi[0][1] = (Bazi[0][0] * 2 + month) % 10;       //月干
	for (int i = 0;i < 2;i++)
	{
		for (int j = 0;j < 4;j++)
			if (i == 0)
				switch (Bazi[i][j])
				{
				case 1:printf("甲\t");break;
				case 2:printf("乙\t");break;
				case 3:printf("丙\t");break;
				case 4:printf("丁\t");break;
				case 5:printf("戊\t");break;
				case 6:printf("己\t");break;
				case 7:printf("庚\t");break;
				case 8:printf("辛\t");break;
				case 9:printf("壬\t");break;
				default:printf("癸\t");break;
				}
			else
				switch (Bazi[i][j])
				{
				case 1:printf("子\t");break;
				case 2:printf("丑\t");break;
				case 3:printf("寅\t");break;
				case 4:printf("卯\t");break;
				case 5:printf("辰\t");break;
				case 6:printf("巳\t");break;
				case 7:printf("午\t");break;
				case 8:printf("未\t");break;
				case 9:printf("申\t");break;
				case 10:printf("酉\t");break;
				case 11:printf("戌\t");break;
				default:printf("亥\t");break;
				}
		printf("\n");
	}
	return 0;
}
int runnian(int y)
{
	if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
		return 1;
	else return 0;
}
int wnl(int y, int m, int d)
{
	int tianshu(int y, int m, int d);
	int z,r=0;
	for (int i = 1900;i < y;i++)
		if (runnian(i) == 1)
			r += 1;
	z = (y - 1901) * 365 + r + tianshu(y, m, d);
	return(z);
	
}
int tianshu(int y, int m, int d)
{
	int days=0;
	for (int i = 0;i < m;i++)
	{
		if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
			days += 31;
		else if (i == 2)
			days += 28;
		else if (i==4||i==6||i==9||i==11)
			days += 30;
	}
	days += d;
	if (runnian(y) == 1 && m > 2) 
		days += 1;
	return(days);
}

转载:https://blog.csdn.net/m0_48574790/article/details/106687542
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场