2016-11-09 45 views
1

我想在C++中编写一个简单的程序,它返回给定日期的星期几。给定日期的C++天数

输入格式为日,月,年。我无法让它与闰年一起工作。当输入年份是闰年时,我试图从a变量中减去一个,但程序只是在没有错误消息的情况下最终崩溃。

我将不胜感激任何建议,但请尽量保持简单,我仍然是初学者。对于这个愚蠢的问题抱歉,请原谅我的错误,这是我第一次在这个网站上发帖。

#include <iostream> 
#include <string> 
#include <vector> 
#include <cmath> 
using namespace std; 


int d; 
int m; 
int y; 


string weekday(int d, int m, int y){ 
    int LeapYears = (int) y/ 4; 
    long a = (y - LeapYears)*365 + LeapYears * 366; 
    if(m >= 2) a += 31; 
    if(m >= 3 && (int)y/4 == y/4) a += 29; 
    else if(m >= 3) a += 28; 
    if(m >= 4) a += 31; 
    if(m >= 5) a += 30; 
    if(m >= 6) a += 31; 
    if(m >= 7) a += 30; 
    if(m >= 8) a += 31; 
    if(m >= 9) a += 31; 
    if(m >= 10) a += 30; 
    if(m >= 11) a += 31; 
    if(m == 12) a += 30; 
    a += d; 
    int b = (a - 2) % 7; 
    switch (b){ 
    case 1: 
     return "Monday"; 
    case 2: 
     return "Tuesday"; 
    case 3: 
     return "Wednesday"; 
    case 4: 
     return "Thursday"; 
    case 5: 
     return "Friday"; 
    case 6: 
     return "Saturday"; 
    case 7: 
     return "Sunday"; 
    } 
} 

int main(){ 
    cin >> d >> m >> y; 
    cout << weekday(d, m, y); 
} 
+3

,当你通过它在你的调试步骤,会发生什么? – MrEricSir

+0

谈到日期时,没有简单的程序这样的事情(去问问jon skeet)。该功能已经存在,为什么重新创建它。 –

+1

有一个简单的计算公式。请参阅https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week –

回答

0

当数字完全被7整除时会发生什么?

14/7 = 2 14%7 = 0

模运算符(%N)将返回从0至n -1

若干如果n除以7的剩余部分可从不被7 所以

int b = (a - 2) % 7; 
    switch (b){ 
    case 1: 
     return "Monday"; 
    case 2: 
     return "Tuesday"; 
    case 3: 
     return "Wednesday"; 
    case 4: 
     return "Thursday"; 
    case 5: 
     return "Friday"; 
    case 6: 
     return "Saturday"; 
    case 7: 
     return "Sunday"; 
    } 
} 

在这种情况下,它不可能是星期天

试试这个

int b = (a - 2) % 7; 
     switch (b){ 
     case 0: 
      return "Sunday"; 
     case 1: 
      return "Monday"; 
     case 2: 
      return "Tuesday"; 
     case 3: 
      return "Wednesday"; 
     case 4: 
      return "Thursday"; 
     case 5: 
      return "Friday"; 
     case 6: 
      return "Saturday"; 
     default: 
      return "Error"; 
     } 
+0

不要听那些说不使用自己的函数的人。 – ComradeJoecool

+0

对于我们所知道的所有地区来说,这是为了好玩,或为了学习体验。 – ComradeJoecool

+0

此外,请记住,您的最终计划在一周中的某一天不正确。那是因为闰年是棘手的事情,并不总是每四年发生一次。闰年的例子不会在第100年发生,但它们确实发生在第400年。所以是的,日历可能会非常快速地变得棘手。我的回答主要是帮助你找到导致程序崩溃的原因。 – ComradeJoecool

2

第一:如果已经有可以处理相同问题的标准化函数,请不要编写自己的函数。要点是,你可能很容易犯一个错误(我现在已经可以在你的weekday()函数的第一行看到一个错误),而标准函数的实现已经过彻底测试,你可以确信它们提供了结果你有望得到。

话虽这么说,这里是用std::localtimestd::mktime一种可能的方法:

#include <ctime> 
#include <iostream> 

int main() 
{ 
    std::tm time_in = { 0, 0, 0, // second, minute, hour 
     9, 10, 2016 - 1900 }; // 1-based day, 0-based month, year since 1900 

    std::time_t time_temp = std::mktime(&time_in); 

    //Note: Return value of localtime is not threadsafe, because it might be 
    // (and will be) reused in subsequent calls to std::localtime! 
    const std::tm * time_out = std::localtime(&time_temp); 

    //Sunday == 0, Monday == 1, and so on ... 
    std::cout << "Today is this day of the week: " << time_out->tm_wday << "\n"; 
    std::cout << "(Sunday is 0, Monday is 1, and so on...)\n"; 

    return 0; 
} 
+0

谢谢您的建议,但编写此程序的重点是练习编程并尝试掌握C++的语法。我不能责怪你提出了一个完全不同的方法,因为我没有在我的文章中提到这一点。但是如果我在编写具有更多经验的更复杂的程序时需要这样的东西,我会确保考虑这种方法! – OerllydSaethwr

+0

一年,〜1800次的意见,而不是一个upvote oO。只是修复它。 @OerllydSaethwr请点击附近的绿色复选标记接受这个(或另一个)答案。 – YSC

+0

关于'std :: localtime'的解决方案是尽快复制它的返回值:'const std :: tm time_out = * std :: localtime(&time_temp);'。 – YSC

1

你的年是什么构成了飞跃的理解是不正确的:

闰年是每4年EXCEPT如果它可以被100整除,但是即使那么它仍然是一个闰年,如果它可以被400整除。

关于如何计算“天数”(dn)的清晰简明的解释可以参见here

一旦你有天数(dn),只需执行一个模数7.结果将是星期几(dow)。

下面是一个例子实现(不检查日期是否有效输入):

#include <iostream> 
#include <iomanip> 

typedef unsigned long ul; 
typedef unsigned int ui; 

// ---------------------------------------------------------------------- 
// Given the year, month and day, return the day number. 
// (see: https://alcor.concordia.ca/~gpkatch/gdate-method.html) 
// ---------------------------------------------------------------------- 
ul CalcDayNumFromDate(ui y, ui m, ui d) 
{ 
    m = (m + 9) % 12; 
    y -= m/10; 
    ul dn = 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (d - 1); 

    return dn; 
} 

// ---------------------------------------------------------------------- 
// Given year, month, day, return the day of week (string). 
// ---------------------------------------------------------------------- 
std::string CalcDayOfWeek(int y, ul m, ul d) 
{ 
    std::string day[] = { 
    "Wednesday", 
    "Thursday", 
    "Friday", 
    "Saturday", 
    "Sunday", 
    "Monday", 
    "Tuesday" 
    }; 

    ul dn = CalcDayNumFromDate(y, m, d); 

    return day[dn % 7]; 
} 

// ---------------------------------------------------------------------- 
// Program entry point. 
// ---------------------------------------------------------------------- 
int main(int argc, char **argv) 
{ 
    ui y = 2017, m = 8, d = 29; // 29th August, 2017. 
    std::string dow = CalcDayOfWeek(y, m, d); 

    std::cout << std::setfill('0') << std::setw(4) << y << "/"; 
    std::cout << std::setfill('0') << std::setw(2) << m << "/"; 
    std::cout << std::setfill('0') << std::setw(2) << d << ": "; 
    std::cout << dow << std::endl; 

    return 0; 
} 
-1
int dayofweek(int day,int month,int year) 
{ 
    int arr[] = {0,3,2,5,3,5,1,4,6,2,4}; 
    if(month<3) 
     year--; 
    return ((year+year/4-year/100+year/400+arr[month-1]+day)%7); 
} 

int main() 
{ 
    int day,month,year; 
    cout<<"Enter the Date for which day of the week need to be find (DD/MM/YYYY)."<<endl; 
    cin>>day>>month>>year; 
    int x = dayofweek(day,month,year); 
    if(x==0) 
     cout<<"Sunday"<<endl; 
    else if(x==1) 
     cout<<"Monday"<<endl; 
    else if(x==2) 
     cout<<"Tuesday"<<endl; 
    else if(x==3) 
     cout<<"Wednesday"<<endl; 
    else if(x==4) 
     cout<<"Thursday"<<endl; 
    else if(x==5) 
     cout<<"Friday"<<endl; 
    else if(x==6) 
     cout<<"Saturday"<<endl; 

}