• 阳历阴历转换


    获取阳历对应的阴历日期:

    /////////////////////////////////////////////////////////////////////////////////////////////////////
    stdcal.h:

    #ifndef __STDCAL_H
    #define __STDCAL_H

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

    #define SPLIT "/"
    #define LEN_DATE 11

    #define START_YEAR 2017
    #define END_YEAR 2020

    void intostr(char *,int,int);
    int intlen(int);

    extern unsigned char const cMonInfo[12];

    int sumDay(char *date);
    void chinaCal(char *year,int sumday,int *);
    void getChinaCal(char *,int);

    #endif

    /////////////////////////////////////////////////////////////////////////////////////////////////////
    mystd.c:
    #include "stdcal.h"


    void intobit(int num)
    {
    int tnum=num;
    int i;
    for(i=31;i>=0;i--)
    {
    int tmpa=tnum;
    tmpa=tnum&(1<<i);
    ((i+1)%8==0&&i!=31)?printf(" %d",tmpa>>i):printf("%d",tmpa>>i);
    }
    printf(" ");
    }

    void chrtobit(char chr)
    {
    char tchr=chr;
    int i;
    for(i=7;i>=0;i--)
    {
    char tmpc=tchr;
    tmpc=tchr&(1<<i);
    printf("%d",tmpc>>i);
    }
    printf(" ");
    }

    void strtobit(char *chr)
    {
    char *pchr=chr;
    int i,j;
    for(i=0;i<strlen(pchr);i++)
    {
    for(j=7;j>=0;j--)
    {
    char tmpp=pchr[i];
    tmpp=pchr[i]&(1<<j);
    printf("%d",tmpp>>j);
    }
    printf(" ");
    }
    printf(" ");
    }

    int intlen(int num)
    {
    int tmpn=num;
    int len=1;
    while(tmpn/=10)
    len++;
    return len;
    }

    void intostr(char *dest,int num,int intlen)
    {
    int tmpn2=num;
    char tmpc[intlen+1];
    int i=intlen-1;
    tmpc[intlen]='';
    while(tmpn2)
    {
    tmpc[i]=48+(tmpn2%10);
    if(!(tmpn2/=10))
    tmpc[i-1]=40+tmpn2;
    i--;
    }
    memcpy(dest,tmpc,sizeof(tmpc));
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////
    stdcal.c:

    #include "stdcal.h"

    unsigned char const cMonInfo[12]={
    0x38,0xca,0x2f,
    0x5e,0x0a,0x56,
    0x48,0x15,0x26,
    0x32,0x8F,0x2A
    };

    int sumDay(char *date)
    {
    char *indate[3];
    struct tm tmptm;
    char tmpdate[LEN_DATE];
    memcpy(tmpdate,date,strlen(date)+1);
    //printf("%s ",date);

    int i=0;
    indate[i]=strtok(tmpdate,SPLIT);
    while(++i<3)
    indate[i]=strtok(NULL,SPLIT);

    //printf("%s ",indate[2]);
    tmptm.tm_year=atoi(indate[0])-1900;
    tmptm.tm_mon=atoi(indate[1])-1;
    tmptm.tm_mday=atoi(indate[2]);
    tmptm.tm_hour=0;
    tmptm.tm_min=0;
    tmptm.tm_sec=0;
    tmptm.tm_isdst=-1;
    //printf("%s ",indate[2]);

    if(mktime(&tmptm)==-1)
    perror("mktime error ");

    //printf("%d ",tmptm.tm_yday);

    return (tmptm.tm_yday+1);
    }

    void chinaCal(char *year,int sumday,int *cal)
    {
    int china[2];
    china[0]=1;china[1]=1;
    char lastyear[11];
    int startnum_Mon=(atoi(year)-START_YEAR)*3;
    //printf("%d ",startnum_Mon);
    int diffday=(int)(sumday-(cMonInfo[startnum_Mon]>>1));
    //printf("sumday:%d ",sumday);
    //printf("(cMonInfo[startnum_Mon]>>1):%d ",(cMonInfo[startnum_Mon]>>1));
    //printf("%d ",diffday);

    if(diffday<0)
    {
    intostr(lastyear,atoi(year)-1,intlen(atoi(year)-1));
    int laststartnum_Mon=(atoi(lastyear)-START_YEAR)*3;
    int newdiffday=sumday;

    int lastmonDay[13];
    lastmonDay[0]=(((cMonInfo[laststartnum_Mon+1]>>4)&0001)==0)?29:30;
    lastmonDay[1]=(((cMonInfo[laststartnum_Mon+1]>>3)&00001)==0)?29:30;
    lastmonDay[2]=(((cMonInfo[laststartnum_Mon+1]>>2)&000001)==0)?29:30;
    lastmonDay[3]=(((cMonInfo[laststartnum_Mon+1]>>1)&0000001)==0)?29:30;
    lastmonDay[4]=(((cMonInfo[laststartnum_Mon+1])&00000001)==0)?29:30;
    lastmonDay[5]=(((cMonInfo[laststartnum_Mon+2]>>7))==0)?29:30;
    lastmonDay[6]=(((cMonInfo[laststartnum_Mon+2]>>6)&01)==0)?29:30;
    lastmonDay[7]=(((cMonInfo[laststartnum_Mon+2]>>5)&001)==0)?29:30;
    lastmonDay[8]=(((cMonInfo[laststartnum_Mon+2]>>4)&0001)==0)?29:30;
    lastmonDay[9]=(((cMonInfo[laststartnum_Mon+2]>>3)&00001)==0)?29:30;
    lastmonDay[10]=(((cMonInfo[laststartnum_Mon+2]>>2)&000001)==0)?29:30;
    lastmonDay[11]=(((cMonInfo[laststartnum_Mon+2]>>1)&0000001)==0)?29:30;
    lastmonDay[12]=(((cMonInfo[laststartnum_Mon+2])&00000001)==0)?29:30;

    lastyear[4]='/';lastyear[5]='1';lastyear[6]='2';lastyear[7]='/';lastyear
    [8]='3';lastyear[9]='1';lastyear[10]='';
    chinaCal(lastyear,sumDay(&lastyear[0]),china);

    if(newdiffday>(lastmonDay[china[0]-1]-china[1]))
    {
    newdiffday-=lastmonDay[china[0]-1]-china[1];
    ++china[0];china[1]=0;
    }
    //printf("%d/%d ",china[0],china[1]);
    //printf("%d ",newdiffday);
    while(newdiffday>0)
    {
    if(newdiffday>=lastmonDay[china[0]])
    {
    newdiffday-=lastmonDay[china[0]];
    china[0]++;
    }else{
    china[1]+=newdiffday;
    newdiffday=0;
    }
    }

    //memcpy(cal,china,sizeof(china));
    printf("%d/%d ",china[0],china[1]);

    exit(0);

    }

    int isleap=(int)((cMonInfo[startnum_Mon+1])>>5);
    //printf("%d ",diffday);
    //printf("isleap:%d ",isleap);
    int monDay[13];
    monDay[0]=(((cMonInfo[startnum_Mon+1]>>4)&0001)==0)?29:30;
    monDay[1]=(((cMonInfo[startnum_Mon+1]>>3)&00001)==0)?29:30;
    monDay[2]=(((cMonInfo[startnum_Mon+1]>>2)&000001)==0)?29:30;
    monDay[3]=(((cMonInfo[startnum_Mon+1]>>1)&0000001)==0)?29:30;
    monDay[4]=(((cMonInfo[startnum_Mon+1])&00000001)==0)?29:30;
    monDay[5]=(((cMonInfo[startnum_Mon+2]>>7))==0)?29:30;
    monDay[6]=(((cMonInfo[startnum_Mon+2]>>6)&01)==0)?29:30;
    monDay[7]=(((cMonInfo[startnum_Mon+2]>>5)&001)==0)?29:30;
    monDay[8]=(((cMonInfo[startnum_Mon+2]>>4)&0001)==0)?29:30;
    monDay[9]=(((cMonInfo[startnum_Mon+2]>>3)&00001)==0)?29:30;
    monDay[10]=(((cMonInfo[startnum_Mon+2]>>2)&000001)==0)?29:30;
    monDay[11]=(((cMonInfo[startnum_Mon+2]>>1)&0000001)==0)?29:30;
    monDay[12]=(((cMonInfo[startnum_Mon+2])&00000001)==0)?29:30;
    //printf("monDay:%d ",monDay[8]);

    int i=0,isfin=1;
    //printf("diffday:%d ",diffday);
    while(diffday>0)
    {
    //printf("diffday:%d ",diffday);
    //printf("now chinaYear:%d ",chinaYear);
    //printf("now chinaMon:%d ",chinaMon);
    if(isleap==i&&isfin==1)
    {
    //printf("leap_monDay[%d]:%d ",i,monDay[12]);
    if(diffday>=monDay[12])
    {
    isfin=0;
    diffday-=monDay[12];
    ++china[0];
    continue;
    }else{
    china[1]+=diffday;
    diffday=0;
    break;
    }
    }
    if(diffday>=monDay[i])
    {
    if(isleap==i+1)
    {
    //printf("monDay[%d]:%d ",i,monDay[i]);
    diffday-=monDay[i];
    }else{
    ++china[0];
    //printf("monDay[%d]:%d ",i+1,monDay[i]);
    diffday-=monDay[i];
    }
    }else if(diffday<monDay[i]){
    china[1]+=diffday;
    diffday=0;
    break;
    }
    ++i;
    }

    //printf("%d/%d ",chinaYear,chinaMon);
    //china[0]=chinaYear;
    //china[1]=chinaMon;

    //return china;
    memcpy(cal,china,sizeof(china));
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////
    execal.c:
    #include "stdcal.h"

    int main(int argc,char *argv[])
    {
    char *indate[3];
    int china[2];
    char tmpdate[LEN_DATE];
    memcpy(tmpdate,argv[1],strlen(argv[1])+1);

    int i=0;
    indate[i]=strtok(tmpdate,SPLIT);
    while(++i<3)
    indate[i]=strtok(NULL,SPLIT);

    chinaCal(indate[0],sumDay(argv[1]),china);
    //getChinaCal(indate[0],sumDay(argv[1]));
    printf("%d/%d ",china[0],china[1]);

    return 0;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////

    makefile:
    obj=execal.o stdcal.o mystd.o
    cal:$(obj)
    cc -o cal $(obj)
    execal.o:execal.c stdcal.h
    cc -c execal.c
    stdcal.o:stdcal.c stdcal.h
    cc -c stdcal.c
    mystd.o:mystd.c stdcal.h
    cc -c mystd.c
    clean:
    rm $(obj)

    /////////////////////////////////////////////////////////////////////////////////////////////////////

  • 相关阅读:
    python运行错误---TabError: Inconsistent use of tabs and spaces in indentation
    python运行错误------Non-UTF-8 code
    opencv错误(Unhandled expection at at 0x0007EEE...)
    fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突——我的解决方案
    基础术语
    opencv
    图像归一化
    人脸相关数据库
    堆排序
    abp学习(二)
  • 原文地址:https://www.cnblogs.com/Mr-Wenyan/p/7532989.html
Copyright © 2020-2023  润新知