2016-03-15 71 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; 
}