C++之程序时间统计类实现

  1 /**********
  2 TimeCounter.h
  3 huangsy13@gmail.com
  4 **********/
  5 #ifndef TIMECOUNTER
  6 #define TIMECOUNTER
  7 
  8 #include <iostream>
  9 #include <cstring>
 10 #include <ctime>
 11 #include <sstream>
 12 #include <cstdlib>
 13 #include "FileStruct.h"
 14 
 15 using namespace std;
 16 
 17 struct TimeCounter{
 18     time_t beginT;
 19     time_t nowT;
 20     int totalTask;
 21     int completeTask;
 22     string lastUseT;
 23     void setUp(int totalTask0){
 24         totalTask = totalTask0;
 25         nowT = 0;
 26         beginT = time(NULL);
 27     }
 28     bool update(int taskNow){
 29         nowT = time(NULL);
 30         completeTask = taskNow;
 31         if (lastUseT == useTime() && completeTask < totalTask){
 32             return false;
 33         }
 34         else{
 35             lastUseT = useTime();
 36             return true;
 37         }
 38     }
 39     string useTime(){
 40         int r = nowT - beginT;
 41         return getTimeStr(r);
 42     }
 43     string restTime(){
 44         if(completeTask == 0){
 45             return "unknow";
 46         }
 47         int restTT = double(totalTask-completeTask)*double(nowT-beginT)/double(completeTask);
 48         return getTimeStr(restTT);
 49     }
 50     string getTimeStr(int r){
 51         int rs = r;
 52         int rh = rs/3600;
 53         int  rm = rs/60;
 54         string str;
 55         if (rh != 0){
 56             str = numToStr(rh) + " hours ";
 57         }
 58         if (rs >= 60){
 59             str += numToStr(rm%60) + " minutes ";
 60         }
 61         str += numToStr(rs%60) + " seconds";
 62         return str;
 63     }
 64     string percent(){
 65         int p = 100*completeTask/totalTask;
 66         return numToStr(p)+"%";
 67     }
 68     void print(bool clear){
 69         if (clear) system("clear");
 70         cout << "use time " << useTime()<<endl;
 71         cout << "rest Time "<< restTime() <<endl;
 72         cout << "complete " << percent() << endl;
 73     }
 74     void updateAndPrint(int nn,bool clear = true){
 75         if(update(nn)){
 76             print(clear);
 77         }
 78     }
 79 };
 80 
 81 #endif
 82 
 83 
 84 /*********
 85 main.cpp
 86 huangsy13@gmail.com
 87 *********/
 88 #include <iostream>
 89 #include <cstring>
 90 #include <ctime>
 91 #include <sstream>
 92 #include <cstdlib>
 93 #include "TimeCounter.h"
 94 
 95 using namespace std;
 96 
 97 int main(){
 98     TimeCounter T;
 99     int totalTask = 5000;
100     T.setUp(totalTask);
101     int taskNow = 0;
102     while(taskNow < totalTask){
103         T.updateAndPrint(++taskNow);
104         for (int i = 0 ; i < 100000 ; i++){
105             int a = 50;
106             int b = 100;
107             while(a != b){
108                 a++;
109             }
110         }
111     }
112 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期;日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期;日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期编写时间CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值