K& R2练习4-12 [英] K&R2 Exercise 4-12

查看:68
本文介绍了K& R2练习4-12的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次尝试解决K& R2练习4-12:


void itoa(int n,char * s)

{


if(n< 0)

{

* s ++ ='' - '';

n = -n;

}


if(n / 10 0)

itoa(n / 10, s);


* s ++ = n%10 +''0'';

* s = 0;

}

我意识到为什么我的代码不能正常工作,这是因为当递归调用放松时,增量

不会继续。


我可以这样解决:

void itoa2(int n,char * s)

{

静态字符* p = 0;


if(p == 0)

p = s;


if(n< 0)

{

* p ++ ='' - '';

n = -n;

}


if(n / 10 0)

itoa2(n / 10,p);


* p ++ = n%10 +''0'';

* p = 0;

}

保持p指针向上 - 在递归调用放松的时候。


我的问题是,有没有人知道一个不需要的解决方案

引入静态持续变量?我的第一次尝试就像是一个小小的改变吗?

Here is my 1st attempt at the solution for K&R2 Exercise 4-12:

void itoa(int n, char *s)
{

if (n < 0)
{
*s++ = ''-'';
n = -n;
}

if( n/10 0 )
itoa( n/10, s );

*s++ = n%10 + ''0'';
*s = 0;
}
I realize why my code is not working, it''s because the increments
don''t carry forward when the recursive calls "unwind."

I can solve this this way:
void itoa2(int n, char *s)
{
static char *p = 0;

if(p == 0)
p = s;

if (n < 0)
{
*p++ = ''-'';
n = -n;
}

if( n/10 0 )
itoa2( n/10, p );

*p++ = n%10 + ''0'';
*p = 0;
}
This keeps the p pointer up-to-date while the recursive calls unwind.

My quesiton is, does anyone know of a solution that doesn''t require
the introduction of a static duration variable? Something like a small
modifcation to my first attempt?

推荐答案

Lax说:
Lax said:

这是我第一次尝试解决K& R2练习4-12:


void itoa(int n,char * s)

{


if(n< 0)

{

* s ++ ='' - '' ;

n = -n;

}


if(n / 10 0)

itoa (n / 10,s);


* s ++ = n%10 +''0'';

* s = 0;

}


我知道为什么我的代码不能正常工作,这是因为增量

在递归时不能继续电话放松。
Here is my 1st attempt at the solution for K&R2 Exercise 4-12:

void itoa(int n, char *s)
{

if (n < 0)
{
*s++ = ''-'';
n = -n;
}

if( n/10 0 )
itoa( n/10, s );

*s++ = n%10 + ''0'';
*s = 0;
}
I realize why my code is not working, it''s because the increments
don''t carry forward when the recursive calls "unwind."



在一个真正的itoa中,你需要采取最大长度,以降低

缓冲区溢出的风险。以下问题解决了我的问题,我认为(虽然我没有实际测试过它)。


void itoasub(int n,char ** s )

{

if(n / 10 0)

{

itoasub(n / 10,s) ;

}

** s = n%10 +''0'';

++ * s;

}

void itoa(int n,char * s)

{

if(n< 0)

{

* s ++ ='' - '';

n = -n; / *注意INT_MIN! * /

}

itoasub(n,& s);

* s = 0;

}


-

Richard Heathfield< http://www.cpax.org.uk>

电邮:-http ://万维网。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

In a real itoa, you''d want to take a maximum length, to reduce the risk of
buffer overruns. The following fixes your problem, I think (although I
haven''t actually tested it).

void itoasub(int n, char **s)
{
if(n / 10 0)
{
itoasub(n / 10, s);
}
**s = n % 10 + ''0'';
++*s;
}
void itoa(int n, char *s)
{
if(n < 0)
{
*s++ = ''-'';
n = -n; /* beware INT_MIN! */
}
itoasub(n, &s);
*s = 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




" Lax" < La ******** @ gmail.comwrote in message

news:17 ********************** ************ @ k37g2000 hsf.googlegroups.com ...

"Lax" <La********@gmail.comwrote in message
news:17**********************************@k37g2000 hsf.googlegroups.com...

这是我对K& R2解决方案的第一次尝试练习4-12:


void itoa(int n,char * s)

{


if( n< 0)

{

* s ++ ='' - '';

n = -n;

}


if(n / 10 0)

itoa(n / 10,s);


* s ++ = n%10 +''0'';

* s = 0;

}
Here is my 1st attempt at the solution for K&R2 Exercise 4-12:

void itoa(int n, char *s)
{

if (n < 0)
{
*s++ = ''-'';
n = -n;
}

if( n/10 0 )
itoa( n/10, s );

*s++ = n%10 + ''0'';
*s = 0;
}



试试这个;它使用一个局部变量,并且调用strlen很多,所以稍微慢一点:b
$ b void itoa(int n,char * s)

{int len;


* s = 0;


if(n< 0)

{

* s ++ ='' - '';

n = -n;

}

if(n> = 10)

itoa(n / 10,s);

len = strlen(s);

s [len] = n%10 +''0'';

s [len + 1] = 0;

}


-

Bart

Try this; it uses a local variable, and calls strlen a lot, so is a bit
slower:

void itoa(int n, char *s)
{int len;

*s=0;

if (n < 0)
{
*s++ = ''-'';
n = -n;
}

if( n>=10 )
itoa( n/10, s );
len=strlen(s);
s[len]= n%10 + ''0'';
s[len+1]=0;
}

--
Bart


Lax< La ******** @ gmail.comwrites:
Lax <La********@gmail.comwrites:

这是我第一次尝试解决K& R2练习4-12:
Here is my 1st attempt at the solution for K&R2 Exercise 4-12:



< snip not quite溶液>

<snip not quite solution>


我可以这样解决这个问题:

void itoa2(int n,char * s)

{

静态字符* p = 0;


if(p == 0)

p = s;


if(n< 0)

{

* p ++ ='' - '';

n = -n;
I can solve this this way:

void itoa2(int n, char *s)
{
static char *p = 0;

if(p == 0)
p = s;

if (n < 0)
{
*p++ = ''-'';
n = -n;



小点:在C -n中可以溢出。我相信K& R并不打算在这里处理这个问题,但真正的itoa必须这样做。

Small point: in C -n can overflow. I am sure that K&R don''t intend
that you deal with this here, but a real itoa would have to.


}


if(n / 10 0)

itoa2(n / 10,p);


* p ++ = n%10 +''0'';

* p = 0;

}


这保留了递归调用放松时p指针是最新的。


我的问题是,有没有人知道一个不需要的解决方案

引入静态持续时间变量?我的第一次尝试就像一个小的改变?b $ b修改?
}

if( n/10 0 )
itoa2( n/10, p );

*p++ = n%10 + ''0'';
*p = 0;
}
This keeps the p pointer up-to-date while the recursive calls unwind.

My quesiton is, does anyone know of a solution that doesn''t require
the introduction of a static duration variable? Something like a small
modifcation to my first attempt?



嗯,不是一个调整,而是一个普遍的想法:递归特别适用于具有简单值参数和返回有用的函数的b $ b />
结果。事实上,这种模式可以发展成一个完整的方法论。

编程。


由于itoa的界面是固定的,你需要一个辅助函数。

技巧正在考虑你能想象到的最有用的功能。经常

这个函数可以用自己的方式编写,所需的

函数变成了一个简单的调用它有点额外的

房子-保持。例如:


给定''char * aux_itoa(int n,char * s);''取正值''n'',

将''n''的字符表示形式放入''s''并返回一个指针

到该表示的结尾,你可以写itoa还是

aux_itoa?


-

Ben。

Well, not a tweak but a general idea: recursion works particularly
well with functions that have simple value parameters and return useful
results. In fact, that pattern can be grown into a whole methodology
of programming.

Since the interface of itoa is fixed, you need a helper function. The
trick is thinking up the most helpful function you can imagine. Often
that function can then be written in terms of itself and the desired
function becomes a simple call of it with a little extra
house-keeping. For example:

Given ''char *aux_itoa(int n, char *s);'' that takes a positive ''n'',
puts the character representation of ''n'' into ''s'' and returns a pointer
to the end of that representation, could you write both itoa and also
aux_itoa?

--
Ben.


这篇关于K&amp; R2练习4-12的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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