C语言,计算距离世界杯时间

//盗墓者是个丑奴儿,原

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct tm * thisTime=NULL;
struct tm World_Cup={0};

 

//返回当前时间,返回结构体

struct tm * getTime(){
        time_t tloc=time(&tloc);
        thisTime=localtime(&tloc);
        thisTime->tm_year=1900+thisTime->tm_year;
        thisTime->tm_mon=1+thisTime->tm_mon;
        return thisTime;
}

 

//设置世界杯时间的方法,返回结构体

struct tm * setWorld_Cup(int y,int m,int d,int h){
        World_Cup=(struct tm){.tm_year=y,.tm_mon=m,.tm_mday=d,.tm_hour=h};
        return &World_Cup;
}

int main(void){

        //World_Cup : 2019年8月31日

        struct tm *  time=getTime();
        struct tm *  World_Cup=setWorld_Cup(2020,6,31,23);

        int year,mon,day,hour,min,sec;

        year=World_Cup->tm_year-time->tm_year;

        if(year>0){
                if(time->tm_mon-World_Cup->tm_mon==0){
                        if(World_Cup->tm_mday>time->tm_mday){
                                printf("距离世界杯还有整%d年%d天 \n",year,World_Cup->tm_mday-time->tm_mday);
                        }else{
                                printf("世界杯在%d年后的本月举办 \n",year);
                        }
                }else{
                        if(World_Cup->tm_mon>time->tm_mon){
                                printf("a:距离世界杯还有%d年%d月 \n",year,World_Cup->tm_mon-time->tm_mon);
                        }else{
                                printf("b:距离世界杯还有%d年%d月 \n",year-1,(12-time->tm_mon)+World_Cup->tm_mon);
                        }
                }
        }else{
                printf("Date parame ERROR \n");
                return -1;
        }

        return 0;
 

}

 

当前时间:2017/7/3

设置世界杯时间:

打印结果:

 

设置世界杯时间:

打印结果:

 

设置世界杯时间:

打印结果:

 

更多测试...................

 

获得当前时间,手动设置世界杯时间,相减,如果参数正确得到距离世界杯的时间,参数错误,世界杯时间设置有问题,关于距离世界杯还有多少天,多少个小时,再次嵌套2个if判断来做出相应的打印

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值