文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>优化后的生日字典模型

优化后的生日字典模型

时间:2006-12-11  来源:tthacker

优化后的生日字典模型,具体代码和功能请看原代码:    

/*
    Demo for creating Dic of birth V.0.7

    Make a birth dic from s_bir to e_bir,the type of bir as follows:
    
    Type : 1.1986-11-15 2.-11-15 3.1986--15 4.1986-11-
    
    Compile : gcc -o bir_dic bir_dic.c
    
    Usage : bir_dic <options> <s_bir> <e_bir> <dic_name>
    
    Option : -a 198513
              -b 19850103
    
    E.g : ./bir_dic -b 1985-1-3 1986-11-12 dic
    
    Cat dic : 19850103 ... 19861112
    
    By wzt send bug to <[email protected]>
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#define MEMORY 10000 /* change it you want */
#define DEBUG
#define MON 12
#define DAY 31

typedef struct struct_birth{ /* struct of birth */
    int year;
    int mon;
    int day;
}birth;

int fd;
int flag_year = 0;
int flag_len = 0;
long length = 0;

void usage(char *pro)
{
    fprintf(stdout,"usage : %s <options> <s_bir> <e_bir> <dic_name>\n",pro);
    fprintf(stdout,"\noption:\n");
    fprintf(stdout,"-a eg. 198513\n");
    fprintf(stdout,"-b eg. 19850103\n");
    exit(0);
}

birth abstract_bir(char *str)
{
    birth bir;
    char temp_1[10];
    char temp_2[10];
    char temp_3[10];
    char flag_1 = 0,flag_2 = 0,flag_3 = 0;
    int i,j = 0;

    for( i = 0; str[i] != '-'; i++ )
        temp_1[j++] = str[i];
    temp_1[j] = '\0';
    flag_1 = i;

#ifdef DEBUG
    printf("flag_1 : %d, %c temp_1 : %s\n",flag_1,str[flag_1],temp_1);
#endif

    i++;

    if( str[i] == '-' ){
        flag_2 = i;
        i++;
        j = 0;
        for( ; str[i] != '\0'; i++ )
            temp_2[j++] = str[i];
        temp_2[j] = '\0';
    }
    else{
        j = 0;
        for( ; str[i] != '-'; i++ )
            temp_2[j++] = str[i];
        temp_2[j] = '\0';
        flag_2 = i;

    }

#ifdef DEBUG
    printf("flag_2 : %d, %c temp_2 : %s\n",flag_2,str[flag_2],temp_2);
#endif

    if( str[strlen(str) - 1] != '-' && ( flag_1 + 1 != flag_2 ) ){
        i++;
        j = 0;
        flag_3 = 1;
        for( ; str[i] != '\0'; i++ )
            temp_3[j++] = str[i];
        temp_3[j] = '\0';

#ifdef DEBUG
        printf("flag_3 : %d temp_3 : %s\n",flag_3,temp_3);
#endif

    }

    if( flag_1 == 0 ){
        bir.year = 0;
        bir.mon = atoi(temp_2);
        bir.day = atoi(temp_3);
    }
    else{
        bir.year = atoi(temp_1);
        if( flag_1 + 1 == flag_2 ){
            bir.mon = MON;
            bir.day = atoi(temp_2);
        }
        else{
            bir.mon = atoi(temp_2);
            if( flag_3 != 0 )
                bir.day = atoi(temp_3);
            else
                bir.day = DAY;
        }
    }

    return bir;
}

void check_birth(birth s_bir,birth e_bir) /* check correct birth */
{
    if( s_bir.year == e_bir.year )
        flag_year = 1;
        
    if( s_bir.year == e_bir.year && s_bir.mon == e_bir.mon && s_bir.day == e_bir.day ){
        printf("[-] Birth same,Nothing to do.\n");
        exit(1);
    }

    if( s_bir.year > e_bir.year || s_bir.mon > MON || e_bir.mon > MON || s_bir.day > DAY || e_bir.day > DAY ){
        printf("[-] Birth error.Check it out.\n");
        exit(1);
    }
}

void make_optinon(char *temp,char *opt,int i,int j,int k) /* type fo dic , you can add your own type */
{
    if( !strcmp(opt,"-a") ){
        if( i !=0 )
            sprintf(temp,"%d%d%d\n",i,j,k);
        else
            sprintf(temp,"%d%d\n",j,k);
    }
    if( !strcmp(opt,"-b") ){
        if( i !=0 )
            sprintf(temp,"%d%02d%02d\n",i,j,k);
        else
            sprintf(temp,"%02d%02d\n",j,k);
    }
}

char *get_memory() /* malloc memory */
{
    char *buff;

    buff = (char *)malloc(sizeof(char) * MEMORY);
    if( buff == NULL ){
        perror("[-] Malloc.\n");
        exit(1);
    }

    return buff;
}

void make_birth(birth s_bir,birth e_bir,char *opt) /* main of creat dic */
{
    char *dic;
    char temp[20];
    long count = 0;
    int day;
    int i,j,k;

    i = s_bir.year;
    j = s_bir.mon;
    k = s_bir.day;

    dic = get_memory();
    while( i < e_bir.year ){
        while( j <= MON ){
            while( k<= DAY ){
                make_optinon(temp,opt,i,j,k);
                if( count >= MEMORY ){
                    write(fd,dic,count); /* write buffer to file */
                    free(dic);
                    dic = get_memory(); /* new malloc */
                    count = 0;
                }
                memcpy(dic + count,temp,strlen(temp)); /* copy dic to buffer */
                count += strlen(temp);
                k++;
            }
            j++;
            k = 1;
        }
        i++;
    }

    day = DAY;

    if( flag_year == 0 ){
        j =1; k = 1;
    }
    else{
        j = s_bir.mon;
        k = s_bir.day;
    }
    
    while( j <= e_bir.mon){
        if( j == e_bir.mon ){
            day = e_bir.day;
        }
        while( k<= day ){
            make_optinon(temp,opt,i,j,k);
            if( count >= MEMORY ){
                write(fd,dic,count);
                free(dic);
                dic = get_memory();
                count = 0;
            }
            memcpy(dic + count,temp,strlen(temp));
            count += strlen(temp);
            k++;
        }
        j++;
        k = 1;
    }

    dic[count] = '\0';

    write(fd,dic,count); /* write buffer to file */
}

void check_type(char *type) /* check type if is correct */
{
    if( strcmp(type,"-a") || strcmp(type,"-b") ){
        printf("[-] Type Error.\n");
        exit(1);
    }
}

int main(int argc,char **argv)
{
    birth s_birth,e_birth;

    if( argc == 1 )
        usage(argv[0]);

    s_birth = abstract_bir(argv[2]);
    e_birth = abstract_bir(argv[3]);

    check_birth(s_birth,e_birth);

    check_type(argv[1]);
    
    if(( fd = creat(argv[4],0777) ) < -1 ){
        printf("[-] Create dic failed.\n");
        exit(1);
    }

    printf("[+] Create dic ok.\n");
    make_birth(s_birth,e_birth,argv[1]);

    close(fd);
    printf("[+] Done.\n");

    return 0;
}

相关阅读 更多 +
排行榜 更多 +
修狗突围

修狗突围

飞行射击 下载
末日漂移生存

末日漂移生存

休闲益智 下载
水排序谜题吧手机版

水排序谜题吧手机版

休闲益智 下载