标签:style blog io ar color os sp for on
小程序:模拟ATM机取款的过程1.函数声明部分:
<span style="font-size:14px;"> //随机产生卡号 void InputCard(int m[],int count); //随机产生密码 void InputPwd(int m[],int count); //随机产生money void InputMoney(int m[], int count); //判断卡号是否正确 BOOL isCard(int m[],int count,int username); //输出卡号和密码 void OutputCard_Pd(int m[], int count); //判断密码是否正确 BOOL isPwd(int password, int pwd); //根据账号获取密码 int requestPwd(int username, int m[], int count); //取款操作 int OutputMoney(int a, int b); //转账操作 BOOL tranMoney(int a, int m[], int count); //存款操作 int cunMoney(int a , int b); </span>2.对应方法实现
<span style="font-size:14px;"> #import <Foundation/Foundation.h> //随机输入 void InputValue(float a[], int count){ for (int i = 0; i < count; i++) { a[i] = arc4random() % (40 - 20 + 1) + 20.5; } } //升序方法 void tempSort(float a[], int x, int y) { if (a[x] > a[y]) { float temp = a[x]; a[x] = a[y]; a[y] = temp; } } //排序函数 void sort(float a[], int count) { for (int i = 0; i < count - 1; i++) { for (int j = 0 ; j < count - 1 - i; j++) { //换位方法 tempSort(a, j, j+1); } } } //输出 void OutputValue(float a[], int count) { printf("\n"); for (int i = 0; i < count; i++) { printf("%.1f ",a[i]); } } //随机产生卡号 void InputCard(int m[],int count) { for (int i = 0; i < count ; i++) { m[i] = arc4random() % (9000 - 1000 + 1) + 1000; } } //随机产生密码 void InputPwd(int m[],int count) { for (int i = 0; i < count ; i++) { m[i] = arc4random() % (200 - 100 + 1) + 100; } } //随机产生money void InputMoney(int m[],int count) { for (int i = 0; i < count ; i++) { m[i] = arc4random() % (10000 - 10 + 1) + 10; } } //判断卡号是否正确 BOOL isCard(int m[],int count ,int username) { BOOL flag = NO; for (int i = 0; i < count; i++) { if (username == m[i]) { flag = YES; } } return flag; } //判断密码是否正确 BOOL isPwd(int password, int pwd){ BOOL flag = NO; if (password == pwd) { flag = YES; } return flag; } //输出卡号和密码 void OutputCard_Pd(int m[], int count) { for (int i = 0; i < count; i++) { printf("%4d ", m[i]); } printf("\n"); } //通过账号获取密码下标. int requestPwd(int username,int m[],int count) { int index = 0; for (int i = 0; i < count; i++) { if (username == m[i]) { index = i; } } return index; } //取款操作 int OutputMoney(int a, int b) { int x = a - b; a = x; return x; } //存款操作 int cunMoney(int a , int b) { int x = a + b; a = x; return x ; }</span>
<span style="font-size:14px;"> #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { //定义2个整形数组 int username[5] = {0};//定义数组,并初始化为0 int password[5] = {0}; int money[5] = {0};//账户余额 InputCard(username, 5); //随机卡号 InputPwd(password, 5); //随机密码 InputMoney(money, 5); //随机money OutputCard_Pd(username, 5); //取出卡号 OutputCard_Pd(password, 5); //取出密码 OutputCard_Pd(money, 5); //随机money int card_jie[5] = {0, 0, 0, 0, 0};//存放被冻结的卡号. while(1){ //死循环. int user = 0; //定义卡号初始化, int s = 0;//如果s为1,冻结,如果为0,继续执行 while (1) { printf("请输入银行卡号:\n"); scanf("%d",&user); //输入卡号 if (isCard(username, 5, user)) { //判断卡号是否存在 printf("卡号正确!\n"); break; //结束循环 }else{ printf("该卡不存在,请重新输入\n"); } } int j = requestPwd(user, username, 5); //获取对应用户名的下标 int pwd = 0; //定义变量密码,初始化为0 //判断卡是否冻结 for (int i = 0; i < 5; i++) { if (card_jie[i] == username[j]) { //判断密码是否存在 s = 1; //如果存储冻结卡号的数组中有这个卡号 s = 1; printf("你的卡已经被冻结,24小时之内不能操作\n"); break; } } //用户名正确,操作. if (isCard(username, 5, user)) { if (s == 0) { //没有被冻结. //账号正确 for (int i = 3; i >= 0; i--) { printf("请输入密码:\n"); scanf("%d",&pwd); if (isPwd(password[j], pwd)) { //判断密码 printf("你输入的密码正确\n"); break; } else { if (i == 1) { //第三次的时候 printf("密码错误,你已经输入3次,24小时之内不能操作.\n"); //卡冻结时把冻结卡号存放在数组中 for (int i = 0; i < 5; i++) { if (card_jie[i] == 0) { card_jie[i] = username[j]; //如果为0,将卡号保存在数组中 //printf("asdasd = %d\n",card_jie[i]); break;//循环结束,挨个存储. } } break; } printf("你输入的密码不正确,你还有%d 次机会,请重新输入\n",i - 1); } } } } //当账号密码正确 int x = money[j]; //定义变量x,将money操作后的值,赋值给x. if(isCard(username, 5, user)){ if (isPwd(password[j], pwd)) {//用户名密码正确后,进行操作. while(1){ int num = 0; printf("请选择你要进行的操作: 1.查询余额 2.取款 3.转账 4.存款 5.退出\n"); scanf("%d",&num); if (num == 1) { //查询余额操作 printf("你的账户余额为:%d\n",x); } else if(num == 2) { //取款 int mon = 0; printf("请输入你要取款的金额:\n"); scanf("%d",&mon); if (x < mon) { printf("你的余额不足!"); } else { x = OutputMoney(x, mon);//取款 printf("取款成功,卡上余额为:%d\n", x); } } else if(num == 3) { //转账 printf("请输入你要转的账户卡号:\n"); int phone = 0; scanf("%d",&phone); if (isCard(username, 5, phone)) { int c = requestPwd(phone, username, 5); //通过卡号,获取卡号的下标. printf("请输入你要转账的金额:\n"); int m = 0; scanf("%d",&m); if (m > x) { printf("余额不足,你不能转账\n"); } else { x = x - m ; money[c] = money[c] + m; //把对方账户钱数加上转账钱数. printf("转账成功!,你所剩余额为:%d\n",x); } } else { printf("你输入的卡号不正确\n"); } } else if(num == 4) { //存款 printf("请输入你要存的钱数:\n"); int y = 0; scanf("%d",&y); x = cunMoney(x, y);//存款操作 printf("存款成功,你的余额为:%d\n", x); } else if(num == 5) { //退出 money[j] = x; //推出时,把原来的余额替换成操作后的. break; } } } } } return 0; }</span>
【学习ios之路:C语言】小程序:实现类似ATM取款机功能.
标签:style blog io ar color os sp for on
原文地址:http://blog.csdn.net/zfx5130/article/details/41875427