新年第六天

来源:互联网 发布:数组是什么数据类型php 编辑:程序博客网 时间:2024/05/19 13:08

//

//  main.cpp

//  根据键盘输入的身份证号计算相应的年龄,并显示系统地当前日期与时间。

//

//  Created by 张良 on 2016/11/30.

//  Copyright © 2016张良. All rights reserved.

//


#include <iostream>

#include<time.h>

#include<string.h>

class Date

{

    int year,month,day;

public:

    Date(tm *t)

    {

        year=t->tm_year+1900;

        month=t->tm_mon+1;

        day=t->tm_mday;

    }

    int get_year(){return year;}

    int get_month(){return month;}

    int get_day(){return day;}

    void display()

    {

        std::cout<<year<<""<<month<<""<<day<<""<<std::endl;

    }

};

class Time

{

    int hour,minute,second;

public:

    Time(tm *t)

    {

        hour=t->tm_hour+1900;

        minute=t->tm_min+1;

        second=t->tm_sec;

    }


void display()

{

    std::cout<<hour<<":"<<minute<<":"<<second<<std::endl;

}

};

class ID

{

    char id[20];

    int year,month,day;

    Date d1;

    Time t1;

public:

    ID(char *id,tm *t):d1(t),t1(t)

    {

        strcpy(this->id,id);

        fun();

    }

    int val(char *p,int i,int j){

        int n=0,k=i+j;

        while(i<k){

        n=n*10+*(p+i)-'0';

            i++;

        }

        return(n);

    }

};

void fun()

{

    year=va1(id,6,4);

    month=va1(id,10,2);

    day=va1(id,12,2);

}

    int age()

    {

        return(d1.get_year()-year);

    }

    void display()

    {

        std::cout<<"当前日期";

        d1.display();

        std::cout<<"当前时间"

        t1.display();

        std::cout<<"身份证号:"<<id<<std::endl;

        std::<<"年龄:"<<age()<<stde::ndl;

        std::cout<<year<<std::endl;

    }

};

int main()

{

    struct tm *pt;

    time_t timer;

    timer=time(NULLL);

    pt=localtime(&timer);

    char s[20];

    std::cout<<"请输入身份证号:";

    std::cin>>s;

    ID id1(s,pt);

    id1.display();

}


0 0
原创粉丝点击