2013-11-10 116 views
2

我想寫一個輸出日曆的程序。用戶必須輸入月份開始的那一天(星期一-0,星期二-1等)以及月中有多少天。根據月份開始的哪一天,日曆日期將在該特定日期之下開始。我遇到的問題是,我不確定如何讓日曆在特定的一天開始,而且我不確定如何在7天后將日期更改爲新的行。任何幫助,將不勝感激。到目前爲止,我們還沒有學到很多,所以我只允許使用基礎知識,沒有功能或類似的東西。C + +日曆問題

這是我到目前爲止。我可能會遠離。

#include <iostream> 
#include <iomanip> 
#include <conio.h> 

using namespace std; 

int main() 
{ 
    int monthStartDay, daysInMonth; 

    cout << "Enter the first day of the month: "; 
    cin >> monthStartDay; 
    cout << "Enter how many days are in the month: "; 
    cin >> daysInMonth; 

    cout<<"\nSun Mon Tue Wed Thu Fri Sat"; 
    cout<<"\n\n"<<setw(2); 

    for (int x=1; x <= daysInMonth; x++){ 
     cout << x << setw(6); 

     for (int i=1; i == 6; i++) { 
      cout << "\n"; 
     } 
} 
    return 0; 
} 
+0

我有一個在相關職位上的答案 http://stackoverflow.com/questions/37582163/c-calendar-loop-design/37582461#37582461 – winux

回答

0

你有內部循環檢查,如果我== 6,那永遠不會發生。也許你爲什麼感覺卡住了?

這裏有兩個問題需要解決:如何把新線放在正確的位置,以及如何放置到第一天。

我會給你留下第一個問題。

在沒有給出答案的情況下,很難給出具體的幫助。讓我開始。如果我說第一天是第五天,而且有30天,那麼你打算打印出35件事情。前五個是空的空間,其餘的是當天的數字。

+0

謝謝。我現在已經開始工作了。 – apache

0

我想,這是你正在尋找的東西:

#include <iostream> 
    #include <iomanip> 
    #include <conio.h> 

    using namespace std; 

    int main() 
    { 
     int monthStartDay, daysInMonth; 

     cout << "Enter the first day of the month: "; 
     cin >> monthStartDay; 
     cout << "Enter how many days are in the month: "; 
     cin >> daysInMonth; 

     cout<<"\nSun Mon Tue Wed Thu Fri Sat"; 
     cout<<"\n\n"<<setw(2); 


     int offset = monthStartDay;   // offset for the first date 
     for (int i = 0; i < offset; ++i) 
      cout << "" << setw(6);    // output blank space 

     for (int x=1 ; x <= daysInMonth; x++) 
     { 
      cout << x << setw(6);   
      if ((x+offset)%7 == 0)   // after each 7th output we have to 
       cout << "\n ";    // make a new line 
     } 
     return 0; 
    } 

而且我相信,在您的日曆頭的情況下,這將是星期日= 0,星期一= 1,...

+0

謝謝,我欣賞它。沒有複製和粘貼你的代碼行,但確保我瞭解他們,我已經將他們寫入我的程序,現在它可以工作。謝謝。 – apache

2

解決方案使用的是新索引,該索引將在日曆行中顯示位置。那就是:

int startDayPostion = (monthStartDay + 1) % 7; 

因爲您從星期一開始計數爲零,但您的打印是從星期日開始的。因此需要「向右移」。在閱讀monthStartDay後添加上面的行。

你需要再添加一個循環,這將打印您需要的所有空間,而將轉向上述位置所需startDayPostion

int p = 0; 
for (; p < startDayPostion; ++p) { 
    cout << "" << setw(6); 
} 

(您for循環與x前插入此)

現在,當你有一個班次時,你可以簡單地填充其餘的單元格,記住你在結束之前(Sat)。

cout << x << setw(6); 

不停變動的幫助索引:

++p; 

,然後,如果你是用線後,進入到新的生產線和復位號碼:

if (p > 6) { 
    cout << '\n'; 
    p = 0; 
} 

我不知道你爲什麼把這裏放一個for (int i=1; i == 6; i++)循環...您可以簡單地刪除這些代碼行。

+0

謝謝。我現在已經開始工作了。 – apache

+0

不客氣。我很高興我的支持很有用。請點擊我答案附近的向上箭頭按鈕:D :)如果您有任何其他問題,請隨時詢問。 – ktalik

0

這是我鞭打的東西 - 希望它有幫助!

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 

using namespace std; 


int main() 
{ 
    int i, days, startday; 

    cout << "Please enter the number of days in the month:" << endl; 
    cin >> days; 

    cout << "Please enter the starting date of the month (1 = Monday, 7 = Sunday):" << endl; 
    cin >> startday; 

    for (i = 1; i < startday; i++) 
    { 
     cout << " "; 
    } 

    for (i = 1; i <= days; i++) 
    { 
     cout << setw(3) << i; 

     if ((startday + i - 1) % 7 == 0) 
     { 
      cout << "\n"; 
     } 
    } 

    cout << endl; 

    system("pause"); 

    return 0; 
} 

讓我知道是否需要澄清。