TGDZCalc by Dart

写这个没花什么时间,很快就通过了。

点击查看代码
void main() {
  for (var i=-400; i<-300; i++){
 
  String tgdz=YearToTGDZ(i);
  int bY=GetBaseYear(i);
  int y2=TGDZToYear(tgdz,bY);
  
  print("$i, $tgdz, $bY, $y2");
};
}



// ignore: non_constant_identifier_names
int GetBaseYear(int y) {
  int b = 0;
  if (y > 0) {
    b = 4;
  } else {
    b = -57;
  }

  while (b > y) {
    b -= 60;
  }
  while (b + 60 <= y) {
    b += 60;
  }
  ;
  return b;
}

String YearToTGDZ(int year)
{
  if (year<0) {year-=3;}
 
  if (year>0) {year -=4;}
  
  while  (year<0) {year +=60;}
  
  int t= year % 10;
  int d= year % 12;
  
   return '甲乙丙丁戊已庚辛壬癸'.substring(t,t+1) + '子丑寅卯辰已午未申酉戌亥'.substring(d,d+1);
  }

int TGDZToYear(String tgdz,  int baseYear)
{
  String tg=tgdz.substring(0,1);
  String dz=tgdz.substring(1,2);
  
  int ti='甲乙丙丁戊已庚辛壬癸'.indexOf(tg);
  int di='子丑寅卯辰已午未申酉戌亥'.indexOf(dz);
  
  int m=ti-di;
  if (m<0) {m+=12;}
  
  return baseYear + m*5 +ti; 
  
} 

//这个语言比较易学。在dart.cn的网页(Dart 3.8.1 + Flutter 3.32.1) 中调试通过。

posted @ 2025-06-02 00:15  dingxianghuan  阅读(6)  评论(0)    收藏  举报