循环不会按照预期的方式运行。 [英] Loop doesn't behave the way it's supposed to.

查看:52
本文介绍了循环不会按照预期的方式运行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好,


新年快乐。我现在写的一个程序有点问题

。一个循环不会按照我想要的方式行事(即,

break被忽略)。这是有问题的代码:


------------------------------- ---

star [s] .planet [0] .orbit_distance =

pow(SOLDENSITY / EARTHDENSITY,1.0 / 3.0)* sqrt(star [s] .mass )* get_random(50.0,150.0)/100.0;


for(p = 0; p< orbcount-1; p ++){

x = p + 1 ;

star [s] .planet [x] .orbit_distance =

star [s] .planet [p] .orbit_distance * get_random(1250.0,2500.0)/1000.0;

if(star [s] .planet [p] .orbit_distance> star [s] .orbit_limit)

break;

} //结束


star [s] .number_of_orbits = p;

------------------- ---------------


" orbcount"是int类型的变量,通常具有介于

1和20之间的值。star [s] .orbit_limit"是一个上限(类型为double)。


一般的想法是,在达到极限时,循环结束并且

star [s]。 number_of_orbits"得到p的最后一个值。


我在de.comp.lang.c中问了同样的问题,那里的人们出现了

with这个解决方案:


----------------------------------

ptr = star [s] .planet;

e = ptr + orbcount;

d =

pow(SOLDENSITY / EARTHDENSITY,1.0 / 3.0)* sqrt(star [s] .mass)* get_random(50.0,150.0)/100.0;

while(d< = star [s] .orbit_limit&& ptr< e){

ptr-> orbit_distance = d;

d * = get_random(1250.0,2500.0)/1000.0;

++ ptr;

} //结束时

star [s] .number_of_orbits = ptr-star [s] .planet;

----- -----------------------------


问题是:它不起作用无论是。我犯了同样的错误。为了说明

我的意思是这个程序生成的系统示例:


------------- ------------------

恒星特征1

类型:K3 V

质量:0.689太阳质量

光度:0.225太阳能光量

温度:4518 K

峰值波长:596 nm

直径:1081470 km

重力:31.519 G

寿命:305.81亿年

轮换期:193.5小时

轴向倾角:18.0度。

轨道数:13

轨道极限:4.286 AU< ------这是极限...

生态区

-inner边界:0.463 AU

-outer边界:0.743 AU



行星1出现在:

0.549 AU

1.208 AU

2.257 AU

5.395 AU< ; ----- ......被忽略

11.480 AU

14.862 AU

35.608 AU

51.075 AU

113.629 AU

224.052 AU

445.400 AU

765.494 AU

1840.211 AU

-------------- -----------------


任何人都有意见吗?我使用lcc-win32和gcc,所以它可能不是编译器。


希望得到答案,


问候,

斯蒂芬

Hi folks,

a happy new year. I have a little problem with a program I am writing at
the moment. A loop doesn''t behave the way I''d like to (namely, the
"break" is ignored). This is the code in question:

----------------------------------
star[s].planet[0].orbit_distance =
pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;

for (p=0;p<orbcount-1;p++){
x = p+1;
star[s].planet[x].orbit_distance =
star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
break;
}//end for

star[s].number_of_orbits = p;
----------------------------------

"orbcount" is a variable of type int which normally has a value between
1 and 20. "star[s].orbit_limit" is an upper limit (type double).

The general idea was that upon reaching the limit, the loop ends and
"star[s].number_of_orbits" gets the last value of "p".

I asked the same question in de.comp.lang.c and the folks there came up
with this solution:

----------------------------------
ptr = star[s].planet;
e = ptr+orbcount;
d =
pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0;
while (d<=star[s].orbit_limit&&ptr<e){
ptr->orbit_distance = d;
d *= get_random(1250.0,2500.0)/1000.0;
++ptr;
}//end while
star[s].number_of_orbits = ptr-star[s].planet;
----------------------------------

Problem is: It doesn''t work either. I get the same error. To illustrate
what I mean here is an example of a system generated with this program:

-------------------------------
stellar characteristics of 1
type : K3 V
mass : 0.689 solar mass
luminosity : 0.225 solar lum
temperature : 4518 K
peak wavelength : 596 nm
diameter : 1081470 km
gravity : 31.519 G
lifespan : 30.581 billion years
rotational period : 193.5 hours
axial inclination : 18.0 deg.
number of orbits : 13
orbital limit : 4.286 AU <------ this is the limit...
eco zone
-inner boundary : 0.463 AU
-outer boundary : 0.743 AU
%
planets of 1 present at:
0.549 AU
1.208 AU
2.257 AU
5.395 AU <----- ...that is ignored
11.480 AU
14.862 AU
35.608 AU
51.075 AU
113.629 AU
224.052 AU
445.400 AU
765.494 AU
1840.211 AU
-------------------------------

Any idea anyone? I use lcc-win32 and gcc, so it probably isn''t the compiler.

hoping for answers,

regards,
Stephan

推荐答案

Stephan Aspridis< s。******** @ t-online &由Matchi.com提供回到GT;潦草地写道:
Stephan Aspridis <s.********@t-online.de> scribbled the following:
嗨伙计们,
新年快乐。我现在写的一个程序有点问题。循环不会按照我想要的方式行事(即,
break被忽略)。这是有问题的代码:
----------------------------------
star [s] .planet [0] .orbit_distance =
pow(SOLDENSITY / EARTHDENSITY,1.0 / 3.0)* sqrt(star [s] .mass)* get_random(50.0,150.0)/100.0;
for(p = 0; p< orbcount-1; p ++){
x = p + 1;
star [s] .planet [x] .orbit_distance =
star [s] .planet [p] .orbit_distance * get_random(1250.0,2500.0)/1000.0;
if(star [s] .planet [p] .orbit_distance> star [s] .orbit_limit)
break ;


可能是个愚蠢的问题,因为我懒得计算出

数学算法,但你确定这不是因为你是

分配给star [s] .planet [x] .orbit_distance,但比较

star [s] .planet [p] .orbit_distance?这是两个不同的变量。

} //结束于
star [s] .number_of_orbits = p;
-------------- --------------------
Hi folks, a happy new year. I have a little problem with a program I am writing at
the moment. A loop doesn''t behave the way I''d like to (namely, the
"break" is ignored). This is the code in question: ----------------------------------
star[s].planet[0].orbit_distance =
pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.0)/100.0; for (p=0;p<orbcount-1;p++){
x = p+1;
star[s].planet[x].orbit_distance =
star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
break;
Probably stupid question, since I was too lazy to work out the
mathematical algorithm, but are you sure it isn''t because you are
assigning to star[s].planet[x].orbit_distance, but comparing
star[s].planet[p].orbit_distance? Those are two different variables.
}//end for star[s].number_of_orbits = p;
----------------------------------




-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\- - http://www.helsinki.fi/~palaste ---- -----------------规则! -------- /

更强,没有。黑暗面更具诱惑力,狡猾,更脆弱。

- Mika P. Nieminen



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen


" Joona I Palaste" < PA ***** @ cc.helsinki.fi>在消息中写道

news:bt ********** @ oravannahka.helsinki.fi ...
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bt**********@oravannahka.helsinki.fi...
Stephan Aspridis< s。*** *****@t-online.de>潦草地写道:
Stephan Aspridis <s.********@t-online.de> scribbled the following:
嗨大家好,
新年快乐。我现在写的一个程序有点问题。循环不会按照我想要的方式行事(即,
break被忽略)。这是有问题的代码:
a happy new year. I have a little problem with a program I am writing at
the moment. A loop doesn''t behave the way I''d like to (namely, the
"break" is ignored). This is the code in question:


--------------------------- -------
star [s] .planet [0] .orbit_distance =
----------------------------------
star[s].planet[0].orbit_distance =



pow(SOLDENSITY / EARTHDENSITY,1.0 / 3.0)* sqrt(star [s] .mass)* get_random(50.0,150。

0)/100.0;


pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.
0)/100.0;

for(p = 0; p< orbcount-1; p ++ ){
x = p + 1;
star [s] .planet [x] .orbit_distance =
star [s] .planet [p] .orbit_distance * get_random(1250.0,2500.0) /1000.0;
if(star [s] .planet [p] .orbit_distance> star [s] .orbit_limit)
break;
for (p=0;p<orbcount-1;p++){
x = p+1;
star[s].planet[x].orbit_distance =
star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
break;



可能是愚蠢的问题,因为我懒得研究
数学算法,但你确定这不是因为你分配给星[。] .planet [x] .orbit_distance,而是比较
star [s] .planet [p] .orbit_distance?这是两个不同的变量。



Probably stupid question, since I was too lazy to work out the
mathematical algorithm, but are you sure it isn''t because you are
assigning to star[s].planet[x].orbit_distance, but comparing
star[s].planet[p].orbit_distance? Those are two different variables.




我也注意到了,但这不应该只会导致不必要地计算一个太多的轨道? start [s] .planet [p] .orbit_distance

总是在比较的时刻知道...


Peter



I noticed that too, but shouldn''t that only result in unnecessarily
calculating one too many orbits? start[s].planet[p].orbit_distance
is always known at the moments of the comparison...

Peter


Peter Pichler< pi ***** @ pobox.sk>潦草地写道:
Peter Pichler <pi*****@pobox.sk> scribbled the following:
" Joona I Palaste" < PA ***** @ cc.helsinki.fi>在消息中写道
新闻:bt ********** @ oravannahka.helsinki.fi ...
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bt**********@oravannahka.helsinki.fi...
Stephan Aspridis< s。******* *@t-online.de>潦草地写下:
Stephan Aspridis <s.********@t-online.de> scribbled the following:
>嗨伙计们,
>新年快乐。我在编写的程序中有一点问题
>此时此刻。一个循环不会按照我想要的方式行事(即,
>"break被忽略)。这是有问题的代码:
> a happy new year. I have a little problem with a program I am writing at
> the moment. A loop doesn''t behave the way I''d like to (namely, the
> "break" is ignored). This is the code in question:


> ----------------------------------
> star [s] .planet [0] .orbit_distance =
>
> ----------------------------------
> star[s].planet[0].orbit_distance =
>


pow(SOLDENSITY / EARTHDENSITY,1.0 / 3.0)* sqrt(star [s] .mass) * get_random(50.0,150。
0)/100.0;


pow(SOLDENSITY/EARTHDENSITY,1.0/3.0)*sqrt(star[s].mass)*get_random(50.0,150.
0)/100.0;

> for(p = 0; p< orbcount-1; p ++){
> x = p + 1;
> star [s] .planet [x] .orbit_distance =
> star [s] .planet [p] .orbit_distance * get_random(1250.0,2500.0)/1000.0;
> if(star [s] .planet [p] .orbit_distance> star [s] .orbit_limit)
>打破;
> for (p=0;p<orbcount-1;p++){
> x = p+1;
> star[s].planet[x].orbit_distance =
> star[s].planet[p].orbit_distance*get_random(1250.0,2500.0)/1000.0;
> if (star[s].planet[p].orbit_distance>star[s].orbit_limit)
> break;



可能是个愚蠢的问题,因为我懒得弄清楚
数学算法,但你确定这不是因为你是
分配给星[。] .planet [x] .orbit_distance,但比较了星星[s] .planet [p] .orbit_distance?这是两个不同的变量。



Probably stupid question, since I was too lazy to work out the
mathematical algorithm, but are you sure it isn''t because you are
assigning to star[s].planet[x].orbit_distance, but comparing
star[s].planet[p].orbit_distance? Those are two different variables.



我也注意到了这一点,但这不应该导致不必要地计算一个太多的轨道吗?开始[s] .planet [p] .orbit_distance
总是在比较的时刻知道...


I noticed that too, but shouldn''t that only result in unnecessarily
calculating one too many orbits? start[s].planet[p].orbit_distance
is always known at the moments of the comparison...




你是对的。好吧,*我*可以想象的唯一的事情就是

orbit_distance字段翻转,因此永远不会比orbit_limit字段大于

。由于OP没有向我们展示有问题的

结构的定义,我只是在黑暗中拍摄。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \\ \\

\ - http://www.helsinki。 fi / ~palaste ---------------------规则! -------- /

计算机程序执行你告诉它要做的事情,而不是你想要它做什么。

- Anon



You are right. Well, the only thing *I* can figure is that maybe the
orbit_distance field rolls over, and thus can never become bigger than
the orbit_limit field. As the OP hasn''t shown us the definition of the
structures in question, I''m just shooting in the dark here.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon


这篇关于循环不会按照预期的方式运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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