| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 870 人关注过本帖
标题:讲一个日期根据给定的字符串输出相应的格式
只看楼主 加入收藏
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 9楼 rjsp
这是程序显示的错误!
2015-11-09 20:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
回复 11楼 鱼游海底
不好意思,能贴文字就贴文字,不能贴文字就贴图。
你放在rar中我不想打开,我也没有MS Word软件。再说看到你出错了有什么用呢,又不知道你代码是怎么写的。
2015-11-10 08:16
农民工
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:109
专家分:639
注 册:2015-8-22
收藏
得分:2 
错误原因 9楼已经说得很清楚了
2015-11-10 14:18
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 12楼 rjsp
你好,代码有一点长!就是实现日期Date类的,如果你需要的话我就贴过来!从结果看,问题应该就出在这里。
2015-11-10 17:35
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 13楼 农民工
可是main函数中我没有使用这个格式控制啊!我将数字改后还是一样的错误!
2015-11-10 17:36
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
程序代码:
main.cpp:::
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;
int main() {
    Date d(2008, 8, 8);
    d.Print();
    cout << d.Date2Days() << endl;
    d = Date::Days2Date(d.Date2Days());
    d.Print();
    d = Date::Days2Date(2000);
    d.Print();
    cout << d.toString("MM/dd/yyyy") <<endl;
    cout << d.nthDay() << endl;
    cout << d.nthWeek() <<endl;
    d.NextDate().Print();
    d.PrevDate().Print();
    cout << d.DiffDate(Date::Today()) << endl;
    d.AfterNDays(100).Print();
    cout << d.Equal(Date::Today()) << endl;
    cout << d.Earlier(Date::Today()) << endl;
}
Date.h:::
#ifndef DATE_CLASS
#define DATE_CLASS
#include <iostream>
#include  <string>
using namespace std;
class Date {
private:
    int year, month, day;
private:
    enum {YearBase = 1970};
    enum {MonthBase = 1};
    enum {DayBase = 1};
    static int nDaysofMonth[13];
    void LeapYearEnabled() const;
public:
    Date(int y = YearBase, int m = MonthBase, int d = DayBase);
    Date(const Date& d);
    Date& operator = (const Date& rhs);
    void SetDate(int y, int m, int d);
    void Print() const;
    static bool IsLeapYear(int y);
    static int DaysOfThisYear(int y);
    int Date2Days() const;
    static Date Days2Date(int n);
    string toString(string format_flag)  const;
    static Date Today();
    int nthDay() const;
    int nthWeek() const;
    Date NextDate() const;
    Date PrevDate() const;
    int DiffDate(const Date& d) const;
    Date AfterNDays(int n) const;
    bool Equal(const Date& d) const;
    bool Earlier(const Date& d) const;
};
#endif
程序代码:
Date.cpp:::
#include <iostream>
#include <stdio.h>
#include <cassert>
#include <time.h>
#include "Date.h"
using namespace std;
int Date::nDaysofMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date(int y, int m, int d) {
    SetDate(y, m, d);
}
Date::Date(const Date& d) {
    if(this != &d) {
        year = d.year;
        month = d.month;
        day = d.day;
    }
}
Date& Date::operator = (const Date& rhs) {
    if(this != &rhs) {
        year = this->year;
        month = this->month;
        day = this->day;
    }
    return *this;
}
void Date::SetDate(int y, int m, int d) {
    year = y >= YearBase ? y : YearBase;
    month = m >= 1 && m <= 12 ? m : MonthBase;
    this->LeapYearEnabled();
    day = d >= 1 && d <= nDaysofMonth[month] ? d : DayBase;
}
void Date::LeapYearEnabled() const {
    if(IsLeapYear(year))
        nDaysofMonth[2] = 29;
    else
        nDaysofMonth[2] = 28;
}
void Date::Print() const {
    cout << this->year << "-" << this->month
        << "-" << this->day << endl;
}
bool Date::IsLeapYear(int y) {
    return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
int Date::DaysOfThisYear(int y) {
    if(IsLeapYear(y)) return 366;
    else return 365;
}
int Date::Date2Days() const {
    this->LeapYearEnabled();
    int s = 0;
    s += day;
    for(int m = 1; m < month; ++m)
        s += nDaysofMonth[m];
    for(int y = YearBase; y < year; ++y)
        s += DaysOfThisYear(y);
    return s;
}
Date Date::Days2Date(int n) {
    Date d;
    int y = YearBase;
    while(n >= DaysOfThisYear(y)) {
        n -= DaysOfThisYear(y);
        ++y;
        if(n == DaysOfThisYear(y))
            break;
    }
    d.year = y;
    d.LeapYearEnabled();
    int m = 1;
    while(n >= nDaysofMonth[m]) {
        n -= nDaysofMonth[m];
        ++m;
        if(n == nDaysofMonth[m])
            break;
    }
    d.month = m;
    d.day = n;
    return d;
}
string Date::toString(string format_flag)  const {
    int length = format_flag.size();
    char *a = new char;
    if(length == 6) {sprintf(a, "%s/%s/%s", month, day, year);}
    if(length == 10) {sprintf(a, "%02s/%02s/%04s", month, day, year);}
    if(length == 12) {
        switch(month) {
        case 1 : sprintf(a, "January %02s,%04s", day, year); break;
        case 2 : sprintf(a, "Febrary %02s,%04s", day, year); break;
        case 3 : sprintf(a, "March %02s,%04s", day, year); break;
        case 4 : sprintf(a, "April %02s,%04s", day, year); break;
        case 5 : sprintf(a, "May %02s,%04s", day, year); break;
        case 6 : sprintf(a, "June %02s,%04s", day, year); break;
        case 7 : sprintf(a, "July %02s,%04s", day, year); break;
        case 8 : sprintf(a, "Augest %02s,%04s", day, year); break;
        case 9 : sprintf(a, "September %02s,%04s", day, year); break;
        case 10 : sprintf(a, "October %02s,%04s", day, year); break;
        case 11 : sprintf(a, "November %02s,%04s", day, year); break;
        case 12 : sprintf(a, "December %02s,%04s", day, year); break;
        }
    }
    return string((string)a);
}
Date Date::Today() {
    time_t timer;
    time(&timer);
    tm* t_tm = localtime(&timer);
    return Date(t_tm->tm_year, t_tm->tm_mon, t_tm->tm_mday);
}
int Date::nthDay() const {
    Date now;
    now = now.Today();
    LeapYearEnabled();
    int sum = 0;
    sum += day;
    for(int m = 1; m <= month; m++)
        sum += nDaysofMonth[m];
    return sum;
}
int Date::nthWeek() const {
    int sum(nthDay());
    return (sum / 7 + 1);
}
Date Date::NextDate() const {
    int sum(Date2Days() + 1);
    return Days2Date(sum);
}
Date Date::PrevDate() const {
    int sum(Date2Days() - 1);
    return Days2Date(sum);
}
int Date::DiffDate(const Date& d) const {
    int sum1(Date2Days());
    int sum2(d.Date2Days());
    return (sum1 - sum2) > 0 ? (sum1 - sum2) : (sum2 - sum1);
}
Date Date::AfterNDays(int n) const {
    int sum(Date2Days() + n);
    return Days2Date(sum);
}
bool Date::Equal(const Date& d) const {
    if(year == d.day && month == d.month && day == d.day) return true;
    else return false;
}
bool Date::Earlier(const Date& d) const {
    if(year < d.year || (year == d.year && month < d.month) ||
        (year == d.year && month == d.month && day < d.day)) return true;
    else return false;
}
2015-11-10 17:43
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
其实是sprintf()里面格式控制写错了,不过获取系统当前时间也错了,谁可以帮我改一下那个实现(static Date today (){})。输出的不是当前日期,而是1970,10,10, ,这个不正确啊!!!#
2015-11-10 19:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
错误太多,你自己慢慢找吧,比如

Date& Date::operator = (const Date& rhs) {
    if(this != &rhs) {
        year = this->year; // ---fuck 1:year = rhs.year
        month = this->month;
        day = this->day;

    }
    return *this;
}
///////////////////////////////////////////////////////////////////
char *a = new char; // ---fuck 2: char *a = new char[100];
///////////////////////////////////////////////////////////////////
sprintf(a, "%02s/%02s/%04s", month, day, year); // ---fuck 3: sprintf(a, "%02d/%02d/%04d", month, day, year);
///////////////////////////////////////////////////////////////////
return Date(t_tm->tm_year, t_tm->tm_mon, t_tm->tm_mday); // ---fuck 4: return Date(1900+t_tm->tm_year, 1+t_tm->tm_mon, t_tm->tm_mday);
2015-11-11 08:28
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 18楼 rjsp
多谢版主的细心指导!(不过第二个事实证明是可以这样写的,而且结果都出来了。)
2015-11-11 11:13
鱼游海底
Rank: 1
来 自:中国
等 级:新手上路
帖 子:132
专家分:2
注 册:2015-4-16
收藏
得分:0 
回复 18楼 rjsp
程序代码:
Date Date::Today() {
    time_t timer;
    time(&timer);
    tm* t_tm = localtime(&timer);
    return Date(1900 + t_tm->tm_year, 1 + t_tm->tm_mon, t_tm->tm_mday);
//    return Date(Days2Date(timer/60/60/24));     //  ?? 看问号这里,为什么输出的是昨天呢,需要将Days2Date()里面的数加上1才是今天。但是我检查不出问题 
}
2015-11-11 12:25
快速回复:讲一个日期根据给定的字符串输出相应的格式
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025215 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved