VC++打印日历中某月第一天是星期几的问题 已知1900年1月1日为星期一

出现的问题是 不管输入什么,结果总是weekday=monday 的前一天,即星期日
如果输入,weekday=tuesday, 得出 星期一
小弟苦苦思索,仍然无果, 希望哪位大侠出手相助啊!!!
代码如下:
#include<stdio.h>
#include<iostream>

#define sunday 0
#define monday 1
#define tuesday 2
#define wednesday 3
#define thurday 4
#define friday 5
#define saturday 6

void giveinstructions(void);
int getyearfromuser(void);
int getmonthfromuser(void);
int monthdays(int month, int year);
int firstdayofmonth(int month, int year);
bool isleapyear(int year);

void main(void)
{
int year,month,weekday;
giveinstructions();
year=getyearfromuser();
month=getmonthfromuser();
weekday=firstdayofmonth(year,month);
printf("%d",weekday);
getchar();
}

void giveinstructions(void)
{
printf("This program displays a calendar for a full\n");
printf("year. The year must not be before 1900.\n");
}

int getyearfromuser(void)
{
int year;
while(1)
{
printf("Which year?\n");
scanf("%ld",&year);
getchar();

if(year>=1900)
return(year);
printf("The year must be at least 1900.\n");
}
}
int getmonthfromuser(void)
{
int month;
while(1)
{
printf("Which month?");
scanf("%d",&month);
getchar();
return month;
}
}

int monthdays(int month,int year)
{

switch(month)
{
case 2: if(isleapyear(year))
return (29);
else
return (28);
case 4:
case 6:
case 9:
case 11:
return(30);
default :
return(31);
}
}

int firstdayofmonth(int month, int year)
{
int weekday,i;

weekday=tuesday;
for(i=1900;i<year;i++)
{
weekday=(weekday+365)%7;
if(isleapyear(i))
weekday=(weekday+1)%7;
}
for(i=1;i<=month;i++)
{
weekday=(weekday+monthdays(i,year))%7;
}
return (weekday);
}

bool isleapyear(int year)
{
return(((year%4)==0)&&(year%100==0)||(year%400==0));
}

#include<stdio.h>
#include<iostream>

#define sunday 0
#define monday 1
#define tuesday 2
#define wednesday 3
#define thurday 4
#define friday 5
#define saturday 6

void giveinstructions(void);
int getyearfromuser(void);
int getmonthfromuser(void);
int monthdays(int month, int year);
int firstdayofmonth(int month, int year);
bool isleapyear(int year);

void main(void)
{
int year,month,weekday;
giveinstructions();
year=getyearfromuser();
month=getmonthfromuser();
weekday=firstdayofmonth(month,year);
printf("%d",weekday);
getchar();
}

void giveinstructions(void)
{
printf("This program displays a calendar for a full\n");
printf("year. The year must not be before 1900.\n");
}

int getyearfromuser(void)
{
int year;
while(1)
{
printf("Which year?\n");
scanf("%ld",&year);
getchar();

if(year>=1900)
return(year);
printf("The year must be at least 1900.\n");
}
}
int getmonthfromuser(void)
{
int month;
while(1)
{
printf("Which month?\n");
scanf("%d",&month);
getchar();
return month;
}
}

int monthdays(int month,int year)
{

switch(month)
{
case 1:
return 0;
case 2: if(isleapyear(year))
return (29);
else
return (28);
case 4:
case 6:
case 9:
case 11:
return(30);
default :
return(31);
}
}

int firstdayofmonth(int month, int year)
{
int weekday,i;

weekday=monday;
for(i=1900;i<year;i++)
{
weekday=(weekday+365)%7;
if(isleapyear(i))
weekday=(weekday+1)%7;
}
for(i=1;i<month;i++)
{
weekday=(weekday+monthdays(i,year))%7;
}
return (weekday);
}

bool isleapyear(int year)
{
return(((year%4)==0)&&(year%100==0)||(year%400==0));
}追问

您做了修改了吗!!
还是老样子啊

追答

首先睁大眼睛,其次你把我改过的运行下不就知道了,亲测正常,还有你的编程习惯不好,有很多小错误

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-28
单独用for的话太麻烦了
你还要考虑闰年啊 每月多少天啊 这些都要做判断的
如果不考虑这些问题的话
你输入年月日 year month day
( (year-1900)*365 + (month-1)*31 +day + 1 )%7 看余数就行了 余数是几就是星期几
这是最简单的 但是没有意义

我感觉出这道题的人的用意 应该是让你练习 for循环的多层嵌套
三层循环+判断即可 不用纠结于闰年啊 月份天数不同之类的
第2个回答  2011-09-14
..
相似回答