cuối môn word
cuối môn word
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define MAX_ACCOUNTS 5
typedef struct
{
int pin;
long long accountNumber;
long long balance;
char accountName[100];
} ACC;
// tao so the bat ki
long long randomSoThe(long long soThe)
{
srand(time(NULL));
soThe = rand() % (9999999999999999 -
1020000000000000 + 1) + 1020000000000000;
return soThe;
}
// tao the ATM
void createATMAccount(long long soThe, int
password, long long main_balance, char
main_name[100])
{
FILE *f = fopen("account-number.dat", "a"); //
Mở file để append thông tin tài khoản mới
if (f != NULL)
{
ACC newAccount;
newAccount.pin = password;
newAccount.balance = main_balance;
newAccount.accountNumber = soThe;
strcpy(newAccount.accountName, main_name);
fclose(f);
}
}
int accountIndex = 0;
while (1)
{
int result = fscanf(f, "pin:%d\nbalance:
%lld\naccountNumber:%lld\naccountName:%99[^\n]\n\
n",
&accounts[accountIndex].pin,
&accounts[accountIndex].balance,
&accounts[accountIndex].accountNumber,
accounts[accountIndex].accountName);
if (result == EOF || accountIndex >=
MAX_ACCOUNTS)
{
break;
}
accountIndex++;
}
fclose(f);
}
// ghi vao file dung format
void writeAccountsToFile(ACC accounts[], int count)
{
FILE *f = fopen("main_account.txt", "w");
if (f != NULL)
{
for (int i = 0; i < count; i++)
{
fprintf(f, "pin:%d\nbalance:%lld\
naccountNumber:%lld\naccountName:%s\n\n",
accounts[i].pin,
accounts[i].balance,
accounts[i].accountNumber,
accounts[i].accountName);
}
fclose(f);
}
}
// tim con tro tai khoan
ACC *findAccountByNumber(ACC accounts[], int count,
long long targetAccountNumber)
{
for (int i = 0; i < count; i++)
{
if (accounts[i].accountNumber ==
targetAccountNumber)
{
return &accounts[i];
}
}
return NULL; // Trả về NULL nếu không tìm thấy
tài khoản
}
// cập nhập số dư
void updateAccountBalance(ACC accounts[], int
count, long long targetAccountNumber, long long
newBalance)
{
ACC *accountToUpdate =
findAccountByNumber(accounts, count,
targetAccountNumber);
if (accountToUpdate != NULL)
{
accountToUpdate->balance = newBalance;
}
}
scanf("%c", &choice);
ACC *targetAccount =
findAccountByNumber(accounts, MAX_ACCOUNTS,
main_stk);
if (targetAccount != NULL && main_stk ==
targetAccount->accountNumber && password ==
targetAccount->pin)
{
main_stk = targetAccount-
>accountNumber;
password = targetAccount->pin;
main_balance = targetAccount->balance;
strcpy(main_name, targetAccount-
>accountName); // Sử dụng strcpy để gán chuỗi vào
main_name
// printf("%d\n", password);
// printf("%s", main_name);
}
else
{
flag2++;
goto nhap;
}
}
else if (choice == 'N' || choice == 'n')
{
return 0;
}
else
{
flag2++;
goto nhap;
}
case 2:
withDrawl = 200000;
break;
case 3:
withDrawl = 500000;
break;
case 4:
withDrawl = 1000000;
break;
case 5:
withDrawl = 2000000;
break;
default:
printf(" Nhap so tien can rut(VND):
");
scanf("%lld", &withDrawl);
break;
}
if (withDrawl > money)
{
printf("\n So du tai khoan khong
du:\n");
printf("------------------------------\n");
goto pick;
}
printf("------------------------------\
n");
printf(" Rut tien thanh cong!\n");
printf("------------------------------\
n");
long long newBalance = money -
withDrawl;
updateAccountBalance(accounts,
MAX_ACCOUNTS, targetAccount->accountNumber,
newBalance);
writeAccountsToFile(accounts,
MAX_ACCOUNTS);
}
else if (n == 3)
{
long long transfer;
long long current_balance =
targetAccount->balance;
long long transfer_account;
long long new_balance;
long long new_transfered_balance;
//
chuyen1:
printf("------------------------------\
n");
printf(" Transfer to account: ");
scanf("%lld", &transfer_account);
ACC *check =
findAccountByNumber(accounts, MAX_ACCOUNTS,
transfer_account);
if (check == NULL)
{
printf("\n Khong ton tai tai
khoan:\n");
goto chuyen1;
}
printf("------------------------------\
n");
chuyen2:
printf(" Money to transfer(VND): ");
scanf("%lld", &transfer);
if (transfer > current_balance)
{
printf("\n So du tai khoan khong
du:\n");
printf("------------------------------\n");
goto chuyen2;
}
printf("------------------------------\
n");
printf(" Transfer complete!\n");
printf("------------------------------\
n");
//
ACC *targetAccount2 =
findAccountByNumber(accounts, MAX_ACCOUNTS,
transfer_account);
new_balance = current_balance -
transfer;
new_transfered_balance =
targetAccount2->balance + transfer;
updateAccountBalance(accounts,
MAX_ACCOUNTS, targetAccount->accountNumber,
new_balance);
updateAccountBalance(accounts,
MAX_ACCOUNTS, targetAccount2->accountNumber,
new_transfered_balance);
writeAccountsToFile(accounts,
MAX_ACCOUNTS);
}
else if (n == 4)
{
int newPin;
checkpin:
printf("------------------------------\
n");
printf(" Ma pin moi(6 number): ");
scanf("%d", &newPin);
if (newPin < 100000 || newPin > 999999)
{
printf(" Ma pin khong hop le:\n");
goto checkpin;
}
printf("------------------------------\
n");
printf("doi ma pin thanh cong!\n");
updateAccountPin(accounts,
MAX_ACCOUNTS, targetAccount->accountNumber,
newPin);
writeAccountsToFile(accounts,
MAX_ACCOUNTS);
}
else if (n == 5)
{
// khai bao gia tri ATM moi
long long soThe;
int new_password;
long long new_balance;
char x[100];
int space = 0;
// nhap gia tri cua ATM
printf("==============================\
n");
printf(" VTC Academy\n");
printf("==============================\
n");
printf(" Create ATM Card\n");
printf("------------------------------\
n");
x:
printf(" Input Account Name : ");
getchar();
fgets(x, 100, stdin);
//chuan hoa xau ki tu
for (int i = 0; i < strlen(x); i++)
{
if (x[i] == ' ')
{
space++;
}
else if (islower(x[i]))
{
space = 0;
printf("ten tai khoan khong hop
le\n");
goto x;
}
else if(x[i]==' '&&x[i+1]==' '){
space = 0;
printf("ten tai khoan khong hop
le\n");
goto x;
}
else if(x[0]==' '){
space = 0;
printf("ten tai khoan khong hop
le\n");
goto x;
}
}
if (space <2||space>3)
{
printf("ten tai khoan khong hop le\
n");
space=0;
goto x;
}
soThe:
printf(" Input accountNumber : ");
scanf("%lld", &soThe);
if (soThe < 1000000000000 || soThe >
9999999999999)
{
printf("so the khong hop le:\n");
goto soThe;
}
pincode:
printf(" Input Pin code : ");
scanf("%d", &new_password);
if (new_password < 100000 ||
new_password > 999999)
{
printf("ma pin khong hop le\n");
goto pincode;
}
printf(" Input balance : ");
scanf("%lld", &new_balance);
createATMAccount(soThe, new_password,
new_balance, x);
printf("Create complete!\n");
}
else if (n == 6)
{
printf("ket thuc chuong trinh");
return 0;
}
else
{
menu++;
goto console;
}
}
return 0;
}
FILE Lưu TRỮ main_account.txt:
Menu 1:
Menu 2:
Menu 3:
Menu 4:
Menu 5:
Menu 6