Overclock.net - An Overclocking Community - View Single Post - [C++] Keyboard Typing Practice

View Single Post
post #3 of (permalink) Old 11-27-2018, 04:12 AM
Mrzev
New to Overclock.net
 
Mrzev's Avatar
 
Join Date: Feb 2008
Location: Texas
Posts: 2,258
Rep: 96 (Unique: 76)
use the code tags =D [.code] [./code] (those without the . ) Simply put, web browsers truncate multiple spaces. When you use the code block the page has a CSS style to present it nicely.
Code:
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
#include <string>

//#define DEBUG_MODE

using namespace std;

class type_text
{
public:
	type_text() :
		is_EOF(0), is_newline(0)
	{}

	vector<char>* string_vt;
	int is_EOF;
	int is_newline;
};

class type_correctivity
{
public:
	type_correctivity() :
		total_length(0), count_wrong(0)
	{}

	int total_length;
	int count_wrong;
};

type_text* print_string(ifstream& input_text)
{
	type_text* text = new type_text();

	char temp;
	text->string_vt = new vector<char>;

	while (1)
	{
		temp = input_text.get();

		if (temp == EOF)
		{
			text->is_EOF = 1;
			cout << endl << "==========================================" << endl;
			return text;
		}
		else if (temp == '\n')
		{
			text->is_newline = 1;
			cout << endl << "==========================================" << endl;
			return text;
		}

		text->string_vt->push_back(temp);
		cout << temp;
	}

}

type_correctivity* check_correct(type_text* text, string* input_text)
{
	type_correctivity* correct_rate = new type_correctivity();
	correct_rate->total_length = text->string_vt->size();

	for (unsigned int vector_i = 0, i = 0; vector_i < text->string_vt->size(); vector_i++, i++)
	{
		if (input_text->operator[](i) == NULL)
		{
			correct_rate->count_wrong += text->string_vt->size() - input_text->length();
			break;
		}

		if (text->string_vt->operator[](vector_i) != input_text->operator[](i))
			correct_rate->count_wrong++;
	}

	return correct_rate;
}

int main(int argc, char** argv)
{
	ifstream fin;
	type_text *input_text;
	type_correctivity* correct_rate;
	string test_text;

	time_t start, end, total = 0;

	int count_length = 0, count_wrong = 0;

	char* check_text = new char[100];

	if (argc == 1)
	{
		cout << "No file name parameter." << endl;
		fin.open("test.txt");

		if (!fin)
			return 0;

		while (1)
		{
			input_text = print_string(fin);

			start = clock();
			getline(cin, test_text);
			end = clock();

			cout << endl << "==========================================" << endl;

			total += end - start;

			correct_rate = check_correct(input_text, &test_text);
			count_length += correct_rate->total_length;
			count_wrong += correct_rate->count_wrong;

			if (input_text->is_EOF == 1)
				break;
		}

		cout << "Correct Rate : " << (count_length - count_wrong) / count_length * 100 << endl;
		cout << "Speed : " << (double)count_length / total * 1000 * 60 << " Types per minute." << endl;

#ifdef DEBUG_MODE
		cout << "### Debug Mode ###" << endl << correct_rate->count_wrong << endl << correct_rate->total_length << endl;
#endif
	}
	else if (argc = 2)
	{
		fin.open(argv[1]);

		if (!fin)
			return 0;

		while (1)
		{
			input_text = print_string(fin);

			start = clock();
			getline(cin, test_text);
			end = clock();

			cout << endl << "==========================================" << endl;

			total += end - start;

			correct_rate = check_correct(input_text, &test_text);
			count_length += correct_rate->total_length;
			count_wrong += correct_rate->count_wrong;

			if (input_text->is_EOF == 1)
				break;
		}

		cout << "Correct Rate : " << (count_length - count_wrong) / count_length * 100 << endl;
		cout << "Speed : " << (double)count_length / total * 1000 * 60 << "Types per minute." << endl;
	}
	else if (argc > 2)
	{
		for (int arg_count = 1; arg_count < argc; arg_count++)
		{
			fin.open(argv[arg_count]);

			if (!fin)
				return 0;

			while (1)
			{
				input_text = print_string(fin);

				start = clock();
				getline(cin, test_text);
				end = clock();

				cout << endl << "==========================================" << endl;

				total += (end - start);

				correct_rate = check_correct(input_text, &test_text);
				count_length += correct_rate->total_length;
				count_wrong += correct_rate->count_wrong;

				if (input_text->is_EOF == 1)
					break;
			}

			cout << "Correct Rate : " << (count_length - count_wrong) / count_length * 100 << endl;
			cout << "Speed : " << (double)count_length / total * 1000 * 60 << "Types per minute." << endl;

			cout << endl;
		}
	}

	return 0;
}




Last edited by Mrzev; 11-27-2018 at 04:15 AM.
Mrzev is offline