闲来无事,写了一个入门级的程序
程序代码:
/*=====================
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;
}
----------------解决方案--------------------------------------------------------
程序代码:
/*=====================
Designer: suixin
Date:1-21-2008 20:20
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;
void fstr(string &s, int &y, int &m, int &d)
{
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 IsLeapyear(int &year)
{
if ((year%4==0 && year%100!=0) || (year%4==0 && year%400==0))
return true;
else
return false;
}
int calculate(int &y, int &m, int &d)
{
if (m == one || m == two)
return (m - 1) * B + d;
int days = 0;
if (IsLeapyear(y))
++days;
for (m -= 1; m != 0; --m)
{
if (m == two)
days += T;
else if (m == four || m == six || m == nine || m == el)
days += L;
else
days += B;
}
return days + d;
}
int main()
{
int year = 0, month = 0, day = 0;
cout << "样例输入: 2008年01月21日 或 2008--01--01(注意月日前面的零)" << endl;
string date;
cin >> date;
fstr(date, year, month, day);
cout << date << "是该年的第 " << calculate(year, month, day)<< " 天." << endl;
system("pause");
return 0;
}
----------------解决方案--------------------------------------------------------
再来一个C语言版的
程序代码:
/*=====================
Designer: suixin
Date:1-21-2008 20:30
QQ group:21035626
=====================*/
#include <stdio.h>
#include <stdlib.h>
#define B 31
#define L 30
#define T 28
#define ONE 1
#define TWO 2
#define FOUR 4
#define SIX 6
#define NINE 9
#define EL 11
int IsLeapyear(int year)
{
if ((year%4==0 && year%100!=0) || (year%4==0 && year%400==0))
return 1;
else
return 0;
}
int calculate(int y, int m, int d)
{
if (m == ONE || m == TWO)
return (m - 1) * B + d;
int days = 0;
if (IsLeapyear(y))
++days;
for (m -= 1; m != 0; --m)
{
if (m == TWO)
days += T;
else if (m == FOUR || m == SIX || m == NINE || m == EL)
days += L;
else
days += B;
}
return days + d;
}
int main(void)
{
int year, month, day;
printf("输入年:");
scanf("%d", &year);
printf("输入月:");
scanf("%d", &month);
printf("输入日:");
scanf("%d", &day);
printf("%d年已经过去%d天\n", year, calculate(year, month, day));
system("pause");
return 0;
}
----------------解决方案--------------------------------------------------------
我够无聊。
----------------解决方案--------------------------------------------------------
难道你不能优化一下你的代码么?
这样的代码看着就累
----------------解决方案--------------------------------------------------------
楼主,你的OnlineJudge写好了没?
----------------解决方案--------------------------------------------------------
CPP
----------------解决方案--------------------------------------------------------
还没有,不是我想象的那么简单
----------------解决方案--------------------------------------------------------
原帖由 [bold][underline]随心[/underline][/bold] 于 2008-1-27 11:24 发表 [url=http://bbs.bccn.net/redirect.php?goto=findpost&pid=1188648&ptid=198844][/url]
还没有,不是我想象的那么简单
还没有,不是我想象的那么简单
嗯嗯,你终于发现了
----------------解决方案--------------------------------------------------------