Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 查找特定年份和闰年中的特定日期_C++ - Fatal编程技术网

C++ 查找特定年份和闰年中的特定日期

C++ 查找特定年份和闰年中的特定日期,c++,C++,所以,我已经完成了大部分程序,我仍然需要确定当前年份和闰年中的1月1日是一周中的哪一天:闰年是一个数字可以被4整除的年份。然而,如果世纪年正好可以被400整除,那么世纪年就是闰年。因此,1900年不是闰年,而2000年是。我有点纠结于从这里走向何方,我在头脑中理解它,但我很难将我的想法转化为代码。如果有人能把我推向正确的方向,或者你有解决办法,我真的很感谢你的帮助 #include <ctime> #include <iostream> using namespace s

所以,我已经完成了大部分程序,我仍然需要确定当前年份和闰年中的1月1日是一周中的哪一天:闰年是一个数字可以被4整除的年份。然而,如果世纪年正好可以被400整除,那么世纪年就是闰年。因此,1900年不是闰年,而2000年是。我有点纠结于从这里走向何方,我在头脑中理解它,但我很难将我的想法转化为代码。如果有人能把我推向正确的方向,或者你有解决办法,我真的很感谢你的帮助

#include <ctime>
#include <iostream>
using namespace std;

int main()
{   
    tm dateTime;        
    time_t systemTime = time(0);        
    localtime_s( &dateTime, &systemTime ); 
    int day = dateTime.tm_mday;//theTime.tm_mday;
    int month = dateTime.tm_mon+1;//theTime.tm_mon;
    int year = dateTime.tm_year + 1900;//theTime.tm_year + 1900;
    int weekDay  = dateTime.tm_wday;
    cout << "Today is ";
    switch (weekDay){
        case 0: cout << "Sunday, ";
            break;
        case 1: cout << "Monday, ";
            break;
        case 2: cout << "Tuesday, ";
            break;
        case 3: cout << "Wednesday, ";
            break;
        case 4: cout << "Thursday, ";
            break;
        case 5: cout << "Friday, ";
            break;
        case 6: cout << "Saturday, ";
            break;
    }
    cout << month << "/" << day << "/" << year << endl;
}
#包括
#包括
使用名称空间std;
int main()
{   
tm日期时间;
时间\u t系统时间=时间(0);
本地时间(日期时间和系统时间);
int day=dateTime.tm\u mday;//theTime.tm\u mday;
int month=dateTime.tm\u mon+1;//theTime.tm\u mon;
int year=dateTime.tm_year+1900;//theTime.tm_year+1900;
int weekDay=dateTime.tm_wday;

cout若要检查给定的数字是否可被另一个数字整除,请使用模(
%
)运算符。如果
a%b==0
,则表示
a
可被
b
整除

bool is_leap_year(int year) {
    if (year % 4 != 0) return false;
    // Year is divisible by 4; It is a leap year
    if (year % 100 == 0) {
        // Unless it is also divisible by 100, in which case it is not a leap year
        // Except when it is divisible by 400
        if (year % 400 == 0) return true;
        return false;
    }
    return true;
}

// Equivalent to
bool is_leap_year(int year) {
    return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}
然后,在找出从当前日期算起的1月1日的工作日时,您必须再次使用模数运算符。这次,您将使用
a%7
,当某个值除以7时,将得到余数(因此15天前是另一周的
15%7==1

  • 使用模(
    %
    )确定年份是否可除以4。
    • 如果不是,那就不是跳跃
  • 请注意,
    运算符%
    的结果等于0,当且仅当lhs可除以rhs

  • 然后,用alghoritm后面的逻辑应用相同的运算符,确定年份是否为闰年,如您在问题中所述。详细信息见我的答案代码注释
  • 例如:

    #include <iostream>
    
    #包括
    
    intmain()
    {
    
    std::cout使用历元作为参考日期(我们真的可以选择任何日期),我们只需计算当前日期即可得到一周中的哪一天

    一个优化是,我们可以先增加年份,然后让
    dayofweek
    变量增加1或2,这取决于我们当前是否在计算闰年

    bool isLeapYear(int year)
    {
        if ((year % 400) == 0)
            return true;
        if ((year % 100) == 0)
            return false;
        return ((year % 4) == 0);
    }
    
    // return the day of the week for a given month/day/year value
    // Sunday is 0.  Monday is 1.... Saturday is 6;
    int GetDayOfWeek(int month, int day, int year)
    {
       int dayOfWeek = 5; // January 1, 1970 was a Thursday
       int daysOfMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31];
       int d = 1;
       int m = 1;
       int y = 1970;
    
       if (year < 1970)
         return -1;
    
       // go "year by year" incrementing dayOfWeek by 1 or 2 based on leap year
       while (y < year)
       {
           dayOfWeek = (isLeapYear(y) ? 2 : 1) % 7;
           y++;
       }
    
       while (d != day && m != month)
       {
          // increment the day
          d++;
          dayOfWeek = (dayOfWeek + 1) % 7;
          if (d > daysOfMonth[m]) // new month
          {
              d = 1;
              m++;
          }
       }
    
       return dayOfWeek;
    }
    
    bool isLeapYear(整数年)
    {
    如果((年份%400)==0)
    返回true;
    如果((年份%100)==0)
    返回false;
    回报率((第%4年)=0);
    }
    //返回给定月/日/年值的星期几
    //星期天是0。星期一是1…星期六是6;
    int GetDayOfWeek(int月、int日、int年)
    {
    int dayOfWeek=5;//1970年1月1日是星期四
    int daysOfMonth=[0,31,28,31,30,31,31,30,31,31];
    int d=1;
    int m=1;
    int y=1970;
    如果(年份<1970年)
    返回-1;
    //以闰年为基础,将每周的dayOfWeek增加1或2
    而(y<年)
    {
    星期天=(年?2:1)%7;
    y++;
    }
    而(d!=日和m!=月)
    {
    //日增
    d++;
    dayOfWeek=(dayOfWeek+1)%7;
    如果(d>daysOfMonth[m])//新月份
    {
    d=1;
    m++;
    }
    }
    星期五返回日;
    }
    
    只是IM程序试图解决的参数的一部分,我编辑过了,也许现在它更合理了。你应该使用STD::时间。有一个函数:ISHLeAP.对于赋值,它必须使用我们迄今所学的,而且我还没有学会STD::但是,它必须非常简单。但是我对C++来说是如此新。不到两周的时间,我被困在这里,不知道该去哪里。你基本上只需要一个函数来计算一年是否是闰年?是的,然后确定一月一日是闰年的哪一天。谢谢你,这是我脑子里想的,但无法用语言表达,我需要学习如何理解e更好的是,我认为这将对我的编程技能有很大帮助。因此,基本上如果我理解正确,我可以将这个加上查找一月一日的最后一个方法添加到我的程序末尾,然后添加适当的cout,它应该会起作用?@TyDoris这个函数在查找一月一日的工作日时非常有用,因为您可以尝试它,好像它如果不是闰年,那么
    if(is_leap_year(year)&&month>2){/*还有一天是计算出来的*/jan1_weekday=(jan1_weekday-1)%7;}
    。这取决于你想做的事情。我喜欢这种方法,让我来讨论一下这个事实上是检查闰年的唯一答案。
    int main()
    {
        std::cout << std::boolalpha << IsLeap(2003) << std::endl; // false (not dividable by 4)
        std::cout << std::boolalpha << IsLeap(2004) << std::endl; // true  (dividable by 4 and not dividable by 100)
        std::cout << std::boolalpha << IsLeap(2000) << std::endl; // true  (dividable by 4, 100 and 400)
        std::cout << std::boolalpha << IsLeap(1900) << std::endl; // false (dividable by 4 and 100, but not by 400)
    }
    
    bool isLeapYear(int year)
    {
        if ((year % 400) == 0)
            return true;
        if ((year % 100) == 0)
            return false;
        return ((year % 4) == 0);
    }
    
    // return the day of the week for a given month/day/year value
    // Sunday is 0.  Monday is 1.... Saturday is 6;
    int GetDayOfWeek(int month, int day, int year)
    {
       int dayOfWeek = 5; // January 1, 1970 was a Thursday
       int daysOfMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31];
       int d = 1;
       int m = 1;
       int y = 1970;
    
       if (year < 1970)
         return -1;
    
       // go "year by year" incrementing dayOfWeek by 1 or 2 based on leap year
       while (y < year)
       {
           dayOfWeek = (isLeapYear(y) ? 2 : 1) % 7;
           y++;
       }
    
       while (d != day && m != month)
       {
          // increment the day
          d++;
          dayOfWeek = (dayOfWeek + 1) % 7;
          if (d > daysOfMonth[m]) // new month
          {
              d = 1;
              m++;
          }
       }
    
       return dayOfWeek;
    }