If statement ';如果';gnuplot中的逻辑语句
新的gnuplot(5.x)有新的逻辑语法,但我无法让'elseif'语句工作。例如:If statement ';如果';gnuplot中的逻辑语句,if-statement,logic,gnuplot,If Statement,Logic,Gnuplot,新的gnuplot(5.x)有新的逻辑语法,但我无法让'elseif'语句工作。例如: if(flag==1){ plot sin(x) } else{ plot cos(x) } 确实有效,但: if(flag==1){ plot sin(x) } else if(flag==2){ plot cos(x) } else if(flag==3){ plot tan(x) } 没有。我尝试了许多组合{}和“if”和“else”的位置,但都没有效果。有人知道如何在gnuplot 5.x中正确实
if(flag==1){
plot sin(x)
}
else{
plot cos(x)
}
确实有效,但:
if(flag==1){
plot sin(x)
}
else if(flag==2){
plot cos(x)
}
else if(flag==3){
plot tan(x)
}
没有。我尝试了许多组合{}和“if”和“else”的位置,但都没有效果。有人知道如何在gnuplot 5.x中正确实现“else if”吗
gnuplot指南()中没有使用“else if”的新逻辑语法示例,但有使用旧语法的示例,但我宁愿避免使用旧语法。根据对最新版本gnuplot中
command.c
的源代码的简要检查,我认为不支持此功能。更具体地说,相关零件可在第1163行找到(见下文)。解析器首先确保if
后面跟着一个括号中的条件。如果以下标记是一个{
,它将激活新语法,隔离包含在一对匹配的{}
中的整个If块,并可选地查找else
,但它也只允许后跟一个{}
封闭子句。因此,可以使用一个简单的脚本,例如:
if(flag == 1){
print 1;
}else if(flag == 2){
print 2;
}
确实会生成错误消息预期的{else子句}
。一种解决方法是将if语句嵌套为:
if(flag == 1){
}else{
if(flag == 2){
}else{
if(flag == 3){
}
}
}
这是公认的稍微详细一点
void
if_command()
{
double exprval;
int end_token;
if (!equals(++c_token, "(")) /* no expression */
int_error(c_token, "expecting (expression)");
exprval = real_expression();
/*
* EAM May 2011
* New if {...} else {...} syntax can span multiple lines.
* Isolate the active clause and execute it recursively.
*/
if (equals(c_token,"{")) {
/* Identify start and end position of the clause substring */
char *clause = NULL;
int if_start, if_end, else_start=0, else_end=0;
int clause_start, clause_end;
c_token = find_clause(&if_start, &if_end);
if (equals(c_token,"else")) {
if (!equals(++c_token,"{"))
int_error(c_token,"expected {else-clause}");
c_token = find_clause(&else_start, &else_end);
}
end_token = c_token;
if (exprval != 0) {
clause_start = if_start;
clause_end = if_end;
if_condition = TRUE;
} else {
clause_start = else_start;
clause_end = else_end;
if_condition = FALSE;
}
if_open_for_else = (else_start) ? FALSE : TRUE;
if (if_condition || else_start != 0) {
clause = new_clause(clause_start, clause_end);
begin_clause();
do_string_and_free(clause);
end_clause();
}
在第二个示例中,您可以避免使用
else
,并获得所需的内容。感谢您对此进行研究。放弃对“else if”的支持是多么遗憾。我想您的解决方法是在这种情况下可以做到的最好的方法。