You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define BUFFERSIZE 1024
#define MAXACCOUNT 1000
#define ADMINISTRATIOR 123456
typedef struct bankaccount//定义账户格式
{
int account;
int key;
char name[32];
float balance;
}BANKACCOUNT;
BANKACCOUNT accountcollection[MAXACCOUNT];
int cur_account = 0;
void insert_account(FILE *fp, int accountnum)//新建账户
{
BANKACCOUNT newaccount;
newaccount.account = accountnum;
int subkey;
printf("请输入账号信息\n");
printf(">>您的账号为:");
printf("%d\n", accountnum);
printf(">>请您输入密码:");
scanf("%d",&(newaccount.key));
system("cls");
printf("请您再次输入刚才的密码:\n");
do{
scanf("%d", &subkey);
getchar();
if (subkey == newaccount.key)
break;
else
printf("两次的密码不一样!\n请再次输入第一次的密码或输入0退出注册\n");
}while (subkey != 0);
if (subkey != 0)
{
printf("密码设置成功!\n 请您完善个人信息\n");
printf(">>姓名:");
scanf("%s",newaccount.name);
printf(">>请您输入初始余额:");
scanf("%f",&(newaccount.balance));
fseek(fp,0L,SEEK_END);
fprintf(fp,"%d %d %s %.2f\n",newaccount.account,newaccount.key,newaccount.name,newaccount.balance);
printf("账户创建成功!\n");
}
else
printf("创建失败!");
}
void store_account(FILE*fp)
{
int i = 0;
for(i = 0;i<cur_account-1;i++)
fprintf(fp,"%d %d %s %.2f\n",
accountcollection[i].account,accountcollection[i].key,accountcollection[i].name,accountcollection[i].balance);
}
void get_account(FILE *fp)//查询账号信息
{
int accountnum;
int key;
char name[32];
float balance;
int i =0,j;
char buffer[BUFFERSIZE];
int len;
cur_account = 0;
fseek(fp,0,SEEK_SET);
while(!feof(fp)) // 因为feof()最后会读2遍所以最后curAccount多加了1
{
fscanf(fp,"%d %d %s %f",&accountnum,&key,name,&balance);
accountcollection[cur_account].account = accountnum;
accountcollection[cur_account].key = key;
strcpy(accountcollection[cur_account].name ,name);
accountcollection[cur_account].balance = balance;
cur_account++;
}
}
void list_account(FILE *fp)
{
int i =0;
printf("合计%d个账号:\n",cur_account-1);// cur_account减去多加的1
for(i = 0;i< cur_account-1;i++)
{
printf("账户:%d:\n",i+1);
printf("账号:%d\n",accountcollection[i].account);
printf("姓名:%s\n",accountcollection[i].name);
printf("余额:%.2f\n",accountcollection[i].balance);
}
}
int search_key(FILE *fp,int key,int i)
{
if (accountcollection[i].key == key)
return 1;
else
return 0;
}
int search_account(FILE *fp,int accountnum,int flag)
{
int i =0;
for(i = 0;i< cur_account-1;i++)
{
if(accountcollection[i].account == accountnum)
{
if(flag == 1)
{
printf("账户:%d:\n",i+1);
printf("账号:%d\n",accountcollection[i].account);
printf("姓名:%s\n",accountcollection[i].name);
printf("余额:%.2f\n",accountcollection[i].balance);
}
return i + 1;
}
}
return 0;
}
void del_account(FILE *fp,int accountnum)
{
int i, key;
if(search_account(fp,accountnum,1)==0)
printf("无法找到此账户!\n");
else
{
i = search_account(fp,accountnum,0) - 1;
printf("请输入密码\n");
scanf("%d",&key);
while (!search_key(fp, key, i) && key != 0)
{
system("cls");
printf("您输入的密码有误!\n请重新输入或输入0放弃删除\n");
scanf("%d", &key);
}
system("cls");
if (key)
{
for(i = 0;i<cur_account-1;i++)
if(accountcollection[i].account != accountnum)
fprintf(fp,"%d %d %s %.2f\n",
accountcollection[i].account,accountcollection[i].key,accountcollection[i].name,accountcollection[i].balance);
printf("注销成功!\n");
}
else
printf("注销失败!\n");
}
}
int main(void)
{
FILE *fp;
int accountnum;
int i;
float withdraw, store;
do
{
system("cls"); //清屏
puts("********************************************");
puts("可选操作: ");
puts("1 : 创建新账户 ");
puts("2 : 管理员权限 ");
puts("3 : 查询账户 ");
puts("4 : 注销账户 ");
puts("5 : 存取款 ");
puts("6 : 结束使用 ");
puts("********************************************");
printf("请选择功能:");
scanf("%d",&i);
system("cls"); //清屏
if (i < 7 && i > 0)
{
switch(i)
{
case 1:
if(!(fp = fopen("account.txt","a+")))
{
printf("打开账户文件失败!\n");
exit(0);
}
printf("请您输入初始账户:\n");
scanf("%d", &accountnum);
while (search_account(fp,accountnum,0) && accountnum != 0)
{
printf("该账户号已被占用!\n您可以继续输入账户号或连续输入两次0结束创建。\n");
scanf("%d\n", &accountnum);
}
if(accountnum != 0)
insert_account(fp, accountnum);
else
printf("创建失败!\n");
printf("点击回车返回主菜单!\n感谢您的使用!");
getch();
fclose(fp);
break;
case 2:
if(!(fp = fopen("account.txt","r")))
{
printf("该系统暂无用户信息!\n");
exit(0);
}
printf("请输入管理员密码:\n");
long int n;
scanf("%ld", &n);
while (n != ADMINISTRATIOR)
{
printf("请输入正确的密码或输入0返回上一层:\n");
scanf("%ld", &n);
if (n == 0)
break;
}
if (n != 0)
{
get_account(fp);
list_account(fp);
fclose(fp);
printf("点击回车结束本次操作!\n感谢您的使用!\n");
getch();
}
break;
case 3:
printf("请输入账号:\n");
scanf("%d",&accountnum);
if(!(fp = fopen("account.txt","r")))
{
printf("打开账户文件失败!\n");
exit(0);
}
get_account(fp);
if(!search_account(fp,accountnum,1))
printf("此账户不存在:%d\n",accountnum);
fclose(fp);
printf("点击回车结束本次操作!\n感谢您的使用!");
getch();
break;
case 4:
printf("请输入账号:\n");
scanf("%d",&accountnum);
if(!(fp = fopen("account.txt","r")))
{
printf("打开账户文件失败!\n");
exit(0);
}
get_account(fp);
fclose(fp);
if(!(fp = fopen("account.txt","w+")))
{
printf("打开账户文件失败!\n");
exit(0);
}
del_account(fp,accountnum);
fclose(fp);
printf("点击回车结束本次操作!\n感谢您的使用!");
getch();
break;
case 5:
printf("请输入账号\n");
scanf("%d",&accountnum);
if(!(fp = fopen("account.txt","r")))
{
printf("打开账户文件失败!\n");
exit(0);
}
get_account(fp);
fclose(fp);
if(!(fp = fopen("account.txt","w+")))
{
printf("打开账户文件失败!\n");
exit(0);
}
if(search_account(fp,accountnum,0))
{
int i = search_account(fp,accountnum, 0) - 1;
int key;
do{
printf("请输入正确的密码或输入0退出该功能项");
scanf("%d", &key);
system("cls");
if(search_key(fp, key, i))
break;
}while ( key != 0);
while (key != 0)
{
system("cls");
printf("********************************************\n");
printf("请输入开头的数字选择您要进行的操作\n");
printf("0 :退出选择\n");
printf("1 :取款\n");
printf("2 :存款\n");
printf("********************************************\n");
scanf("%d", &key);
system("cls");
if (key >= 0&&key <=2)
{
switch(key)
{
case 1:
printf("请您输入您要取出的金额:");
scanf("%f", &withdraw);
if(accountcollection[i].balance > withdraw)
{
accountcollection[i].balance -= withdraw;
printf("\n您现在的余额为 %.2f¥\n", accountcollection[i].balance);
}
else
printf("您的余额不足\n");
printf("点击回车继续操作\n");
getch();
break;
case 2:
printf("请您输入您要存入的金额:");
scanf("%f", &store);
accountcollection[i].balance += store;
printf("\n您现在的余额为 %.2f¥\n", accountcollection[i].balance);
printf("点击回车继续操作\n");
getch();
break;
default :
printf("感谢使用\n");
break;
}
}
else
{
printf("无效指令请输入1 —2中的任意一个整数.\n");
printf("点击回车继续操作\n");
getch();
}
}
}
else
printf("账户不存在\n");
store_account(fp);
fclose(fp);
printf("点击回车继续操作\n");
getch();
break;
default:
printf("感谢您的使用!\n");
break;
}
}
else
{
printf("无效指令请输入1 —6中的任意一个整数.\n");
printf("点击回车继续操作\n");
getch();
}
}while(i != 6);
return 0;
}