It seems captivating riddle, so if you want to solve it in C++ have two way. The hard way is implementing a small math parser using some algorithms like Shunting-yard algorithm. Or instead of, if you are familiar with library linking in C++, it is better to use a mathematical expression parser libraries. There are many libraries on Internet. Here, I suggest one of them as below.
mathematical expression library I personalty have tested it and obviously is fast. you can clone source code in GitHub
Anyway, you can not solve this case with lambda functions because, the input is a mathematical expression you should parse and calculate it runtime.
if you use python see this post.