2016-11-09 46 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; 

}