强制处理输入的代码 [英] Robustify code dealing with input

查看:59
本文介绍了强制处理输入的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请考虑以下完整程序:


#include< assert.h>

#include< ctype.h>

#include< stdlib.h>

#include< stdio.h>

#include< string.h>

#include< time.h>


static int has_char(const char *,const char);

static void handle_guess(const char * ,char *,const char);


int

main()

{

const char * words [] =

{

" hello"," usenet"," breakfast"

};

const char * actual = NULL;

char * current = NULL;


srand(time(NULL));


actual = words [rand()%3];

current = calloc(strlen(actual)+ 1,sizeof(char));

断言(当前);

memset(当前,'''',strlen(实际));


而(1)

{

char c =''\ 0'';


if(!has_char(current,''?''))

break;


printf("这个词是:%s \ n" ,当前);


printf("键入一个字母:");


c = getc(stdin);

getc(stdin);


handle_guess(实际,当前,tolower(c));

}


printf(恭喜!这个词是:%s \ n",current);


免费(当前);


返回EXIT_SUCCESS;

}


static int

has_char(const char * str,const char c)

{

unsigned short i = 0;


for(i = 0; i< strlen(str); ++ i)

if (str [i] == c)

返回1;


返回0;

}


static int

find_position(const char * str,const char c,const unsigned short start)

{

unsigned short i = start;


if(start> strlen(str))

返回-1;


for(i = start; i< strlen(str); ++ i)

if(str [i] == c)

return i;


返回-1;

}


static void

handle_guess(const char * actual,char * current,const char c)

{

int position = 0;


assert(strlen(actual) == strlen(current));


if( (position = find_position(actual,c,0))== -1)

{

printf("字母%c不匹配,抱歉。\ n,c);


返回;

}


if(当前[位置]!=' '?'')

{

printf(你已经猜到了字母%c \ n,c);


返回;

}


当前[position] = c;


while((position = find_position(actual,c,position + 1))!= -1)

{

断言(当前[位置] ==''?'');


当前[位置] = c;

}

}

它维护一个单词列表和

程序启动时,从列表中随机选择一个单词。这个单词被打印到屏幕上,但所有

字符都被替换为''?''。用户被要求在单词中猜出一个

字母,如果他猜对了''?'隐藏了那个

特定字母被字母替换了本身。这一直持续到

整个单词已被公布。


我的问题是获取用户输入的代码不是很强大。

假设用户按下< tab> the_letter< enter>,它就不再工作了。

无论我接下来输入什么,传递给handle_guess()的字符都是混乱来自这个会话的屏幕转储:

$ ./guess_word.exe

这个词是:?? ???????

键入一封信:b

这个词是:b ????????

类型一封信:a

这个词是:b ?? a ?? a ??

键入一个字母:s< ----这里我输入了< tab> ; s< Enter>

这封信不匹配,抱歉。

这个词是:b ?? a ?? a ??

输入一封信:i

这封信不匹配

,对不起。

这个词是:b ?? a ?? a ??

输入一封信:t

这封信不匹配

,抱歉。


As你可以看到,由于< tab>而它仍然处于破碎状态。

如何改进处理用户输入的代码以便我可以解决这个问题?
问题?


欢迎任何有关代码的其他评论。


/ E

Hello, consider the following complete program:

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

static int has_char(const char *, const char);
static void handle_guess(const char *, char *, const char);

int
main()
{
const char * words[] =
{
"hello", "usenet", "breakfast"
};
const char *actual = NULL;
char *current = NULL;

srand(time(NULL));

actual = words[rand() % 3];
current = calloc(strlen(actual) + 1, sizeof(char));
assert(current);
memset(current, ''?'', strlen(actual));

while(1)
{
char c = ''\0'';

if(!has_char(current, ''?''))
break;

printf("The word is: %s\n", current);

printf("Type a letter: ");

c = getc(stdin);
getc(stdin);

handle_guess(actual, current, tolower(c));
}

printf("Congratulations! The word was: %s\n", current);

free(current);

return EXIT_SUCCESS;
}

static int
has_char(const char *str, const char c)
{
unsigned short i = 0;

for(i = 0; i < strlen(str); ++i)
if(str[i] == c)
return 1;

return 0;
}

static int
find_position(const char *str, const char c, const unsigned short start)
{
unsigned short i = start;

if(start > strlen(str))
return -1;

for(i = start; i < strlen(str); ++i)
if(str[i] == c)
return i;

return -1;
}

static void
handle_guess(const char *actual, char *current, const char c)
{
int position = 0;

assert(strlen(actual) == strlen(current));

if((position = find_position(actual, c, 0)) == -1)
{
printf("No match for the letter %c, sorry.\n", c);

return;
}

if(current[position] != ''?'')
{
printf("You already guessed the letter %c\n", c);

return;
}

current[position] = c;

while((position = find_position(actual, c, position + 1)) != -1)
{
assert(current[position] == ''?'');

current[position] = c;
}
}
It maintains a list of words and randomly selects a word from the list when
the program is started. The word is printed to the screen, but all
characters have been replaced with a ''?''. The user is asked to guess a
letter in the word, and he if he guesses correctly the ''?''s hiding that
particular letter is replaced with the letter itself. This continues until
the entire word has been revealed.

My problem is that the code for obtaining user input isn''t very robust.
Say the user presses <tab>the_letter<enter>, it doesn''t work anymore.
Whatever I type next, the character passed to handle_guess() is "messed up".
Here''s a screen dump from such a session:
$ ./guess_word.exe
The word is: ?????????
Type a letter: b
The word is: b????????
Type a letter: a
The word is: b??a??a??
Type a letter: s <---- Here I typed <tab>s<Enter>
No match for the letter , sorry.
The word is: b??a??a??
Type a letter: i
No match for the letter
, sorry.
The word is: b??a??a??
Type a letter: t
No match for the letter
, sorry.

As you can see, it remains in a broken state because of that <tab>.
How can improve the code for dealing with user input so I can counter this
problem?

Any other comments regarding the code are welcome also.

/ E

推荐答案

./ guess_word.exe

这个词是:?????????

输入一封信:b

这个词是:b ????????

键入一个字母:a

这个词是:b ?? a ?? a ??

键入一个字母:s< ----这里我输入< tab> s< Enter>

这封信不匹配,抱歉。

这个词是:b ?? a ?? a ??

键入一封信:i

不匹配的信件

,抱歉。

这个词是:b ?? a ?? a ??

键入一封信:t

不匹配的字母

,对不起。


正如你所看到的那样,它因为< tab>而仍处于破碎状态。

如何可以改进处理代码用户输入所以我可以反击这个

问题?


欢迎任何有关代码的其他评论。


/ E
./guess_word.exe
The word is: ?????????
Type a letter: b
The word is: b????????
Type a letter: a
The word is: b??a??a??
Type a letter: s <---- Here I typed <tab>s<Enter>
No match for the letter , sorry.
The word is: b??a??a??
Type a letter: i
No match for the letter
, sorry.
The word is: b??a??a??
Type a letter: t
No match for the letter
, sorry.

As you can see, it remains in a broken state because of that <tab>.
How can improve the code for dealing with user input so I can counter this
problem?

Any other comments regarding the code are welcome also.

/ E


Eric Lilja写道:
Eric Lilja wrote:

您好,请考虑以下完整程序:

#include< assert.h>
#include< ctype.h>
#include< stdlib.h>
#include< stdio.h>
#include < string.h>
#include< time.h>

static int has_char(const char *,const char);
static void handle_guess(const char *, char *,const char);

int
main()
{
const char * words [] =
{
"你好",usenet",早餐
};
const char * actual = NULL;
char * current = NULL;

srand(time (NULL));

actual = words [rand()%3];
current = calloc(strlen(actual)+ 1,sizeof(char));
asse rt(当前);
memset(当前,'''',strlen(实际));

while(1)
{
char c =' '\0'';

if(!has_char(current,''?''))
break;

printf("这个词是:%s \ n",current);

printf("键入一个字母:");

c = getc(stdin);
getc(stdin);

handle_guess(实际,当前,tolower(c));


printf("祝贺!这个词是:%s \ n",current);

免费(当前);

返回EXIT_SUCCESS;
}
static int
has_char(const char * str,const char c)
{
unsigned short i = 0;

for(i = 0; i< strlen(str); ++ i)
if(str [i] == c)
返回1;

返回0;
}

static int
find_position(const char * str,const char c,const unsigned short start)
{
unsigned short i = start;

如果(开始> strlen(str))
返回-1;

for(i = start; i< strlen(str); ++ i)
if(str) [i] == c)
返回i;

返回-1;
}
静态void
handle_guess(const char * actual,char * current,const char c)
{
int position = 0;

assert(strlen(actual)== strlen(current));

if((position = find_position(actual,c,0))== -1)
{
printf(" N o匹配字母%c,抱歉。\ n",c);

返回;
}
如果(当前[位置]!=' '?'')
{
printf(你已经猜到了字母%c \ n,c);

返回;
}

当前[位置] = c;

而((position = find_position(actual,c,position + 1))!= -1)
{
断言(当前[位置] ==''?'');

当前[位置] = c;
}
}

它保持一个单词列表,并在程序启动时从列表中随机选择一个单词。该字打印在屏幕上,但所有
字符都替换为''?''。要求用户在单词中猜出一个
字母,如果他猜对了?隐藏了那个特定的字母被字母本身替换了。这一直持续到整个单词被揭示出来。

我的问题是获取用户输入的代码不是很强大。
说用户按下< tab> ; the_letter< enter>,它不再起作用了。
无论我接下来输入什么,传递给handle_guess()的字符都是搞砸了。
这里是来自这样的屏幕转储会话:

Hello, consider the following complete program:

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

static int has_char(const char *, const char);
static void handle_guess(const char *, char *, const char);

int
main()
{
const char * words[] =
{
"hello", "usenet", "breakfast"
};
const char *actual = NULL;
char *current = NULL;

srand(time(NULL));

actual = words[rand() % 3];
current = calloc(strlen(actual) + 1, sizeof(char));
assert(current);
memset(current, ''?'', strlen(actual));

while(1)
{
char c = ''\0'';

if(!has_char(current, ''?''))
break;

printf("The word is: %s\n", current);

printf("Type a letter: ");

c = getc(stdin);
getc(stdin);

handle_guess(actual, current, tolower(c));
}

printf("Congratulations! The word was: %s\n", current);

free(current);

return EXIT_SUCCESS;
}

static int
has_char(const char *str, const char c)
{
unsigned short i = 0;

for(i = 0; i < strlen(str); ++i)
if(str[i] == c)
return 1;

return 0;
}

static int
find_position(const char *str, const char c, const unsigned short start)
{
unsigned short i = start;

if(start > strlen(str))
return -1;

for(i = start; i < strlen(str); ++i)
if(str[i] == c)
return i;

return -1;
}

static void
handle_guess(const char *actual, char *current, const char c)
{
int position = 0;

assert(strlen(actual) == strlen(current));

if((position = find_position(actual, c, 0)) == -1)
{
printf("No match for the letter %c, sorry.\n", c);

return;
}

if(current[position] != ''?'')
{
printf("You already guessed the letter %c\n", c);

return;
}

current[position] = c;

while((position = find_position(actual, c, position + 1)) != -1)
{
assert(current[position] == ''?'');

current[position] = c;
}
}

It maintains a list of words and randomly selects a word from the list when
the program is started. The word is printed to the screen, but all
characters have been replaced with a ''?''. The user is asked to guess a
letter in the word, and he if he guesses correctly the ''?''s hiding that
particular letter is replaced with the letter itself. This continues until
the entire word has been revealed.

My problem is that the code for obtaining user input isn''t very robust.
Say the user presses <tab>the_letter<enter>, it doesn''t work anymore.
Whatever I type next, the character passed to handle_guess() is "messed up".
Here''s a screen dump from such a session:


./ guess_word.exe
这个词是:?????????
输入一封信:b 键入一封信:a
这个词是:b ?? a ?? a ??
键入一封信:s < ----在这里我输入了< tab> s< Enter>
这封信不匹配,抱歉。
这个词是:b ?? a ?? a ??
输入一封信:i
这封信不匹配
,抱歉。
这个词是:b ?? a ?? a ??
键入一封信:t
不相信这封信
,对不起。

正如你所看到的那样,它因为< tab>而仍处于破碎状态。
如何改进用于处理用户输入的代码,以便我可以解决这个问题吗?

关于代码的任何其他意见也欢迎。

/ E
./guess_word.exe
The word is: ?????????
Type a letter: b
The word is: b????????
Type a letter: a
The word is: b??a??a??
Type a letter: s <---- Here I typed <tab>s<Enter>
No match for the letter , sorry.
The word is: b??a??a??
Type a letter: i
No match for the letter
, sorry.
The word is: b??a??a??
Type a letter: t
No match for the letter
, sorry.

As you can see, it remains in a broken state because of that <tab>.
How can improve the code for dealing with user input so I can counter this
problem?

Any other comments regarding the code are welcome also.

/ E




/ * BEGIN new.c * /


#include< ctype.h>

#include< ; stdlib.h>

#include< stdio.h>

#include< string.h>

#include< time .h>


static void

handle_guess(const char *,char *,const int);


int

main(无效)

{

const char * words [] = {

" hello" ,usenet,早餐

};

const char * actual = NULL;

char * current = NULL;


srand(time(NULL));

actual = words [rand()%3];

current = calloc( strlen(actual)+ 1,sizeof(char));

if(current == NULL){

exit(EXIT_FAILURE);

}

memset(当前,''''',strlen(当前l));

while(strchr(当前,''?'')!= NULL){

int c;


printf(" The word is:%s \ n",current);

printf(" Type a letter:");

fflush( stdout);

做{

c = getc(stdin);

} while(!isalpha(c));

getc(stdin);

handle_guess(实际,当前,tolower(c));

}

printf("祝贺) !这个词是:%s \ n",current);

免费(当前);

返回EXIT_SUCCESS;

}


static void

handle_guess(const char * actual,char * current,const int c)

{

char * position;


position = strchr(actual,c);

if(position == NULL){

printf(不匹配字母%c,抱歉。\ n,c);

返回;

}

如果(当前[位置 - 实际]!=''?''){

printf(你已经猜到了字母%c \ n,c);

返回;

}

做{

当前[position - actual] =(char)c;

position = strchr(position + 1,c);

} while(position!= NULL);

}


/ * END new.c * /


-

pete



/* BEGIN new.c */

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

static void
handle_guess(const char *, char *, const int);

int
main(void)
{
const char * words[] = {
"hello", "usenet", "breakfast"
};
const char *actual = NULL;
char *current = NULL;

srand(time(NULL));
actual = words[rand() % 3];
current = calloc(strlen(actual) + 1, sizeof(char));
if (current == NULL) {
exit(EXIT_FAILURE);
}
memset(current, ''?'', strlen(actual));
while (strchr(current, ''?'') != NULL) {
int c;

printf("The word is: %s\n", current);
printf("Type a letter: ");
fflush(stdout);
do {
c = getc(stdin);
} while (!isalpha(c));
getc(stdin);
handle_guess(actual, current, tolower(c));
}
printf("Congratulations! The word was: %s\n", current);
free(current);
return EXIT_SUCCESS;
}

static void
handle_guess(const char *actual, char *current, const int c)
{
char *position;

position = strchr(actual, c);
if (position == NULL) {
printf("No match for the letter %c, sorry.\n", c);
return;
}
if (current[position - actual] != ''?'') {
printf("You already guessed the letter %c\n", c);
return;
}
do {
current[position - actual] = (char)c;
position = strchr(position + 1, c);
} while (position != NULL);
}

/* END new.c */

--
pete


这篇关于强制处理输入的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆