ATM机程序
主函数:
// main.m
// test7.10
//
// Created by dllo on 15/7/10.
// Copyright (c) 2015年 cml. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyFunction.h"
int main(int argc, const char * argv[]) {
Person per1 = {111,123,400};
Person per2 = {222,234,500};
Person per3 = {333,345,500};
Person per4 = {444,456,600};
Person per[4] ={per1,per2,per3,per4};
Person *p = &per;
int count = 3;
int result =0;
while (1) {
int enterCard = 0;
int passWord = 0;
printf("请输入账号:\n");
scanf("%d",&enterCard);
printf("请输入密码:");
scanf("%d",&passWord);
result = checkPassCardAndPassWord(enterCard, passWord, p, 4);
if (result == 4) {
count--;
if(count!=0){
printf("密码错误,还有%d次机会\n",count);
continue;
}
if (count == 0) {
printf("24小时内不可以在操作");
}
}else{
printf("登陆成功\n");
break;
}
}
while (1) {
printf("1.取款\n2.存款\n3.退出");
int num =0;
scanf("%d",&num);
if(num == 1){
printf("请输入钱数:");
int getMoney =0;
scanf("%d",&getMoney);
checkMoney(result, p,getMoney);
}else if(num == 2){
printf("请放入钱,请输入钱数:");
int inMoney =0;
scanf("%d",&inMoney);
printf("已存款,余额:%d\n",p[result].moneyCount+inMoney);
}else if(num ==3){
break;
}else{
printf("输入错误,请重新输入\n");
}
}
return 0;
}
.m文件
/ MyFunction.m
// test7.10
// Created by dllo on 15/7/10.
// Copyright (c) 2015年 cml. All rights reserved.
#import "MyFunction.h"
int checkPassCardAndPassWord(int enterCard,int passWord,Person *p,int count){
for (int i =0; i<4; i++) {
if ((enterCard == p[i].cardNum)&&(passWord == p[i].passWord)) {
return i;
}
}
return 4;
}
void checkMoney(int postion, Person *p,int getMoney){
if (getMoney >p[postion].moneyCount) {
printf("余额不足");
}else{
printf("取款成功,余额:%d\n",p[postion].moneyCount -getMoney);
}
.h文件
// MyFunction.h
// test7.10
// Created by dllo on 15/7/10.
// Copyright (c) 2015年 cml. All rights reserved.
#import <Foundation/Foundation.h>
struct person{
int cardNum;
int passWord;
int moneyCount;
};
typedef struct person Person;
// 查账号和密码
int checkPassCardAndPassWord(int enterCard,int passWord,Person *p,int count);
// 操作
void checkMoney(int postion, Person *p,int getMoney);