How to resolve the algorithm Chinese zodiac step by step in the PureBasic programming language
How to resolve the algorithm Chinese zodiac step by step in the PureBasic programming language
Table of Contents
Problem Statement
Traditionally, the Chinese have counted years using two lists of labels, one of length 10 (the "celestial stems") and one of length 12 (the "terrestrial branches"). The labels do not really have any meaning outside their positions in the two lists; they're simply a traditional enumeration device, used much as Westerners use letters and numbers. They were historically used for months and days as well as years, and the stems are still sometimes used for school grades. Years cycle through both lists concurrently, so that both stem and branch advance each year; if we used Roman letters for the stems and numbers for the branches, consecutive years would be labeled A1, B2, C3, etc. Since the two lists are different lengths, they cycle back to their beginning at different points: after J10 we get A11, and then after B12 we get C1. The result is a repeating 60-year pattern within which each pair of names occurs only once. Mapping the branches to twelve traditional animal deities results in the well-known "Chinese zodiac", assigning each year to a given animal. For example, Sunday, January 22, 2023 CE (in the common Gregorian calendar) began the lunisolar Year of the Rabbit. The celestial stems do not have a one-to-one mapping like that of the branches to animals; however, the five pairs of consecutive stems are each associated with one of the five traditional Chinese elements (Wood, Fire, Earth, Metal, and Water). Further, one of the two years within each element is assigned to yin, the other to yang. Thus, the Chinese year beginning in 2023 CE is also the yin year of Water. Since 12 is an even number, the association between animals and yin/yang doesn't change; consecutive Years of the Rabbit will cycle through the five elements, but will always be yin. You may optionally provide more information in the form of the year's numerical position within the 60-year cycle and/or its actual Chinese stem-branch name (in Han characters or Pinyin transliteration). Thus, year 1 of a cycle is the year of the Wood Rat (yang), year 2 the Wood Ox (yin), and year 3 the Fire Tiger (yang). The year 2023 - which, as already noted, is the year of the Water Rabbit (yin) - is the 40th year of the current cycle. Therefore 1984 was 甲子 (jiă-zĭ, or jia3-zi3). 2023 is 癸卯 (gŭi-măo or gui3-mao3).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Chinese zodiac step by step in the PureBasic programming language
Source code in the purebasic programming language
EnableExplicit
#BASE=4
#SPC=Chr(32)
Procedure.s ChineseZodiac(n.i)
Define cycle_year.i=n-#BASE,
stem_number.i = cycle_year%10+1,
element_number.i = Round(stem_number/2,#PB_Round_Nearest),
branch_number.i = cycle_year%12+1,
aspect_number.i = cycle_year%2+1,
index.i = cycle_year%60+1,
celestial$ = Chr(PeekU(?Celestial_stem+SizeOf(Character)*(stem_number-1))),
c_pinyin$ = StringField(PeekS(?Stem),stem_number,"\"),
element$ = StringField(PeekS(?Element),element_number,"\"),
branch_han$ = Chr(PeekU(?Terrestrial_branch+SizeOf(Character)*(branch_number-1))),
b_pinyin$ = StringField(PeekS(?Branch),branch_number,"\"),
animal$ = StringField(PeekS(?Zodiac_animal),branch_number,"\"),
aspect$ = StringField(PeekS(?Aspect),aspect_number,"\"),
YearOfCycle$ = Str(index)
ProcedureReturn Str(n)+#SPC+
LSet(element$,7,#SPC)+#SPC+
LSet(animal$,7,#SPC)+#SPC+
LSet(aspect$,6,#SPC)+#SPC+
RSet(YearOfCycle$,11)+#SPC+
LSet(c_pinyin$+"-"+b_pinyin$,9,#SPC)+#SPC+
celestial$+branch_han$
EndProcedure
LoadFont(0,"Consolas",12)
If OpenWindow(0,#PB_Ignore,#PB_Ignore,600,400,"Chinese Zodiac",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
EditorGadget(0, 8, 8, 600-16, 400-16) : SetGadgetFont(0,FontID(0))
Define header$="Year Element Animal Aspect YearOfCycle ASCII Chinese"
AddGadgetItem(0,-1,header$)
AddGadgetItem(0,-1,ChineseZodiac(1935))
AddGadgetItem(0,-1,ChineseZodiac(1938))
AddGadgetItem(0,-1,ChineseZodiac(1968))
AddGadgetItem(0,-1,ChineseZodiac(1972))
AddGadgetItem(0,-1,ChineseZodiac(1976))
AddGadgetItem(0,-1,ChineseZodiac(1984))
AddGadgetItem(0,-1,ChineseZodiac(Year(Date())))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
DataSection
Celestial_stem: : Data.u $7532, $4E59, $4E19, $4E01, $620A, $5DF1, $5E9A, $8F9B, $58EC, $7678
Terrestrial_branch: : Data.u $5B50, $4E11, $5BC5, $536F, $8FB0, $5DF3, $5348, $672A, $7533, $9149, $620C, $4EA5
Zodiac_animal: : Data.s "Rat\Ox\Tiger\Rabbit\Dragon\Snake\Horse\Goat\Monkey\Rooster\Dog\Pig"
Element: : Data.s "Wood\Fire\Earth\Metal\Water"
Aspect: : Data.s "yang\yin"
Stem: : Data.s "jiă\yĭ\bĭng\dīng\wù\jĭ\gēng\xīn\rén\gŭi"
Branch: : Data.s "zĭ\chŏu\yín\măo\chén\sì\wŭ\wèi\shēn\yŏu\xū\hài"
EndDataSection
You may also check:How to resolve the algorithm Substitution cipher step by step in the Wren programming language
You may also check:How to resolve the algorithm Inverted syntax step by step in the Python programming language
You may also check:How to resolve the algorithm Generator/Exponential step by step in the Perl programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the EasyLang programming language
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the Julia programming language