请指点迷津
关于上次“大家讨论一下”贴、谢谢大家支持和帮助、我试着编写了一个程序、可编译时却出现错误显示不能包含<stdbool.h>文件。现在把原题和源码贴下、请大家指点。原题如下:
Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.
There are few facts you need to know before you can solve this problem:
- January 1, 1900 was on a Monday.
- Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
- Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
- The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please.
PROGRAM NAME: friday
INPUT FORMAT
One line with the integer N.SAMPLE INPUT (file friday.in)
20
OUTPUT FORMAT
Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.SAMPLE OUTPUT (file friday.out)
36 33 34 33 35 35 34 源码如下:
#include <stdio.h>
#include <stdbool.h>
struct date
{
int month,day,year;
};
int main(void)
{
int firstday,N,i,j,Mon,Tue,Wed,Thu,Fri,Sat,Sun;
struct date dateupdate(struct today);
int findday(struct today);
struct date thisday,nextday;
thisday.month=1;
thisday.day=1;
thisday.year=1990;
scanf("%i",&N);
for(i=0; i<N; i++)
{
for(j=1; j<=12; j++)
{
do
{
nextday=dateupdate(thisday);
thisday=nextday;
}while (thisday.day!=13);
int findday(thisday);
}
}
printf("%i%i%i%i%i%i%i",Mon,Tue,Wed,Thu,Fri,Sat,Sun);
rerurn 0;
}
struct date dateupdate(struct today)
{
int numberofdays(struct today);
struct date tomorrow;
if(today.day!=numberofdays(today))
{
tomorrow.day=today.day+1;
tomorrow.month=today.month;
tomorrow.year=today.year;
}
else if (today.month==12)
{
tomorrow.day=1;
tomorrow.month=1;
tomorrow.year=today.year+1;
}
else
{
tomorrow.day=1;
tomorrow.month=today.month+1;
tomorrow.year=today.year;
}
return tomorrow;
}
int numberofdays(struct date d)
{
int days;
bool isLeapYear(struct date d);
const int dayspermonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if (isLeapYear==true&&d.month==2)
days=29;
else
days=28;
}
bool isLeapYear(struct date d)
{
bool LeapYearFlag;
if ((d.year%4 ==0 && d.year%100 !=0 || d.year%400 == 0)
LeapYearFlag=true;
else
LeapYearFlag=false;
return LeapYearflag;
}
int findday(struct today)
{
int findfirstday();
switch((fistday+5)%7)
{
case 1: Mon++;
break;
case 2: Tue++;
break;
case 3: Wed++;
break;
case 4: Thu++;
break;
case 5: Fri++;
break;
case 6: Sat++;
break;
case 7: Sun++;
break;
}
}
int findfirstday
{
if (i=0 && j=1)
firstday=1;
else
firstday=((firstday+5)%7+(days-27))%7;
}