-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfruitOrder.c
48 lines (39 loc) · 1021 Bytes
/
fruitOrder.c
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
/*
* Program with enum, struct and union
*/
#include <stdio.h>
typedef enum {
COUNT, POUNDS, PINTS
} unit_of_measure;
typedef union {
short count;
float weight;
float volume;
} quantity;
typedef struct {
const char *name;
const char *country;
// union type: quantity
quantity amount;
// enum type: unit_of_measure
unit_of_measure units;
} fruit_order;
void display(fruit_order order) {
printf("This order contains ");
if (order.units == PINTS) {
printf("%2.2f pints of %s\n", order.amount.volume, order.name);
} else if (order.units == POUNDS) {
printf("%2.2f lbs of %s\n", order.amount.weight, order.name);
} else {
printf("%i %s\n", order.amount.count, order.name);
}
}
int main() {
fruit_order apples = {"apples", "England", .amount.count = 144, COUNT};
fruit_order strawberries = {"strawberries", "Spain", .amount.weight = 17.6, POUNDS};
fruit_order oj = {"orange juice", "U.S.A", .amount.volume = 10.5, PINTS};
display(apples);
display(strawberries);
display(oj);
return 0;
}