-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinput.cpp
More file actions
211 lines (174 loc) · 3.89 KB
/
Copy pathinput.cpp
File metadata and controls
211 lines (174 loc) · 3.89 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<bits/stdc++.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#include<chrono>
using namespace std::chrono;
using namespace std;
class Time
{
private:
inline static high_resolution_clock::time_point start = high_resolution_clock::now();
inline static high_resolution_clock::time_point stop = high_resolution_clock::now();
inline static int target = 0;
public:
Time() {}
static void startClock()
{
start = high_resolution_clock::now();
}
static void stopClock()
{
stop = high_resolution_clock::now();
}
static void lap() {
stop = high_resolution_clock::now();
}
static double getTime()
{
duration<double> elapsedTime = duration_cast<duration<double>>(stop - start);
return elapsedTime.count();
}
static int getTarget()
{
return target;
}
static void setTarget(int tar1)
{
target = tar1;
}
static void startsIn()
{
cout << "\nTo stop, press ^\n";
cout << "Starting in 3... ";
Sleep(1000);
cout << "2... ";
Sleep(1000);
cout << "1... ";
Sleep(1000);
cout << endl;
}
};
class UserInput: public Time
{
private:
int errors;
int mode; // 0 for Classic; 1 for TimeAttack
int timeFinished;
int wpm;
int total;
int fixed;
int position;
string s;
char *typed;
char current;
double accuracy;
int terminate;
public:
UserInput(int mode, string str)
{
this->s = str;
this->mode = mode;
typed = new char[str.length()];
accuracy = 0.00;
position = 0;
errors = 0;
terminate = 0;
timeFinished = 0;
wpm = 0;
}
void type()
{
cout << endl << s << endl << endl;
while (position != s.length())
{
if(mode == 1) {
Time::lap();
if(Time::getTime() > Time::getTarget()) {
timeFinished = 1;
break;
}
}
current = getche();
if (current == '^')
{
terminate = 1;
break;
}
if (current == 8)
{
position--;
continue;
}
else if (current != s[position])
{
errors++;
}
typed[position] = current;
position++;
}
}
int isTimeFinished() {
return timeFinished;
}
int isTerminated() {
return terminate;
}
int getErrors() {
return errors;
}
int getTotal() {
return (position == s.length() ? s.length() : position);
}
double getAccuracy()
{
double accu;
if(position == s.length())
{
accu = (double) (s.length()-errors)*100/s.length();
}
else
{
if(position!=0) {
accu = (double) (position-errors)*100/position;
}
else
{
accu = 0;
}
}
return accu;
}
int getFixedErrors() {
int final_errors = 0 ;
if(position == s.length())
{
for(int i=0;i<s.length();i++) {
if(typed[i] != s[i]) {
final_errors++;
}
}
}
else
{
for(int i=0;i<position;i++) {
if(typed[i] != s[i]) {
final_errors++;
}
}
}
return errors-final_errors;
}
int getWordCount() {
int count = 0;
for(int i=0; i<position;i++) {
if(typed[i] == ' ') {
count++;
}
}
return count;
}
~UserInput() {
delete(typed);
}
};