哈喽
今天一打开洛谷,发现
啊这
算了,编程放松一下(放松?)
c++作为一个平时玩耍的好伙伴,
也是有很多好玩或者实用的功能,
今天来教一下大家如何用c++获取当前时间
嘿嘿
这个其实也很简单
time.h头文件详解
定义
- 世界标准时间:格林尼治时间(Greenwich Mean Time,GMT),中国内地时间为UTC+8,美国是UTC-5
- 日历时间:从一个标准时间点到此时的时间经过的秒数,“相对时间”
- 时间点:长整型,当前时间和标准时间相差的秒数。
- 时钟计时单元:C/C++基本计时单位
说明
这个可以自己看time.h的代码可以找到
我已经整理出来了
<time.h>
clock_t clock(void);//从进程开始到调用clock之间的CPU时钟计时单元
CLOCKS_PER_SEC//CPU时钟计时单元,1秒钟会有多少个时钟计时单元
#define CLOCKS_PER_SEC ((clock_t)1000)
time_t time(time_t* timer);//返回从1970年1月1日0时0分0秒到现在的秒数
struct tm* gmtime(const time_t *timer);//将日历时间转化为世界标准时间(格林尼治时间)
struct tm* localtime(const time_t *timer);//将日历时间转化为本地时间
char* asctime(const struct tm* timeptr);//通过tm结构生成具有固定格式的时间字符串
char* ctime(const time_t *timer);//通过日历时间生成时间字符串
实例
时间转字符串
size_t strftime(char* strDest,size_t maxsize,const char* format,const struct tm *timeptr);
time_t timer=time(NULL);
char szbuf[256]={0};
strftime(szbuf,sizeof(szbuf),"%Y-%m-%d %H:%M:%S",localtime(&timer));
上面用了 time_t 获取了从1970年1月1日0时0分0秒到现在的秒数,
%Y是年
%m是月
%d是天
%H是时
%M是分
%S是秒
经过这段操作,就可以输出当前时间2022-4-24 17:33:38
因为今天还暂时用不到别的,所以先说这一个
实例2:判断当前时间在不在某时间段内
int huoqu()
{
time_t now_time;
now_time = time(NULL);
struct tm *local;
local=localtime(&now_time); //获取当前系统时间
char buf[80];
strftime(buf,80,"%H0%M",local);
if ((buf[0]-'0')*10+(buf[2]-'0')==20)
{
return 1;
}
else
{
return 0;
}
}
这行 if(........==20)可能乍一看看不懂
这行代码的意思是24时制小时数是晚上八点
可以判断当前时间在不在八点到九点之间
因为如果是八点到九点,时间肯定是20:xx
所以只要判断小时是不是20就行了
实例三:防沉迷系统
这个其实我之前发过,今天把它又改了一下,顺便发出来了
代码游戏部分有重复的部分,没有什么学习价值,可以自己复制玩玩
#include <iostream>
#include <string>
#include <iomanip>
#include <time.h>
#include<stdlib.h>
#include<windows.h>
#include<cstring>
#include<conio.h>
using namespace std;
int sfz()
{
int a[18],i,n,b;
printf("当前未进行认证\n");
printf("请输入身份证号:");
for(i=0;i<18;i++)
scanf("%1d",&a[i]);
n=a[6]*1000+a[7]*100+a[8]*10+a[9];
b=2022-n;
if(b<18)
return 0;
else
return 1;
}
int huoqu()
{
time_t now_time;
now_time = time(NULL);
struct tm *local;
local=localtime(&now_time); //获取当前系统时间
char buf[80];
strftime(buf,80,"%H0%M",local);
if ((buf[0]-'0')*10+(buf[2]-'0')==20)
{
return 1;
}
else
{
return 0;
}
}
int jiancha()
{
time_t now_time;
now_time = time(NULL);
struct tm *local;
local=localtime(&now_time); //获取当前系统时间
char buf[80];
strftime(buf,80,"%H0%M",local);
if ((buf[0]-'0')*10+(buf[2]-'0')!=20)
{
return 0;
}
}
int main()
{
system("color F0");
if (sfz()==0)
{
if (huoqu()==1)
{
start:
cout<<" 21点"<<endl;
cout<<"----------------------"<<endl;
cout<<"输入1开始"<<endl;
int n;
cin>>n;
if (n==1)
{
system("cls");
cout<<"开始"<<endl;
//输入昵称
string name1,name2;
cout<<"请输入玩家1昵称"<<endl;
cin>>name1;
cout<<"请输入玩家2昵称"<<endl;
cin>>name2;
/**/
string op1,op2;
int n1=0,n2=0;
int s1=0,s2=0;
//玩家1
cout<<"请玩家1拿牌"<<endl;
while (true)
{
cout<<"是否继续拿?(y/n)"<<endl;
cin>>op1;
if (op1=="y")
{
srand(time(NULL));
s1 += (rand()%(7-1+1)+1);
cout<<"你的牌一共有"<<s1<<endl;
n1++;
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
}
else if (op1=="n")
{
cout<<"你一共拿了"<<n1<<"张牌";
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
break;
}
else
{
cout<<"请不要乱输!";
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
}
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
}
Sleep(3000);
system("cls");
//玩家2
cout<<"请玩家2拿牌"<<endl;
while (true)
{
cout<<"是否继续拿?(y/n)"<<endl;
cin>>op2;
if (op2=="y")
{
srand(time(NULL));
s2 += (rand()%(7-1+1)+1);
cout<<"你的牌一共有"<<s2<<endl;
n2++;
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
}
else if (op1=="n")
{
cout<<"你一共拿了"<<n2<<"张牌";
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
break;
}
else
{
cout<<"请不要乱输!";
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
}
if (jiancha()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(1000);
exit(0);
}
}
Sleep(3000);
system("cls");
cout<<"正在统计....."<<endl;
Sleep(1000);
cout<<"玩家1总分数:"<<s1<<endl;
cout<<"玩家2总分数:"<<s2<<endl;
if (s1<=21 && s2<=21)
{
if (s1>s2) {
cout<<"【"<<name1<<"】"<<"is the winner!!!";
cout<<"\n按a重来";
int chch = _getch();
if (chch==97) goto start;
}
if (s1==s2) {
cout<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
cout<<"\n按a重来";
int chch = _getch();
if (chch==97) goto start;
}
if (s1<s2) {
cout<<"【"<<name2<<"】"<<"is the winner!!!";
cout<<"\n按a重来";
int chch = _getch();
if (chch==97) goto start;
}
}
if(s1>21 && s2>21)
{
cout<<"两方都超过21分,"<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
cout<<"\n按a重来";
int chch = _getch();
if (chch==97) goto start;
}
if (s1>21 && s2<=21) {
cout<<"【"<<name2<<"】"<<"is the winner!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start;
}
if (s2>21 && s1<=21) {
cout<<"【"<<name1<<"】"<<"is the winner!!!";
cout<<"\n按a重来";
int chch = _getch();
if (chch==97) goto start;
}
}
}
if (huoqu()==0)
{
cout<<"\n腾讯成长守护提醒您:\n根据国家防沉迷通知的相关要求和腾讯最新强化的防沉迷策略,由于您是未成年玩家,仅能在\n周五、周六、和法定节假日每日20时至21时登录游戏,在上述时间段外无法游戏,请注\n意休息。\n";
Sleep(3000);
exit(0);
}
}
if (sfz()==1)
{
start1:
cout<<" 21点"<<endl;
cout<<"----------------------"<<endl;
cout<<" 输入1开始"<<endl;
int n;
cin>>n;
if (n==1)
{
system("cls");
cout<<"开始"<<endl;
//输入昵称
string name1,name2;
cout<<"请输入玩家1昵称"<<endl;
cin>>name1;
cout<<"请输入玩家2昵称"<<endl;
cin>>name2;
/**/
string op1,op2;
int n1=0,n2=0;
int s1=0,s2=0;
//玩家1
cout<<"请玩家1拿牌"<<endl;
while (true)
{
cout<<"是否继续拿?(y/n)"<<endl;
cin>>op1;
if (op1=="y")
{
srand(time(NULL));
s1 += (rand()%(7-1+1)+1);
cout<<"你的牌一共有"<<s1<<endl;
n1++;
}
else if (op1=="n")
{
cout<<"你一共拿了"<<n1<<"张牌";
break;
}
else
{
cout<<"请不要乱输!";
}
}
Sleep(3000);
system("cls");
//玩家2
cout<<"请玩家2拿牌"<<endl;
while (true)
{
cout<<"是否继续拿?(y/n)"<<endl;
cin>>op2;
if (op2=="y")
{
srand(time(NULL));
s2 += (rand()%(7-1+1)+1);
cout<<"你的牌一共有"<<s2<<endl;
n2++;
}
else if (op1=="n")
{
cout<<"你一共拿了"<<n2<<"张牌";
break;
}
else
{
cout<<"请不要乱输!";
}
}
Sleep(3000);
system("cls");
cout<<"正在统计....."<<endl;
Sleep(1000);
cout<<"玩家1总分数:"<<s1<<endl;
cout<<"玩家2总分数:"<<s2<<endl;
if (s1<=21 && s2<=21)
{
if (s1>s2) {
cout<<"【"<<name1<<"】"<<"is the winner!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start1;
}
if (s1==s2){
cout<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start1;
}
if (s1<s2){
cout<<"【"<<name2<<"】"<<"is the winner!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start1;
}
}
if(s1>21 && s2>21)
{
cout<<"两方都超过21分,"<<"【"<<name1<<"】"<<"and"<<"【"<<name2<<"】"<<"come out even!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start1;
}
if (s1>21 && s2<=21) {
cout<<"【"<<name2<<"】"<<"is the winner!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start1;
}
if (s2>21 && s1<=21) {
cout<<"【"<<name1<<"】"<<"is the winner!!!";
cout<<"\n按a重来\n";
int chch = _getch();
if (chch==97) goto start1;
}
}
}
return 0;
}
好了,效果就是要先输入身份证号,再判断一下
如果是未成年,如果时间是晚上八点到九点之间,可以进入游戏,如果不在八点到九点之间,不能进入游戏
如果是成年人,直接进入游戏
结束
哈哈,做完了
大家应该对time.h也有了一些新的了解
拜拜