Menu
Home
Create new Paste
Log in
operator tree
Code
Theme: cobalt
Theme: eclipse
Theme: elegant
Theme: monokai
Theme: neat
Theme: night
Theme: rubyblue
import std.stdio; import std.regex; import std.algorithm : min; import std.range : retro; char[ulong] Diff(string a, string b) { char[ulong] diff; const ulong len = min(a.length, b.length); uint j = 0; for (uint i = 0; i < len; ++i, j++) { if (a[i] != b[j]) { diff[i] = a[i]; j--; } } return diff; } void main() { string term = "4+5*8"; //string term = "2*2+2"; //string term = "2*3+4+5*6"; auto chars = term.splitter(regex(r"(\+|\-)")); writeln(chars); string cmp; foreach (str; chars) { cmp ~= str; } writeln(cmp); const char[ulong] diff = Diff(term, cmp); writeln(diff); uint id = 0; string[] op_tree; foreach (str; chars) { if (str.length > 1) { writeln(" >> ", str.splitter(regex(r"(\*|\/|%)"))); auto sub_chars = str.splitter(regex(r"(\*|\/|%)")); cmp = ""; foreach (sub_str; sub_chars) { cmp ~= sub_str; } const char[ulong] sub_diff = Diff(str, cmp); writeln(" --> ", diff, sub_chars); string[] values; foreach (t; sub_chars) { values ~= t; } uint tid = 0; foreach (val; retro(values)) { if (tid < sub_diff.values.length) { op_tree = [sub_diff.values[tid++] ~ "", val] ~ op_tree; } else { op_tree = op_tree[0 .. tid] ~ val ~ op_tree[tid .. $]; } } } else { op_tree ~= [diff.values[id++] ~ "", str]; } } writeln("----"); writeln(op_tree); }
Result:
Compilation error
/
Return code: 9 (Killed)
/
Compilation time:
5.31
seconds
Username
Message
Add comment
Paste info
Author:
Namespace
Views:
229
Private:
no
Expires:
Never
Uploaded:
17.08.12 1:18
Votes
:
0
Tweet
Compilation
Compiler:
DMD 2.062
Pointer size:
m64
Actions
Download
Fork
Raw
×
Confirm
Are you sure you want to delete this paste?
There's no way back!
×
Confirm
Reason