在C中以日期形式组合4个单独的整数

时间:2012-08-20 21:42:24

标签: c

我有一个程序读取EMSO(一个数字,告诉你出生日期,状态,性别和其他一些东西)。它是生成的,它是一个13位数字。最后一个是控制,前两个是日,第二个是月,接下来的三个是年。我希望该计划通过EMSO和当前计算机日期计算,告诉您您的年龄。这是我到目前为止所写的内容:

#include <stdio.h>
#include <windows.h>

int main (void) 
{
unsigned long long int  emso;
int kontrola;
int a,b,c,d,e,f,g,h,i,j,k,l;
int x,y;
int kraj;

system("cls");  

printf("Enter your EMSO: ");
while(scanf("%lld",&emso)!=1)fflush(stdin);



    while(emso!=-1){

    kontrola=emso%10;
    emso/=10;
    l=emso%10;
    emso/=10;
    k=emso%10;
    emso/=10;
    j=emso%10;
    emso/=10;
    i=emso%10;
    emso/=10;
    h=emso%10;
    emso/=10;
    g=emso%10;
    emso/=10;
    f=emso%10;
    emso/=10;
    e=emso%10;
    emso/=10;
    d=emso%10;
    emso/=10;
    c=emso%10;
    emso/=10;
    b=emso%10;
    emso/=10;
    a=emso%10;

    x= (7*a+ 6*b+ 5*c+ 4*d+ 3*e+ 2*f+ 7*g+ 6*h+ 5*i+ 4*j+ 3*k+ 2*l);
    x=x%11;

    if (x!=0){
        x=11-x;     
    }


    kraj=h*10+i;

    if (x==kontrola){
        printf("\n\nEMSO is legitimate!\n");
        if(j==0){
            printf("\nPerson is MALE\n");
        }
            else if (j==5){
                printf("\nPerson in FEMALE\n");
            }


        if (e==9){
            y=1;
        }

        else if (e==0){
            y=2;
        }

        printf("\nDate of birth: %d%d.%d    %d.%d%d%d%d.\n",a,b,c,d,y,e,f,g);   

        if (kraj >=00 && kraj <10){
            printf("\nPerson was born abroad!\n");
        }

        if (kraj>=10 && kraj < 20){
            printf("\nPerson was born in BiH.\n");
        }

        if(kraj >=20 && kraj < 30){
            printf("\nPerson was born in Montenegro.\n");
        }

        if (kraj >= 30 && kraj < 40){
            printf("\nPerson was born in Croatia.\n");
            if (kraj == 33){
                printf("\tPerson was born in Zagreb.\n");
            }
        }

        if (kraj >=40 && kraj < 50){
            printf("\nPerson was born in Macedoniji.\n");
        }

        if (kraj >= 50 && kraj < 60){
            printf("\nPerson was born in Slovenia.\n");
        }

        if (kraj >=70 && kraj < 80){
            printf("\nPerson was born in Srbia.\n");
            if (kraj == 71){
                printf("\tPerson was born in Beogerad.\n");
                }
        }

        if (kraj >=80 && kraj < 90){
            printf("\nPerson was born in Vojvodina.\n");
            if(kraj == 80){
                printf("\tPerson was born in Novi Sad.\n");
                }
        }

        if (kraj >=90 && kraj < 100){
            printf("\nPerson was born in Kosovo.\n");
        }
    }

    if (x!=kontrola){
        printf("\nEMSO is NOT legitimate!\n");
    }

    printf("\n");
    system("pause");
    system("cls");  


    printf("Enter your EMSO: ");
    while(scanf("%lld",&emso)!=1)fflush(stdin);
}


return 0;

}

在所有这些混乱中,a&amp; b表示日期,cd表示月份,efg表示年份。

此外,fflush事情无法正常工作。它摧毁了生日印记。

为了更好地理解,这里是我的emso:0701996500037。这是whar在正常的一天出来: EMSO是合法的!

人是男性

出生日期:1996年1月7日。

人出生在斯洛文尼亚。

我很高兴。如果我写这个:0701996500037ž,没问题,输出相同。 但是如果我输入z0701996500037,则没有任何回复,就像输入任何内容一样。如果我写0701u996500037,验证是可以的,但所有其他都是错误的。地方和出生都是错误的

2 个答案:

答案 0 :(得分:0)

time返回自Epoch以来的秒数。 mktime返回自Epoch以来struct tm *参数的秒数。因此:

struct tm date = { /* fill tm_year, tm_mon, etc. */ };
double seconds_since_date = difftime(time(0), mktime(&date));

会给你自日期以来传递的秒数。 (这不考虑时区。)

答案 1 :(得分:-1)

请查看下面的链接,可能这可以帮到你

http://www.cplusplus.com/reference/clibrary/ctime/time/