#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/types.h>
typedef struct {
char name[20];
u_int number;
u_int password;
u_int money;
char delete_flg;
} customer_t;
#define NUM_CUSTOMER 512
customer_t customerList[NUM_CUSTOMER];
customer_t *sentinel = &customerList[NUM_CUSTOMER];
void InitCustomer(u_int index, char *name, u_int number, u_int money)
{
strncpy(customerList[index].name, name, sizeof(customerList[0].name));
customerList[index].name[strlen(name)] = '\0';
customerList[index].number = number;
customerList[index].password = (number+1111)%10000;
customerList[index].money = money;
customerList[index].delete_flg = 0;
}
void InitCustomerList()
{
customer_t *current = customerList;
do {
current->delete_flg = 1;
} while (++current != sentinel);
InitCustomer(0, "立命太郎", 1234, 1234560);
InitCustomer(1, "野路花子", 5678, 987600);
InitCustomer(2, "衣笠一郎", 9012, 538600);
}
void ShowAll()
{
customer_t *current = customerList;
puts("預金残高は次のとおりです.");
do {
if (current->delete_flg) continue;
printf("%s (%d) %d 円\n", current->name, current->number, current->money);
} while (++current != sentinel);
}
customer_t *Search(u_int number, u_int password)
{
customer_t *current = customerList;
do {
if (current->delete_flg) continue;
if (current->number == number && current->password == password) {
return current;
}
} while (++current != sentinel);
return NULL;
}
void Hikidashi()
{
// 入力用
ssize_t len;
char *line = NULL;
size_t lineLength;
// 入力値
int number;
int password;
int money;
// 処理対象顧客
customer_t *customer;
while(1) {
printf("口座番号を入力してください:");
len = getline(&line, &lineLength, stdin);
number = atoi(line);
printf("暗証番号を入力してください:");
len = getline(&line, &lineLength, stdin);
password = atoi(line);
if (number < 0 || password < 0 ||
(customer = Search(number, password)) == NULL) {
puts("口座番号または暗証番号が間違っています.");
continue;
}
printf("引出し額を入力してください:");
len = getline(&line, &lineLength, stdin);
money = atoi(line);
if (customer->money < money) {
puts("預金額が不足しています.");
continue;
}
customer->money -= money;
printf("ありがとうございました.残高は %d 円です.\n", customer->money);
break;
}
}
void Drawer()
{
// 入力用
ssize_t len;
char *line = NULL;
size_t lineLength;
while(1) {
printf("[0]終了 [1]残高表示 [2]引出し:");
if ((len = getline(&line, &lineLength, stdin)) != 2) {
continue;
}
switch(line[0]) {
case '0':
free(line);
return;
case '1':
ShowAll();
break;
case '2':
Hikidashi();
break;
default:
break;
}
}
}
int main(int argc, char *argv[])
{
InitCustomerList();
ShowAll();
Drawer(); // 引き出し機能をなくす場合はこの行を削除
return 0;
}