魔镜魔镜,明天几号

1、简单判断第二天的日期

#include<stdio.h>


int main(void) {
  
struct date
{
    int month;
    int day; int year;
};
    struct date today, tomorrow;
    const int daypermonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    printf("enter today s date (mm dd yyyy):");
    scanf_s("%i%i%i", &today.month, &today.day, &today.year);
    if (today.day != daypermonth[today.month -1]) {//如果不是月份最后一天,直接天数加1
        tomorrow.day = today.day + 1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
 }
    else if (today.month == 12) {//是天数为月份的最后一天的情况下
        tomorrow.month = 1;
        tomorrow.day = 1;
        tomorrow.year = today.year + 1;
    }
    else {//月份最后一天
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }
    printf("tomorrow 's date is %i%i%.2i.\n", tomorrow.month, tomorrow.day, tomorrow.year % 100);
    return 0;
}

2、判断第二天的日期,多考虑了闰年的情况,在这个程序中,结构体函数放到了主函数的外面,如果像第一种情况,放在主函数里面,则程序无法将结构体当成参数传递,因为如果结构体放在某个函数内部,则只有该函数才可以调用,此时,结构体的作用域是局部的,如果结构体放在所有函数的外面,则结构体的作用域就是全局的,在整个源文件中都可以使用该结构的定义,这一点上看,参数的全局与局部定义与之非常相像。

#include<stdio.h>
#include<stdbool.h>
struct date//结构体要当成参数传递到自定义函数中时,需要放在主函数外面,当成全局变量。
    int month;
    int day; int year;
};
int main(void) {
  

    struct date today, tomorrow;
    int numberofdays(struct date d);
   
    printf("enter today s date (mm dd yyyy):");
    scanf_s("%i%i%i", &today.month, &today.day, &today.year);
    if (today.day != numberofdays(today)) {//如果不是月份最后一天,直接天数加1
        tomorrow.day = today.day + 1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
 }
    else if (today.month == 12) {//是天数为月份的最后一天的情况下
        tomorrow.month = 1;
        tomorrow.day = 1;
        tomorrow.year = today.year + 1;
    }
    else {//月份最后一天
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }
    printf("tomorrow 's date is %i%i%.2i.\n", tomorrow.month, tomorrow.day, tomorrow.year % 100);
    return 0;
}
int numberofdays(struct date d) {
    int days;
    bool isleapyear(struct date d);
    const int daypermonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    if (isleapyear(d) == true && d.month == 2)
        days = 29;
    else
        days = daypermonth[d.month - 1];
    return days;
}
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;
}

3、判断日期用另一个结构体表示

#include<stdio.h>
#include<stdbool.h>
struct date//结构体要当成参数传递到自定义函数中时,需要放在主函数外面。
    int month;
    int day; int year;
};
struct date dateupdate(struct date today) {
    struct date  tomorrow; int numberofdays(struct date d);
    if (today.day != numberofdays(today)) {//如果不是月份最后一天,直接天数加1
        tomorrow.day = today.day + 1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
    }
    else if (today.month == 12) {//是天数为月份的最后一天的情况下
        tomorrow.month = 1;
        tomorrow.day = 1;
        tomorrow.year = today.year + 1;
    }
    else {//月份最后一天
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }

    return tomorrow;
}
int main(void) {
    struct date dateupdate(struct date today);
    struct date thisday, nextday;    
    printf("enter today s date (mm dd yyyy):");
    scanf_s("%i%i%i", &thisday.month, &thisday.day, &thisday.year);
    nextday = dateupdate(thisday);
    printf("tomorrow 's date is %i%i%.2i.\n", nextday.month, nextday.day, nextday.year % 100);
    return 0;
}
int numberofdays(struct date d) {
    int days;
    bool isleapyear(struct date d);
    const int daypermonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    if (isleapyear(d) == true && d.month == 2)
        days = 29;
    else
        days = daypermonth[d.month - 1];
    return days;
}
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;
}

代码取自《c语言编程》一书

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值