示例#1
0
/* print_table_fxy
 * prints f(x,y) tables given range for x and y
 */
void print_table_fxy(db_fxy fun,
        double x_start, double x_end, double x_step, char xch,
        double y_start, double y_end, double y_step, char ych)
{
    printf("%c, %c\n", ych, xch);
    printf("         ");
    for (double x = x_start; end_cond(x, x_step, x_end) == 0; x += x_step) {
        printf("% 8.4lf ", x);
    }
    printf("\n");
    for (double y = y_start; end_cond(y, y_step, y_end) == 0; y += y_step) {
        printf("% 8.4lf ", y);
        for (double x = x_start; end_cond(x, x_step, x_end) == 0; x += x_step) {
            double f = fun(x,y);
            printf("% 8.4lf ", f);
        }
        printf("\n");
    }
}
示例#2
0
ScriptLine* ScriptLineBlock::createLine(const Kernel::EParser& code)
{
    if (code.name() == ":") // define variable
    {
        std::string type = code[0].name();
        std::string name,value = "";
        if (code[1].name() != "=")
        {
            name = code[1].name();
        }
        else
        {
            name = code[1][0].name();
            Expression valExpr(m_local_namespace,code[1][1].str());
            value = valExpr.eval().toString();
        }
        return new ScriptLineDef(m_local_namespace,type,name,value);
    }
    else if (code.name() == "=") // assign new value
    {
        std::string name = code[0].name();
        const Kernel::EParser& value = code[1];
        return new ScriptLineSet(m_local_namespace,name,Expression_ptr(new Expression(m_local_namespace,value)));
    }
    else if (code.name() == "if") // if(cond1:expr1,cond2:(expr2;expr3;expr4;...),def_expr)
    {
        ScriptLineIf *sif = new ScriptLineIf(m_local_namespace);
        size_t n = code.size();
        for(size_t i=0; i<n; ++i)
        {
            const Kernel::EParser& clause = code[i];
            if (clause.name() == ":")
            {
                Expression* cond = new Expression(m_local_namespace,clause[0]);
                ScriptAnonymousBlock* block = new ScriptAnonymousBlock(this);
                //block->addLines(clause[1]);
                sif->addCondition(Expression_ptr(cond),ScriptAnonymousBlock_ptr(block));
            }
            else
            {
                ScriptAnonymousBlock* block = new ScriptAnonymousBlock(this);
                //block->addLines(clause);
                sif->addDefault(ScriptAnonymousBlock_ptr(block));
            }
        }
        return sif;
    }
    else if (code.name() == "for") // for(Scalar: i=0,i<10,i = i + 1,(line1;line2;line3;...))
    {
        if (code.size() != 4)
        {
            throw std::runtime_error("Script line -for- has a wrong structure");
        }
        ScriptAnonymousBlock_ptr block = ScriptAnonymousBlock_ptr(new ScriptAnonymousBlock(this));
        ScriptLine* init = block->createLine(code[0]);
        Expression_ptr end_cond(new Expression(block->getLocalNamespace_ptr(),code[1]));
        ScriptLine* next = block->createLine(code[2]);
        //block->addLines(code[3]);
        ScriptLineFor* sfor = new ScriptLineFor(init,end_cond,next,block);
        return sfor;
    }
    else // function call
    {
        std::string fnName = code.name();
        ScriptLineCall* call = new ScriptLineCall(m_local_namespace,*this,fnName);
        for(int i=0; i<code.size(); ++i) // looping the function arguments
        {
            const Kernel::EParser& arg = code[i];
            if (arg.name() != "=")
            {
                throw std::invalid_argument("Actual function arguments must be passed as name=value");
            }
            std::string name = arg[0].name();
            const Kernel::EParser& value = arg[1];
            call->setArgument(name,value);
        }
        call->check();
        return call;
    }
    return NULL;
}