Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
|
|
@ -0,0 +1,15 @@
|
|||
##
|
||||
var a := new Stack<real>;
|
||||
var b := new Dictionary<string, (real, real) -> real>;
|
||||
b['+'] := (x, y) -> y + x;
|
||||
b['-'] := (x, y) -> y - x;
|
||||
b['*'] := (x, y) -> y * x;
|
||||
b['/'] := (x, y) -> y / x;
|
||||
b['^'] := (x, y) -> y ** x;
|
||||
|
||||
foreach var c in '3 4 2 * 1 5 - 2 3 ^ ^ / +'.Split do
|
||||
begin
|
||||
if c in b then a.Push(b[c](a.Pop, a.Pop))
|
||||
else a.Push(c.ToReal);
|
||||
println(c, a);
|
||||
end;
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
const std = @import("std");
|
||||
|
||||
fn rpn(text: []const u8, allocator: std.mem.Allocator) !f64 {
|
||||
var tokens = std.mem.splitScalar(u8, text, ' ');
|
||||
var stack = std.ArrayList(f64).init(allocator);
|
||||
defer stack.deinit();
|
||||
|
||||
std.debug.print("input operation stack\n", .{});
|
||||
|
||||
while (tokens.next()) |token| {
|
||||
if (token.len == 0) continue;
|
||||
|
||||
std.debug.print("{s:^5} ", .{token});
|
||||
|
||||
const num = std.fmt.parseFloat(f64, token) catch {
|
||||
if (stack.items.len < 2) {
|
||||
return error.NotEnoughOperands;
|
||||
}
|
||||
|
||||
const b = stack.pop().?;
|
||||
const a = stack.pop().?;
|
||||
|
||||
if (std.mem.eql(u8, token, "+")) {
|
||||
try stack.append(a + b);
|
||||
} else if (std.mem.eql(u8, token, "-")) {
|
||||
try stack.append(a - b);
|
||||
} else if (std.mem.eql(u8, token, "*")) {
|
||||
try stack.append(a * b);
|
||||
} else if (std.mem.eql(u8, token, "/")) {
|
||||
try stack.append(a / b);
|
||||
} else if (std.mem.eql(u8, token, "^")) {
|
||||
try stack.append(std.math.pow(f64, a, b));
|
||||
} else {
|
||||
std.debug.panic("unknown operator {s}", .{token});
|
||||
}
|
||||
|
||||
std.debug.print("calculate {any}\n", .{stack.items});
|
||||
continue;
|
||||
};
|
||||
|
||||
try stack.append(num);
|
||||
std.debug.print("push {any}\n", .{stack.items});
|
||||
}
|
||||
|
||||
return if (stack.items.len > 0) stack.pop().? else 0.0;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
const text = "3 4 2 * 1 5 - 2 3 ^ ^ / +";
|
||||
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const result = try rpn(text, allocator);
|
||||
std.debug.print("\nresult: {d}\n", .{result});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue