/**********
TimeCounter.h
huangsy13@gmail.com
**********/
#ifndef TIMECOUNTER
#define TIMECOUNTER
#include <iostream>
#include <cstring>
#include <ctime>
#include <sstream>
#include <cstdlib>
#include "FileStruct.h"
using namespace std;
struct TimeCounter{
time_t beginT;
time_t nowT;
int totalTask;
int completeTask;
string lastUseT;
void setUp(int totalTask0){
totalTask = totalTask0;
nowT = ;
beginT = time(NULL);
}
bool update(int taskNow){
nowT = time(NULL);
completeTask = taskNow;
if (lastUseT == useTime() && completeTask < totalTask){
return false;
}
else{
lastUseT = useTime();
return true;
}
}
string useTime(){
int r = nowT - beginT;
return getTimeStr(r);
}
string restTime(){
if(completeTask == ){
return "unknow";
}
int restTT = double(totalTask-completeTask)*double(nowT-beginT)/double(completeTask);
return getTimeStr(restTT);
}
string getTimeStr(int r){
int rs = r;
int rh = rs/;
int rm = rs/;
string str;
if (rh != ){
str = numToStr(rh) + " hours ";
}
if (rs >= ){
str += numToStr(rm%) + " minutes ";
}
str += numToStr(rs%) + " seconds";
return str;
}
string percent(){
int p = *completeTask/totalTask;
return numToStr(p)+"%";
}
void print(bool clear){
if (clear) system("clear");
cout << "use time " << useTime()<<endl;
cout << "rest Time "<< restTime() <<endl;
cout << "complete " << percent() << endl;
}
void updateAndPrint(int nn,bool clear = true){
if(update(nn)){
print(clear);
}
}
};
#endif
/*********
main.cpp
huangsy13@gmail.com
*********/
#include <iostream>
#include <cstring>
#include <ctime>
#include <sstream>
#include <cstdlib>
#include "TimeCounter.h"
using namespace std;
int main(){
TimeCounter T;
int totalTask = ;
T.setUp(totalTask);
int taskNow = ;
while(taskNow < totalTask){
T.updateAndPrint(++taskNow);
for (int i = ; i < ; i++){
int a = ;
int b = ;
while(a != b){
a++;
}
}
}
}