【C++】ATM机模拟系统 :完整窗口实现
下面是一个完整的C++ ATM机模拟系统,使用Windows API实现窗口界面,包含所有基础功能模块。
我将提供完整代码并详细解释实现原理。
#include <windows.h>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
// 全局变量
const string CARD_NUMBER = "1234567890";
const string PIN = "1234";
float balance = 10000.0;
float dailyWithdrawn = 0.0;
int attempts = 0;
bool atmLocked = false;
string transactionHistory = "";
// 控件ID
#define ID_CARD_EDIT 101
#define ID_PIN_EDIT 102
#define ID_LOGIN_BTN 103
#define ID_BALANCE_BTN 104
#define ID_DEPOSIT_BTN 105
#define ID_WITHDRAW_BTN 106
#define ID_EXIT_BTN 107
#define ID_RECEIPT_BTN 108
#define ID_AMOUNT_EDIT 109
#define ID_CONFIRM_BTN 110
#define ID_BACK_BTN 111
#define ID_INFO_TEXT 112
#define ID_HISTORY_LIST 113
// 函数声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateLoginControls(HWND);
void CreateMainControls(HWND);
void CreateTransactionControls(HWND, const string&);
void ProcessLogin(HWND);
void ShowBalance(HWND);
void DepositMoney(HWND);
void WithdrawMoney(HWND);
void PrintReceipt(HWND);
void ResetDailyLimit();
void SaveTransaction(const string&);
void UpdateInfoText(HWND, const string&);
string GetCurrentDateTime();
// 主函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
static TCHAR szAppName[] = TEXT("ATM Simulation");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("银行ATM系统"),
WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 400,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// 窗口过程
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
static HWND hCardEdit, hPinEdit, hLoginBtn, hBalanceBtn, hDepositBtn, hWithdrawBtn;
static HWND hExitBtn, hReceiptBtn, hAmountEdit, hConfirmBtn, hBackBtn, hInfoText, hHistoryList;
switch (message) {
case WM_CREATE:
CreateLoginControls(hwnd);
ResetDailyLimit();
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_LOGIN_BTN:
ProcessLogin(hwnd);
break;
case ID_BALANCE_BTN:
ShowBalance(hwnd);
break;
case ID_DEPOSIT_BTN:
CreateTransactionControls(hwnd, "存款");
break;
case ID_WITHDRAW_BTN:
CreateTransactionControls(hwnd, "取款");
break;
case ID_RECEIPT_BTN:
PrintReceipt(hwnd);
break;
case ID_EXIT_BTN:
DestroyWindow(hwnd);
break;
case ID_CONFIRM_BTN:
if (GetWindowTextLength(GetDlgItem(hwnd, ID_AMOUNT_EDIT)) {
char amountText[20];
GetWindowText(GetDlgItem(hwnd, ID_AMOUNT_EDIT), amountText, 20);
float amount = atof(amountText);
if (GetWindowTextLength(GetDlgItem(hwnd, ID_CONFIRM_BTN)) {
char btnText[20];
GetWindowText(GetDlgItem(hwnd, ID_CONFIRM_BTN), btnText, 20);
if (strcmp(btnText, "存款确认") == 0) {
balance += amount;
SaveTransaction("存款: +" + string(amountText) + "元");
UpdateInfoText(hwnd, "存款成功!当前余额: " + to_string(balance) + "元");
}
else if (strcmp(btnText, "取款确认") == 0) {
if (amount > balance) {
UpdateInfoText(hwnd, "余额不足!");
}
else if (amount > 2000) {
UpdateInfoText(hwnd, "单笔取款不能超过2000元");
}
else if (dailyWithdrawn + amount > 20000) {
UpdateInfoText(hwnd, "今日取款已超限额(20000元)");
}
else {
balance -= amount;
dailyWithdrawn += amount;
SaveTransaction("取款: -" + string(amountText) + "元");
UpdateInfoText(hwnd, "取款成功!当前余额: " + to_string(balance) +