#include
#include
#include
#define LEN sizeof(struct Account)
#define RECORDCOUNT 5
#define MESSAGELENGTH 100
#define NAMELENGTH 100
#define VIEWCOUNT 5
#define DEFAULTBALANCE 0.00
#define SAVE 1
struct Account *head,*tail,*p; /*账户头部和尾部指针*/
struct Account
{
char name[NAMELENGTH]; /* 账户名称 */
float balance; /* 账户余额*/
char message[RECORDCOUNT][MESSAGELENGTH]; /* 交易记录 */
int maxMessageIndex;
struct Account *next; /* 指向下一账户 */
};
void freeMemory(); /*释放内存*/
void removeFirst(struct Account * account); /*移除第一个元素*/
void showMenu(); /*输出选择菜单*/
struct Account * findAccountByName(char name[]); /*通过账户名称找到对应的账户 */
void writeMessage(struct Account * account,char message[]); /* 写入交易记录 */
void init(); /* 创建链表,并返回表头指针 */
void openAccount(); /* 开户 */
void closeAccount(); /* 销户 */
void deposit(); /* 存款*/
void withdraw(); /* 取款 */
void transfer(); /* 转账 */
void print(); /*输出账户列表*/
void printMessage(); /*输出账户交易记录*/
int main()
{
init();
printf("欢迎使用ATM管理系统\n");
showMenu();
char choice;
fflush(stdin);
choice = getchar();
while(choice != '8')
{
switch(choice)
{
case '1':
openAccount();
showMenu();
fflush(stdin);
choice = getchar();
break;
case '2':
closeAccount();
showMenu();
fflush(stdin);
choice = getchar();
break;
case '3':
deposit();
showMenu();
fflush(stdin);
choice = getchar();
break;
case '4':
withdraw();
showMenu();
fflush(stdin);
choice = getchar();
break;
case '5':
transfer();
showMenu();
fflush(stdin);
choice = getchar();
break;
case '6':
print();
showMenu();
fflush(stdin);
choice = getchar();
break;
case '7':
printMessage();
showMenu();
fflush(stdin);
choice = getchar();
break;
default:
printf("无效菜单!\n");
showMenu();
fflush(stdin);
choice = getchar();
break;
}
}
freeMemory(); /*释放内存*/
return 0;
}
void showMenu()
{
printf("请选择 1.开户 2.销户 3.存款 4.取款 5.转账 6.查看所有账户 7. 最近五笔交易 8.退出\n");
}
//通过账户名称找到对应的账户
struct Account * findAccountByName(char name[])
{
if(strlen(name)>0)
{
p = head;
while(p != NULL)
{
if(strcmp(p->name,name)==0)
{
return p;
}
p = p->next;
}
}
return NULL;
}
//写入交易记录
void writeMessage(struct Account * account,char message[])
{
if(account->maxMessageIndex >= RECORDCOUNT)
{
removeFirst(account);
strcpy((account->message)[RECORDCOUNT-1],message);
account->maxMessageIndex = account->maxMessageIndex+1;
}
else
{
strcpy((account->message)[account->maxMessageIndex],message);
account->maxMessageIndex = account->maxMessageIndex+1;
}
}
void removeFirst(struct Account * account)
{
int i=0;
for(i=1; imessage)[i-1],(account->message)[i]);
}
/* 初始化三个默认账户 */
void init()
{
p = (struct Account *)malloc(LEN); /* 创建第一个账户 */
p->balance=DEFAULTBALANCE;
p->next=NULL;
p->maxMessageIndex=0;
strcpy(p->name,"001");
head=p;
tail = p;
p = (struct Account *)malloc(LEN); /* 创建第二个账户 */
p->balance=DEFAULTBALANCE;
p->next=NULL;
p->maxMessageIndex=0;
strcpy(p->name,"002");
head->next = p;
tail = p;
p = (struct Account *)malloc(LEN); /* 创建第三个账户 */
p->balance=DEFAULTBALANCE;
p->next=NULL;
p->maxMessageIndex=0;
strcpy(p->name,"003");
tail->next = p;
tail = p;
}
//输出账户列表信息
void print()
{
int i;
p = head;
i = 1;
while(p != NULL)
{
printf("第%d个账户的数据 账户名称:%s 余额:%.2f\n",i++,p->name,p->balance);
p = p->next;
}
}
//释放内存
void freeMemory()
{
p = head;
while(p != NULL)
{
head = p;
free(head);
p = p->next;
}
}
//开户
void openAccount()
{
char name[NAMELENGTH];
printf("请输入新开立的账户名称 : ");
fflush(stdin);
gets(name);
if(strlen(name)>0)
{
p = (struct Account *)malloc(LEN); /* 创建一个新账户 */
p->balance=DEFAULTBALANCE;
p->next=NULL;
strcpy(p->name,name);
tail->next=p;
tail = p;
}
}
//销户
void closeAccount()
{
char name[NAMELENGTH];
printf("请输入要销户的账户名称 : ");
fflush(stdin);
gets(name);
if(strlen(name)>0)
{
struct Account *pre;
p = head;
pre=head;
int find=0;
while(p != NULL)
{
if(strcmp(p->name,name)==0)
{
if(head == p)
head=p->next;
else if(tail==p)
{
pre->next=NULL;
tail = pre;
}
else
{
pre->next = p->next;
}
find=1;
break;
}
pre = p;
p = p->next;
}
if(find==0)
printf("没有此账户,无法完成销户!\n");
}
}
//存款
void deposit()
{
char name[NAMELENGTH];
printf("请输入要存款的账户名称 :");
fflush(stdin);
gets(name);
p = findAccountByName(name);
if(p!=NULL)
{
float savemoney;
printf("请输入要存款的金额 : ");
scanf("%f",&savemoney);
while(savemoney<=0)
{
printf("存款额不能小于等于零,请重新输入! ");
scanf("%f",&savemoney);
}
p->balance = p->balance+savemoney; /*存入账户金额*/
char message[MESSAGELENGTH];
sprintf(message,"账户%s成功存入%.2f元!\n",name,savemoney);
writeMessage(p,message); /*写入交易记录*/
}
else
printf("没有此账户!\n");
}
//取款
void withdraw()
{
char name[NAMELENGTH];
printf("请输入要取款的账户名称 :");
fflush(stdin);
gets(name);
p = findAccountByName(name);
if(p!=NULL)
{
float savemoney;
printf("请输入要取款的金额 : ");
fflush(stdin);
scanf("%f",&savemoney);
while(savemoney<=0 || savemoney> p->balance)
{
printf("存款额不能小于等于零或者大于余额,请重新输入! ");
scanf("%f",&savemoney);
}
p->balance = p->balance-savemoney; /*存入账户金额*/
char message[MESSAGELENGTH];
sprintf(message,"账户%s成功取出%.2f元!\n",name,savemoney);
writeMessage(p,message); /*写入交易记录*/
}
else
printf("没有此账户!\n");
}
//转账
void transfer()
{
char outname[NAMELENGTH];
char inname[NAMELENGTH];
struct Account *outAccount;
struct Account *inAccount;
printf("请输入要转出的账户名称 :");
fflush(stdin);
gets(outname);
outAccount = findAccountByName(outname);
if(outAccount!=NULL)
{
printf("请输入要转入的账户名称 :");
fflush(stdin);
gets(inname);
inAccount = findAccountByName(inname);
if(inAccount != NULL)
{
float money;
printf("请输入要转账的金额 : ");
fflush(stdin);
scanf("%f",&money);
while(money<=0 || money> outAccount->balance)
{
printf("转账金额不能小于等于零或者大于转出账户余额,请重新输入! ");
scanf("%f",&money);
}
outAccount->balance = outAccount->balance-money; /*减少转出账户金额*/
inAccount->balance = inAccount->balance+money; /*增加转入账户金额*/
char outmessage[MESSAGELENGTH];
sprintf(outmessage,"账户%s成功转出%.2f元!\n",outname,money);
writeMessage(outAccount,outmessage); /*写入转出交易记录*/
char inmessage[MESSAGELENGTH];
sprintf(inmessage,"账户%s成功转入%.2f元!\n",inname,money);
writeMessage(inAccount,inmessage); /*写入转出交易记录*/
}
}
else
printf("没有此转出账户!\n");
}
//查看账户最近五笔交易历史
void printMessage()
{
char name[NAMELENGTH];
printf("请输入要查看最近五笔交易历史的账户名称 :");
fflush(stdin);
gets(name);
p = findAccountByName(name);
if(p!=NULL)
{
int i=0;
int end = (p->maxMessageIndex)>=VIEWCOUNT?VIEWCOUNT:(p->maxMessageIndex);
for(i=0; imessage)[i]);
}
else
printf("没有此账户!\n");
}