简体   繁体   English

计算一年中的哪一天 C++

[英]calculate the day of the year c++

I'm new in programming and I'm currently taking C++ .我是编程新手,目前正在学习C++

For example, if the year entered is below 1583 , but month and day value is within range, then the program will display error message.例如,如果输入的year低于1583 ,但monthday值在范围内,则程序将显示错误消息。 However, if the year value is above 1582 , but the month or the day value is out of range, then it will still proceed to calculate the day of the year .但是,如果year值高于1582 ,但monthday值超出范围,那么它仍然会继续计算day的的year

here is my code这是我的代码

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

void showOption();
bool validMonth (int month);
bool validYear (int year);
bool leapYear (int year);
bool validDay (int year, int month, int day);
void getData (int& month, int& day, int& year);
void easterDate (int& month, int& day, int& year);
int daysBetween (int month1, int day1, int month2, int day2, int year);
int dayOfYear (int day, int month, int year);

const int january = 31;
const int february = 28;
const int leapYearFeb = 29;
const int march = 31;
const int april = 30;
const int may = 31;
const int june =30;
const int july = 31;
const int august = 31;
const int september = 30;
const int october = 31;
const int november = 30;
const int decemebr = 31;

int main()
{
    int option;
    int day, month, year;
    int easterDay, easterMonth;
    int day1, day2, month1, month2;

    bool repeat = false;

    showOption();

    cout << "please enter your option: ";
    cin >> option;
    cout << "" << endl;

    switch (option)
    {
        case 1:

            do
            {
                getData(month, day, year);

                validDay(year, month, day);

                if(validDay(year, month, day) == true)
                {
                    cout << "month " << month << " day " << day << " year " << year << endl;
                    cout << " " << endl;
                    cout << "They day of the year based on the date you entered is "<< dayOfYear(day, month, year) << endl;
                    cout << " " << endl;
                }
                else
                {

                    cout << "it is not a valid date." << endl;
                }


                cout << "do you still want to continue?" << endl;
                cout << "0 = no.     1 = yes.     ";
                cin >> repeat;
                cout << " " << endl;

            } while(repeat);

            break;
        case 2:

            do
            {

                cout << "do you still want to continue?" << endl;
                cout << "0 = no.     1 = yes.     ";
                cin >> repeat;

            } while(repeat);

            break;

        case 3:

            do
            {

                cout << "do you still want to continue?" << endl;
                cout << "0 = no.     1 = yes.     ";
                cin >> repeat;

            } while(repeat);

            break;
    }

}

void showOption()
{
    cout << " ------------------MENU------------------ " << endl;
    cout << " 1) Day of the year." << endl;
    cout << " 2) Date of Easter day." << endl;
    cout << " 3) Number of days between 2 days entered." << endl;
    cout << "" << endl;
    cout << "" << endl;
}

bool validMonth (int month)
{
    if (month > 0 || month < 13)
        return true;
    else
        return false;
}

bool validYear (int year)
{
    if (year > 1582)
        return true;
    else
        return false;
}

bool leapYear (int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return true;
    else
        return false;
}

void getData (int& month, int& day, int& year)
{
    cout << "enter the number of year: ";
            cin >> year;
            cout << "" << endl;
            cout << "enter the number of month: ";
            cin >> month;
            cout << "" << endl;
            cout << "enter the number of day: ";
            cin >> day;
            cout << "" << endl;
}

bool validDay (int year, int month, int day)
{
    if(year > 1582)
    {
        if(validMonth(month))
        {
            if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month || 8) || (month == 10) || (month || 12))
            {
                if((day > 0) || (day < 32))
                    return true;
                else
                    return false;
            }
            if((month == 4) || (month == 6) || (month == 9) || (month == 11))
            {
                if((day > 0) || (day < 31))
                    return true;
                else
                    return false;
            }
            if(month == 2)
            {
                if((day > 0) || (day < 29))     
                    return true;
                else
                    return false;
            }
            if((leapYear(year)))
            {
                if((month == 2) && ((day > 0) || (day < 30)))
                    return true;
                else
                    return false;
            }
        }
    }
}


int dayOfYear (int day, int month, int year)
{
    int dayTotal = 0;

    if(validDay(year, month, day))
    {
        if(month == 1)
        {
            dayTotal = 0 + day;
        }

        if(month == 2)
        {
            dayTotal = january + day;
        }

        if((month == 3) && (year > 1582))
        {
            dayTotal = january + february + day;

            if((month == 3) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + day;
            }
        }

        if((month == 4) && (year > 1582))
        {
            dayTotal = january + february + march + day;

            if((month == 4) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + day;
            }
        }

        if((month == 5) && (year > 1582))
        {
            dayTotal = january + february + march + april + day;

            if((month == 5) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + day;
            }
        }

        if((month == 6) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + day;

            if((month == 6) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + day;
            }
        }

        if((month == 7) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + june + day;

            if((month == 7) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + june + day;
            }
        }

        if((month == 8) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + june + july + day;

            if((month == 8) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + june + july + day;
            }
        }

        if((month == 9) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + june + july + august 
                       + day;

            if((month == 9) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + june + july + august 
                       + day;
            }
        }

        if((month == 10) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + june + july + august 
                       + september + day;

            if((month == 10) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + june + july + august
                       + september + day;
            }
        }

        if((month == 11) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + june + july + august 
                       + september + october + day;

            if((month == 11) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + june + july + august 
                       + september + october + day;
            }
        }

        if((month == 12) && (year > 1582))
        {
            dayTotal = january + february + march + april + may + june + july + august
                       + september + october + november + day;

            if((month == 5) && (year > 1582) && (leapYear(year)))
            {
                dayTotal = january + leapYearFeb + march + april + may + june + july + august
                       + september + october + november + day;
            }
        }
    }

    return dayTotal;
}

It compile but the output is not correct.它编译但输出不正确。

Question:题:

I need help and wonder if someone can take a look at my code and tell me what is wrong with it?我需要帮助,想知道是否有人可以查看我的代码并告诉我它有什么问题?

Note: I have been working in this code for 2 days and I cant figure out what I did wrong.注意:我已经在这段代码中工作了 2 天,但我无法弄清楚我做错了什么。 I really appreciate your help and feedback.我非常感谢您的帮助和反馈。 Thank you very much非常感谢

You're using many disjunctions ("or", || ) where you should use conjunctions ("and", && ).您在应该使用连词(“and”、 && )的地方使用了许多析取符(“or”、 || )。
For instance, month > 0 || month < 13例如, month > 0 || month < 13 month > 0 || month < 13 is true for a month of -10 or 1432. month > 0 || month < 13对于 -10 或 1432 month为真。

There are also some places where a ||也有一些地方是|| should be == .应该是==

Month length implement as月长执行为

const int month_lenght [ 31,28,31 ... ];

Warning: january here==0, that problem is present in Java Date january=0 but day 1=first.警告:这里是 january==0,该问题存在于 Java 日期 january=0 但第 1 天=第一天。 So alternative can be:所以替代方案可以是:

const int month_lenght [ 0 /*dummy*/  31,28,31 ... ];

then no那就不要

    if(month == 1)
    {
        dayTotal = 0 + day;
    }

    if(month == 2)
    {
        dayTotal = january + day;
    }

but for loop.但是for循环。

if is required only for February and leap year如果仅在二月和闰年需要

EDIT: agree, errors with && and ||编辑:同意,&& 和 || 的错误

EDIT2: probably EDIT2:可能

void easterDate (int& month, int& day, int& year);

change declaration, maybe to:更改声明,也许是:

void easterDate (int& month /* out */, int& day /*out */ , int year /* in */);

BTW all world except english-USA culture think (and compute) y/m/d or d/m/y顺便说一句,除英美文化之外的所有世界都认为(和计算)y/m/d 或 d/m/y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM