闲来无事,写了一个入门级的程序
程序代码:
/*=====================
Designer: suixin
Date:1-26-2008 20:23
QQ group:21035626
=====================*/
#include <iostream>
#include <string>
using namespace std;
const int B = 31, L = 30, T = 28;
const int one = 1, two = 2, four = 4, six = 6, nine = 9, el = 11;
class date
{
public:
void fstr();
bool IsLeapyear() const;
int calculate();
void display();
string getdate() const;
friend istream& operator>>(istream& in, date& a);
friend ostream& operator<<(ostream& out, date& b);
date();
~date() {};
private:
string s;
int days, y, m, d;
};
istream& operator>>(istream& in, date& a)
{
in >> a.s;
return in;
}
ostream& operator<<(ostream& out, date& b)
{
out << b.calculate();
return out;
}
date::date()
{
days = 0;
y = 0;
m = 0;
d = 0;
}
string date::getdate() const
{
return s;
}
void date::fstr()
{
y = atoi(s.substr(0, 4).c_str());
m = atoi(s.substr(6, 2).c_str());
d = atoi(s.substr(10, 2).c_str());
}
bool date::IsLeapyear() const
{
if ((y%4==0 && y%100!=0) || (y%4==0 && y%400==0))
return true;
else
return false;
}
int date::calculate()
{
fstr(); // distill year、month、day from s
if (m == one || m == two) // dispose 1、2 month
return days = (m - 1) * B + d; // return total number of days
if (IsLeapyear()) // is it the leapyear?
++days;
for (m -= 1; m != 0; --m) // dispose other month
{
if (m == two)
days += T;
else if (m == four || m == six || m == nine || m == el)
days += L;
else
days += B;
}
return days + d; // return total number of days
}
int main()
{
cout << "样例输入: 2008年01月21日 或 2008--01--01(注意月日前面的零)" << endl;
date a;
cin >> a;
cout << a.getdate() << "是该年的第 " << a << " 天" << endl;
system("pause");
return 0;
}








我够无聊。


[/url]