# 50023. Combination Lock

## Problem Description

Implement a combination lock. A combination lock has a combination of 4 digits, like 0256. A combination lock only opens when the correct combination is entered. You must implement the following functions.

• void init(Lock *lock);
Initialize a combination lock. Initially the lock is unlocked.
• int lock(Lock *lock, int combination);
To lock an unlocked lock.
• If the lock is already locked then do nothing and return -1 as error.
• If the lock is unlocked then lock it with the combination and return the combination if it is valid. A valid combination is an integer from 0 to 9999. After that the lock is locked. If the combination is not valid return 10000 as error.
• int unlock(Lock *lock, int combination);
To unlock a locked lock.
• If the lock is already unlocked then do nothing and return -1 as error.
• If the lock is locked then unlock it with the combination. If the combination is correct then unlock the lock and return 0. If the combination is incorrect then the lock remains locked and return 1 as error.
• int isLocked(Lock *lock);
Return 1 if the lock is locked, 0 if unlocked.

• 10pt. Implement init and isLocked.
• 30pt. Implement init, lock, and isLocked. The combination for lock is always valid.
• 10pt. Implement init, lock, and isLocked. The combination for lock may be invalid.
• 50pt. Implement init, lock, unlock, and isLocked.

## locker.h

123456789101112#ifndef _LOCKER_H#define _LOCKER_H /*    define structure Lock*/ void init(Lock *lock);int lock(Lock *lock, int combination);int unlock(Lock *lock, int combination);int isLocked(Lock *lock);#endif


## main.c

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#include "locker.h" #include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h> void test_specall() {#define UNLOCK 0#define LOCKED 1#define OPERROR -1#define PEERROR 10000    int n, cmds, cmd, lid, combination;    scanf("%d", &n);    Lock *locker[32];    for (int i = 0; i < n; i++)        locker[i] = (Lock *) malloc(sizeof(Lock));    for (int i = 0; i < n; i++)        init(locker[i]);    scanf("%d", &cmds);    for (int i = 0; i < cmds; i++) {        scanf("%d %d", &cmd, &lid);        if (cmd == 1) {            int ret = isLocked(locker[lid]);            printf("isLocked %d %s\n", lid, ret == OPERROR ? "NOTHING HAPPENED" :                ((ret == LOCKED) ? "LOCKED" : (ret == UNLOCK ? "UNLOCK" : "INVALID VALUE")));        } else if (cmd == 2) {            scanf("%d", &combination);            int ret = lock(locker[lid], combination);            if (ret == OPERROR)                printf("lock %d %s\n", lid, "NOTHING HAPPENED");            else if (ret == PEERROR)                printf("lock %d %s\n", lid, "INVALID COMBINATION");            else                printf("lock %d new pwd: %d\n", lid, ret);        } else if (cmd == 3) {            scanf("%d", &combination);            int ret = unlock(locker[lid], combination);            printf("unlock %d %s\n", lid, ret == OPERROR ? "NOTHING HAPPENED" :                ((ret == LOCKED) ? "LOCKED" : (ret == UNLOCK ? "UNLOCK" : "INVALID VALUE")));        } else {            assert(0);        }    }}int main() {    test_specall();    return 0;}


## Sample Input

3 122 0 99993 1 12341 01 23 0 12343 0 100002 0 5143 0 99992 0 5143 0 99991 01 1


## Sample Output

lock 0 new pwd: 9999unlock 1 NOTHING HAPPENEDisLocked 0 LOCKEDisLocked 2 UNLOCKunlock 0 LOCKEDunlock 0 LOCKEDlock 0 NOTHING HAPPENEDunlock 0 UNLOCKlock 0 new pwd: 514unlock 0 LOCKEDisLocked 0 LOCKEDisLocked 1 UNLOCK