2016-03-15 78 views
0

已經在這項任務上工作了20多個小時,並且我理解人們出於某種原因從不想幫助完成家庭作業,但我從字面上看不到任何地方,並且認爲我會至少嘗試並要求幫助,因爲我曾嘗試在多個論壇上無濟於事。我的代碼如下...日曆在C編程語言作業

#define _CRT_SECURE_NO_WARNINGS 
 
#include <stdio.h> 
 

 
int getdaycode(int month, int year); 
 
void printheader(int month, int year); 
 
int getndim(int month, int year); 
 

 
int main() 
 
{ 
 
\t int day, month, year, nummonths; 
 
\t { 
 
\t \t printf("Enter Month, Year, and Number of Months"); 
 
\t \t scanf("%d %d %d", &month, &year, &nummonths); 
 
\t \t 
 
\t \t for (nummonths; nummonths < 13; nummonths++) 
 
\t \t \t if (nummonths > 13) 
 
\t \t \t \t year = year + 1; 
 

 
\t \t printheader(month, year); 
 
\t \t int numdays = getndim(month, year); 
 
\t \t int daycode = getdaycode(month, year); 
 
\t \t 
 
\t \t for (day = 1; day <= 1 + daycode * 4; day++) 
 
\t \t \t printf(" "); 
 
\t \t 
 
\t \t for (day = 1; day <= numdays; day++) 
 
\t \t 
 
\t \t { 
 
\t \t \t printf("%2d", day); 
 
\t \t \t if ((day + daycode) % 7 > 0) 
 

 
\t \t \t \t printf(" "); 
 
\t \t \t else 
 
\t \t \t \t printf("\n"); 
 
\t \t } 
 
\t \t daycode = (daycode + numdays) % 7; 
 
\t } 
 
} 
 

 

 
int getdaycode(int month, int year) 
 
{ 
 
\t int numdays; 
 
\t { 
 
\t \t numdays = ((year - 1) * 365 + ((year - 1)/4) - ((year - 1)/100) + ((year - 1)/400)); // how many days including exceptions 
 

 
\t \t if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) \t \t //check if leapyear 
 
\t \t { 
 
\t \t \t if (month == 1) \t \t \t \t \t \t \t // January 
 
\t \t \t \t numdays = numdays; 
 
\t \t \t if (month == 2) \t \t \t \t \t \t \t // February 
 
\t \t \t \t numdays = numdays + 31; 
 
\t \t \t if (month == 3) \t \t \t \t \t \t \t // March 
 
\t \t \t \t numdays = numdays + 28 + 31 + 1; 
 
\t \t \t if (month == 4) \t \t \t \t \t \t \t // April 
 
\t \t \t \t numdays = numdays + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 5) \t \t \t \t \t \t \t // May 
 
\t \t \t \t numdays = numdays + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 6) \t \t \t \t \t \t \t // June 
 
\t \t \t \t numdays = numdays + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 7) \t \t \t \t \t \t \t // July 
 
\t \t \t \t numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 8) \t \t \t \t \t \t \t // August 
 
\t \t \t \t numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 9) \t \t \t \t \t \t \t // September 
 
\t \t \t \t numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 10) \t \t \t \t \t \t \t // October \t \t \t \t \t \t 
 
\t \t \t \t numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 11) \t \t \t \t \t \t \t // November 
 
\t \t \t \t numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t \t if (month == 12) \t \t \t \t \t \t \t // December 
 
\t \t \t \t numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1; 
 
\t \t } 
 
\t \t else 
 
\t \t { 
 
\t \t \t if (month == 1) \t \t \t \t \t \t \t // January 
 
\t \t \t \t numdays = numdays; 
 
\t \t \t if (month == 2) \t \t \t \t \t \t \t // February 
 
\t \t \t \t numdays = numdays + 31; 
 
\t \t \t if (month == 3) \t \t \t \t \t \t \t // March 
 
\t \t \t \t numdays = numdays + 28 + 31; 
 
\t \t \t if (month == 4) \t \t \t \t \t \t \t // April 
 
\t \t \t \t numdays = numdays + 31 + 28 + 31; 
 
\t \t \t if (month == 5) \t \t \t \t \t \t \t // May 
 
\t \t \t \t numdays = numdays + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 6) \t \t \t \t \t \t \t // June 
 
\t \t \t \t numdays = numdays + 31 + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 7) \t \t \t \t \t \t \t // July 
 
\t \t \t \t numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 8) \t \t \t \t \t \t \t // August 
 
\t \t \t \t numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 9) \t \t \t \t \t \t \t // September 
 
\t \t \t \t numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 10) \t \t \t \t \t \t \t // October \t \t \t \t \t \t 
 
\t \t \t \t numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 11) \t \t \t \t \t \t \t // November 
 
\t \t \t \t numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; 
 
\t \t \t if (month == 12) \t \t \t \t \t \t \t // December 
 
\t \t \t \t numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; 
 
\t \t } 
 

 
\t \t int daycode = numdays % 7; 
 
\t \t switch (daycode) 
 
\t \t { 
 
\t \t case 0: 
 
\t \t \t printf("Sunday\n"); 
 
\t \t \t break; 
 

 
\t \t case 1: 
 
\t \t \t printf("Monday\n"); 
 
\t \t \t break; 
 

 
\t \t case 2: 
 
\t \t \t printf("Tuesday\n"); 
 
\t \t \t break; 
 

 
\t \t case 3: 
 
\t \t \t printf("Wednesday\n"); 
 
\t \t \t break; 
 

 
\t \t case 4: 
 
\t \t \t printf("Thursday\n"); 
 
\t \t \t break; 
 

 
\t \t case 5: 
 
\t \t \t printf("Friday\n"); 
 
\t \t \t break; 
 

 
\t \t case 6: 
 
\t \t \t printf("Saturday\n"); 
 
\t \t \t break; 
 

 
\t \t default: printf("unexpected error (daycode case) daycode = %d", daycode); 
 
\t \t \t break; 
 
\t \t } 
 
\t \t return daycode; 
 
\t } 
 
} 
 

 
void printheader(int month, int year) 
 
\t { 
 
\t \t \t printf("%14d %1d\n", month, year); 
 
\t \t \t printf("Sun "); 
 
\t \t \t printf("Mon "); 
 
\t \t \t printf("Tue "); 
 
\t \t \t printf("Wed "); 
 
\t \t \t printf("Thu "); 
 
\t \t \t printf("Fri "); 
 
\t \t \t printf("Sat\n"); 
 
\t \t } 
 

 
int getndim(int month, int year) 
 
{ 
 
\t int numdays; 
 
\t if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) \t \t //check if leapyear 
 
\t { 
 
\t \t if (month == 1) \t \t \t \t \t \t \t // January 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 2) \t \t \t \t \t \t \t // February 
 
\t \t \t numdays = 29; 
 
\t \t if (month == 3) \t \t \t \t \t \t \t // March 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 4) \t \t \t \t \t \t \t // April 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 5) \t \t \t \t \t \t \t // May 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 6) \t \t \t \t \t \t \t // June 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 7) \t \t \t \t \t \t \t // July 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 8) \t \t \t \t \t \t \t // August 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 9) \t \t \t \t \t \t \t // September 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 10) \t \t \t \t \t \t \t // October \t \t \t \t \t \t 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 11) \t \t \t \t \t \t \t // November 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 12) \t \t \t \t \t \t \t // December 
 
\t \t \t numdays = 31; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t if (month == 1) \t \t \t \t \t \t \t // January 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 2) \t \t \t \t \t \t \t // February 
 
\t \t \t numdays = 28; 
 
\t \t if (month == 3) \t \t \t \t \t \t \t // March 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 4) \t \t \t \t \t \t \t // April 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 5) \t \t \t \t \t \t \t // May 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 6) \t \t \t \t \t \t \t // June 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 7) \t \t \t \t \t \t \t // July 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 8) \t \t \t \t \t \t \t // August 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 9) \t \t \t \t \t \t \t // September 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 10) \t \t \t \t \t \t \t // October \t \t \t \t \t \t 
 
\t \t \t numdays = 31; 
 
\t \t if (month == 11) \t \t \t \t \t \t \t // November 
 
\t \t \t numdays = 30; 
 
\t \t if (month == 12) \t \t \t \t \t \t \t // December 
 
\t \t \t numdays = 31; 
 
\t } 
 
\t return numdays; 
 
}

enter image description here

我想知道的是如何正確地調用我的getdaycode功能爲我的主要功能。此外,當這不是問題時,這一天繼續出現在我的標題下方,它隨着排列日曆的日子排序而混亂。這是我在這一點上最大的兩個問題,我真的很喜歡和某人一起工作......真的很困惑......並且在你回憶你的材料之前......我是編程時沒有背景的全新人物,以及本課程不提供任何資源,例如書籍或網站以供參考。 (這使得極其困難)。任何幫助非常感謝!

+0

「我理解人們出於某種原因從來沒有想幫助做家庭作業「 - 這是不正確的。人們不想要的是爲懶蟲編寫代碼。 [這裏是](http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions)在家庭作業上接受的政策:不解決,做*幫助*解決。至於你的問題,多一點細節會很好。例如,「如何正確地將我的getdaycode函數調用到我的主函數中」 - 現在你調用它的方式有什麼問題? – Amadan

+0

「這一天一直顯示在我的標題下方,它與混亂日曆的日子的順序混亂」 - 在你的問題中顯示輸出,不要讓人編譯 - 並顯示你將如何*喜歡* (以防止我們猜測你想要什麼)。描述該程序是什麼*爲*。一個很好的問題是獲得很好的答案。 – Amadan

+0

我不知道如何發佈我的輸出/調試過程中顯示的結果 – tyooo

回答

0

它可能對你有幫助和有用。

int main() 
{ 
int day, month, year, nummonths; 
    printf("Enter Month, Year, and Number of Months"); 
    scanf("%d %d %d", &month, &year, &nummonths); 
    if (nummonths > 12) 
     year = year + nummonths%12; 

    printheader(month, year); 
    int numdays = getndim(month, year); 
    int daycode = getdaycode(month, day, year); 

    for (day = 1; day <= numdays; day++) 
    { printf("%4d", day); 
     if (day % 7 > 0) 
      printf("\t"); 
     else 
      printf("\n"); 
    } 
} 

但除了形成該具體問題的解決方案,你需要工作的一些技能,可以幫助你。

請喜歡的一些書和網上資源,並完成其中任何一個

1>書: - 讓我們C,通過: - balaguruswami

2>教程http://www.w3schools.in/c/intro/

3>教程http://www.tutorialspoint.com/cprogramming/

+0

很酷的感謝,這是我現在正在處理的格式。只是好奇,我知道\ n是新線,不是什麼意思? – tyooo

+0

\ t是標籤空間 –

+0

此外,爲什麼我得到'int daycode = getdaycode(month,day,year)'的本地錯誤;' – tyooo

0
int getdaycode(int month, int day, int year) 
{ 
    int numdays; 
    numdays = (whatever it is); // how many days including exceptions 
      if (month == 1)       
       numdays = numdays; 
      if (month == 2)       
       numdays = numdays + 31; 
      if (month == 3)       
       numdays = numdays + 28 + 31; 
      . 
      . 
      . 
      . 
      . 
     if (check condition)  //check if leapyear 
     { 
      if (month == 1 || month == 2)       // January 
       numdays = numdays; 
      else 
       numdays = numdays +1; 
     }  
     int daycode = numdays % 7; 
     return daycode; 
} 

void printheader(int month, int year) 
{ 
     printf("\n\tSun "); 
     printf("\tMon "); 
     printf("\tTue "); 
} 
+0

那麼你肯定知道你在做什麼,= P,但我認爲我的功能一切都很好,因爲它們是? – tyooo

+0

他們有問題嗎?或者你只是以某種方式整理它? – tyooo

1
int getndim(int month, int year) 
{ 
int numdays;    
     //---whatever your code is 
    if(check condition)  
    { 
     if (month == 2)       
      numdays = 29; 
    } 
    return numdays; 
}