TGDZCalc by R (36th)

R语言是一门侧重于统计分析的免费软件,它也可以用来编程。 TGDZCalc用它来写,如下:

点击查看代码
YearToTGDZ <- function(y) {
if (y>0) y=y-4 else y=y-3
while(y<0) {y=y+60}
t=y %% 10
d=y %% 12
tgArr=c("甲","乙","丙","丁","戊","已","庚","辛","壬","癸")
dzArr=c("子","丑","寅","卯","辰","已","午","未","申","酉","戌","亥")
tgdz=paste(c(tgArr[t+1], dzArr[d+1]),collapse="")
return(tgdz)
}

GetBaseYear <- function(y){
if (y<0) b=-57 else b=4
while (b>y) {b=b-60}
while (b+60<=y) {b=b+60}
return(b)
}

TGDZToYear <- function(tgdz, bY){
tg=substring(tgdz,1,1)
dz=substring(tgdz,2,2)
ti=which(tgArr==tg)
di=which(dzArr==dz)
if (ti>=di) m=ti-di else m=12+ti-di
return (bY + m*5 + ti-1)
}

test<-function(startY,endY) {
for (i in seq(startY,endY)){
bY=GetBaseYear(i)
tgdz=YearToTGDZ(i)
year2=TGDZToYear(tgdz,bY)
resultArr=c(i,bY,tgdz,year2)
print(resultArr)
}
}

test(1900,2100)
# 在R 4.5.0中测试通过 2025-6-2,函数写法有点象Maple。R的控制台 对中文显示有点不太友好,要复制粘贴后再使用,不然光标位置容易乱。

posted @ 2025-06-02 19:44  dingxianghuan  阅读(12)  评论(0)    收藏  举报