#include <iostream>//C++头文件
#include "stdio.h"
#include "string"// 包含string 数据类型
using namespace std;//c++命名空间
struct client //客户的结构体
{
string name;//姓名
string mobile;//手机号
int cardnumber;//卡号
string password;//密码
float balance;//余额
client *next;//指向下一个节点
};
client *head=NULL;//全局变量头节点,里面装有第一个客户信息
int kahao=1000000;//卡号变量
int compare(int );
void addclient(client *a)//添加一个新客户
{
client *p;//创建一个临时指针,将会指向头节点。
if(head==NULL)//如果是第一个客户,添加到头节点
{
head =a;
head->next=NULL;
cout<<"注册成功"<<endl;
}
else
{
p = head;
while(p)//只要p不为空就循环
{
if(p->next==NULL)//找到尾节点
{
p->next =a;
a->next =NULL;
cout<<"注册成功"<<endl;
}
p= p->next;
}
}
}
void display()//输出所有客户信息。
{
client *p;
p=head;
while(p)
{
cout<<p->name<<endl;
cout<<p->cardnumber<<endl;
p=p->next;
}
}
void deleteclient()//删除客户信息。
{
int cardnumber;
cout<<"请输入卡号"<<endl;
cin>>cardnumber;
if(compare(cardnumber)==1)//若账号密码正确则compare函数返回值为1
{
client *p,*pre;
if (head==NULL)
{
cout<<"该卡号不存在,无法注销。"<<endl;
return;
}
p =pre=head;
while(p)
{
if (p->cardnumber==cardnumber)
{
if(p==head)
{
head = head->next;
free(p);
p=NULL;
cout<<"注销成功"<<endl;
return;
}
else
{
pre->next =p->next;
free(p);
p=NULL;
cout<<"注销成功"<<endl;
return;
}
}
else
{
pre =p;
p= p->next;
}
}
}
}
int compare(int cardnumber)//判断卡号密码是否正确。
{
client *p=head;
string password;
while(p)
{
if(p->cardnumber==cardnumber)
{
cout<<"请输入密码"<<endl;
cin>>password;
if(p->password==password)
{
return 1;
}
}
p=p->next;
}
cout<<"卡号或密码错误"<<endl;
return 0;
}
void drawmoney()//取款
{
float money;
int cardnumber;
cout<<"请输入卡号"<<endl;
cin>>cardnumber;
if(compare(cardnumber)==1)
{
client *p,*pre;
if (head==NULL)
{
cout<<"卡号不存在"<<endl;
return;
}
p =pre=head;
while(p)
{
if (p->cardnumber==cardnumber)
{
cout<<"请输入取款金额"<<endl;
cin>>money;
if(p->balance-money<0)
{
cout<<"余额不足"<<endl;
return;
}
else
{
p->balance=p->balance-money;
cout<<"取款成功"<<endl;
return;
}
}
else
{
pre =p;
p= p->next;
}
}
}
}
void depositmoney()//存款
{
float money;
int cardnumber;
cout<<"请输入卡号"<<endl;
cin>>cardnumber;
if(compare(cardnumber)==1)
{
client *p,*pre;
if (head==NULL)
{
cout<<"卡号不存在"<<endl;
return;
}
p =pre=head;
while(p)
{
if (p->cardnumber==cardnumber)
{
cout<<"请输入存款金额"<<endl;
cin>>money;
p->balance=p->balance+money;
cout<<"存款成功"<<endl;
return;
}
else
{
pre =p;
p= p->next;
}
}
}
}
void balanceinquiry()//查询余额
{
int cardnumber;
cout<<"请输入卡号"<<endl;
cin>>cardnumber;
if(compare(cardnumber)==1)
{
client *p,*pre;
if (head==NULL)
{
cout<<"卡号不存在"<<endl;
return;
}
p =pre=head;
while(p)
{
if (p->cardnumber==cardnumber)
{
cout<<"您的余额为:"<<p->balance<<endl;
return;
}
else
{
pre =p;
p= p->next;
}
}
}
}
void menu()//菜单
{
cout<<"1开户,2注销,3输出所有用户信息,4存款,5取款,6查询余额"<<endl;
}
int main()
{
int imp;
menu();
while(1)
{
cout<<"请输入选项"<<endl;
cin>>imp;
if(imp==1)//开户
{
cout<<"姓名:";
client *t;
t=new client;
cin>>t->name;
cout<<endl;
cout<<"手机号:";
cin>>t->mobile;
cout<<endl;
cout<<"请输入密码:";
cin>>t->password;
cout<<endl;
cout<<"您的卡号为:"<<kahao<<endl;
t->cardnumber=kahao;
t->balance=0;
kahao++;
addclient(t);
}
else if(imp==2)//注销卡号
{
deleteclient();
}
else if(imp==3)//输出所有用户信息
{
display();
}
else if(imp==4)//存款
{
depositmoney();
}
else if(imp==5)
{
drawmoney();//取款
}
else if(imp==6)//查询余额
{
balanceinquiry();
}
else
{
cout<<"错误选项"<<endl;
}
menu();
}
return 0;
}