递归时的Sierpinski代码问题 [英] Sierpinski code problem with recursion

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

问题描述

您好,


我这里有一个递归的Sierpinski代码。

代码是正确的,每行都能正常工作。我希望所有的

他们都可以调用DrawSierpinski函数。但是在这个cae中它只调用

第一个重复函数,并且很快就会停止n = 4。我该怎么办呢

所有8行都得到4次妄想


谢谢

ken

int m = 0;

void

DrawSierpinski(HDC hDC,float a,float b,float c,float d)//再添加

你需要的参数

{

矩形(hDC,((2 * a + c)/ 3.0),((2 * b + d)/ 3.0) ,((a + 2 * c)/

3.0),(b + 2 * d)/ 3.0);


if(m <4)

{

m ++;

DrawSierpinski(hDC,a,b,(2 * a +

c)/ 3.0,(2 * b + d)/ 3.0);

DrawSierpinski(hDC,(2 * a + c)/ 3.0,b,(a + 2 *

c) )/ 3.0,(2 * b + d)/ 3.0);

DrawSierpinski(hDC,(a + 2 * c)/ 3.0,b,c

,( 2 * b + d)/ 3.0);


DrawSierpinski(hDC,a,(2 * b + d)/ 3.0,(2 * a +

c)/ 3.0,(b + 2 * d)/ 3.0);

DrawSierpinski(hDC,(a + 2 * c)/ 3.0,(2 * b + d)/ 3.0,c < b r />
,(b + 2 * d)/ 3.0);


DrawSierpinski(hDC,a,(b + 2 * d)/ 3.0,(2 * a +

c)/ 3.0,d);

DrawSierpinski(hDC,(2 * a + c)/ 3.0,(b + 2 * d)/ 3.0,( a + 2 *

c)/ 3.0,d);

DrawSierpinski(hDC,(a + 2 * c)/ 3.0,(b + 2 * d)/ 3.0 ,c

,d);

}


}

解决方案

* Ken:


int m = 0;




不要使用全局变量。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么会这样这么糟糕吗?

A:热门帖子。

问:usenet和电子邮件中最烦人的是什么?




" Alf P. Steinbach" <人*** @ start.no>在消息中写道

news:42 **************** @ news.individual.net ...

* Ken:


int m = 0;



不要使用全局变量。

-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
答:热门帖子。
问:什么是最烦人的usenet和电子邮件中的东西?




好​​吧,我会试试


谢谢你/>


Ken写道:

int m = 0;
void
DrawSierpinski(HDC hDC,float a,float b,float c,float d)//添加你需要的更多
参数
{
矩形(hDC,((2 * a + c)/ 3.0),((2 * b + d) /()),((a + 2 * c)/
3.0),(b + 2 * d)/ 3.0);

if(m <4)
{
m ++;
DrawSierpinski(hDC,a,b,(2 * a +
c)/ 3.0,(2 * b + d)/ 3.0);
DrawSierpinski(hDC,(2 * a + c)/ 3.0,b,(a + 2 *
c)/ 3.0,(2 * b + d)/ 3.0 );




正如你所指出的那样(可能是因为我实际上并不知道是什么,因为Sirpinski是什么)全局变量。每次递归

" m"在全局范围内递增,而不是在本地递归

上下文。一旦第一次递归完成m,具有值4,

禁止进一步递归。根据您实际需要继续进行

递归的方式,将m设为m。每个函数调用的参数:


if(m <4)

{

++ m;

DrawSierpinski(m,...);

DrawSierpinski(m,...);

}


Tobias

-

重要提示:此电子邮件和附件的内容是保密的

,可能需要法律特权和/或受版权保护。

禁止将其任何部分复制或传播给他人,并且可能是非法的。


Hello,

I have a recursive Sierpinski code here.
The code is right and every line works fine by itself. I wish for all of
them to call the function DrawSierpinski. But in this cae it only calls the
first recusive function and at soon as n = 4 its stop. How do I go about so
that all 8 lines get 4 recusion

thanks
ken
int m= 0;
void
DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more
parameters you need
{
Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) /
3.0), (b + 2 * d) / 3.0);

if (m< 4)
{
m++;
DrawSierpinski(hDC, a , b , (2 * a +
c) / 3.0, (2 * b + d) / 3.0);
DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 *
c) / 3.0, (2 * b + d) / 3.0);
DrawSierpinski(hDC, (a + 2 * c) / 3.0, b , c
, (2 * b + d) / 3.0);

DrawSierpinski(hDC, a , (2 * b + d) / 3.0, (2 * a +
c) / 3.0, (b + 2 * d) / 3.0);
DrawSierpinski( hDC,(a + 2 * c) / 3.0, (2 * b + d) / 3.0, c
, (b + 2 * d) / 3.0);

DrawSierpinski(hDC, a , (b + 2 * d) / 3.0, (2 * a +
c) / 3.0, d );
DrawSierpinski(hDC, (2 * a + c) / 3.0, (b + 2 * d) / 3.0, (a + 2 *
c) / 3.0, d );
DrawSierpinski(hDC, (a + 2 * c) / 3.0, (b + 2 * d) / 3.0, c
, d );
}

}

解决方案

* Ken:


int m= 0;



Don''t use globals.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Alf P. Steinbach" <al***@start.no> wrote in message
news:42****************@news.individual.net...

* Ken:


int m= 0;



Don''t use globals.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



HUmm ok, I''ll try that

thank you


Ken wrote:

int m= 0;
void
DrawSierpinski(HDC hDC, float a, float b, float c, float d) // add any more
parameters you need
{
Rectangle( hDC, ((2 * a + c) / 3.0) , ((2 * b + d) / 3.0), ((a + 2 * c) /
3.0), (b + 2 * d) / 3.0);

if (m< 4)
{
m++;
DrawSierpinski(hDC, a , b , (2 * a +
c) / 3.0, (2 * b + d) / 3.0);
DrawSierpinski( hDC,(2 * a + c) / 3.0, b , (a + 2 *
c) / 3.0, (2 * b + d) / 3.0);



As pointed out your problem is (probably, since I actually don''t know
what Sirpinski is) the global variable. With each recursion
"m" is being incremented at a global context, not at a local recursive
context. Once the first recursion is done "m" has the value 4,
prohibiting further recursions. Depending on how you actually want the
recursions to proceed, make "m" an argument with each function call:

if( m < 4 )
{
++m;
DrawSierpinski( m, ... );
DrawSierpinski( m, ... );
}

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.


这篇关于递归时的Sierpinski代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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