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

Forum Jump: 

[C++] Keyboard Typing Practice

 
Thread Tools
post #1 of 5 (permalink) Old 11-21-2018, 08:32 AM - Thread Starter
New to Overclock.net
 
Join Date: Nov 2018
Location: South Korea
Posts: 10
Rep: 0
[C++] Keyboard Typing Practice

I'm learning STL and C++ now, and I'm beginner and noob in this field
This code may have numerous inefficient, slow codes...






#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;
}
gunner_D is offline  
Sponsored Links
Advertisement
 
post #2 of 5 (permalink) Old 11-25-2018, 07:03 PM
RIP OCN
 
Join Date: Dec 2014
Posts: 561
Rep: 25 (Unique: 19)
yikes
anti-clockwize is offline  
post #3 of 5 (permalink) Old 11-27-2018, 04:12 AM
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  
Sponsored Links
Advertisement
 
post #4 of 5 (permalink) Old 12-17-2018, 11:10 AM - Thread Starter
New to Overclock.net
 
Join Date: Nov 2018
Location: South Korea
Posts: 10
Rep: 0
Thx. I didn't know that useful code...!
gunner_D is offline  
post #5 of 5 (permalink) Old 12-17-2018, 11:34 AM
Overclocker
 
JackCY's Avatar
 
Join Date: Jun 2014
Posts: 9,738
Rep: 329 (Unique: 236)
#1 no code comments
#2 no statement what it's supposed to do and what you need help with
#3 a sample test file is not provided as such it doesn't do anything without your inputs: https://ideone.com/aelkma
#4 forums needs CODE tag to keep formatting, which is not necessary for C/C++ as it is for Python (because it's stupid) but it's very welcome for human beings
JackCY is offline  
Reply

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the Overclock.net - An Overclocking Community forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
If you do not want to register, fill this field only and the name will be used as user name for your post.
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page


Forum Jump: 

Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off