出现的问题是 不管输入什么,结果总是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));
}
您做了修改了吗!!
还是老样子啊
首先睁大眼睛,其次你把我改过的运行下不就知道了,亲测正常,还有你的编程习惯不好,有很多小错误