| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 620 人关注过本帖
标题:自己写的一个小程序
取消只看楼主 加入收藏
阿冲
Rank: 2
等 级:论坛游民
帖 子:29
专家分:40
注 册:2009-10-5
结帖率:75%
收藏
 问题点数:0 回复次数:0 
自己写的一个小程序
程序代码:
#include <iostream>
using namespace std;
int g_Month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};//月份
int g_WeenDay[7] = {1,2,3,4,5,6,7}; //星期
int g_iYear = 2000 , g_iMonth = 1 , g_iDay = 1 , g_iWeenDay = 6;//初始化2000年1月1日为星期六;

class cDateTime
{
private:
    int M_Year;
    int    M_Month;
    int    M_Day;
    int    M_DayNum;//相隔的天数
    int M_YearNum;//一年的天数
public:
    cDateTime();
    void SetInfo(int iYear , int iMonth , int iDay);
    void bRunYear(int iYear);
    void iYearCount(int iYear = g_iYear , int iMonth = g_iMonth , int iDay = g_iDay);
    int WeenDay(int iTmp);
    void fDay();//某个月第一天是星期几
    void sDay();//两个时间相隔多少天
    void nDay();//某一天的下一天是几号
    void wDay(int iWeenDay);//求某年某月的第几周是哪几天
};
cDateTime::cDateTime()
{
    M_Year = 0;
    M_Month = 0;
    M_Day = 0;
    M_DayNum = 0;
}
void cDateTime::SetInfo(int iYear , int iMonth , int iDay)
{
    M_Year = iYear;
    M_Month = iMonth;
    M_Day = iDay;
}

void cDateTime::bRunYear(int iYear)
{
    if (iYear % 4 == 0 && iYear % 100 != 0 || iYear % 100 ==0 && iYear % 400 ==0)
    {    
        g_Month[1] = 29;
        M_YearNum = 366;
    } 
    else
    {
        g_Month[1] = 28;
        M_YearNum = 365;
    }
}
void cDateTime::iYearCount(int iYear , int iMonth , int iDay)//总共的天数
{
    //int iDayNum = 0;
    for (int i = iYear; i < M_Year; ++i)
    {
        bRunYear(i);
        M_DayNum += M_YearNum;
    }
    
    bRunYear(M_Year);
        
    for (i = 0; i < M_Month - 1; ++i)
    {
        M_DayNum += g_Month[i];
    }    
    
    M_DayNum += M_Day;
    
    ////////////////开始计算多出来的天数//////////////////
    bRunYear(iYear);
    if (iMonth > 1)
        {
            for (i = 0; i < iMonth - 1; ++i)
            {
                M_DayNum -= g_Month[i];
                
        }        
    }
    
    if (iDay > 1)
    {
        M_DayNum -= iDay;
        ++M_DayNum;
    }     
    
    /////////////////减掉多出来的天数/////////////////////
}
int cDateTime::WeenDay(int iTmp)
{
    int iTmpDay = 1 , iTmpWeenday = g_iWeenDay - 1 , Tmp;

    while (iTmpDay <= iTmp)
    {
        if (iTmpWeenday > 6)
        {
            iTmpWeenday = 0;
        }
        Tmp = g_WeenDay[iTmpWeenday];
        ++iTmpWeenday;
        ++iTmpDay;        
    }
    return Tmp;
    
}
void cDateTime::fDay()
{
    cout << M_Year << "年" << M_Month << "月的第1天是星期" << WeenDay(M_DayNum) << endl;
}
void cDateTime::sDay()
{
    cout << "您输入的这两个日期相差" << M_DayNum - 1 << "天" << endl;
}

void cDateTime::nDay()
{
    bRunYear(M_Year);
    ++M_Day;
    if (M_Day > g_Month[M_Month - 1])
    {
        M_Day = 1;
    }
    cout << "您输入的日期下一天为:" << M_Day << "号" << endl;
}

void cDateTime::wDay(int iWeenDay)
{
    int iTmp = 0;
    for (int i = 0; i < g_Month[M_Month - 1]; ++i)
    {
        if (WeenDay(M_DayNum + i) == 1)//第一个周开始
        {
            ++iTmp;
        }
        if (iTmp==iWeenDay)
        {
            cout << "第" << iWeenDay << "周有:" << i+1 << " " << endl;
        }
        
    }

}

int main()
{

    int iYear , iMonth;
    cDateTime Fist;
    cout << "求某个月的第一天是星期几" << endl;
    cout << "请输入年:";
    cin >> iYear;
    cout << "请输入月:";
    cin >> iMonth;
    Fist.SetInfo(iYear , iMonth , 1);
    Fist.iYearCount();

    Fist.fDay();

    cout << "-------------------黄金分割线----------------------------" << endl;

     int StarYear , StarMonth , StarDay , LastYear , LastMonth , LastDay;
    cDateTime Second;
    cout << "求两个时间相隔多少天" << endl;
    cout << "请输入起始年:";
    cin >> StarYear;
    cout << "请输入起始月:";
    cin >> StarMonth;
    cout << "请输入起始日:";
    cin >> StarDay;

    cout << "请输入结束年:";
    cin >> LastYear;
    cout << "请输入结束月:";
    cin >> LastMonth;
    cout << "请输入结束日:";
    cin >> LastDay;
    Second.SetInfo(LastYear , LastMonth , LastDay);
    Second.iYearCount(StarYear , StarMonth , StarDay);
    Second.sDay();

    cout << "-------------------黄金分割线----------------------------" << endl;
    
    cDateTime Again;
    cout << "求某天的下一天是几号" << endl;
    cout << "请输入年:";
    cin >> StarYear;
    cout << "请输入月:";
    cin >> StarMonth;
    cout << "请输入日:";
    cin >> StarDay;
    Again.SetInfo(StarYear , StarMonth , StarDay);
    Again.nDay();

    cout << "-------------------黄金分割线----------------------------" << endl;

    cDateTime Four;
    cout << "求某年某月的第几周是哪几天" << endl;
    cout << "请输入年:";
    cin >> StarYear;
    cout << "请输入月:";
    cin >> StarMonth;
    cout << "请输入第几周:";
    cin >> StarDay;
    Four.SetInfo(StarYear , StarMonth , 1);
    Four.iYearCount();
    Four.wDay(StarDay);
    return 0;
}
搜索更多相关主题的帖子: private include public 
2009-10-30 11:20
快速回复:自己写的一个小程序
数据加载中...
 
   



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

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