1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// abc.cpp
//----------------------------------------------------------------------------
// Calculate the time it takes to type the ABC's.
//----------------------------------------------------------------------------
// Copyright Michael Thomas Greer 2010
// Distributed under the Boost Software License, Version 1.0.
// ( See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt )
//----------------------------------------------------------------------------
// See http://www.cplusplus.com/forum/beginner/28855/
#include <ciso646>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
//----------------------------------------------------------------------------
typedef double epoch_time_t;
//----------------------------------------------------------------------------
#ifdef __WIN32__
#include <windows.h>
epoch_time_t get_epoch_time() // Since 1601 Jan 1
{
FILETIME f;
GetSystemTimeAsFileTime( &f );
LARGE_INTEGER t =
{ {
f.dwLowDateTime,
f.dwHighDateTime
} };
return t.QuadPart / 10000000.0; // 100 nano-second intervals
}
void waitforkeypress()
{
WaitForSingleObject(
GetStdHandle( STD_INPUT_HANDLE ),
INFINITE
);
}
//----------------------------------------------------------------------------
#else // POSIX
#include <sys/time.h>
epoch_time_t get_epoch_time() // Since 1970 Jan 1
{
struct timeval tv;
if (gettimeofday( &tv, NULL )) return 0.0;
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
#include <unistd.h>
#include <termios.h>
#include <poll.h>
#define INFINITE (-1)
void waitforkeypress()
{
struct termios initial_settings;
tcgetattr( STDIN_FILENO, &initial_settings );
struct termios settings = initial_settings;
settings.c_lflag &= ~(ICANON);
tcsetattr( STDIN_FILENO, TCSANOW, &settings );
struct pollfd pls[ 1 ];
pls[ 0 ].fd = STDIN_FILENO;
pls[ 0 ].events = POLLIN | POLLPRI;
poll( pls, 1, INFINITE );
tcsetattr( STDIN_FILENO, TCSANOW, &initial_settings );
}
#endif
//----------------------------------------------------------------------------
const char* ABCS = "abcdefghijklmnopqrstuvwxyz";
//----------------------------------------------------------------------------
int main()
{
epoch_time_t start, stop;
while (true)
{
string abcs;
ios::sync_with_stdio( true );
cout <<
"Please enter the alphabet (in miniscules) as quickly as you can.\n"
"You will be timed to see how long it takes.\n"
"Begin when you are ready.\n\n"
"> "
<< flush;
waitforkeypress();
start = get_epoch_time();
getline( cin, abcs );
stop = get_epoch_time();
double time_spent = stop - start;
unsigned minutes = (unsigned) time_spent / 60;
unsigned seconds = (unsigned) time_spent % 60;
unsigned hundredths = (unsigned)(time_spent * 100) % 100;
cout << "It took you "
<< setfill( '0' ) << minutes << ":"
<< setw( 2 ) << seconds << "."
<< setw( 2 ) << hundredths;
if (abcs == ABCS)
{
cout << ". Good job!\n";
break;
}
cout << ", but you mistyped the alphabet.\n"
"Would you like to try again ([y]/n)? "
<< flush;
getline( cin, abcs );
if (!abcs.empty() and (abcs[ 0 ] == 'n')) break;
cout << "\n\n";
}
return 0;
}
// end abc.cpp
|