function ziwei(year,month,day,hour,minute)
% ziwei(year,month,day,hour,minute)
% 紫微排盘软件
% 输入测算者的出生年月日和时间
[yearGan,yearZhi,LunarMonth,monthGan,monthZhi,LunarDay,dayGan,dayZhi,hourZhi] = LunarConvert(year,month,day,hour,minute); %计算农历的年月日时的干支.
MingGongZhi = mod((monthZhi-1)-(hourZhi-1),12)+1; % 命宫: 生月月支顺数生时;
ShenGongZhi = mod((monthZhi-1)+(hourZhi-1),12)+1; % 身宫: 生月月支逆数生时;
MingGong = MingGongZhi;
XiongDiGong = mod((MingGongZhi-1)-1,12)+1; % 兄弟宫
FuQiGong = mod((MingGongZhi-2)-1,12)+1; % 夫妻宫
ZiNvGong = mod((MingGongZhi-3)-1,12)+1; % 子女宫
CaiBoGong = mod((MingGongZhi-4)-1,12)+1; % 财帛宫
JiEGong = mod((MingGongZhi-5)-1,12)+1; % 疾厄宫
QianYiGong = mod((MingGongZhi-6)-1,12)+1; % 迁移宫
PuYiGong = mod((MingGongZhi-7)-1,12)+1; % 仆役宫
GuanLuGong = mod((MingGongZhi-8)-1,12)+1; % 官禄宫
TianZhaiGong= mod((MingGongZhi-9)-1,12)+1; % 田宅宫
FuDeGong = mod((MingGongZhi-10)-1,12)+1; % 福德宫
FuMuGong = mod((MingGongZhi-11)-1,12)+1; % 父母宫
WuXingJuVal = [2 6 3 5 4 6 % 年的天干和月的地支对应的五行局表
6 5 4 3 2 5
5 3 2 4 6 3
3 4 6 2 5 4
4 2 5 6 3 2];
% NaYinWuXingVal = [ % 纳音五行对应的表, 行为天干, 列为地支.
% 4 0 2 0 6 0 4 0 2 0 6 0 % 水2, 木3, 金4, 土5, 火6
% 0 4 0 2 0 6 0 4 0 2 0 6
% 2 0 6 0 5 0 2 0 6 0 5 0
% 0 2 0 6 0 5 0 2 0 6 0 5
% 6 0 5 0 3 0 6 0 5 0 3 0
% 0 6 0 5 0 3 0 6 0 5 0 3
% 5 0 3 0 4 0 5 0 3 0 4 0
% 0 5 0 3 0 4 0 5 0 3 0 4
% 3 0 4 0 2 0 3 0 4 0 2 0
% 0 3 0 4 0 2 0 3 0 4 0 2];
WuXingJu = WuXingJuVal(mod(yearGan-1,5)+1, floor((MingGongZhi+1)/2)); %根据年干和命宫地支查表求得五行局
ShuiJuVal = [2,3]; %水二局的循环数值
MuJuVal = [5,2,3]; %木三局的循环数值
JinJuVal = [12,5,2,3];
TuJuVal = [7,12,5,2,3];
HuoJuVal = [10,7,12,5,2,3];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch WuXingJu % 确定紫微星所在的宫
case 2
ZiWei = ShuiJuVal(mod(LunarDay-1,2)+1) + floor(LunarDay/2);
case 3
ZiWei = MuJuVal(mod(LunarDay-1,3)+1) + floor(LunarDay/3);
case 4
ZiWei = JinJuVal(mod(LunarDay-1,4)+1) + floor(LunarDay/4);
case 5
ZiWei = TuJuVal(mod(LunarDay-1,5)+1) + floor(LunarDay/5);
case 6
ZiWei = HuoJuVal(mod(LunarDay-1,6)+1) + floor(LunarDay/6);
otherwise
error('错误的五行局数');
end
TianJi = mod((ZiWei-1)-1,12)+1; % 天机星
TaiYang = mod((ZiWei-1)-3,12)+1; % 太阳星
Wuqu = mod((ZiWei-1)-4,12)+1; % 武曲星
TianTong= mod((ZiWei-1)-5,12)+1; % 天同星
LianZhen= mod((ZiWei-1)-8,12)+1; % 廉贞星
TianFu = mod(2-((ZiWei-1)-2),12)+1; % 天府星
TaiYin = mod((TianFu-1)+1,12)+1; % 太阴星
TanLang = mod((TianFu-1)+2,12)+1; % 贪狼星
JuMen = mod((TianFu-1)+3,12)+1; % 巨门星
TianXiang = mod((TianFu-1)+4,12)+1; % 天相星
TianLiang = mod((TianFu-1)+5,12)+1; % 天梁星
QiSha = mod((TianFu-1)+6,12)+1; % 七杀星
PoJun = mod((TianFu-1)+10,12)+1; % 破军星
return;
function [yearGan,yearZhi,LunarMonth,monthGan,monthZhi,offset,dayGan,dayZhi,hourZhi] = LunarConvert(y,m,d,h,minute)
% function [yearGan,yearZhi,monthGan,monthZhi,hourZhi] = LunarConvert(y,m,d,h,minute)
%
% 农历查询,改编自hjdwg@smth的m文件
if nargin==0;
cccc=clock;
y=cccc(1);m=cccc(2);d=cccc(3);
end
Animals={'鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪'};
solarTerm ={'小寒','大寒','立春','雨水','惊蛰','春分', ...
'清明','谷雨','立夏','小满','芒种','夏至', ...
'小暑','大暑','立秋','处暑','白露','秋分', ...
'寒露','霜降','立冬','小雪','大雪','冬至'};
CnDayStr={'初一', '初二', '初三', '初四', '初五', ...
'初六', '初七', '初八', '初九', '初十', ...
'十一', '十二', '十三', '十四', '十五', ...
'十六', '十七', '十八', '十九', '二十', ...
'廿一', '廿二', '廿三', '廿四', '廿五', ...
'廿六', '廿七', '廿八', '廿九', '三十'};
CnMonthStr= { '正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊'};
monthName = {'JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'};
lunarInfo=[19416,19168,42352,21717,53856,55632,91476,22176,39632,21970, ...
19168,42422,42192,53840,119381,46400,54944,44450,38320,84343, ...
18800,42160,46261,27216,27968,109396,11104,38256,21234,18800, ...
25958,54432,59984,28309,23248,11104,100067,37600,116951,51536, ...
54432,120998,46416,22176,107956,9680,37584,53938,43344,46423, ...
27808,46416,86869,19872,42448,83315,21200,43432,59728,27296, ...
44710,43856,19296,43748,42352,21088,62051,55632,23383,22176, ...
38608,19925,19152,42192,54484,53840,54616,46400,46496,103846, ...
38320,18864,43380,42160,45690,27216,27968,44870,43872,38256, ...
19189,18800,25776,29859,59984,27480,21952,43872,38613,37600, ...
51552,55636,54432,55888,30034,22176,43959,9680,37584,51893, ...
43344,46240,47780,44368,21977,19360,42416,86390,21168,43312, ...
31060,27296,44368,23378,19296,42726,42208,53856,60005,54576, ...
23200,30371,38608,19415,19152,42192,118966,53840,54560,56645, ...
46496,22224,21938,18864,42359,42160,43600,111189,27936,44448];
lYearDays=[384,354,355,383,354,355,384,354,355,384, ...
354,384,354,354,384,354,355,384,355,384, ...
354,354,384,354,354,385,354,355,384,354, ...
383,354,355,384,355,354,384,354,384,354, ...
354,384,355,354,385,354,354,384,354,384, ...
354,355,384,354,355,384,354,383,355,354, ...
384,355,354,384,355,353,384,355,384,354, ...
355,384,354,354,384,354,384,354,355,384, ...
355,354,384,354,384,354,354,384,355,355, ...
384,354,354,383,355,384,354,355,384,354, ...
354,384,354,355,384,354,385,354,354,384, ...
354,354,384,355,384,354,355,384,354,354, ...
384,354,355,384,354,384,354,354,384,355, ...
354,384,355,384,354,354,384,354,354,384, ...
355,355,384,354,384,354,354,384,354,355];
leapDays=[29,0,0,29,0,0,30,0,0,29, ...
0,29,0,0,30,0,0,29,0,30, ...
0,0,29,0,0,30,0,0,29,0, ...
29,0,0,29,0,0,30,0,30,0, ...
0,30,0,0,30,0,0,29,0,29, ...
0,0,30,0,0,30,0,29,0,0, ...
29,0,0,29,0,0,29,0,29,0, ...
0,29,0,0,29,0,29,0,0,30, ...
0,0,29,0,29,0,0,29,0,0, ...
29,0,0,29,0,29,0,0,29,0, ...
0,29,0,0,29,0,29,0,0,29, ...
0,0,29,0,29,0,0,30,0,0, ...
29,0,0,29,0,29,0,0,29,0, ...
0,29,0,29,0,0,30,0,0,29, ...
0,0,29,0,29,0,0,30,0,0];
leapMonth=[8,0,0,5,0,0,4,0,0,2, ...
0,6,0,0,5,0,0,2,0,7, ...
0,0,5,0,0,4,0,0,2,0, ...
6,0,0,5,0,0,3,0,7,0, ...
0,6,0,0,4,0,0,2,0,7, ...
0,0,5,0,0,3,0,8,0,0, ...
6,0,0,4,0,0,3,0,7,0, ...
0,5,0,0,4,0,8,0,0,6, ...
0,0,4,0,10,0,0,6,0,0, ...
5,0,0,3,0,8,0,0,5,0, ...
0,4,0,0,2,0,7,0,0,5, ...
0,0,4,0,9,0,0,6,0,0, ...
4,0,0,2,0,6,0,0,5,0, ...
0,3,0,7,0,0,6,0,0,5, ...
0,0,2,0,7,0,0,5,0,0];
offset=datenum(y,m,d)-datenum(1900,1,31)+1;
dayCyl = offset + 40;
monCyl = 14;
cumLYearDays=cumsum([0,lYearDays]);
LunarYear=find(offset>cumLYearDays);
LunarYear=LunarYear(end);
monCyl=monCyl+(LunarYear-1)*12;yearCyl = LunarYear+36;
offset=offset-cumLYearDays(LunarYear);
monthDays=[29,30];
monthDays=monthDays((bitand(lunarInfo(LunarYear),bitshift (65536,-(1:12)))~=0)+1);
leap = leapMonth(LunarYear);
if leap
monthDays=[monthDays(1:leap),leapDays(LunarYear),monthDays(leap+1:end)];
end
cumMonthDays=cumsum([0,monthDays]);
LunarMonth=find(offset>cumMonthDays);LunarMonth=LunarMonth(end);
offset=offset-cumMonthDays(LunarMonth);
ch_run_ch='';
if leap
if LunarMonth==(leap+1),ch_run_ch='闰'; end
if (LunarMonth>leap),LunarMonth=LunarMonth-1;end
end
monCyl=monCyl+LunarMonth;
xx=['农历',Animals{rem(yearCyl-1,12)+1},'年',ch_run_ch,CnMonthStr{LunarMonth},'月',CnDayStr{offset}];
xx={xx;[cyclical(yearCyl),'年 ',cyclical(monCyl),'月 ',cyclical(dayCyl),'日']};
[yearGan, yearZhi] = cyclical(yearCyl);
[monthGan, monthZhi] = cyclical(monCyl);
[dayGan, dayZhi] = cyclical(dayCyl);
hourZhi = rem(floor((h+1)/2),12)+1;
return
function [gan,zhi]=cyclical(num)
Ganval={'甲','乙','丙','丁','戊','己','庚','辛','壬','癸'};
Zhival={'子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'};
gan=rem(num-1,10)+1;
zhi=rem(num-1,12)+1;