forked from christianhujer/expensereport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24583d6
commit 6d668eb
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ExpenseReport | ||
zig-cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const std = @import("std"); | ||
const ExpenseType = enum { DINNER, BREAKFAST, CAR_RENTAL }; | ||
const print = std.debug.print; | ||
|
||
const Expense = struct { | ||
type: ExpenseType, | ||
amount: u32, | ||
}; | ||
|
||
fn printReport(expenses: []Expense) void { | ||
var total: u32 = 0; | ||
var meals: u32 = 0; | ||
|
||
print("Expense Report: {s}\n", .{""}); | ||
|
||
for (expenses) |expense| { | ||
if (expense.type == ExpenseType.DINNER or expense.type == ExpenseType.BREAKFAST) { | ||
meals += expense.amount; | ||
} | ||
|
||
var name: []const u8 = ""; | ||
switch (expense.type) { | ||
ExpenseType.DINNER => { name = "Dinner"; }, | ||
ExpenseType.BREAKFAST => { name = "Breakfast"; }, | ||
ExpenseType.CAR_RENTAL => { name = "Car Rental"; }, | ||
} | ||
|
||
var mealOverExpensesMarker: []const u8 = if (expense.type == ExpenseType.DINNER and expense.amount > 5000 or expense.type == ExpenseType.BREAKFAST and expense.amount > 1000) "X" else " "; | ||
|
||
print("{s}\t{d}\t{s}\n", .{name, expense.amount, mealOverExpensesMarker}); | ||
|
||
total += expense.amount; | ||
} | ||
|
||
print("Meal expenses: {d}\n", .{meals}); | ||
print("Total expenses: {d}\n", .{total}); | ||
} | ||
|
||
pub fn main() void { | ||
var expenses: [5]Expense = .{ | ||
Expense {.type = ExpenseType.DINNER, .amount = 5000}, | ||
Expense {.type = ExpenseType.DINNER, .amount = 5001}, | ||
Expense {.type = ExpenseType.BREAKFAST, .amount = 1000}, | ||
Expense {.type = ExpenseType.BREAKFAST, .amount = 1001}, | ||
Expense {.type = ExpenseType.CAR_RENTAL, .amount = 4}, | ||
}; | ||
printReport(&expenses); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
SHELL:=/bin/bash | ||
|
||
export PATH:=${HOME}/Software/zig-linux-x86_64-0.9.0/:${PATH} | ||
|
||
.PHONY: all | ||
all: run | ||
|
||
.PHONY: run | ||
run: ExpenseReport | ||
./ExpenseReport | ||
|
||
%: %.zig | ||
zig build-exe $^ | ||
|
||
.PHONY: clean | ||
clean:: | ||
$(RM) *.[adios] ExpenseReport |