How does C++ implement simple timer resolution

  • 2020-05-12 02:54:14
  • OfStack

Implementation analysis

First of all, we first analyze the timer 1 some functions, easy 1 timer including start, pause, stop, and shows the basic function, these functions to C + + object-oriented programming thought (OOP) abstracts, four of the members is the timer class (Timer) function, of course we want to put these functions as a public, because they are left to the external interface (interface).

Then we can analyze the three states of the timer: stop, running, and pause (note: pause is not stop). How can we record the three states of the timer?

Here we use a Boolean variable to record the three states of the timer, bool is_pause, bool is_stop, and here we must pay attention to the name of the variable, just like this. To encapsulate the C++ class, of course, use the two bool variables as private members of the timer class (Timer).

Implementation method

So the big question is, how do we do that? Well, you know time() Function? Simply put, 1 time function: this function is in the time.h header file, and it returns the number of seconds of the current time since Unix era (January 1 1970 00:00:00 GMT), which is a long shaping (long). In this way, we get the return value of the time function at the start of the timer, save it to a variable, and save the pause time with a variable. When we start the timer, we enter an infinite loop, and we use it all the time time() - the start time is the time of the current timer. (don't be in a hurry. Read on for details.) In order to save the time at the start and pause moments, you must add two long shaping (long) variables to the timer class (Timer) : start_time,pause_time, of course, as private variables.

Like there's something missing... In order for the outside world to get the state of the current timer, we also need two functions to return the state of the timer, is_pause() Is the timer on hold, is_stop() Whether the timer is stopped or not, the return value is of type bool.

Don't forget Timer's constructor, which does some initialization!

Ok, now that our timer class design is complete (prefect), the code is as follows:


class Timer 
{ 
 private: 
 long start_time; 
 long pause_time; 
 // two bool Value tag 4 Kind of state  
 bool is_pause; // Record the status of the timer   (whether in pause state)  
 bool is_stop;// Is it in the stop state  
 public: 
 Timer(); 
 bool isPause(); // Return timer status  
 bool isStop(); 
 // The timer 3 Seed action (function)  
 void Start(); 
 void Pause(); 
 void Stop(); 
 inline long getStartTime() {return start_time;} 
 void show(); 
}; 

The next task is to implement the member functions of Timer...

The first is the constructor Timer::Timer() , complete some initialization work:


Timer::Timer() 
{ 
 is_pause = false; // Initializes the timer state  
 is_stop = true; 
} 

The timer should stop before it starts! (the timer can only be in one state. Don't get confused!)

A member function isPause() It just makes the external fetch timer pause or not, so easy


bool Timer::isPause() 
{ 
 if(is_pause) 
 return true; 
 else 
 return false; 
} 

The isStop function and isPause1 are just like an interface, allowing the external access to the status of the timer:

bool Timer::isStop()
{
if(is_stop)
return true;
return false;
}

The following is Timer::Start() The implementation of the function, which is the function to run when the timer starts:


void Timer::Start() // start  
{ 
 if(is_stop) 
 { 
 start_time = time(0); 
 is_stop = false; 
 } 
 else if(is_pause) 
 { 
 is_pause = false; 
 start_time += time(0)-pause_time; // Update start time: at this time  -  The time taken to pause  +  on 1 Time to start  =  The start time at this point  
 } 
} 

First, we judge the state of timing under 1. If it is in the stop state, we get the start time, and then update the state of timer. If the timer is in the pause state, we let the timer continue, and I adjust the timing by changing the start time (start_time) : (using the time at this point - the time taken at the pause + the time taken at the previous start = the start time at this point). If the timer is running, do nothing! (I don't know if you can understand...)

This is the pause function Timer::Pause() The implementation of the:


void Timer::Pause() // suspended  
{ 
 if(is_stop||is_pause) // If I'm at stop / hold , This action returns without any processing  
 return; 
 else // Otherwise the modulation is suspended  
 { 
 is_pause = true; 
 pause_time = time(0); // Get pause time  
 } 
} 

If you are not running, that is, in a pause or stop state, do nothing and return. Otherwise, handle the pause request: now that we've paused, we need to change the state of the timer, set the state to pause, and save the time at the moment. .

And then we implement the stop function Timer::Stop():


void Timer::Stop() // stop  
{ 
 if(is_stop) // If you are in a stop state (not a pause state), do nothing  
 return ; 
 else if(is_pause) // Change timer state  
 { 
 is_pause = false; 
 is_stop = true; 
 } 
 else if(!is_stop) 
 { 
 is_stop = true; 
 } 
} 

If it's in a stop state, go straight back. Otherwise if you're in a pause state change the state of the timer to is_stop = true ; Otherwise, it is in the running state and directly changes the state of the timer to stop.

Here is the function that shows the time time()0 :


void Timer::show() 
{ 
 long t = time(0) - start_time; 
 gotoxy(35,12); 
 cout<<setw(2)<<setfill('0')<<t/60/60<<":" 
 <<setw(2)<<setfill('0')<<t/60<<":" 
 <<setw(2)<<setfill('0')<<t%60<<endl; 
} 

I'm going to say 1 here gotoxy(int x,int y) Function, which is used to position the console cursor to the coordinates (x, y), show is to be placed in a loop, so the output time is always printed to a place, to achieve the update of the time (I am not very smart); setw(int x) Is to set the output word width, setfill(char  ch) Character padding is set. The time function returns seconds. t/60/60 gets hours, t/60 gets minutes, and t%60 gets seconds.

So now we have our main function main()


int main() 
{ 
 Timer t; 
 char ch; 
 hidden();// Hide the cursor  
 system("color 02"); 
 gotoxy(35,12); 
 cout<<"00:00:00"; 
 gotoxy(20,18); 
 cout<<" According to the a Start, pause by space, press s stop "; 
 while(1) 
 { 
 if(kbhit()) 
 { 
  ch = getch(); 
  switch (ch) 
  { 
  case 'a':t.Start();break; 
  case 's':t.Stop();break; 
  case ' ':t.Pause();break; 
  default :break; 
  } 
 } 
 if(!t.isStop()&&!t.isPause()) 
 { 
  t.show(); 
 } 
 } 
 
} 

Timer t; Define a timer. hidden() ; Is used to hide the console cursor, not required.

And then you go into a dead loop, kbhit() Function is to detect whether there is a key, if there is a key to return non-0 value, no key to return 0; with getch() Get the key and use it

The switch case branch handles different keys.

At this point, our timer design is complete! Doesn't it feel simple! tongue

Let's take a look at the results of 1:

The following are the header files used in this program:


#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <conio.h> 
#include <iomanip> 
#include <windows.h> 

Here are the functions used in the code:

void gotoxy(int x,int y)


void gotoxy(int x, int y)// Position the cursor, x For the row ,y For the column coordinate  
{ 
 COORD pos = {x,y};// (coordinate   Position);  
 HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // Get standard processing (standard output processing);  
 SetConsoleCursorPosition(hOut, pos);// Set the position of the console cursor;  
} 

void hidden( )


Timer::Timer() 
{ 
 is_pause = false; // Initializes the timer state  
 is_stop = true; 
} 
0

conclusion

The above is the entire content of this article, I hope the content of this article for everyone's learning or can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: