我正在尝试用Windows命令提示符编写一个程序,我可以用来练习触摸打字。我希望通过程序提示输入一封信来做到这一点,一旦我输入了一封信,我希望它能够记录这封信是否正确并在退出之前重复预定次数并告诉我时间和准确性。 通过在每个字母之间按下输入来使其工作很容易,但我觉得它不会像我没有按下输入那样有用。我在大学做了一个类似组件的项目,但那是在Linux中使用C ++。我不想为整个程序设置一个虚拟盒子等。
//The linux program included something like this:
//collecting original structure formate
tcgetattr(STDIN_FILENO, &normTerm);
//assigning the original structure format to the temporary structure format
tempTerm = normTerm;
//making the temporary structure format into raw form
cfmakeraw(&tempTerm);
//setting the structure format to the raw form
tcsetattr(STDIN_FILENO, TCSANOW, &tempTerm);
//cfmakeraw() turns the structure at the address into the raw terminal attributes desired
//insert function that asks the user to input their password with all the flags changed so that you can't read what you have typed
tcsetattr(STDIN_FILENO, TCSANOW, &normTerm);
如果我可以在Windows中使用C在命令提示符上创建具有相同功能的内容,那将是很好的,但是人们一直在说#34;在C"中无法实现便携式解决方案。我不介意它只适用于这台PC,我只是想让它起作用。
我想到的另一个想法是找到一个重复刷新键盘缓冲区的函数,但仍然可以看到它已刷新的内容。然后,只要它足够快,它最多只能包含一个字符。这个函数是否存在?显然它可以用conio.h库来完成,但据说这是来自DOS并且不适用于现代系统(拒绝在我的工作中工作)。
The code I have written so far is below
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <termios.h>
void main()
{
//seeding random number generator
srand(time(NULL));
//char variables for actual and desired user input
char input = 0;
char testCharacter = 1;
//float variables for calculating accuracy
float countCorrect = 0;
float countTotal = 5;
float accuracy = 0;
//temp variables for program operations
int iterations = int(countTotal);
int characterIndex = 0;
long startTime = time(NULL);
while(iterations>0)
{
//I am aware of the asymmetry of this, I might get around to fixing it latter
characterIndex = (rand() %52) + 1;
//printf("Value returned by random num gen is %d\n", characterIndex);
//The following is messy because I don't use all the ascii characters
//I could also probably just write a number to the char variable and then treat it as a letter to do away with the switch case statements, but I will look into that latter
characterIndex += 64;
if(characterIndex >= 91)
{
characterIndex = characterIndex + 7;
}
//switch case statements go here
printf("Please type the letter below:\n%c\n", testCharacter);
//%$&$&%*&)()*)&^%&$^&(*)_(*^&$%#^&$^%^*(&)*)(_)_*&^$%^#$^$&*(&)*(*&(^
//This is the bit I want to modify to not require the enter key
scanf("%c", &input);
getchar();
//something like
while(keyboard buffer is empty)
{
}
flush keyboard into &input
//maybe I could use a shell command to manually press enter whenever the keyboard buffer isn't empty???
//(*()%&$^#$%$&^(*)*&(^$%#%$&^(*)*)&^($%&&^*(&)&*&(^$*(&)*&^($&(***&^$%*^&
printf("\n");
//keeps track of correct answers
if(input == testCharacter)
{
countCorrect++;
//printf("The letter %c was typed and was correct\n", input);
}
else
{
//printf("The letter %c was typed and was incorrect\n", input);
}
iterations = iterations - 1;
}
//calculates time difference in seconds
long timeDifference = time(NULL) - startTime;
//calculates accuracy
accuracy = 100.0 * (countCorrect / countTotal);
printf("Accuracy achieve was %f%\nThe time taken was %d seconds\nPress any key to continue: ", accuracy, timeDifference);
scanf("%c", &input);
return 0;
}
答案 0 :(得分:2)
this怎么样?基本上,您将控制台重置为无缓冲,其中没有换行,没有回声,这会阻止角色出现。要回显,请将SetConsoleMode更改为ENABLE_ECHO_INPUT而不是0.一旦您想要正常输入,就可以重置控制台模式,这是最后一行。
#include <windows.h>
#include <stdio.h>
int main()
{
DWORD mode;
HANDLE hstdin;
hstdin = GetStdHandle( STD_INPUT_HANDLE );
GetConsoleMode( hstdin, &mode );
SetConsoleMode( hstdin, 0 );
char result;
printf("Press x to exit");
while(1){
result = getchar();
if(result == 'x')
break;
}
SetConsoleMode(hstdin, mode);
return 0;
}