- typedef int INTEGER;
- typedef int NUM[4];
- struct DATE{
- int year;
- int month;
- int day;
- };
- main()
- {
- INTEGER num;
- num = 1999;
- printf("%d/n",num);
- NUM scores;
- scores[0] = 98;
- scores[1] = 89;
- scores[2] = 95;
- scores[3] = 88;
- INTEGER i = 0;
- for(i = 0; i < 4; i++)
- {
- printf("%d/n",scores[i]);
- }
- struct DATE day1;
- day1.year = 2008;
- day1.month = 9;
- day1.day = 9;
- printf("%d is leap?%d/n",day1.year,isleap(day1.year));
- printf("1999-9-9 is the %d of the year!/n",days(&day1));
- }
- int days(struct DATE *p)
- {
- int leap = isleap(p->year);
- int tdays = 0;
- if(leap == 1)//闰年
- {
- if(p->month < 3)
- {
- tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) + p->day;
- }
- else if( p->month >= 3 && p->month <=12)
- {
- tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) - 1 + p->day;
- }
- }
- else
- {
- if(p->month < 3)
- {
- tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) + p->day;
- }
- else if( p->month >= 3 && p->month <=12)
- {
- tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) - 2 + p->day;
- }
- }
- return tdays;
- }
- int isleap(int year)
- {
- int leap = 0;
- if(year%4==0)
- {
- if(year%100==0)
- {
- if(year%400==0)
- {
- leap = 1;
- }
- }
- else
- {
- leap = 1;
- }
- }
- return leap;
- }
这是谭老师的书《C程序设计第二版》中第11章(page296)第1、2道习题。 我参考前面的例题做出来的答案,不知道对错。