File tree Expand file tree Collapse file tree 1 file changed +7
-12
lines changed Expand file tree Collapse file tree 1 file changed +7
-12
lines changed Original file line number Diff line number Diff line change @@ -234,19 +234,14 @@ void main()
234
234
{
235
235
import std.stdio, std.string, std.algorithm, std.conv;
236
236
237
- // Reduce the RPN expression using a stack
238
- readln.split.fold!((stack, op)
237
+ // arr is real[] and sym is the current symbol
238
+ readln.split.fold!((arr, sym)
239
239
{
240
- switch (op)
241
- {
242
- // Generate operator switch cases statically
243
- static foreach (c; "+-*/")
244
- case [c]:
245
- return stack[0 .. $ - 2] ~
246
- mixin("stack[$ - 2] " ~ c ~
247
- " stack[$ - 1]");
248
- default: return stack ~ op.to!real;
249
- }
240
+ static foreach (c; "+-*/")
241
+ if (sym == [c])
242
+ return mixin("arr[0 .. $-2] ~ (arr[$-2] " ~ c ~ " arr[$-1])");
243
+
244
+ return arr ~ sym.to!real;
250
245
})((real[]).init).writeln;
251
246
}
252
247
----
You can’t perform that action at this time.
0 commit comments