diff --git a/Src/TypingSpeedTester/TypingSpeedTester.cpp b/Src/TypingSpeedTester/TypingSpeedTester.cpp new file mode 100644 index 0000000..41775c0 --- /dev/null +++ b/Src/TypingSpeedTester/TypingSpeedTester.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::chrono; + +string getRandomSnippet() { + vector snippets = { + "for(int i = 0; i < n; ++i) cout << arr[i] << ' ';", + "if(a > b) swap(a, b);", + "while(left <= right) mid = (left + right) / 2;", + "vector nums = {1, 2, 3, 4, 5};", + "cout << \"Hello, World!\" << endl;" + }; + srand((unsigned) time(0)); + return snippets[rand() % snippets.size()]; +} + +int countMistakes(const string& original, const string& typed) { + int mistakes = 0; + int len=max(original.size(), typed.size()); + for (int i = 0; i < len; ++i) { + if (i>=original.size()||i>=typed.size()||original[i]!=typed[i]) + mistakes++; + } + return mistakes; +} + +void showResults(const string& snippet, const string& typed, double duration) { + int totalChars=snippet.size(); + int mistakes=countMistakes(snippet, typed); + double accuracy=((double)(totalChars - mistakes) / totalChars) * 100.0; + + double minutes = duration/60.0; + double wpm = (typed.size()/5.0)/minutes; + + cout << "\n\n========== RESULTS ==========\n"; + cout << "Time Taken: " << fixed << setprecision(2) << duration << " seconds\n"; + cout << "Mistakes: " << mistakes << "\n"; + cout << "Accuracy: " << fixed << setprecision(2) << accuracy << "%\n"; + cout << "Typing Speed: " << fixed << setprecision(2) << wpm << " WPM\n"; + cout << "=============================\n\n"; +} + +int main() { + cout << "==============================\n"; + cout << " Code Typing Speed Tester\n"; + cout << "==============================\n\n"; + + string snippet = getRandomSnippet(); + cout << "Type the following code snippet exactly as shown:\n\n"; + cout << "-> " << snippet << "\n\n"; + cout << "Press ENTER when you’re done typing.\n\n"; + + cout << "Start typing below:\n> "; + auto start = high_resolution_clock::now(); + + string typed; + getline(cin, typed); + + auto end = high_resolution_clock::now(); + duration diff = end - start; + + showResults(snippet, typed, diff.count()); + + cout << "Thank you for using the Code Typing Speed Tester!\n"; + return 0; +} diff --git a/Src/TypingSpeedTester/TypingSpeedTester.exe b/Src/TypingSpeedTester/TypingSpeedTester.exe new file mode 100644 index 0000000..2b27fe8 Binary files /dev/null and b/Src/TypingSpeedTester/TypingSpeedTester.exe differ