2017-09-30 56 views
1

新的gnuplot(5.x)具有新的逻辑语法,但我无法使'else if'语句正常工作。例如:'else if'gnuplot中的逻辑语句

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指南(http://www.bersch.net/gnuplot-doc/if.html)没有使用'else if'的新逻辑语法的示例,但确实有使用旧语法的示例,但我宁愿避免使用旧的。

+0

你能避免使用在你的第二个例子'else'并获得你所需要的。 –

回答

2

基于在最新版本的Gnuplot中对command.c的源代码的简要检查,我会说这个功能不被支持。更具体地说,相关部分可以在1163(见下面)中找到。解析器首先确保if后面跟着括号中的条件。如果以下标记为{,则它会激活新语法,将封闭在匹配的一对{}中的整个if块隔离,并且可选地查找else,但是只允许使用{}(含)条款。由于这个原因,一个简单的脚本,例如:

if(flag == 1){ 
    print 1; 
}else if(flag == 2){ 
    print 2; 
} 

确实产生错误信息expected {else-clause}。一个解决办法是嵌套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(); 
    } 
+0

感谢您关注此事。真的很遗憾,对'else if'的支持已经被抛弃了。我想你的解决方法是在这种情况下可以做到的最好的解决方案。 – Mead