题目:编写处理一下日期问题的函数:给定两个日期,计算两者之间的天数;给定一个日期,返回值为周几;给定月和年,使用字符数组生成该月的日历。
「分析」
1. 给定两个日子,计算两个日子之间的天数
解决思路:
①计算该日子是该年当中的第几天;
②闰年的处理;
③两个日子的年份之间经过几个闰年。

2. 给定某个日子,返回它在一周中属于第几天
解决思路:
①给出一个是星期一的具体日子;
②(计算当前日子-指定日子)相差的天数%7 + 1

3. 给定某个某年某月,打印这一月的日历
解决思路:
①某年某月的第一天是星期几;
②打印的格式
《编程珠玑》习题3.7.3_职场
《编程珠玑》习题3.7.3_编程_02#include <stdio.h>
《编程珠玑》习题3.7.3_编程_02#include <assert.h>
《编程珠玑》习题3.7.3_编程_02//日期用结构体表示
《编程珠玑》习题3.7.3_编程_02typedef struct date{
《编程珠玑》习题3.7.3_编程_02        int year;
《编程珠玑》习题3.7.3_编程_02        int month;
《编程珠玑》习题3.7.3_编程_02        int day;
《编程珠玑》习题3.7.3_编程_02}Date;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02// 闰年:1月到12月每个月的天数 31,29,31,30,31,30,31,31,30,31,30,31
《编程珠玑》习题3.7.3_编程_02//非闰年:1月到12月每个月的天数 31,28,31,30,31,30,31,31,30,31,30,31
《编程珠玑》习题3.7.3_编程_02//daysMonth存的是非闰年当前月之前的总天数
《编程珠玑》习题3.7.3_编程_02int daysMonth[13] = { 0, 0 , 31, 59, 90, 120, 151,
《编程珠玑》习题3.7.3_编程_02                                         181, 212, 243, 273, 304, 334};
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02//判断是否是闰年,闰年返回1
《编程珠玑》习题3.7.3_编程_02int isLeap(const int year)
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        return ( (year % 4 == 0) && (year % 100 != 0) ||
《编程珠玑》习题3.7.3_编程_02                         (year % 400 == 0) );
《编程珠玑》习题3.7.3_编程_02}
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02//当前日期是一年当中的第几天
《编程珠玑》习题3.7.3_编程_02int days(const Date * currDate)
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        int sum = daysMonth[currDate->month] + currDate->day;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        if ( isLeap(currDate->year) && (currDate->month >= 3) ){
《编程珠玑》习题3.7.3_编程_02                sum++;
《编程珠玑》习题3.7.3_编程_02        }
《编程珠玑》习题3.7.3_编程_02        return sum;
《编程珠玑》习题3.7.3_编程_02}
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02//交换两个日期
《编程珠玑》习题3.7.3_编程_02void swapDate(Date *date1, Date *date2)
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        int tmp;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        tmp = date1->year;
《编程珠玑》习题3.7.3_编程_02        date1->year = date2->year;
《编程珠玑》习题3.7.3_编程_02        date2->year = tmp;
《编程珠玑》习题3.7.3_编程_02        
《编程珠玑》习题3.7.3_编程_02        tmp = date1->month;
《编程珠玑》习题3.7.3_编程_02        date1->month = date2->month;
《编程珠玑》习题3.7.3_编程_02        date2->month = tmp;
《编程珠玑》习题3.7.3_编程_02        
《编程珠玑》习题3.7.3_编程_02        tmp = date1->day;
《编程珠玑》习题3.7.3_编程_02        date1->day = date2->day;
《编程珠玑》习题3.7.3_编程_02        date2->day = tmp;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02}
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02//两个日期间相差的天数,
《编程珠玑》习题3.7.3_编程_02int getIntervalTime(Date *date1, Date *date2)
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        int days1, days2, sumDays;
《编程珠玑》习题3.7.3_编程_02        int i;
《编程珠玑》习题3.7.3_编程_02        int years;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        //保证后面的比前面的大
《编程珠玑》习题3.7.3_编程_02        if(date1->year > date2->year){
《编程珠玑》习题3.7.3_编程_02                swapDate(date1, date2);
《编程珠玑》习题3.7.3_编程_02        }
《编程珠玑》习题3.7.3_编程_02        years = date2->year - date1->year;
《编程珠玑》习题3.7.3_编程_02        
《编程珠玑》习题3.7.3_编程_02        //当前日期是一年当中的第几天
《编程珠玑》习题3.7.3_编程_02        days1 = days(date1);
《编程珠玑》习题3.7.3_编程_02        days2 = days(date2);
《编程珠玑》习题3.7.3_编程_02        sumDays = days2 - days1;
《编程珠玑》习题3.7.3_编程_02        
《编程珠玑》习题3.7.3_编程_02        //两者相差一年
《编程珠玑》习题3.7.3_编程_02        if( years == 1){
《编程珠玑》习题3.7.3_编程_02                sumDays += 365;
《编程珠玑》习题3.7.3_编程_02        }else if(years > 1){//两个日子的年份之间经过几个闰年
《编程珠玑》习题3.7.3_编程_02                for(i = date1->year; i < date2->year; i++){
《编程珠玑》习题3.7.3_编程_02                        sumDays += 365;
《编程珠玑》习题3.7.3_编程_02                        if(isLeap(i)){
《编程珠玑》习题3.7.3_编程_02                                sumDays++;
《编程珠玑》习题3.7.3_编程_02                        }
《编程珠玑》习题3.7.3_编程_02                }
《编程珠玑》习题3.7.3_编程_02        }
《编程珠玑》习题3.7.3_编程_02        return sumDays;
《编程珠玑》习题3.7.3_编程_02}
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02//这一天是星期几,解决[给定某个日子,返回它在一周中属于第几天]
《编程珠玑》习题3.7.3_编程_02int whichDay(Date *date)
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        Date orgDate;
《编程珠玑》习题3.7.3_编程_02        int sum;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        //1980年1月7日是星期一
《编程珠玑》习题3.7.3_编程_02        orgDate.year = 1980;
《编程珠玑》习题3.7.3_编程_02        orgDate.month = 1;
《编程珠玑》习题3.7.3_编程_02        orgDate.day = 7;
《编程珠玑》习题3.7.3_编程_02        sum = getIntervalTime(date, &orgDate);
《编程珠玑》习题3.7.3_编程_02        sum = (sum % 7 + 1);
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        return sum;
《编程珠玑》习题3.7.3_编程_02}
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02//打印该月的日历,解决[给定某个某年某月,打印这一月的日历]
《编程珠玑》习题3.7.3_编程_02void printCalenda(Date *date)
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        int today;
《编程珠玑》习题3.7.3_编程_02        int i;
《编程珠玑》习题3.7.3_编程_02        int days;
《编程珠玑》习题3.7.3_编程_02        int cnt;
《编程珠玑》习题3.7.3_编程_02        int m[] = {31,28,31,30,31,30,31,31,30,31,30,31};
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        days = m[date->month - 1];
《编程珠玑》习题3.7.3_编程_02        if(isLeap(date->year) && date->month >= 2){
《编程珠玑》习题3.7.3_编程_02                days++;
《编程珠玑》习题3.7.3_编程_02        }
《编程珠玑》习题3.7.3_编程_02        
《编程珠玑》习题3.7.3_编程_02        assert(date->day == 1);
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        cnt = today = whichDay(date);
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        //开始打印
《编程珠玑》习题3.7.3_编程_02        printf(" 日 一 二 三 四 五 六\n");
《编程珠玑》习题3.7.3_编程_02        for(i = 0; i < today; i++){ //这个月的第一天是星期几,这天之前打印空格
《编程珠玑》习题3.7.3_编程_02                printf("     ");//三个空格,因为一个汉字占两个字符的宽度
《编程珠玑》习题3.7.3_编程_02        }
《编程珠玑》习题3.7.3_编程_02        for(i = 1; i < days; i++){
《编程珠玑》习题3.7.3_编程_02                printf(" %2d", i);//从这个月的第一天开始打印
《编程珠玑》习题3.7.3_编程_02                cnt++; //从星期日为这个星期的第一天所以先++
《编程珠玑》习题3.7.3_编程_02                if(cnt %7 == 0){
《编程珠玑》习题3.7.3_编程_02                        printf("\n");
《编程珠玑》习题3.7.3_编程_02                }
《编程珠玑》习题3.7.3_编程_02        }
《编程珠玑》习题3.7.3_编程_02        printf("\n");
《编程珠玑》习题3.7.3_编程_02}
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02int main()
《编程珠玑》习题3.7.3_编程_02{
《编程珠玑》习题3.7.3_编程_02        Date date1, date2;
《编程珠玑》习题3.7.3_编程_02        int year, month, day;
《编程珠玑》习题3.7.3_编程_02#if 1
《编程珠玑》习题3.7.3_编程_02        printf("请输入第一个日期,格式是年 月 日(2004 8 30):");
《编程珠玑》习题3.7.3_编程_02        scanf("%d %d %d", &year, &month, &day);
《编程珠玑》习题3.7.3_编程_02        date1.year = year;
《编程珠玑》习题3.7.3_编程_02        date1.month = month;
《编程珠玑》习题3.7.3_编程_02        date1.day = day;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        printf("\n请输入第二个日期,格式是年 月 日(2004 8 30):");
《编程珠玑》习题3.7.3_编程_02        scanf("%d %d %d", &year, &month, &day);
《编程珠玑》习题3.7.3_编程_02        date2.year = year;
《编程珠玑》习题3.7.3_编程_02        date2.month = month;
《编程珠玑》习题3.7.3_编程_02        date2.day = day;
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        printf("两个日期之间相差%d天\n", getIntervalTime(&date1, &date2));
《编程珠玑》习题3.7.3_编程_02#else
《编程珠玑》习题3.7.3_编程_02        date2.year = 2010;
《编程珠玑》习题3.7.3_编程_02        date2.month = 1;
《编程珠玑》习题3.7.3_编程_02        date2.day = 1;
《编程珠玑》习题3.7.3_编程_02        //whichDay(&date2);
《编程珠玑》习题3.7.3_编程_02        printCalenda(&date2);
《编程珠玑》习题3.7.3_编程_02#endif
《编程珠玑》习题3.7.3_编程_02
《编程珠玑》习题3.7.3_编程_02        return 0;
《编程珠玑》习题3.7.3_编程_02        
《编程珠玑》习题3.7.3_编程_02}