旅行模拟问题——关于时间轴

在网上搜索了一堆c++时间函数之后,越发体会到了自己知识储备量的匮乏。由于在旅行模拟问题中,时间轴并不是特别重要,只要能给出程序运行的时间就可以了,所以我找了一个最最简单的时间函数——time()函数

需要的头文件是time.h

返回值是当前程序运行的时间。

另外为了get到一个起始时间,(我不想随便用2016年1月1日凌晨一点这样的,我想用现实时间)我找到了另一个函数——getlocaltime(&lt)

直接贴代码吧

 1 /*此程序为探索c++函数中时间函数的用法 */
 2 /*为数据结构大作业进行必要的准备*/ 
 3 /*time 2019/3/21 programer liu*/
 4 
 5 #include<iostream>
 6 #include<windows.h>
 7 #include<time.h>
 8 using namespace std;
 9 main(){
10     time_t start,stop;
11     start=time(NULL);
12     stop=time(NULL);
13     cout<<"use time:"<<stop-start<<endl;
14     start=time(NULL);
15     /*可以利用上面的函数得到程序运行时间,依此更改时间轴的时间*/
16     /*大概,在暂停的时候要存储几个新的变量*/ 
17     SYSTEMTIME lt;
18     
19     GetLocalTime(&lt);
20     cout<<"the local time is"<<lt.wYear<<""<<
21     lt.wMonth<<""<<lt.wDay<<""<<lt.wHour<<""<<endl;
22     //这个函数可以get到电脑上的显示时间 
23     int hour,month,year,day;
24     hour=lt.wHour;
25     month=lt.wMonth;
26     year=lt.wYear;
27     day=lt.wDay;
28     cout<<"the local time is"<<year<<""<<
29     month<<""<<day<<""<<hour<<""<<endl;  
30     char c;
31     int lasthour=hour;
32     int lastday=day;
33     while(1){
34         cout<<"输入回车键键开始使用查询系统,输入e结束系统..."<<endl;
35         if(c=getchar()){
36             if(c=='e')break;
37             stop=time(NULL);
38             /*尝试通过更改start的值但发现start的值似乎是编译时确定的,有毒,意思就是一直是0*/
39             hour=lasthour+(stop-start)/10;
40             
41             /*尝试解决重复计时的问题*/ 
42             if(hour>24){
43                 day=lastday+hour/24;//所以说是会向上进位?? 不并不是。和hour是一个道理 
44                 //cout<<hour<<" "<<hour/24<<endl;
45                 hour=hour%24;
46                 
47                 if(hour==0){
48                     hour=24;
49                     day--;
50                 }
51             }
52             cout<<"过去了"<<(stop-start)<<""<<endl; 
53             cout<<"当前时刻为:"<<year<<""<<month<<""<<day<<""<<hour<<""<<endl;
54         } /*可以成功的按十秒一小时往前推进这样子*/ 
55     }
56     /*个人认为按照年月日进行计时有一点点麻烦(需要考虑大小月份和二月)
57     (天啊这就有了平年和闰年!)
58     大概可以按照一个月三十天来?然后每个月份的时刻表是相同的*/ 
59 }

以及最终确定每天的时刻表是不变的。

原文地址:https://www.cnblogs.com/liuxinyu/p/10616548.html