0

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!

user7227230
  • 59
  • 1
  • 9

2 Answers2

1

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);
};
user7227230
  • 59
  • 1
  • 9
0

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;
}
  • That's strange. The block of code I posted is actually wrapped with a try / catch. It wasn't throwing for me but you say it does for you? Also I evaluated the expression in the debugger at runtime swapping out n for 0 and got 2 as the result which is what I'd expect. I'll double check... – user7227230 Dec 24 '16 at 14:10
  • Okay. That's crazy. So I've re-run and I am now getting it to throw an exception as you say at position 2 so it looks like it doesn't like the mod '%' symbol. Is there some setup required for mods? – user7227230 Dec 24 '16 at 21:31
  • Ah. It looks like the mod operator isn't supported - https://github.com/beltoforion/muparser/issues/20 That's a shame. I wonder if there's an easy way to add/extend for this? – user7227230 Dec 24 '16 at 21:35