提取计算机当前时间、日期

今日内容:

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

/*根据键盘输入的身份证号计算相应的年龄,并显示系统的当前日期和时间*/
#include<iostream.h>
#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() {cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}
};
class Time//当前时间类
{
	int hour,minute,second;
public:
	Time(tm *t)
	{
		hour = t->tm_hour;
		minute = t->tm_min;
		second = t->tm_sec;
	}
	void display() {cout<<hour<<":"<<minute<<":"<<second<<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)//将字符串中从第i个字符开始的j个连续数字字符转换成一个整数
		//该函数用于身份证号中提取出生日期
	{
		int n = 0,k = i+j;
		while(i < k)
		{
			n = n*10+*(p+i)-'0';//!!!
			i++;
		}
		return(n);
	}
	void fun()
	{
		year = val(id,6,4);
		month =  val(id,10,2);
		day = val(id,12,2);
	}
	int age()
	{
		return (d1.get_year()-year);//!!!
	}
	void display()
	{
		cout<<"当前日期";
		d1.display();
		cout<<"当前时间:";
		t1.display();
		cout<<"身份证号:"<<id<<endl;
		cout<<"年龄:"<<age()<<endl;
		cout<<year<<endl;
	}
};
void main()
{
	//以下四行用于获取当前时间,并存入指针pt所指的tm类型的结构体变量中
	struct tm *pt;
	time_t timer;
	timer = time(NULL);
	pt = localtime(&timer);
	char s[20];
	cout<<"请输入身份证号:";
	cin>>s;
	ID id1(s,pt);
	id1.display();
}


2. 用c++写贪吃蛇的小程序(8%)

背景音乐死活弄不出来,艹 !

明日计划:

1. 英语复习备考

 2. 数电、电路作业

3. 贪吃蛇完成到 30%

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值