用控制语句实现万年历

来源:互联网 发布:常用的数据库软件 编辑:程序博客网 时间:2024/06/14 04:12

万年历

三个基本功能:

1.显示指定一年的日历

2.显示指定一月的日历

3. 查询某天是星期几

 

实现代码如下:

 

/*
 * File:   Calendar.cpp
 * Author: yubao
 *This is a calendar
 * Created on May 9, 2009, 9:59 PM
 */

#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;

char  * week[] = {"Sunday", "Monday", "Tuesday", "Wendsday", "Thursday", "Friday", "Saturday"};

/*
 *coutDays
 *to make sure that a year is leap year or not and count the number of days for the year
 */
int countDays(int year) {
    int flag, yeardays;
    int alldays = 0;
    for (int i = 1980; i < year; i++) {
        if (!(i % 400))
            flag = 1;
        else if (!(i % 4) && i % 100)
            flag = 1;
        else
            flag = 0;
        if (flag)
            yeardays = 366;
        else
            yeardays = 365;
        alldays += yeardays;
    }
    return alldays;
}

/*
 *monthDays
 *to count the days in the month
 */
int monthDays(int year, int month) {
    int flag, days;
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            days = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11: days = 30;
            break;
        case 2:
            if (!(year % 400))
                flag = 1;
            else if (!(year % 4) && year % 100)
                flag = 1;
            else
                flag = 0;
            if (flag)
                days = 29;
            else
                days = 28;
    }
    return days;
}

/*
 *fn1
 *to display the date in a year
 */
void fn1() {
    int year, i, j, k, t;
    cout << "/n Input the year:";
    cin >> year;
    t = (countDays(year) % 7 + 2) % 7;
    for (k = 1; k <= 12; k++) {
        cout << k << "month date is the following:";
        cout << endl;
        cout << setw(6) << "Sun" << setw(6) << "Mon"<<setw(6) << "Tue" << setw(6) << "Thu" << setw(6) << "Fri" << setw(6) << "Sat" << endl;
        for (int i = 1; i <= monthDays(year, 1); i++) {
            if (i == 1) {
                for (j = 0; j < t; j++) {
                    cout.width(6);
                    cout << "   ";
                }
            } else

                cout.setf(ios::right);
            cout.width(6);
            cout << i;
            if ((i + t) % 7 == 0)
                cout << endl;
        }
        t = (t + monthDays(year, k) % 7 % 7);
        cout << endl;
    }
}

/*
 *fn2
 *to display the date in a month
 */
void fn2() {
    int year, month, wkday, daysfrom = 0;
    //------wkday means the week date of the YuanDan Day
    //------daysfrom means the days from the YuanDan
    int mthdays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    cout << "Inout the year and the week day of the YuanDan this year:";
    cin >> year >> wkday;
    cout << "Input the month(1~12):";
    cin >> month;
    cout << setw(6) << endl;
    cout << setw(6) << "Sun" << setw(6) << "Mon"<<setw(6) << "Tue" << setw(6) << "Thu" << setw(6) << "Fri" << setw(6) << "Sat" << endl;
    int i, ans;
    for (int i = 1; i < month; i++)
        daysfrom += mthdays[i];
    if (month > 2)
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            daysfrom += 1;
    ans = (wkday + daysfrom) % 7;
    cout << setw(15) << year << "." << setw(1) << month << setw(1) << "." << 1 << setw(3) << week[ans] << endl;
    cout << "-----------------------------------------" << endl;
    int wday = ans;
    cout << setw(6 * (wday + 1)) << endl;
    for (int i = 2; i <= mthdays[month]; i++) {
        wday++;
        if (wday % 7 == 0) {
            cout << endl;
            wday = 0;
        }
        cout << setw(6) << i;

    }
    cout << endl;
    cout << "--------------------------------------" << endl;
}

void fn3() {
    int year, month, date, w;
    cout << "/n Input the year";
    cin >> year;
    cout << "/nInput the month";
    cin >> month;
    cout << "/n Input the date:";
    cin >> date;
    w = year > 0 ? (5 + (year + 1)+(year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 :
            (5 + year + year / 4 + year / 100 + year / 400);
    w = month > 2 ? (w + 2 * (month + 1) + 3 * (month + 1) / 5) % 7 :
            (w + 2 * (month + 2) + 3 * (month + 2) / 5)%7;
    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
        w = (w + 1) % 7;
    w = (w + date) % 7;
    cout << "/n the date is" << week[w];
    cout << endl;
}

/*
 *
 *
 */
int main(int argc, char** argv) {
    int i = 1;
    cout << endl << endl;
    cout << "     $-------------------------------------------------$" << endl;
    cout << "     |                                                  " << endl;
    cout << "     |   **Welcomt to the Calendar                 **  |" << endl;
    cout << "     |                                                  " << endl;
    cout << "     @--------------------------------------------------" << endl;
    while (i) {
        cout << endl << endl;
        cout << "Please choose the function:" << endl;
        cout << "             1. Display the calendar i a year;" << endl;
        cout << "             2. Display the calendar i a month;" << endl;
        cout << "             3. Display the week day of a day;" << endl;
        cout << "             0. Exit;" << endl;
        cout<<"PLease choose from (0~3):";
        cin>>i;
        cout<<endl;
        if(i>=0&&i<=3)
        {
            switch(i)
            {
                case 1: fn1(); break;
                case 2: fn2();break;
                case 3: fn3(); break;
            }
        }
        else
            cout<<"Wrong Input! Input again!"<<endl;

    }
    return 0;
}

 

原创粉丝点击