double profit_ratio = ((double) definitive_seller_profit) / ((double)definitive_buyer_profit);
if(definitive_buyer_profit > definitive_seller_profit){
if(definitive_buyer_move < 0 && definitive_seller_move < 0 && profit_ratio >= 0.50){ // give seller a chance if it's on the way and at least 50% profit ratio
if((definitive_buyer_move < definitive_seller_move)){
*n = to_fuel_or_not_to_fuel(b, definitive_seller_move, fuel_ratio);
} else {
*n = to_fuel_or_not_to_fuel(b, definitive_buyer_move, fuel_ratio);
}
}
} else if(definitive_buyer_move > 0 && definitive_seller_move > 0 && && profit_ratio >= 0.50){
if(definitive_buyer_move > definitive_seller_move){
*n = to_fuel_or_not_to_fuel(b, definitive_seller_move, fuel_ratio);
}
else {
*n = to_fuel_or_not_to_fuel(b, definitive_buyer_move, fuel_ratio);
}
} else{
*n = to_fuel_or_not_to_fuel(b, definitive_buyer_move, fuel_ratio);
}
}
所以我在行中得到了二进制表达式的无效操作数('void*'和'double'):
else if(definitive_buyer_move > 0 && definitive_seller_move > 0 && &&profit_ratio >= 0.50)
而且我还得到了...未声明的标签“profit_ratio”。
最佳答案
&& &&
在以下情况中没有任何意义:
else if(definitive_buyer_move > 0 && definitive_seller_move > 0 && && profit_ratio >= 0.50){
因此无效的操作数。
对于未声明的标签,修复之前的错误可能会解决问题或
definitive_buyer_profit
不存在;definitive_seller_profit
不存在;definitive_buyer_profit
的类型无法转换为double
;definitive_seller_profit
的类型无法转换为double
无论如何,您提供的整个代码是一堆 if
-else if
-else
,用于在所有情况下调用相同的函数。在这种情况下,请考虑使用另一种(更具可读性,IMO)方法例如
if (b_profit > s_profit)
{
const bool both_move_neg = b_move < 0 && s_move < 0;
const bool both_move_pos = b_move > 0 && s_move > 0;
const bool high_ratio = 2*abs(s_profit) >= abs(b_profit);
const bool pick_s = ((both_move_neg && b_move < s_move) ||
(both_move_pos && s_move < b_move)) && high_ratio;
*n = to_fuel_or_not_to_fuel(b, pick_s ? s_move : b_move, fuel_ratio);
}
需要(注意这是C99代码)
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
我将您的变量重命名为更方便的名称(to_fuel_or_not_to_fuel
可以进行相同的处理;也许fuel
或conditional_fuel
?),我假设s_profit
和 b_profit
为 int
s。
在尝试编辑您的帖子后,我意识到您的代码有多么困惑!我之前的言论(关于命名和使用更易读的条件)不仅仍然正确,而且您必须处理缩进(和括号)!
亲自查看:通过根据您提供的内容“正确”缩进您的代码,我得到了
double profit_ratio = ((double) definitive_seller_profit) / ((double) definitive_buyer_profit);
if (definitive_buyer_profit > definitive_seller_profit){
if (definitive_buyer_move < 0 && definitive_seller_move < 0 && profit_ratio >= 0.50){
// give seller a chance if it's on the way and at least 50% profit ratio
if (definitive_buyer_move < definitive_seller_move){
*n = to_fuel_or_not_to_fuel(b, definitive_seller_move, fuel_ratio);
} else {
*n = to_fuel_or_not_to_fuel(b, definitive_buyer_move, fuel_ratio);
}
}
} else if (definitive_buyer_move > 0 && definitive_seller_move > 0 && && profit_ratio >= 0.50){
if (definitive_buyer_move > definitive_seller_move){
*n = to_fuel_or_not_to_fuel(b, definitive_seller_move, fuel_ratio);
} else {
*n = to_fuel_or_not_to_fuel(b, definitive_buyer_move, fuel_ratio);
}
} else {
*n = to_fuel_or_not_to_fuel(b, definitive_buyer_move, fuel_ratio);
}
}
注意两件事:
- 您的缩进所暗示的
if { if {...} else if {...} else {...} }
不是您所写的内容(请记住,空格字符在 C 中没有任何意义,会被忽略); - 最后一个
}
没有对应的{
。
最后一点让我认为未声明的标签错误来自于以下事实:当您在条件 (else
) 中使用变量时 if (/*...*/profit_ratio >= 0)
你糟糕的缩进会导致括号不匹配,这本身就会导致 profit_ratio
的定义被困在另一个代码块中,导致它被未定义它的使用位置。
最后,请注意,关于 profit_ratio
的错误实际上可能来自(正如我所暗示的)条件,而不是来自其定义(带有比率)。因此,包含检测到错误的行对您来说会更有效。
因此,总而言之,您应该:
- 编写可读代码;
- 了解编译器所说的内容;
- 写出可读性问题;
- 为这些问题添加相关信息。
关于c - 创建比率时,二进制表达式错误的操作数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44345895/