icp/oer/courses/c-newcomers/sections/04-supermarket/03-states/program.c

60 lines
1.2 KiB
C

#include <stdio.h>
#include <stdbool.h>
typedef enum
{
STATE_ready_for_item,
STATE_scanning,
}state;
typedef enum
{
tissues,
pasta,
tomato_sauce,
bread,
butter,
batteries,
toothbrush,
ITEM_TYPE_COUNT,
}item;
int main()
{
bool CustomerHasItems = true;
int TimePassed = 0;
#define CUSTOMER_ITEM_COUNT 5 // This declares a constant
item CustomerItems[CUSTOMER_ITEM_COUNT] = {tissues, pasta, tomato_sauce, bread, butter};
// NOTE: time is in seconds
item Time[ITEM_TYPE_COUNT];
Time[tissues] = 1;
Time[pasta] = 2;
Time[tomato_sauce] = 3;
Time[bread] = 10;
Time[butter] = 2;
Time[batteries] = 2;
Time[toothbrush] = 1;
int ItemsLeft = CUSTOMER_ITEM_COUNT;
state CurrentState = STATE_ready_for_item; // The state the cashier is in
while(CustomerHasItems)
{
// TODO: Implement the state machine here
int CurrentItemIndex = CUSTOMER_ITEM_COUNT - ItemsLeft;
item CurrentItem = CustomerItems[CurrentItemIndex];
TimePassed += Time[CurrentItem];
ItemsLeft--;
// Print this only while scanning
printf("Time since start: %d. Scanning a product. %d to go.\n", TimePassed, ItemsLeft);
if(!ItemsLeft)
CustomerHasItems = false;
}
}