Code and Running Result
Navigate to the ~/project
directory and create a project file named typing3.c
:
cd ~/project
touch typing3.c
The program written according to the above guidelines is shown below:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getputch.h"
#define NO 30 /* Number of exercises */
#define KTYPE 16 /* Number of blocks */
int main(void)
{
char *kstr[] = {"12345", "67890-^\\", /* Layer 1 */
"!\"#$%", "&'() =~|", /* Layer 1 [Shift] */
"qwert", "yuiop@[", /* Layer 2 */
"QWERT", "YUIOP`{", /* Layer 2 [Shift] */
"asdfg", "hjkl;:]", /* Layer 3 */
"ASDFG", "HJKL+*}", /* Layer 3 [Shift] */
"zxcvb", "nm,./\\", /* Layer 4 */
"ZXCVB", "NM<> _", /* Layer 4 [Shift] */
};
int i, stage;
clock_t start, end; /* Start and end times */
init_getputch();
srand(time(NULL)); /* Set the seed for random number generation */
printf("Start typing association exercises.\n");
printf("Please enter the hidden character indicated by '?'.\n");
printf("Press the space key to start.\n");
fflush(stdout);
while (getch() != ' ')
;
start = clock(); /* Start time */
for (stage = 0; stage < NO; stage++) {
int k, p, key;
char temp[10];
do {
k = rand() % KTYPE;
p = rand() % strlen(kstr[k]);
key = kstr[k][p];
} while (key == ' ');
strcpy(temp, kstr[k]);
temp[p] = '?';
printf("%s", temp);
fflush(stdout);
while (getch() != key)
;
putchar('\n');
}
end = clock(); /* End time */
printf("Time taken: %.1f seconds.\n", (double)(end - start) / CLOCKS_PER_SEC);
term_getputch();
return 0;
}
The macro KTYPE
represents the number of blocks, which is 16, and the array kstr
is used to store the strings consisting of characters from each block arranged from left to right.
In terms of training purposes, the question will not contain the character ?
, so the last string for the block declared is NM<> _
instead of NM<>?_
(since the program does not use the space key to generate questions, it will not produce an error).
If your keyboard layout is different from the one shown in this example, please modify the declaration of the kstr
array accordingly.
The first do while
loop is responsible for generating the question.
- The variable
k
indicates which block to generate the question from. Since this value corresponds to the index of the kstr
array, it is set as a random number greater than or equal to 0 and less than KTYPE
.
Since the number of blocks KTYPE
is 16, the generated random number will range from 0 to 15.
- The variable
p
indicates which character within the block should be hidden to generate the question. Since this value corresponds to the index of the string used for generating the question within the block, it is set as a random number greater than or equal to 0 and less than the number of characters within the block.
Assuming k
is 0, the block consists of 5 characters, '12345'
, so p
is set as a random number ranging from 0 to 4. Additionally, if k
is 3, the block consists of 8 characters, '&'()=~|'
, so p
is set as a random number ranging from 0 to 7.
- The variable
key
represents the hidden character.
For example, if k
is 0 and p
is 2, the character '3'
in the block '12345'
is the key
. Since the program has already assigned the space character ''
to characters that should not be used for generating questions, the do-while loop is used to regenerate the question if the hidden character key
is a space character.
Next, the strcpy
function copies kstr[k]
to temp
, and assigns '?' to temp[p]
. This generates the string 12?45
to be displayed on the screen.
If the program is able to display the string temp
and read the character key
input from the keyboard, it is correct. Like the previous typing exercise programs, this program does not accept incorrectly typed characters. After 30 training rounds, the program will end execution.
Compile and run using the following commands:
cd ~/project
gcc -o typing3 typing3.c -lcurses
./typing3
Start typing association exercises.
Please enter the hidden character indicated by '?'.
Press the space key to start.
AS?FG
?m,./\
67890-?\
?XCVB
zx?vb
!"?$%
ZXC?B
hjk?;:]
âĶ(omit)âĶ