关闭

万年历

745人阅读 评论(0) 收藏 举报

源代码如下:

#include <iostream>

using namespace std;

 

void DataShow(int d, int w, int ** p);  // 显示日历函数d为本月共有多少天,w为第一天为星期几,p为存储空间

 

int main()

{

    char key;

    int year = 0;           // 当前查询的年份

    int count = 0 ;         // 从年到当前年闰年的个数(包括当前年)

    int current = 0;        // current为,当前年为闰年;为,当前年为平年

    long int days = 0;      // 从年到当前年闰年的个数(不包括当前年)

    int which = 1;          // 判定当前年的第一天是星期几

    int ** data = new int *[6]; // 分配个空格

    for (int i = 0; i < 6; ++i)

        data[i] = new int[7];

    while (key != 'Q' && key != 'q')

    {

        cout << "请输入所要查询的年份(19999): ";

        cin >> year;

        while (cin.get() != '/n')

                ;

        if (year >=1 && year <= 9999)

        {

            count = 0;      // 全部回复原始设定

            current = 0;

            days = 0;

            which = 1;

            for (int i = 1; i <= year; ++i)

            {

                if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))     // 闰年判定条件:能被整除且不能被整除,或者能被整除

                {

                    count++;

                    if (i == year)

                        current = 1;

                }

            }

            days = 365 * (year - 1) + count;    // 计算从年到当前年的总天数(不包括当前年)

            if (current == 1)                   // 如果当前年为闰年,则总天数减去

                days--;    

            which += days % 7;                  // 计算当前年第一天为星期几

            cout << "/n********* " ;

            cout.width(4);

            cout << year;

            cout << "年年历*********/n";

            cout << "******************************/n"

                 << "           一月份/n"

                 << "******************************/n"

                 << "        /n";

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           二月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 31) % 7;           // 计算当月第一天是星期几

            if (current == 1)

                DataShow(29, which, data);

            else

                DataShow(28, which ,data);

            cout << "******************************/n"

                 << "           三月份/n"

                 << "******************************/n"

                 << "        /n";

            if (current == 1)

            {

                which = (which + 29) % 7;

                DataShow(29, which, data);

            }

            else

            {

                which = (which + 28) % 7;

                DataShow(28, which ,data);

            }

            cout << "******************************/n"

                 << "           四月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 31) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           五月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 30) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           六月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 31) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           七月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 30) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           八月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 31) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           九月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 31) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "           十月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 30) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "          十一月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 31) % 7;

            DataShow(31, which, data);

            cout << "******************************/n"

                 << "          十二月份/n"

                 << "******************************/n"

                 << "        /n";

            which = (which + 30) % 7;

            DataShow(31, which, data);

            cout << "/n退出请按'Q',继续查询请按回车:";

            key = cin.get();

            cout << endl << endl;

        }

        else

            cout << "/n输入错误/n/n";

    }

    cout << "/n程序结束,谢谢使用!^O^/n/n";

    return 0;

}

 

void DataShow(int d, int w, int ** p)

{

    int temp = 1;      

    int number = d;     // 实际天数监测

    for (int i = 0; i < 6; ++i)

    {

        for (int j = 0; j < 7; ++j)

        {

            if ((i == 0 && j < w) || number == 0 )      // 无日期对应项中填充空格

                p[i][j] = 0;

            else

            {

                p[i][j] = temp++;

                number--;

            }

        }

    }

    for (int i = 0; i < 6; ++i)

    {

        for (int j = 0; j < 7; ++j)

        {

            if (p[i][j] == 0)

            {

                cout.width(4);      // 控制输出格式

                cout << "";

            }

            else

            {

                cout.width(4);

                cout << p[i][j];

            }

        }

        cout << endl;

    }

    cout << endl;

}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22626次
    • 积分:439
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:0篇
    • 译文:0篇
    • 评论:10条
    文章分类
    文章存档
    最新评论