|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <vector> |
| 4 | +#include <chrono> |
| 5 | +#include <iomanip> |
| 6 | +#include <cstdlib> |
| 7 | +#include <ctime> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | +using namespace std::chrono; |
| 11 | + |
| 12 | +string getRandomSnippet() { |
| 13 | + vector<string> snippets = { |
| 14 | + "for(int i = 0; i < n; ++i) cout << arr[i] << ' ';", |
| 15 | + "if(a > b) swap(a, b);", |
| 16 | + "while(left <= right) mid = (left + right) / 2;", |
| 17 | + "vector<int> nums = {1, 2, 3, 4, 5};", |
| 18 | + "cout << \"Hello, World!\" << endl;" |
| 19 | + }; |
| 20 | + srand((unsigned) time(0)); |
| 21 | + return snippets[rand() % snippets.size()]; |
| 22 | +} |
| 23 | + |
| 24 | +int countMistakes(const string& original, const string& typed) { |
| 25 | + int mistakes = 0; |
| 26 | + int len=max(original.size(), typed.size()); |
| 27 | + for (int i = 0; i < len; ++i) { |
| 28 | + if (i>=original.size()||i>=typed.size()||original[i]!=typed[i]) |
| 29 | + mistakes++; |
| 30 | + } |
| 31 | + return mistakes; |
| 32 | +} |
| 33 | + |
| 34 | +void showResults(const string& snippet, const string& typed, double duration) { |
| 35 | + int totalChars=snippet.size(); |
| 36 | + int mistakes=countMistakes(snippet, typed); |
| 37 | + double accuracy=((double)(totalChars - mistakes) / totalChars) * 100.0; |
| 38 | + |
| 39 | + double minutes = duration/60.0; |
| 40 | + double wpm = (typed.size()/5.0)/minutes; |
| 41 | + |
| 42 | + cout << "\n\n========== RESULTS ==========\n"; |
| 43 | + cout << "Time Taken: " << fixed << setprecision(2) << duration << " seconds\n"; |
| 44 | + cout << "Mistakes: " << mistakes << "\n"; |
| 45 | + cout << "Accuracy: " << fixed << setprecision(2) << accuracy << "%\n"; |
| 46 | + cout << "Typing Speed: " << fixed << setprecision(2) << wpm << " WPM\n"; |
| 47 | + cout << "=============================\n\n"; |
| 48 | +} |
| 49 | + |
| 50 | +int main() { |
| 51 | + cout << "==============================\n"; |
| 52 | + cout << " Code Typing Speed Tester\n"; |
| 53 | + cout << "==============================\n\n"; |
| 54 | + |
| 55 | + string snippet = getRandomSnippet(); |
| 56 | + cout << "Type the following code snippet exactly as shown:\n\n"; |
| 57 | + cout << "-> " << snippet << "\n\n"; |
| 58 | + cout << "Press ENTER when you’re done typing.\n\n"; |
| 59 | + |
| 60 | + cout << "Start typing below:\n> "; |
| 61 | + auto start = high_resolution_clock::now(); |
| 62 | + |
| 63 | + string typed; |
| 64 | + getline(cin, typed); |
| 65 | + |
| 66 | + auto end = high_resolution_clock::now(); |
| 67 | + duration<double> diff = end - start; |
| 68 | + |
| 69 | + showResults(snippet, typed, diff.count()); |
| 70 | + |
| 71 | + cout << "Thank you for using the Code Typing Speed Tester!\n"; |
| 72 | + return 0; |
| 73 | +} |
0 commit comments