可以使用結構體來存儲賬號、密碼、餘額等。
//
// main.c
// testATM
//
// Created by 趙亞北 on 14-7-10.
// Copyright (c) 2014年 zyb. All rights reserved.
//
#include <stdio.h>
#include <stdbool.h>//結構體
typedef struct card{
int cardNumber;
int cardPassword;
float money;
bool state;
}Card;
int cardManage(Card card[],int cardCount){//驗證卡號
while (1) {
int cardN=0;
printf("input you cardNumber\n");
scanf("%d",&cardN);
for (int i=0; i<cardCount; i++) {
if (cardN==card[i].cardNumber&&card[i].state==1) {
return i;
}
else if (cardN==card[i].cardNumber&&card[i].state==0){
printf("you card was locked ,please go our company for help \n");
return -1;
}
}//for
printf("card number rong\n");
}//while
}
bool pw(Card card[],int i){//驗證密碼
int pw;
int times=3;
while (times--) {
printf("input your password:");
scanf("%d",&pw);
if (pw==card[i].cardPassword) {
printf("password right \n");
return 1;
}
else printf("password is not right! you have %d times to input\n",times);
}
card[i].state=0;
printf("card locked! ");
return 0;
}
void service(Card card[],int i){//賬號密碼均正確後提供服務
while (1) {
printf("1 取款 \n2 存款\n3 查詢\n4 退出\n");
static int order;
scanf("%d",&order);
switch (order) {
case 1:
printf("input money counts less then 2000:");
static int money=0;
scanf("%d",&money);
if (money>2000) {
printf("less then 2000\n");
}
else if (money>card[i].money)printf("money not enough\n");
else {
card[i].money-=money;
printf("you have %f\n",card[i].money);break;
}
break;
case 2:{
printf("input money:");
int money=0;
scanf("%d",&money);
card[i].money+=money;
printf("all right,remain money %g\n",card[i].money);
}
break;
case 3:printf("remain money %g\n",card[i].money);
break;
case 4:
return;
default:
break;
}//switch
}//while
}
int main(int argc, const char * argv[])
{
Card card[4]={{123,222,1000,1},
{255,221,50000,1},
{124,255,50,1},
{125,999,900,1}};
int num=0;
while (1) {
num=cardManage(card, 4);
if (num<0) {
continue;
}
bool result=0;
result=pw(card, num);
if (result==0) {
continue;
}
service(card, num);
}
return 0;
}
運行截圖: