使用月历程序的年份开始日期查找某个月份的开始日期

时间:2021-03-07 23:03:32

标签: c++

想出一种在我的程序中找到一个月的开始日期的方法,我真的在挠头。我正在学习基本的 C++,所以我们只学习了 switch、if/else/while 语句等基础知识。这是我到目前为止所想出的。它很乱,我需要设置一个 switch 语句,但我现在只是想让它工作。我在 0-6 0 是星期日和 6 是坐定年份的开始日。我将如何为单个月找到这个

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
char response;
int numdays;

do {
    // Obtaining the month from user 
    int month;
    cout << " enter a month between 1 and 12 inclusive" << endl;
    cin >> month;
    while (month > 12 || month < 1) //error check
    {
        cout << "Month cannot be less than 1 or more than 12," << endl;
        cout << "please enter the month again";
        cin >> month;
    }


    // Obtaining the year from user 
    int year;
    cout << " enter the year for your calandar in numerical fashion" << endl;
    cin >> year;
    while (year < 2000) //error check
    {
        cout << "Year cannot be less than 2000" << endl;
        cout << "please enter the year again";
        cin >> year;
    }



    int firstday, leapyearcheck;
    firstday = ((int)(((year - 1) * 365) + floor((year - 1) / 4) - floor((year - 1) / 100) + 
floor((year - 1) / 400)) + 1) % 7;
    leapyearcheck = (!(year % 4) && (year % 100)) || !(year % 400);

    while (month <= 12) {
        if (month == 1) {
            numdays = 31;
            cout << setw(12) << "January" <<setw(20)<<year<< endl; break;
        }
        else if (month == 2) {
            if (leapyearcheck == false) {
                numdays = 28;
            }
            else numdays = 29;
            cout << setw(12) << "February" << setw(20) << year << endl; break;
        }
        else if (month == 3) {
            numdays = 31;
            cout << setw(12) << "March" << setw(20) << year << endl; break;
        }
        else if (month == 4) {
            numdays = 30;
            cout << setw(12) << "April" << setw(20) << year << endl; break;
        }
        else if (month == 5) {
            numdays = 31;
            cout << setw(12) << "May" << setw(20) << year << endl; break;
        }
        else if (month == 6) {
            numdays = 30;
            cout << setw(12) << "June" << setw(20) << year << endl; break;
        }
        else if (month == 7) {
            numdays = 31;
            cout << setw(12) << "July" << setw(20) << year << endl; break;
        }
        else if (month == 8) {
            numdays = 31;
            cout << setw(12) << "August" << setw(20) << year << endl; break;
        }
        else if (month == 9) {
            numdays = 30;
            cout << setw(12) << "September" << setw(20) << year << endl; break;
        }
        else if (month == 10) {
            numdays = 31;
            cout << setw(12) << "October" << setw(20) << year << endl; break;
        }
        else if (month == 11) {
            numdays = 30;
            cout << "November" << setw(20) << year << endl; break;
        }
        else if (month == 12) {
            numdays = 31;
            cout << setw(12) << "December" << setw(20) << year << endl; break;
        }


        


    }


    
    
    
    cout<<"S"<< setw(6) <<"M"<< setw(6) <<"T"<< setw(6) <<"W"<< setw(6) <<"T"<< setw(6)<<"F"<< 
setw(6)<<"S"<< endl;
    cout << "_____________________________________" << endl << endl;

    for (int k = 0; k < firstday; k++){

        cout << "      ";

    }

    for (int x = 1; x <= numdays; x++) {
        cout << x << setw(6);
    }
    

        cout << endl << endl << "Would You Like Another Calandar? Y or N " << endl;
        cin >> response;
        cout << endl << endl << endl;
    } while (response == 'Y' || response == 'y');


    return 0;`

0 个答案:

没有答案