首页 > 代码库 > C语言 实现ATM系统
C语言 实现ATM系统
主要用到了指针、结构体、数组、链表、文件读取
由于平时用java,所以感觉自己的代码还是可以看看的,尽量用面向对象的思想去写,这也使得原本近千行代码的程序只用了不到一半的代码完成。
User.h //用户对象
struct User { char UserAccount[100]; char UserPassword[100]; int Money; struct User *next; };
UI.h //就是一个简单的菜单显示
int showMenu(char MenuItem[][20], int itemCount){ system("cls"); for (int i = 0; i < 10;i++){ printf("\n"); } printf("***************************************************\n"); for (int i = 0; i < itemCount; i++){ printf("* "); printf("%d.", i+1); int Str_size = 20; for (int j = 0; j < 10; j++){ if (MenuItem[i][j] != '\0'){ putchar(MenuItem[i][j]); } else{ putchar(' '); } } printf(" *\n"); } printf("***************************************************"); return 0; }
FileContrller.h用于文件相关的操作,还有初始化
void writeIntoFIle(FILE *fp, char path[]){ fopen_s(&fp, path, "w+"); struct User *temp = (struct User *)malloc(sizeof(struct User)); temp = head; for (int i = 0; i < 5; i++){ // printf("%s %s %d\n", temp->UserAccount, temp->UserPassword, temp->Money); fprintf(fp, "%s %s %d\n", temp->UserAccount, temp->UserPassword, temp->Money); if (temp->next != NULL) temp = temp->next; } } void initFile(FILE *fp, char path[]){ //fopen_s(&fp, path, "w+"); if (feof(fp)){ fprintf(fp, "%s %s %d\n", "6210300022352323", "123123", 10000); fprintf(fp, "%s %s %d\n", "6210300022352527", "132132", 1000000); fprintf(fp, "%s %s %d\n", "6210303002352323", "123123", 10000); fprintf(fp, "%s %s %d\n", "6210300202235233", "123123", 10000); fprintf(fp, "%s %s %d\n", "6213002323523233", "123123", 10000); } // fclose(fp); } void initdata(FILE *fp, char path[]){ /*printf("%d", fopen_s(&fp, path, "r+")); printf("%d", fopen_s(&fp, path, "r+")); printf("%d",fgetc(fp)==EOF);*/ if (fopen_s(&fp, path, "r+") == 0 && fgetc(fp) == EOF){ initFile(fp, path); } rewind(fp); struct User *user; Current_user = user = (struct User *)malloc(sizeof(struct User)); char temp[100]; //_getch(); while (fscanf_s(fp, "%s", &user->UserAccount, 100) != EOF){ fscanf_s(fp, "%s", &user->UserPassword, 100); fscanf_s(fp, "%d\n", &user->Money, 100); struct User *nextuser; nextuser = (struct User *)malloc(sizeof(struct User)); printf("%s %s %d\n", user->UserAccount, user->UserPassword, user->Money); if (head == NULL) head = user; user->next = nextuser; user = nextuser; } fclose(fp); }
AccountController.h//与账户相关的操作,查找账户,验证密码什么的
bool checkAccount(char account[],struct User *u){ struct User *temp = (struct User *)malloc(sizeof(struct User)); struct User *headCopy = (struct User *)malloc(sizeof(struct User)); temp = head; *headCopy =* head; while (temp != NULL){ bool isExist = true; /*printf("first%s %d\n", head->UserAccount, head->Money); printf("temp%s %d\n", temp->UserAccount, temp->Money);*/ for (int i = 0; i < 16; i++) { if (account[i]!=temp->UserAccount[i]) { isExist = false; break; } } if (isExist) { u=temp; Current_user = u; //*head =* headCopy; /* printf("findout%s\n",u->UserAccount); printf("afterchange%s %s\n", Current_user->UserAccount, head->UserAccount); printf("headcopy%s %s\n", headCopy->UserAccount, headCopy->UserPassword); _getch();*/ return true; } temp = (temp->next); } return false; } bool checkPassword(char Userpwd[], char Inputpwd[]){ for (int i = 0; i<6; i++){ if (Userpwd[i] != Inputpwd[i]){ return false; } } return true; } void showAccount(char account[]){ int length = strlen(account); if (length>16) length=16; for (int i = 0; i < length; i++){ putchar(account[i]); if ((i+1) % 4 == 0&&i!=0){ putchar(' '); } } } void showPassword(int length){ if (length>6) length = 6; for (int i = 0; i < length; i++){ _putch('*'); } } void InputAccount(char UserAccount[]){ int i = 0; system("cls"); printf("请输入用户名:"); while ((UserAccount[i] = _getch()) != '\r'&&i<16){ if (UserAccount[i] == 8&&i>0){ UserAccount[i] = '\0'; UserAccount[i-1] = '\0'; i--; } else if (UserAccount[i] >= '0' && UserAccount[i] <= '9'){ if (i <= 15) i++; } system("cls"); printf("请输入用户名:"); showAccount(UserAccount); } } void InputPassword(char account[],char pwd[]){ int i = 0; system("cls"); printf("用户名:"); showAccount(account); printf("\n请输入密码:"); while ((pwd[i] = _getch()) != '\r'){ if (pwd[i] == 8){ pwd[i] = '\0'; pwd[i - 1] = '\0'; i--; } else if (pwd[i] >= '0' && pwd[i] <= '9'){ if (i <= 5) i++; } system("cls"); printf("用户名:"); showAccount(account); printf("\n请输入密码:"); showPassword(strlen(pwd)); } } void WithDraw(int Count){ if (Count > Current_user->Money){ printf("取款金额超出存款"); return; } else { system("cls"); printf("取款金额%d\n按任意键返回上一菜单",Count); Current_user->Money -= Count; } } void Deposit(int Count){ system("cls"); printf("存款金额%d\n按任意键返回上一菜单", Count); Current_user->Money += Count; }
主程序入口
Main.cpp
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> struct User *head, *Current_user; #include "User.h" #include "UI.h" #include "AccountController.h" #include "FileController.h" int InputMoney(){ int result = 100; char inputItem = '\0'; int i = 0; system("cls"); printf("输入金额:"); while ((inputItem = _getch()) != '\r'){ system("cls"); printf("输入金额:"); if (inputItem >= '0'&&inputItem <= '9'){ if (result != 100){ result *= 10; result += (inputItem - 48) * 100; } else { result = (inputItem - 48) * 100; } } if (result > 5000){ printf("取款限额为5000"); result = 0; } printf("\n%d", result); } return result; } int SelectMoney(){ int result = 100; char inputItem = '\0'; char MoneyItem[][20] = { "100", "200", "500", "1000", "输入金额" }; showMenu(MoneyItem, 5); while ((inputItem = _getch()) >='1'&&inputItem<='5') { switch (inputItem) { case '1': return 100; break; case '2': return 200; break; case '3': return 500; break; case '4': return 1000; break; case '5': return InputMoney(); break; default: printf("请输入正确选项\n"); } } } bool OperationMenu(){ char input_seletor = '\0'; int Money = 0; struct User *temp = (struct User *)malloc(sizeof(struct User)); char newPassword[7] = { '\0' }; char confirmpassword[7] = { '\0' }; char LoginMenu[][20] = { "取款", "存款", "查询余额", "修改密码" ,"退卡"}; showMenu(LoginMenu, 5); while ((input_seletor = _getch()) >= '1'&&input_seletor <= '5') { switch (input_seletor) { case '1': Money = SelectMoney(); WithDraw(Money); break; case '2': Money = SelectMoney(); Deposit(Money); break; case '3': system("cls"); printf("当前余额:%d\n按任意键返回", Current_user->Money); break; case '4': system("cls"); InputPassword(Current_user->UserAccount, newPassword); system("cls"); printf("再次输入以确认密码\n,按任意键继续输入"); _getch(); InputPassword(Current_user->UserAccount, confirmpassword); if (checkPassword(newPassword, confirmpassword)){ for (int i = 0; Current_user->UserPassword[i] != '\0';i++){ Current_user->UserPassword[i] = newPassword[i]; } printf("%s %s ",newPassword,confirmpassword); printf("\n密码修改成功!"); } else{ system("cls"); printf("两次密码输入不一致"); } break; case '5': return true; break; default: break; } _getch(); break; } OperationMenu(); } void startATM(){ FILE *fp = NULL; char path[] = "C:\\Users\\user\\Desktop\\Account.txt"; initdata(fp, path);//初始化数据 int Islogin = 0; char input_seletor = '\0'; char UserAccount[100] = { '\0' }; char UserPwd[100] = { '\0' }; char InputPwd[100] = { '\0' }; InputAccount(UserAccount); //strcpy_s(UserAccount, "6210300022352527"); if (checkAccount(UserAccount, Current_user)){ while (Islogin<=2){ InputPassword(UserAccount, InputPwd); //strcpy_s(InputPwd, "132132"); if (checkPassword(Current_user->UserPassword, InputPwd)){ printf("login successful\n"); if (OperationMenu()){ break; } Islogin = 0; } else{ Islogin++; printf("password error"); } } if (Islogin >= 2){ system("cls"); printf("密码三次输入错误,吞卡\n"); } else{ writeIntoFIle(fp,path); system("cls"); printf("数据保存成功"); } } _getch(); } int main(){ startATM(); return 0; }
C语言 实现ATM系统
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。