c# - Parse infix expression while keeping delimiters as array element -


does c# have way me parse this:

 "(h1+h2+h3)" string array {"(", "h1", "+", "h2", +, "h3", + ")"} ?  

i implementing shunting-yard algorithm , don't want make work around not having tokens.

shunting-yard algorithm

edit: wrote own solution

private string[] parseexp(string exp) {         // @ least long input string         string[] parsed = new string[exp.length];         int index = 0;          foreach(char c in exp)         {             if(op.contains(c))             {                 index++;                 parsed[index++] += c.tostring();             }else             {                 parsed[index] += c.tostring();             }         }          array.resize(ref parsed, index + 1);          return parsed; } 

you can try rearrange expression can split using string.split operation in case need know possible symbols expression can have.

string input="(h1+h2+h3)"; string newstring = input.replace("("," ( ").replace(")"," )  " ).replace("+"," + "); // reformat string separating symbols string[] splitstr = newstring.split(new char[]{' '}); foreach(string x in splitstr) {   console.writeline(x);     } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -