Bank Accounts

Lab C Advanced 30 points

Manage up to 20 accounts with commands: open NAME, dep NAME AMT, wd NAME AMT, show. Print no such account, exists or insufficient on errors. show prints every account in opening order as NAME BALANCE.

Input

n, then n commands.

Output

Messages and listings.

Example

Input

8
open ann
open bob
dep ann 100
wd bob 5
wd ann 30
open ann
dep cy 5
show

Output

insufficient
exists
no such account
ann 70
bob 0
Sample input available Sample output available
Editor
C

Hints

Read the remaining arguments of a command before checking for errors, so the input stays in step.

Solution explanation

A struct array plus a lookup function is a tiny in-memory database.

Show solution
#include <stdio.h>
#include <string.h>

typedef struct {
    char name[30];
    int balance;
} Account;

Account accounts[20];
int count = 0;

int find(char *name) {
    for (int i = 0; i < count; i++) {
        if (strcmp(accounts[i].name, name) == 0) return i;
    }
    return -1;
}

int main() {
    int n, amount;
    char cmd[10], name[30];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", cmd);
        if (strcmp(cmd, "show") == 0) {
            for (int k = 0; k < count; k++) printf("%s %d\n", accounts[k].name, accounts[k].balance);
            continue;
        }
        scanf("%s", name);
        int idx = find(name);
        if (strcmp(cmd, "open") == 0) {
            if (idx >= 0) printf("exists\n");
            else {
                strcpy(accounts[count].name, name);
                accounts[count].balance = 0;
                count++;
            }
            continue;
        }
        scanf("%d", &amount);
        if (idx < 0) printf("no such account\n");
        else if (strcmp(cmd, "dep") == 0) accounts[idx].balance += amount;
        else if (amount > accounts[idx].balance) printf("insufficient\n");
        else accounts[idx].balance -= amount;
    }
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD