【发布时间】:2014-12-26 05:06:54
【问题描述】:
我正在尝试在 Windows 命令提示符下用 C 语言编写一个程序,我可以用它来练习触摸打字。我想通过让程序提示输入一个字母来做到这一点,一旦我输入了一个字母,我希望它记录那个字母是否正确,并在退出之前重复这个过程预定的次数并告诉我我的时间和准确性。 通过在每个字母之间按回车键使其工作很容易,但我觉得它不会像我不必按回车键那样有用。我在大学做了一个项目,它有一个类似的组件,但那是在 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);
如果我可以使用 C 在 Windows 的命令提示符下制作具有相同功能的东西,那就太好了,但人们一直在说“C 语言中不可能有可移植的解决方案”。我不介意它是否只在这台电脑上工作,我只是希望它工作。
我想到的另一个想法是找到一个反复刷新键盘缓冲区的函数,但仍然可以看到它刷新了什么。那么,只要速度够快,反正最多只能有一个字符。有这个功能吗?显然它可以使用 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;
}
【问题讨论】:
-
不看你的大部分帖子,建议
scanf("% c", &input);(加空格)。 -
原始代码是这样做的,我什至在它之后有 getchar() 从缓冲区中清除输入字符。否则,它将“B[enter key]”解释为 2 个输入,并将 enter 键作为我对下一个提示的回答提交。
-
没有验证以下
getchar()消耗了'\n'或其他东西 - 这是一个猜测。 -
scanf("%c", &input) 函数可以完成这项工作,但问题是它需要按 Enter 键,我正在尝试解决此问题
标签: c windows command-line keyboard