因此,我有一项作业,应该以C++编写玛雅日历的方式,使日历具有以下功能:
argv[1] | argv[2] | argv[3] | output
m+d | Mayan date | number of days | Mayan date
m-d | Mayan date | number of days | Mayan date
m-m | Mayan date | Mayan date | number of days
g= | Gregorian date | | Mayan date
m= | Mayan date | | Gregorian date
玛雅日历单位的设置方式如下:
Days Long Count period Long Count unit
1 1 Kin
20 20 Kin 1 Uinal
360 18 Uinal 1 Tun
7,200 20 Tun 1 Ka'tun
144,000 20 Ka'tun 1 Bak'tun
2,880,000 20 Bak'tun 1 Pictun
57,600,000 20 Pictun 1 Kalabtun
1,152,000,000 20 Kalabtun 1 K'inchiltun
23,040,000,000 20 K'inchiltun 1 Alautun
我在初始化Maya日历对象时遇到麻烦。这是我到目前为止的内容:
class MayanDate {
// Bak'tun, Ka'tun, etc stuff ...
unsigned int Kin = 1;
unsigned int Uinal = 20;
unsigned int Tun = 360;
unsigned int Katun = 7200;
unsigned int Baktun = 144000;
unsigned int Pictun = 2880000;
unsigned int Kalabtun = 57600000;
unsigned long Kinchiltun = 1152000000;
unsigned long Alautun = 23040000000;
public:
MayanDate();
MayanDate( unsigned int, unsigned int, unsigned int, unsigned int, unsigned int);
void set( unsigned int, unsigned int, unsigned int, unsigned int, unsigned int);
MayanDate &operator++();
int operator-( const MayanDate &) const;
MayanDate operator+( unsigned int ) const;
MayanDate operator-( unsigned int) const;
bool operator==( const MayanDate & ) const;
bool operator!=(const MayanDate & m ) const;
void get_string( char*, unsigned int) const;
};
我希望能够将单位设置为等于初始化类时它们所代表的天数,因此使用它们会更容易。
我上面的代码没有编译,我不知道为什么。任何指向我正在做错事情的指示都将非常有帮助。
最佳答案
我认为您应该以玛雅风格在类(class)中保存数据。您必须具有将Maya时代开始的天数转换为它的能力,如下所示:
void toMayan(long long d)
{
kin = d % 20; d /= 20;
unial = d % 18; d /= 18;
tun = d %20; d /= 20;
//...
}
反向转换很简单:
kin + 20 * (unial + 18 * (tun + 20 * (...)))
接下来,您应该知道如何计算公历中从某个日期到另一个日期的天数。例如,您可以查看Java的Date类源(http://www.docjar.com/html/api/java/util/Date.java.html)。
最后-您必须知道公历的玛雅日期才能计算日历之间的天数差异。将一个日期转换为天,减去(或加)差,然后转换为另一日期。
关于c++ - C++中的玛雅历法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10308601/