if-statement - gnuplot 中的“else if”逻辑语句

标签 if-statement logic gnuplot

新的 gnuplot (5.x) 具有新的逻辑语法,但我无法使用“else if”语句。例如:

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”的新逻辑语法示例,但确实有使用旧语法的示例,但我宁愿避免使用旧语法。

最佳答案

基于对command.c的源代码的简要检查在最新版本的 Gnuplot 中,我会说不支持此功能。更具体地说,相关部分可以在线1163上找到。 (见下文)。解析器首先确保 if后跟括在括号中的条件。如果以下 token 是 { ,它激活新语法,隔离包含在一对匹配 {} 中的整个 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();
    }

关于if-statement - gnuplot 中的“else if”逻辑语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46504724/

相关文章:

sql - 从设施表中选择使用不当的开始和结束日期

logic - 为什么编程语言不使用简化的 bool 表达式?

c++ - Gnuplot C++ 从 txt 文件实时绘图

latex - gnuplot - 如何使用 epslatex 终端在标签中获取彩色文本

gnuplot - 在 GNUplot 阶梯图中添加点

c# - 如果对于随机数 C#.net,则替代 else

perl - 有没有办法在 perl if 语句中使用 <=> 运算符?

excel - 使用 if 函数从开始日期和结束日期获取值

java - 支票乱了?

c - 试图理解MD5算法