C++ MuParser parsing not behaving as expected

737 Views Asked by At

I've just started playing with MuParser - seems like a really cool library! However, I'm stuck on parsing the following expression. Can anyone see from the code snippet below where I'm going wrong? Whatever 'count' is the result always seems to be 0??

mu::Parser parser;

string rule = "(n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2)";
parser.DefineConst("n", count);
parser.SetExpr(rule);
int retVal = parser.Eval();

Thanks!

2

There are 2 best solutions below

3
On BEST ANSWER

so, i think your code is wrong. it fall at run time. you can put your code in try{}catch{} to find your problem. for example :

        try
{
    mu::Parser parser;
    string rule = "(n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2)";
    parser.DefineConst("n", count);
    parser.SetExpr(rule);
    int retVal = parser.Eval();

    std::cout << retVal << std::endl;

}
catch (Parser::exception_type &e)
{
    std::cout << e.GetMsg() << std::endl;
}
1
On

For anyone interested. I added a modulo operator by adding the following:

parser.DefineOprtChars("%");
parser.DefineOprt("%", moduloOperator, mu::prINFIX);

double moduloOperator(double v, double w) {
        return (int)v % MAX(1, (int)w);
};