输入词 [英] input word

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

问题描述

我们已经讨论了很多这个节目。

这个代码只剩下两个问题:


/ *一个用ANSI C编写的函数,它从stdin获取一个字并动态管理内存

*

*因为没有普遍认同的单词定义。我采取了一个非常简单的

*方法:

*

*一个单词是一个连续的非空白字符集合。空格意味着

*任何一个空格,换行符或标签。

*

*

* /

#include< stdio.h>

#include< stdlib.h>

#include< ctype.h>

enum {WORD_SIZE = 28};

enum {GSW_OK,GSW_ENOMEM,GSW_ENORESIZE};

int get_single_word(char **);


int main(无效)

{

char * pword;

while((GSW_OK == get_single_word(& pword))&&(* pword!= 0))

{

printf("您输入:[%s] \ n" ;,pword);

免费(pword);

}

返回0;

}



int get_single_word(char ** ppc)

{

unsigned ele_num = 0;

int ch;

size_t word_length = WORD_SIZE;

size_t word_length_interval = 2;


char * word_begin;

char * new_mem;


* ppc = malloc(word_length * sizeo f(** ppc));

word_begin = * ppc;

if(NULL == ppc)

{

返回GSW_ENOMEM;


}

while((EOF!=(ch = getchar()))&& isspace(ch))

{

继续; / *领先的空白* /

}

if(EOF!= ch)

{

* word_begin ++ = ch;

ele_num = 1;

}

while((EOF!=(ch = getchar()))&&( !isspace(ch)))

{

if((word_length - 1)== ele_num ++)

{

new_mem = realloc(* ppc,(word_length_interval * word_length * sizeof * new_mem));


if(new_mem)

{

word_begin = new_mem +(word_begin - * ppc);

word_length * = word_length_interval;

* ppc = new_mem;

}

其他

{

* word_begin =''\ 0'';

返回GSW_ENORESIZE;

}

}


* word_begin ++ = ch;

}

* word_begin =''\''';


返回GSW_OK;

}


1)get_single_word程序是60行,当有

一般协议,没有功能应该长于40

行。


2)区分真正的EOF和错误。



For (1)我能想到的是将while()循环(从

get_single_word)转换为新函数,该函数需要传递6

变量作为指针(或指针指针)参数:new_mem,

word_begin,word_length,word_length_interval,ppc和ele_num。我认为

将是凌乱的解决方案。

关于(2)我必须使用feof()和ferror()这些很少

令人困惑。


-
www。 lispmachine.wordpress.com

我的电子邮件是@上面的博客。

Gooogle群组被阻止。原因:过度垃圾邮件

解决方案

2008年10月3日星期五11:24:02 +0500,arnuld写道:


我们已经讨论了很多这个程序。只有两个问题仍然存在

此代码:


..SNIP ...


我已经尝试了但它是Segfaults:

#include< stdio.h>

#include< stdlib.h>

#include< ctype.h>

enum {WORD_SIZE = 28};

enum {GSW_OK,GSW_ENOMEM,GSW_ENORESIZE};

int get_single_word(char **);

void save_word(char **,size_t *,size_t *);

int main(void)

{

char * pword;

while((GSW_OK == get_single_word(& pword))&&(* pword!= 0 ))

{

printf("你输入:[%s] \ n",pword);

免费(pword) ;

}

返回0;

}


int get_single_word(char ** ppc)

{

unsigned ele_num = 0;

int ch;

size_t word_length = WORD_SIZE;


char * word_begin;

* ppc = malloc(word_length * sizeof(** ppc));

word_begin = * ppc;

if(NULL == ppc)

{

返回GSW_ENOMEM;


}

while((EOF! =(ch = getchar()))&& isspace(ch))

{

继续; / *领先的空白* /

}

if(EOF!= ch)

{

* word_begin ++ = ch;

ele_num = 1;

}

save_word(& word_begin,& ele_num,& word_length);

* word_begin =''\ 0'';


返回GSW_OK;

}


void save_word(char ** word_begin,size_t * ele_num,size_t * word_length)

{

int ch = EOF;

size_t word_length_interval = 2;

char * new_mem = NULL;


char * pc = * word_begin;

while(( EOF!=(ch = getchar()))&&(!isspace(ch)))

{

if((* word_length - 1)== (* ele_num)++)

{

new_mem = realloc(pc,(word_length_interval *(* word_length)* sizeof * new_mem));


if(new_mem)

{

* word_begin = new_mem +(* word_begin - pc);

(* word_length )* = word_length_interval;

pc = new_mem;

}

其他

{

* word_begin =''\ 0'';

返回;

}

}


** word_begin = ch;

* word_begin ++;
< br $>
}

}

-
www.lispmachine.wordpress.com

我的电子邮件是@上面的博客。

Gooogle网站被阻止。原因:过度垃圾邮件


arnuld写道:


> On Fri,2008年10月3日11:24:02 +0500,arnuld写道:


>我们已经讨论了很多这个程序。
此代码只剩下两个问题:


> .. SNIP ...




我试过了,但它是Segfaults:



您是否用调试器检查了解原因?


-

Ian Collins。


arnuld写道:


> On Fri,2008年10月3日21:18:17 +1300,Ian Collins写道:


>您是否使用调试器检查了解原因?




我以为使用调试器(对于这样的小程序)会杀死一个人对语言的理解,这就是为什么我从来没有使用过任何调试器。



你明白为什么你的程序在不使用调试器的情况下失败了吗?你甚至知道它失败了吗?


再见,Jojo


We have discussed this program a lot. Only two problems have remained with
this code:

/* a function written in ANSI C, that takes a word from stdin and manages the memory dynamically
*
* Since there is no generally agreed definition of what a ''word'' is. I have taken a very simple
* approach:
*
* A word is a contiguous collection of non-whitespace characters. A whitespace means anyone of
* a single apace, a newline or a tab.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
enum { WORD_SIZE = 28 };
enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
int get_single_word( char** );

int main( void )
{
char* pword;
while( (GSW_OK == get_single_word(&pword)) && ( *pword != 0) )
{
printf("You entered: [%s]\n", pword);
free( pword );
}
return 0;
}


int get_single_word( char** ppc )
{
unsigned ele_num = 0;
int ch;
size_t word_length = WORD_SIZE;
size_t word_length_interval = 2;

char* word_begin;
char* new_mem;

*ppc = malloc(word_length * sizeof(**ppc));
word_begin = *ppc;
if( NULL == ppc )
{
return GSW_ENOMEM;

}
while( (EOF != (ch = getchar())) && isspace(ch) )
{
continue; /* Leading whitespace */
}
if( EOF != ch )
{
*word_begin++ = ch;
ele_num = 1;
}
while( (EOF != (ch = getchar())) && (! isspace(ch)) )
{
if( (word_length - 1) == ele_num++ )
{
new_mem = realloc( *ppc, (word_length_interval * word_length * sizeof *new_mem) );

if( new_mem )
{
word_begin = new_mem + (word_begin - *ppc);
word_length *= word_length_interval;
*ppc = new_mem;
}
else
{
*word_begin = ''\0'';
return GSW_ENORESIZE;
}
}

*word_begin++ = ch;
}
*word_begin = ''\0'';

return GSW_OK;
}

1) the get_single_word program is 60 lines long when there is
general agreement that no function should be longer than 40
lines.

2) distinguishing between real EOF and the error.


For (1) all I can think of is to take while() loop (from
get_single_word) out into a new function which will require passing 6
variables as pointer (or pointer to pointer) arguments: new_mem,
word_begin, word_length, word_length_interval, ppc and ele_num. I think
that will be messy solution.
Regarding (2) I have to use feof() and ferror() which are little
confusing.

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Gooogle Groups is Blocked. Reason: Excessive Spamming

解决方案

On Fri, 03 Oct 2008 11:24:02 +0500, arnuld wrote:

We have discussed this program a lot. Only two problems have remained with
this code:

..SNIP...


I have tried it but it Segfaults:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
enum { WORD_SIZE = 28 };
enum { GSW_OK, GSW_ENOMEM, GSW_ENORESIZE } ;
int get_single_word( char** );
void save_word( char**, size_t*, size_t*);
int main( void )
{
char* pword;
while( (GSW_OK == get_single_word(&pword)) && ( *pword != 0) )
{
printf("You entered: [%s]\n", pword);
free( pword );
}
return 0;
}


int get_single_word( char** ppc )
{
unsigned ele_num = 0;
int ch;
size_t word_length = WORD_SIZE;

char* word_begin;

*ppc = malloc(word_length * sizeof(**ppc));
word_begin = *ppc;
if( NULL == ppc )
{
return GSW_ENOMEM;

}
while( (EOF != (ch = getchar())) && isspace(ch) )
{
continue; /* Leading whitespace */
}
if( EOF != ch )
{
*word_begin++ = ch;
ele_num = 1;
}
save_word( &word_begin, &ele_num, &word_length );
*word_begin = ''\0'';

return GSW_OK;
}


void save_word( char** word_begin, size_t* ele_num, size_t* word_length )
{
int ch = EOF;
size_t word_length_interval = 2;
char* new_mem = NULL;

char* pc = *word_begin;
while( (EOF != (ch = getchar())) && (! isspace(ch)) )
{
if( (*word_length - 1) == (*ele_num)++ )
{
new_mem = realloc( pc, (word_length_interval * (*word_length) * sizeof *new_mem) );

if( new_mem )
{
*word_begin = new_mem + (*word_begin - pc);
(*word_length) *= word_length_interval;
pc = new_mem;
}
else
{
*word_begin = ''\0'';
return;
}
}

**word_begin = ch;
*word_begin++;

}
}
--
www.lispmachine.wordpress.com
my email is @ the above blog.
Gooogle Groups is Blocked. Reason: Excessive Spamming


arnuld wrote:

>On Fri, 03 Oct 2008 11:24:02 +0500, arnuld wrote:

>We have discussed this program a lot. Only two problems have remained with
this code:

>..SNIP...



I have tried it but it Segfaults:

Have you checked with a debugger to see why?

--
Ian Collins.


arnuld wrote:

>On Fri, 03 Oct 2008 21:18:17 +1300, Ian Collins wrote:

>Have you checked with a debugger to see why?



I thought using a Debugger (for such small programs) kills one''s
understanding of language, thats why I never used any debugger.

do you understand why your program fails without using debugger? Do you even
know where it fails?

Bye, Jojo


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

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