chinese_zodiac.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * cxxomfort example: chinese_zodiac
  3. *
  4. * See https://rosettacode.org/wiki/Chinese_zodiac
  5. *
  6. */
  7. #include <cxxomfort/cxxomfort.hpp>
  8. //#include <cxxomfort/string_view.hpp>
  9. #include <cxxomfort/tuple.hpp>
  10. #include <cxxomfort/array.hpp>
  11. #include <string>
  12. #include <iostream>
  13. enum Animals {
  14. Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Rooster, Dog, Pig
  15. };
  16. enum Elements {
  17. Wood, Fire, Earth, Metal, Water
  18. };
  19. enum Y {
  20. yang, yin
  21. };
  22. const unsigned Y_EPOCH= 1984; // Rat, Wood, yang
  23. typedef struct { Animals a; Elements e; Y y; } CZ;
  24. std::ostream& operator<< (std::ostream& os, CZ const& cz) {
  25. using std::get;
  26. os<< "(";
  27. static const char* animals[]= {"Rat", "Ox", "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat", "Monkey", "Rooster", "Dog", "Pig"};
  28. os<< animals[cz.a]<< " ";
  29. static const char* elements[] = {"Wood", "Fire", "Earth", "Metal", "Water"};
  30. os<< elements[cz.e]<< " ";
  31. os<< (cz.y == 0 ? "yang" : "yin");
  32. os<< ")";
  33. return os;
  34. }
  35. CZ year_cz (int year) {
  36. using namespace std;
  37. static const int epoch = 1984; // rat, wood, yang
  38. int di= year - epoch;
  39. unsigned adiff = ((di % 12) + 12) % 12;
  40. unsigned ediff = ((( (di - (di<0)) / 2) % 5 ) + 5) % 5;
  41. int ydiff = (year - epoch) % 2;
  42. // cout<< "a,e : "<< adiff << " "<< ediff<< endl;
  43. CZ ret= { Animals(adiff), Elements(ediff), Y(ydiff) };
  44. return ret;
  45. }
  46. int main () {
  47. using namespace std;
  48. cxxomfort::output_info(); cout<< endl;
  49. //unsigned Y = 1984;
  50. for (unsigned i= 1984; i < 2046; ++i) {
  51. cout<< i<< " "<< year_cz(i)<< endl;
  52. }
  53. }