| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 958 人关注过本帖
标题:闲来无事,写了一个入门级的程序
只看楼主 加入收藏
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
结帖率:100%
收藏
 问题点数:0 回复次数:8 
闲来无事,写了一个入门级的程序
程序代码:
/*=====================
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;
}
搜索更多相关主题的帖子: 入门 闲来无事 
2008-01-26 22:34
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分: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;
}

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-01-26 22:35
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分: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;
}

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-01-26 22:37
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分:0 
我够无聊。

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-01-26 22:39
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
难道你不能优化一下你的代码么?
这样的代码看着就累

2008-01-27 01:39
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
楼主,你的OnlineJudge写好了没?

2008-01-27 01:44
linsua
Rank: 1
等 级:新手上路
帖 子:275
专家分:0
注 册:2008-1-2
收藏
得分:0 
CPP

如果你对我的话感到再明白不过 那恐怕你是误解了我的意思
2008-01-27 10:40
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分:0 
还没有,不是我想象的那么简单

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-01-27 11:24
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
原帖由 [bold][underline]随心[/underline][/bold] 于 2008-1-27 11:24 发表 [url=http://bbs.bccn.net/redirect.php?goto=findpost&pid=1188648&ptid=198844][/url]
还没有,不是我想象的那么简单

嗯嗯,你终于发现了
2008-01-27 12:18
快速回复:闲来无事,写了一个入门级的程序
数据加载中...
 
   



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

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