-4

Is it possible in c++ to do such string conversion as:

Example:

From:

F = a && b && c;
H = p ^ 2 + w
K = H > 10 || e < 5;
J = F && !K;

To:

J = (a && b && c) && !( (p ^ 2 + w) > 10 || e < 5);
Anna K.
  • 95
  • 10
  • Yes, this is possible. If in doubt, consult the [operator precedence table](http://en.cppreference.com/w/cpp/language/operator_precedence). – Henri Menke May 22 '17 at 00:14
  • @HenriMenke I mean to convert it from the first form to the other. I guess some kind of recursion is in place, maybe my question is not very clear and people started downvoting for that reason. – Anna K. May 22 '17 at 00:16
  • ` a && b && c;` returns bool values – Raindrop7 May 22 '17 at 00:18
  • @Raindrop7 Context is an example. – Anna K. May 22 '17 at 00:20
  • @AnnaK. The two code snippets are not equivalent by the way. https://godbolt.org/g/SrJwv8 – Henri Menke May 22 '17 at 00:31
  • @HenriMenke `p` and `w` are not boolean – Anna K. May 22 '17 at 00:36
  • @AnnaK. That's not hard to notice. In my example `e`, `p`, and `w` are integers. Also that does not change the statement that the two lines are not equivalent, which is also why the compiler can *never* turn one into the other. – Henri Menke May 22 '17 at 00:38

1 Answers1

3

It looks like you're asking for interpolation. In which case sure! First you'll want to construct a map<string, string> of the keys and values, for example:

map<string, string> interpolate = { { "F"s, "a && b && c"s }, { "H"s, "p ^ 2 + w"s }, { "K"s, "H > 10 || e < 5"s }, { "J"s, "F && !K"s } };

Then just use for_each, stding::find, and string::replace.

for(const auto& i : interpolate) for_each(begin(interpolate), end(interpolate), [&](auto& it){ for(auto pos = it.second.find(i.first); pos != string::npos; pos = it.second.find(i.first, pos)) it.second.replace(pos, i.first.size(), '(' + i.second + ')'); });

Live Example

After running this code, when outputting interpolate with for(const auto& i : interpolate) cout << i.first << " : " << i.second << endl You'll get:

F : a && b && c
H : p ^ 2 + w
J : (a && b && c) && !((p ^ 2 + w) > 10 || e < 5)
K : (p ^ 2 + w) > 10 || e < 5

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Not only you understood my question but you covered the theory behind this (and imparted us, those that do not how to name this type of problem, a little more wisdom) *and* you provided an example. People who down-voted this should take example from you Sir: first you read the question to understand what's being asked *then* you judge and possibly give a valid reason why you downvoted . Thank you so much for keeping this site bright like a diamond! – Anna K. May 22 '17 at 14:14
  • Also, what's the purpose of `s` after each string? – Anna K. May 22 '17 at 14:22
  • 1
    @AnnaK. That's the [`""s` operator](http://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s). `"F"s` is equivalent to `string("F")`. – Jonathan Mee May 22 '17 at 14:31
  • Hi Jonathan, I think there is a minor flaw with a special case: what happens if we have a circle like: `J = J && F && !K;`? (live compiler seems to abort). – Anna K. Jun 09 '17 at 19:57
  • @AnnaK That would be an infinite loop, no matter how many times you interpolated J there would still be a J that needed to be interpolated. So no, my algorithm cannot output an infinitely repeating sequence, but since neither can anything else... I'm not gonna consider that a problem. – Jonathan Mee Jun 09 '17 at 21:16
  • I'm sorry I put it wrong maybe: what if instead of looping through infinite times, the moment it saw a cycle recursion, it stopped (for that particular variable analysis). – Anna K. Jun 09 '17 at 22:41
  • 1
    @AnnaK. You can do that with a conditional of course. But if you find you need help writing it, it's outside the scope of your original question, so you'll need to post a new question on that. If you post a link I'll gladly come take a stab at answering. – Jonathan Mee Jun 09 '17 at 23:51
  • I have a problem porting it to c++98: https://stackoverflow.com/questions/44637892/port-string-interpolation-from-c14-to-c98 – Anna K. Jun 19 '17 at 19:11