-
-
Save takkanm/f8822c0a97e9f1ee1a05 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
#define show_shop printf("%s\n", dump_shop(shop)) | |
enum CustomerState { LEAVE, WAIT, EAT, REST }; | |
struct Seat { | |
enum CustomerState state; | |
struct Seat *left; | |
}; | |
struct RarmenShop { | |
int seat_size; | |
struct Seat* first; | |
}; | |
struct Seat* alloc_seat() { | |
return (struct Seat *)malloc(sizeof(struct Seat)); | |
} | |
struct Seat * create_seats(int seat_size) { | |
int i; | |
struct Seat* first; | |
struct Seat* current = NULL; | |
for(i =0; i < seat_size; i++) { | |
if (current == NULL) { | |
current = alloc_seat(); | |
memset(current, 0, sizeof(struct Seat)); | |
first = current; | |
} else { | |
current->left = alloc_seat(); | |
current = current->left; | |
} | |
} | |
current->left = first; | |
return first; | |
} | |
struct RarmenShop* create_shop(int seat_size) { | |
struct RarmenShop* shop = (struct RarmenShop *)malloc(sizeof(struct RarmenShop)); | |
memset(shop, 0, sizeof(struct RarmenShop)); | |
shop->seat_size = seat_size; | |
shop->first = create_seats(seat_size); | |
return shop; | |
} | |
void close_shop(struct RarmenShop* shop ) { | |
struct Seat* current = shop->first; | |
struct Seat* next; | |
do { | |
next = current->left; | |
free(current); | |
current = next; | |
} while(current != shop->first); | |
free(shop); | |
} | |
int is_empty_seat(const struct Seat* seat) { | |
return seat->state == LEAVE; | |
} | |
char* dump_shop(struct RarmenShop* shop) { | |
char* result = (char *)malloc(sizeof(char) * shop->seat_size + 1); | |
struct Seat* current = shop->first; | |
int i; | |
for(i = 0; i < shop->seat_size; i++) { | |
if (is_empty_seat(current)) { | |
result[i] = '0'; | |
} else { | |
result[i] = '1'; | |
} | |
current = current->left; | |
} | |
result[i] = '\0'; | |
return result; | |
} | |
void leav_seat(struct Seat* seat) { | |
seat->state = LEAVE; | |
} | |
void sitdown(struct Seat* seat) { | |
seat->state = WAIT; | |
} | |
void change_state(struct Seat* seat) { | |
switch(seat->state) { | |
case WAIT: | |
seat->state = EAT; | |
break; | |
case EAT: | |
seat->state = REST; | |
break; | |
case REST: | |
leav_seat(seat); | |
break; | |
default: | |
break; | |
} | |
} | |
void change_shop_state(struct RarmenShop* shop) { | |
struct Seat* current = shop->first; | |
do { | |
change_state(current); | |
current = current->left; | |
} while(current != shop->first); | |
} | |
int can_sitdown(struct Seat* seat, int custormer_size) { | |
int i; | |
struct Seat* current = seat; | |
for (i = 0; i < custormer_size; i++) { | |
if(is_empty_seat(current)) { | |
current = current->left; | |
} else { | |
return FALSE; | |
} | |
} | |
return TRUE; | |
} | |
void sitdown_customer(struct Seat* seat, int custormer_size) { | |
int i; | |
struct Seat* current = seat; | |
for (i = 0; i < custormer_size; i++) { | |
sitdown(current); | |
current = current->left; | |
} | |
} | |
int guide_customer(struct RarmenShop* shop, int custormer_size) { | |
struct Seat* current = shop->first; | |
do { | |
if (can_sitdown(current, custormer_size)) { | |
sitdown_customer(current, custormer_size); | |
return TRUE; | |
} | |
current = current->left; | |
} while(current != shop->first); | |
return FALSE; | |
} | |
void test(char* customer, char* expect) { | |
struct RarmenShop* shop = create_shop(8); | |
int group_size = strlen(customer); | |
int i; | |
char* result; | |
for(i = 0; i < group_size; i++) { | |
do { | |
change_shop_state(shop); | |
} while(!guide_customer(shop, (int)customer[i] - 48)); | |
} | |
result = dump_shop(shop); | |
if(!strcmp(result, expect)) { | |
printf("true : "); | |
} else { | |
printf("false : "); | |
} | |
printf("%s,%s,%s\n", customer, result, expect); | |
close_shop(shop); | |
free(result); | |
} | |
int main() { | |
test("3316", "11111110"); | |
test("3342153", "11111111"); | |
test("8", "11111111"); | |
test("232624", "11110110"); | |
test("1", "10000000"); | |
test("224673432", "11111000"); | |
test("123464334", "11111110"); | |
test("44372332", "11111111"); | |
test("2344", "11111111"); | |
test("6448476233", "11111100"); | |
test("4345174644", "11111111"); | |
test("77743", "11111110"); | |
test("2136524281", "10000000"); | |
test("344373", "11100000"); | |
test("434226", "11111111"); | |
test("7433223", "11111110"); | |
test("2246534", "11110111"); | |
test("634", "11111110"); | |
test("2222", "11111100"); | |
test("2442343238", "11111111"); | |
test("7243344", "11111111"); | |
test("26147234", "10111111"); | |
test("34424", "10011111"); | |
test("6334", "11011111"); | |
test("3828342", "11011110"); | |
test("4431", "11110000"); | |
test("2843212125", "11111111"); | |
test("333336482", "11000000"); | |
test("374", "11110000"); | |
test("4382333", "11100111"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment