Overclock.net - An Overclocking Community

Overclock.net - An Overclocking Community (https://www.overclock.net/forum/)
-   Coding and Programming (https://www.overclock.net/forum/142-coding-programming/)
-   -   [C++] Keyboard Typing Practice (https://www.overclock.net/forum/142-coding-programming/1713914-c-keyboard-typing-practice.html)

gunner_D 11-21-2018 08:32 AM

[C++] Keyboard Typing Practice
 
I'm learning STL and C++ now, and I'm beginner and noob in this field :o:o
This code may have numerous inefficient, slow codes... :o






#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;
}

anti-clockwize 11-25-2018 07:03 PM

yikes

Mrzev 11-27-2018 04:12 AM

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;
}


gunner_D 12-17-2018 11:10 AM

Thx. I didn't know that useful code...! :)

JackCY 12-17-2018 11:34 AM

#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


All times are GMT -7. The time now is 05:17 AM.

Powered by vBulletin® Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.

User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
vBulletin Security provided by vBSecurity (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.

vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.