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.
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
Post a Comment