I am generating strings containing if else
expressions. I was wondering if I can execute this expressions at run time? Here's an example:
string s = "if(x > 10) {Fly = true;} else {Fly = False;}";
Execute (s);
Is it even possible to do this?
I am generating strings containing if else
expressions. I was wondering if I can execute this expressions at run time? Here's an example:
string s = "if(x > 10) {Fly = true;} else {Fly = False;}";
Execute (s);
Is it even possible to do this?
It is possible to use TCC ( http://bellard.org/tcc/ ). It allows to compile and run code natively at runtime. Another approach is to use an interpreter, there are plenty out there (LUA, Python etc, see list wiki).
One does not simply interpret C/C++ code... AFAIK you just can't.
(except if you compile another binary and run it from cmd line maybe...)
Note: You can write
fly = (x > 10);
instead of
if(x > 10){
fly = true;
}else{
fly = false;
}
No. C++ is a compiled language and has no eval-function or the-like. You may want to include a scripting engine into your program, like Lua
int n; cin >> n;
for (int i = 0; i < n; i++) {
cout << string((n - 1) - i, ' ') << string(2 * i + 1, '*') << endl;
}
for (int i = n - 2; i >= 0; i--) {
cout << string((n - 1) - i, ' ') << string(2 * i + 1, '*') << endl;
}
return 0;
© 2022 - 2024 — McMap. All rights reserved.