forked from christianhujer/expensereport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpenseReport.m
65 lines (53 loc) · 1.66 KB
/
ExpenseReport.m
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <time.h>
typedef enum {
DINNER,
BREAKFAST,
CAR_RENTAL
} ExpenseType;
struct Expense {
ExpenseType type;
int amount;
} Expense;
void printReport(struct Expense expenses[], size_t numExpenses) {
int total = 0;
int mealExpenses = 0;
int i;
time_t now;
if (time(&now) == -1)
return;
printf("Expenses %s:\n", ctime(&now));
for (i = 0; i < numExpenses; i++) {
struct Expense expense = expenses[i];
if (expense.type == DINNER || expense.type == BREAKFAST) {
mealExpenses += expense.amount;
}
char *expenseName;
switch (expense.type) {
case DINNER:
expenseName = "Dinner";
break;
case BREAKFAST:
expenseName = "Breakfast";
break;
case CAR_RENTAL:
expenseName = "Car Rental";
break;
}
char *mealOverExpensesMarker = ((expense.type == DINNER && expense.amount > 5000) || (expense.type == BREAKFAST && expense.amount > 1000)) ? "X" : " ";
printf("%s\t%d\t%s\n", expenseName, expense.amount, mealOverExpensesMarker);
total += expense.amount;
}
printf("Meal expenses: %d\n", mealExpenses);
printf("Total expenses: %d\n", total);
}
int main(void) {
struct Expense expenses[5];
expenses[0].type = DINNER; expenses[0].amount = 5000;
expenses[1].type = DINNER; expenses[1].amount = 5001;
expenses[2].type = BREAKFAST; expenses[2].amount = 1000;
expenses[3].type = BREAKFAST; expenses[3].amount = 1001;
expenses[4].type = CAR_RENTAL; expenses[4].amount = 4;
printReport(expenses, 5);
return 0;
}