一个有点难度的C++ 练习题,大家一起来。
已知一个字符串,保存了当前的日期,格式是 YYYY-MM-DD
其中YYYY----年份,如2005
MM ----月份
DD ----天
举例:2005-05-31
现在就是要根据这个串,求出该串表示日期的前7天的字符串表示是多少?
举例:
如果源串是 2005-05-31
那么返回的结果串就是
2005-05-30, 2005-05-29,2005-05-28,005-05-27,2005-05-26,2005-05-25,
2005-05-24
这7个字符串。
当然必须考虑闰年的问题,和每个月的月大、月小的问题。
问题点数:40、回复次数:11Top
1 楼steedhorse(晨星)回复于 2005-06-01 13:32:39 得分 5
使用MFC的CTime和CTimeSpan吧。Top
2 楼steedhorse(晨星)回复于 2005-06-01 13:33:20 得分 0
解析字符串编程日期可以使用COleDateTimeTop
3 楼Zephyrzzz()回复于 2005-06-01 13:38:31 得分 8
这种题就是烦些,先把字符串转成数字,然后就一天一天减吧,直接在下面写的一个不完善的程序供参考.
int m[12]={31,31,28,31,30,31,30,31,31,30,31,30}; //从0(12)月~11月大小
int is_leap(int year)
{
return (year%100!=0 && year%4==0 || year%400==0);
}
main()
{
sscanf(str,"%d%c%d%c%d",year,ch,month,ch,day);
for (loop=0;loop<7;loop++) {
day--;
if (day<0) {
month--;
day=(month==2)?m[month]+is_leap(year):m[month];
if (month<0) year--,month=12;
}
将year/month/day转换成字符串
}
}Top
4 楼hou2003(阿超)回复于 2005-06-01 13:38:56 得分 2
这个不是有难度,而是比较麻烦,各种情况一定要想清楚了就行了Top
5 楼Zephyrzzz()回复于 2005-06-01 13:43:30 得分 0
sscanf(str,"%d%c%d%c%d",&year,&ch,&month,&ch,&day); //这行漏了& ^_^Top
6 楼thuers(我什么都不会,所以我想多看看)回复于 2005-06-01 14:27:15 得分 2
就是判断闰年和每个月天数~!
有点繁琐,但不难~!Top
7 楼Stephen_Ma(极品飞马)回复于 2005-06-01 14:29:59 得分 15
#include <time.h>
void main(void)
{
char szSampleData[] = "2005-05-31";
int nYear;
int nMonth;
int nDay;
sscanf(szSampleData, "%4d-%02d-%02d", &nYear, &nMonth, &nDay);
struct tm tmSampleData = { 0, 0, 1, nDay, nMonth - 1, nYear - 1900 };
for (int i = 0; i < 7; i++)
{
tmSampleData.tm_mday--;
time_t tData = mktime(&tmSampleData);
char szData[20] = { 0 };
strftime(szData, 20, "%Y-%m-%d", localtime(&tData));
printf("%s,", szData);
}
}
就不多说了,自己看吧Top
8 楼djfu(飞龙在天)回复于 2005-06-01 14:44:02 得分 0
Stephen_Ma(极品飞马)
-----------------------
你这个完全正确,请问那些判断闰年、闰月的细节被你隐藏到哪了?Top
9 楼Stephen_Ma(极品飞马)回复于 2005-06-01 16:17:48 得分 0
由系统函数自动处理了。mktime,localtime等。Top
10 楼jhw4048(雨之魂)回复于 2005-06-01 19:08:24 得分 8
我来个C++写的
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::istream;
#include <iomanip>
using std::setfill;
using std::setw;
#include <cstdlib>
const int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//date1.h
//Data class definition
class Date
{
friend istream &operator>>(istream&, Date &);
public:
Date(int = 2005, int = 6, int = 1);
void print();
private:
int year;
int month;
int day;
int checkDay(int) const;
void checkdd();
};
//date1.cpp
//Member-function definitions for class Date.
Date::Date(int yr, int mn, int dy)
{
year = yr;
if(mn > 0 && mn < 12)
month = mn;
else
{
cout << "Your enter a wrong month!\n";
exit(1);
}
day = checkDay(dy);
print();
cout << endl;
}
void Date::print()
{
for(int i = 0; i < 7; ++i)
{
--day;
checkdd();
cout << setfill('0') << setw(4) << year << '-'
<< setw(2) << month << '-'
<< setw(2) << day << "\t";
}
}
int Date::checkDay(int testDay) const
{
if(testDay > 0 && testDay <= daysPerMonth[month])
return testDay;
if(month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 ==0 && year % 100 != 0)))
return testDay;
cout << "Your Enter a wrong day!\n";
exit(1);
}
void Date::checkdd()
{
if(day <= 0)
{
month = month - 1;
if(month == 0)
{
month = 12;
--year;
}
day = daysPerMonth[month] + day - 1;
}
}
int main()
{
Date haha(2005, 5, 31);
return 0;
}Top
11 楼jhw4048(雨之魂)回复于 2005-06-02 00:26:38 得分 0
HOHO,楼主好淫呀,给分啦,唉,就是少了点呀Top




