-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZIGCalculator.zig
More file actions
29 lines (24 loc) · 1.28 KB
/
ZIGCalculator.zig
File metadata and controls
29 lines (24 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const std = @import("std");
pub fn main() !void {
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
var buf_input_num1: [128]u8 = undefined;
try stdout.print("Enter the first number: ", .{});
const input_num1 = try stdin.readUntilDelimiterOrEof(&buf_input_num1, '\n');
const num1 = try std.fmt.parseFloat(f64, input_num1.?);
var buf_input_operator: [2]u8 = undefined;
try stdout.print("Choose between:\n1) Addition\n2) Subtraction\n3) Multiplication\n4) Division\n", .{});
const input_operator = try stdin.readUntilDelimiterOrEof(&buf_input_operator, '\n');
const operator = try std.fmt.parseInt(i8, input_operator.?, 10);
var buf_input_num2: [128]u8 = undefined;
try stdout.print("Enter the second number: ", .{});
const input_num2 = try stdin.readUntilDelimiterOrEof(&buf_input_num2, '\n');
const num2 = try std.fmt.parseFloat(f64, input_num2.?);
switch (operator) {
1 => std.debug.print("{d}\n", .{num1 + num2}),
2 => std.debug.print("{d}\n", .{num1 - num2}),
3 => std.debug.print("{d}\n", .{num1 * num2}),
4 => std.debug.print("{d}\n", .{num1 / num2}),
else => std.debug.print("{d} is not a valid function\n", .{operator}),
}
}