在c++中,使用循环查找任意给定日期的天数

In C++, using a loop to find the # of days on any given date?

本文关键字:日期 任意 查找 c++ 循环      更新时间:2023-10-16

是的,这是一个作业——所以请不要发布解决方案,但详细的伪代码非常有帮助!

我已经用c++编写了一个程序,它可以接受用户的日期并确定它是否是闰年。

到目前为止,这是我的闰年函数(我希望这是正确的逻辑):

bool isLeapYear (int year){
    int leapYear = 0;
    //leapyear = 1 when true, = 0 when false
    if ((year%400 == 0) && (year%100 != 100)) {
        leapYear = 1;
        }
    else if (year%4 == 0) {
        leapYear = 1;
        }
    else {
        leapYear = 0;
        }

    if (leapYear == 1) {
        return 1;
    }
    else {
        return 0;
    }     
}

下面是我接下来要做的事情:

你必须使用一个循环,每次一个月加一个累积的总和。不要使用任何硬编码值例如152表示闰年的前5个月

澄清一下,这是我第一次上编程课,到现在我已经上c++课一个月了。所以如果有人能帮我弄清楚如何做循环语句来添加月份的数量,我将不胜感激。(例如:12/31/2013在非闰年应该是"365",在闰年应该是"366")。

我知道这是错误的,但这是我到目前为止为一个函数"dayNumber"简单地返回一年中主函数的天数(这是调用dayNumber函数):

int dayNumber (int day, int month, int year){
    //variable declarations
    int sumTotal = 0;

    for ( int loop = 1; loop < month; loop++) {
        sumTotal = (( month-1 ) * 31);
        sumTotal = sumTotal + day + 1;
        if ( (loop==4) || (loop=6) || (loop=9) ||
            (loop==11) ) {
            sumTotal = ( sumTotal - 1 );
        }
        else if ( isLeapYear(year) == 1 ) {
            sumTotal = (sumTotal - 2);
        }
        else  {
            sumTotal = (sumTotal - 3);
        }
    }

    return sumTotal;
}

为了得到一个合适的值,我开始乱搞了好几天,我知道,但这更把它搞砸了,哈哈。

如果有人有任何关于如何适当地做循环的指导,我将非常感激!:)



编辑:好了,我想我可能已经回答了我自己的问题。

int dayNumber (int日,int月,int年){//变量声明int sumTotal = 0;

for (int loop = 1;循环& lt;月;循环+ +){sumTotal = (sumTotal + 31);

   if ( (loop==4) || (loop==6) || (loop==9) ||
       (loop==11) ) {
       sumTotal = ( sumTotal - 1 );
   }

}If ((month !=2) &&(月> 1)){if (isLeapYear(year) ==1) {sumTotal = (sumTotal - 2);}其他{sumTotal = (sumTotal - 3);}}

其他{sumTotal = sumTotal;}= sumTotal + day;

返回将sumtotals;}

我确实需要练习我的循环。我很感激你让我知道我的"="应该是"=="!

我相信这是一个适当的代码使用足够简单的循环吗?我将测试更多日期。到目前为止,我只测试了课程网站上提供的几个。

我不能回复我自己的帖子,我没有足够的声誉。

我知道一个答案已经被接受了,但是我花了一些时间来写我自己的答案,让我们看看它是否可以添加一些总体信息


让我们慢慢复习一下。

首先,你的isLeapYear()函数不完全正确。

在不深入研究算法部分的情况下,有两三件事可以改进。

  1. 您返回的是bool,但是您的返回语句返回的是int s。这本身并没有错,但是使用truefalse关键字可以提高可读性和一致性。

  2. 你应该立即返回你的结果,而不是创建、赋值和返回一个变量。

  3. 在操作符周围增加空格:year%400应该变成year % 400


现在你的代码。

此条件:

if ((year%400 == 0) && (year%100 != 100))

…尤其是这部分:

(year%100 != 100)

没有做你期望的事。

总的来说,算法如下:

如果年不能被4整除普通年
如果 year不能被100整除闰年
如果 year不能被400整除普通年份
else闰年

翻译成代码:

/**/ if (year % 4 != 0)
    return false;
else if (year % 100 != 0)
    return true;
else if (year % 400 != 0)
    return false;
else
    return true;

现在让我们简化一下:

/**/ if (year % 4 == 0 && year % 100 != 0)
    return true;
else if (year % 400 == 0)
    return true;
else
    return false;

:

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    return true;
else
    return false;

最后,可以直接返回整个布尔表达式:

return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

现在你的函数是正确的,让我们尝试一个算法到你的dayNumber函数:

如果参数中提供的日期被认为是正确的,那么算法非常简单:

  • 0启动sum
  • 排除从1到month的循环
    • 如果month是二月,那么如果isLeapYear返回true,则添加29,否则28
    • 如果month是1月、3月、5月、7月、8月、10月或12月,添加31
    • 添加30
  • 添加daysum

我知道给出详细的伪代码比一个解决方案更有帮助,但我还不是一个好老师:/祝你好运,当你完成后睡个好觉:)


我假设dayNumber(8, 3, 1914)应该返回1914年的ISO日数,即3月8日。

你要做的是:

procedure dayNumber(theDay, theMonth, theYear):
    set answer to 1  // the first day of the year is day 1
    for every month 'thisMonth', up to and excluding theMonth:
        increment answer by the number of days in thisMonth
    increment answer by (theDay - 1)  // the first day of the month is monthday 1
    return answer

在迭代语句(或"循环体")中,有三种情况:

  1. 2月:如果是闰年,则thisMonth增加29,否则增加28。
  2. 短月份(4月、6月、9月、11月):增加30天。
  3. 长月份(1月、3月、5月、7月、8月、10月、12月):增加31天。

转换成:

if (thisMonth == 2) {
    // February
    if (isLeapYear(theYear))
        thisMonth += 29;
    else
        thisMonth += 28;
} else if (thisMonth == 4 || thisMonth == 6 || thisMonth == 9 || thisMonth == 11) {
    // Short month
    thisMonth += 30;
} else {
    // Long month
    thisMonth += 31;
}

(注:X += Y是或应该是X = X + Y的缩写)

你使用的循环逻辑在我看来大部分都是正确的,除了1)因为你从第0天开始,而偏离1的错误,以及2)在循环内而不是在循环外添加月日:

int dayNumber (int theDay, int theMonth, int theYear) {
    int result = 1;
    for (int thisMonth = 1; thisMonth < theMonth; thisMonth++) {
        /* ... see above ... */
    }
    return result + theDay - 1;
}

现在,关于使迭代语句更漂亮,基本上有两种方法(如果您的课程还没有涉及到它们,我当然建议不要在答案中使用它们)。一种是使用switch语句,但我真的不喜欢它们,而且与我已经给出的代码相比,它并没有提供多少好处。另一种是使用数组:

int dayNumber (int theDay, int theMonth, int theYear) {
    int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31
                        , 31, 30, 31, 30, 31 }
    int result = 1;
    for (int thisMonth = 1; thisMonth < theMonth; thisMonth++) {
        if (thisMonth == 2 && isLeapYear(theYear))
            // Special case: February in a leap year.
            result += 29;
        else
            /* Take the month length from the array.
             * Because C++'s array indices begin at 0,
             * but our first month is month 1,
             * we have to subtract one from thisMonth.
             */
            result += monthDays[thisMonth - 1];
    }
    return result + theDay - 1;
}
#include <ctime>
static int GetDaysInMonthOfTheDate(std::tm curDate)
{
    std::tm date = curDate;
    int i = 0;
    for (i = 29; i <= 31; i++)
    {
        date.tm_mday = i;
        mktime(&date);
        if (date1->tm_mon != curDate.tm_mon)
        {
            break;
        }
    }
    return i - 1;    
}