为什么if语句不能正常工作[c ++] [英] Why if statement does not work correctly [c++]

查看:100
本文介绍了为什么if语句不能正常工作[c ++]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..我正在开发一个我正在创建的c ++模拟,这让我很烦恼,我无法解决它...

我的问题是IF和ELSE似乎有时不起作用,因为如果它正常工作就一定没有错......

这是我的代码:

 < span class =code-keyword> for (NC =  1 ; NC< = INN; NC ++){
RRQLC = 0 ;
for (RRC = 1 ; RRC< = INN; RRC ++){
if (NC!= RRC){
if (node [NC] .x- node [RRC] .x< node [NC] .RADIO_RANGE&& node [NC] .x-node [RRC] .x> node [NC] .RADIO_RANGE-(node [NC] .RADIO_RANGE * 2 )){
node [NC] .RRQ ++;
node [NC] .RRQL.push_back(RRC);
if (abs(node [NC] .x-node [RRC] .x)> 0 ){
ENUM ++;
node [NC] .E_NODE.push_back(node [RRC] .id);
SO<< RRC<< \ nNode<<节点[NC] .id<< E_NODE<<节点[NC] .E_NODE [RRQLC]<< \ n;
} else {
ANUM ++;
node [NC] .A_NODE.push_back(node [RRC] .id);
SO<< RRC<< \ nNode<<节点[NC] .id<< A_NODE<<节点[NC] .A_NODE [RRQLC]<< \ n;
}

node [NC] .SPACE.push_back(abs(node [NC] .x-node [RRC] .x));
SO<< 节点<< RRC<< 位于节点<<的RADIO_RANGE中NC<< #\ nRRQL<<节点[NC] .RRQL [RRQLC]<< \ n;
RRQLC ++;
printf( 节点%d位于节点%d \ n的RADIO_RANGE中,节点[RRC] .ID,节点[NC] .ID);
// sqrt(pow((node [NC] .x-node [RRC] .x), 2)+ pow((节点[NC] .y-node [RRC] .y),2));
}
}
}
if (node [NC] .RRQ< 1 ){
NONE_NODE.push_back(node [NC] ]。ID);
NBR ++;
}
printf( %d \ n,节点[NC ] .RRQ);
}





最里面的'if'和'else'剂量在某个时候不起作用,因为如果这样做会推动将一个数字返回到E_NODE或A_NODE但没有一个正常工作...当我在文件上打印它时它显示错误的值,因为'if'和'else'不起作用并且它没有推回任何值向量,当最外面的FOR更改它打印的值不存在时,它会显示一些内存值,如825241632

我在谈论E_NODE [RRQLC]和A_NODE [RRQLC]

请帮我解决这个问题,如果你有任何想法...

解决方案

你反复忘记带上abs() .x差异术语(可推测表示某种距离)。我建议您使用局部变量临时存储该距离,因此您不必多次重写整个术语:

  for (NC ...){
RRQLC = 0 ;
for (RRC ...){
if (NC!= RRC ){
auto node_dist = abs(node [NC] .x - node [RRC] .x);



此外,第二个if条件似乎没有意义:

  if  (节点[NC] .x节点[RRC] .x<节点[NC] .RADIO_RANGE&&节点[NC] .x节点[RRC] .x>节点[NC] .RADIO_RANGE-(节点[NC] .RADIO_RANGE *  2 )){



右手术语计算从一个术语到一个术语的差异两次相同的任期。我怀疑你打算在这里引用两个不同的术语? (例如节点[RRC] .RADIO_RANGE而不是节点[NC.RADIO_RANGE?)



我不能说最内层有什么问题,因为目前还不清楚你期待什么。但我建议你先解决上述问题。可能它只是在那之后工作。


伙计们我想出了代码的问题,我将与你分享。

但是它仍然是如此奇怪而且我仍然对此感到困惑。

我确实在最里面的'if'中放了一个等号,它返回了我需要的值。!!!

任何人都可以告诉它为什么这样做?

  if (abs(node [NC] .x-node [RRC ] .x)> =  0 ){


检查数组大小和索引。您的索引从1运行到 INN ,因此大小必须至少为 INN + 1



C / C ++数组的索引为零。因此,最好将for循环更改为从零运行到 INN-1 (结束条件< INN )。

Hi.. I'm working on a c++ simulation that I'm creating and something bothers me so much and i couldn't solve it...
My Problem is that the IF and ELSE seems to not work some times because if it work correctly there must be no mistake...
this is my code :

for (NC = 1; NC <= INN; NC++) {
		RRQLC = 0;
		for (RRC = 1; RRC <= INN; RRC++) {
			if (NC != RRC) {
				if (node[NC].x-node[RRC].x < node[NC].RADIO_RANGE && node[NC].x-node[RRC].x > node[NC].RADIO_RANGE-(node[NC].RADIO_RANGE*2)) {
					node[NC].RRQ++;
					node[NC].RRQL.push_back(RRC);
					if (abs(node[NC].x-node[RRC].x) > 0) {
						ENUM++;
						node[NC].E_NODE.push_back(node[RRC].id);
						SO << RRC << "\nNode " << node[NC].id << " E_NODE " << node[NC].E_NODE[RRQLC] << "\n";
					} else {
						ANUM++;
						node[NC].A_NODE.push_back(node[RRC].id);
						SO << RRC << "\nNode " << node[NC].id << " A_NODE " << node[NC].A_NODE[RRQLC] << "\n";
					}
					node[NC].SPACE.push_back(abs(node[NC].x-node[RRC].x));
					SO << "Node " << RRC << " is in RADIO_RANGE of node " << NC << "    #\nRRQL " << node[NC].RRQL[RRQLC] << "\n";
					RRQLC++;
					printf("node %d is in RADIO_RANGE of node %d\n",node[RRC].id,node[NC].id);
					//sqrt(pow((node[NC].x-node[RRC].x),2)+pow((node[NC].y-node[RRC].y),2));
				}
			}
		}
		if (node[NC].RRQ < 1) {
			NONE_NODE.push_back(node[NC].id);
			NBR++;
		}
		printf("%d\n",node[NC].RRQ);
	}



The innermost 'if' and the 'else' dose not work sometime because if it does it'll push back a number to E_NODE or A_NODE but none of it works correctly...and when i print it on a file it shows me wrong values because the 'if' and 'else' did not work and it didn't push back any value to the vectors and when the outermost FOR change the value that it prints does not exist then it shows some value of memory like 825241632
I'm talking about the E_NODE[RRQLC] and the A_NODE[RRQLC]
please help me to solve this problem if you have any idea...

解决方案

You repeatedly forgot to take abs() on the .x difference terms (which presumable denote some kind of distance). I suggest you use a local variable to temporarily store that distance, so you don't have to rewrite the entire term so many times:

for (NC...) {
   RRQLC=0;
   for (RRC...) {
      if (NC != RRC) {
         auto node_dist = abs(node[NC].x - node[RRC].x);


Also, that second if condition doesn't appear to make sense:

if (node[NC].x-node[RRC].x < node[NC].RADIO_RANGE && node[NC].x-node[RRC].x > node[NC].RADIO_RANGE-(node[NC].RADIO_RANGE*2)) {


The right hand term calculates the difference from one term to twice the same term. I suspect you meant to reference two different terms here? (e. g. node[RRC].RADIO_RANGE rather than node[NC.RADIO_RANGE?)

I can't say what is wrong with the innermost if, because it is unclear what you expect. But I suggest if you just fix the above things first. May be it just works after that.


Guys i figured out the problem of the code and i will share it with you.
but still it's so much weird and I'm still confused about it.
I did put an Equal sign in the innermost 'if' and it returned the value i needed.!!!
Can anyone tell why it does that?

if (abs(node[NC].x-node[RRC].x) >= 0) {


Check your array sizes and indexes. Your indexes are running from 1 to INN so the sizes must be at least INN+1.

With C/C++ arrays are indexed zero based. So it might be better to change the for loops to run from zero to INN-1 (end condition < INN).


这篇关于为什么if语句不能正常工作[c ++]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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