#include "LHashL.h"
using namespace std;
//构造函数,初始化散列表;
static int user=0;//全局变量 用以记录账户个数;
static int T_Money=0;
static char adimi_password=666666;
template<class T>
LHList<T>::LHList(int MaxSize)
{
m=MaxSize;
HT=new LNode*[m];
for(int i=0;i<m; i++)
HT[i]=NULL;
}
//析构函数,清空一个散列表;
template <class T>
LHList<T>::~LHList()
{LNode* p;
for(int i=0; i<m; i++){
p=HT[i];
while(p!=NULL){
HT[i]=p->next;
delete p;
p=HT[i];}}
delete [] HT;
}
//闲散列表中插入一个元素;
template <class T>
void LHList<T>::Insert(T item)
{// 计算items 的散列地址;
//int first;
int d=HashAddress(item , m);
//维新元素分配存储节点;
LNode* p=new LNode;
//把新节点擦到链表表头;
p->setname();
p->setID();
p->setAddress();
p->setpassword();
p->data=item;
p->money=0;//默认开户金额为0;
p->next=HT[d];
HT[d]=p;
cout<<"账户建立成功!"<<endl;
cout<<"请妥善保管账户和密码!"<<endl;
//****************时间控制机制*************;
fstream outfile,infile;
char buffer[1024];
SYSTEMTIME s;
GetLocalTime(&s);
sprintf(buffer, "%d-%d-%d %d:%d:%d", s.wYear, s.wMonth, s.wDay, s.wHour, s.wMinute, s.wSecond);
cout<<"北京时间:"<<buffer<<endl;
//**********生成用户文件************;
char *temp=new char[100];
//char *txt=".txt";
sprintf(temp,"%d",item);
strcat(temp,".txt");
outfile.open(temp,ios::app);
outfile<<"*****************账户"<<p->data<<"******************"<<endl;
outfile<<buffer<<' '<<" 开户金额:"<<p->money<<"账户余额:"<<p->money<<endl;
outfile<<"——*——*——*——*——*——*——*——*——*——"<<endl;
outfile.close();
//**********生成文件完毕************;
outfile.open("Inform.txt",ios::app);
outfile<<"*************账户"<<p->data<<"***************"<<endl;
outfile<<"开户时间是:"<<buffer<<endl;
outfile<<"用户名:"<<p->name<<endl;
outfile<<"账号是:"<<p->data<<endl;
outfile<<"身份证号码是:"<<p->IDnumber<<endl;
outfile<<"所在地址是:"<<p->Address<<endl;
outfile<<"——*——*——*——*——*——*——"<<endl;
outfile<<endl;
outfile.close();
//******************Account()*****************
outfile.open("Account.txt",ios::app);
outfile<<buffer<<" "<<"账户:"<<" "<<p->data<<" "<<"注册"<<endl;
outfile<<"目前共有"<<" "<<user<<" "<<"个账户!"<<endl;
outfile<<endl;
outfile.close();
//******************money()*****************
outfile.open("Money.txt",ios::app);
outfile<<buffer<<" "<<"金库账目:"<<" "<<T_Money<<endl;
outfile<<endl;
outfile.close();
}
//冲散列表中查找一个元素;
template <class T>
bool LHList<T>::Search(T item)
{//计算items的散列地址;
int d=HashAddress(item , m);
//得到对应单链表表头指针;
LNode* p=HT[d];
//从该单链表中顺序查找匹配的元素;
while(p!=NULL){
if(p->data==item){
//p2->check_password();
while(p->check_password()){
cout<<"找到!"<<' '<<"账户余额是:"<<p->money<<endl;
fstream infile,outfile;
char buffer[1024];
SYSTEMTIME s;
GetLocalTime(&s);
sprintf(buffer, "%d-%d-%d %d:%d:%d", s.wYear, s.wMonth, s.wDay, s.wHour, s.wMinute, s.wSecond);
cout<<"北京时间:"<<buffer<<endl;
//**********生成用户文件************;
char *temp=new char[100];
//char *txt=".txt";
sprintf(temp,"%d",item);
strcat(temp,".txt");
outfile.open(temp,ios::app);
//outfile<<"***********账户"<<p->data<<"***************"<<endl;
outfile<<buffer<<' '<<" 查询账户:"<<' '<<"账户余额:"<<p->money<<endl;
outfile<<"——*——*——*——*——*——*——"<<endl;
outfile.close();
return true;}}
else p=p->next;}
//失败则返回错误信息;
cout<<"账户不存在!!"<<endl;
return false;
}
//重散列列表中删除 某个元素;
template <class T>
bool LHList<T>::Delete(T item)
{ // items的散列地址;
int d=HashAddress(item , m);
//p指向对应单链表的表头指针;
LNode* p=HT[d];
//若单链表为空,则删除失败并返回;
if(p==NULL)
return false ;
//若表头节点恰为将被删除的节点,这删除它并返回真;
if(p->data==item) {
while(p->check_password()){
HT[d]=p->next;
delete p;
user--;
return true;}}
//否则 从第二个结点开始查找相关元素
//找到后删除 并返回空;
LNode* q=p->next;
while(q!=NULL){
if(q->data=item){while(p->check_password()){
p->next=q->next;
delete p;
user--;
return true;}}
else {p=q;q=q->next;}
}
//没有可以删除的元素,则返回假;
return false;
}
//在账户中存入现金;
template<class T>
bool LHList<T>::Save(T item)
{//计算items的散列地址;
int x;
//X 用来表示用户想要存入的钱;
int d=HashAddress(item , m);
//得到对应单链表表头指针;
LNode* p=HT[d];
//从该单链表中顺序查找匹配的元素;
while(p!=NULL){
if(p->data==item){
while(p->check_password()){
cout<<"找到!"<<"账户余额是:"<<p->money<<endl;
cout<<"请输入您要存的金额数目:"<<endl;
cin>>x;
p->money+=x;
T_Money+=x;
cout<<"存款成功!"<<"您的账户现在有: "<<' '<<p->money<<" "<<"元!"<<endl;
fstream infile,outfile;
char buffer[1024];
SYSTEMTIME s;
GetLocalTime(&s);
sprintf(buffer, "%d-%d-%d %d:%d:%d", s.wYear, s.wMonth, s.wDay, s.wHour, s.wMinute, s.wSecond);
cout<<"北京时间:"<<buffer<<endl;
//**********生成用户文件************;
char *temp=new char[100];
//char *txt=".txt";
sprintf(temp,"%d",item);
strcat(temp,".txt");
outfile.open(temp,ios::app);
//outfile<<"***********账户"<<p->data<<"***************"<<endl;
outfile<<buffer<<' '<<" 用户存款:"<<' '<<x<<endl;
outfile<<"账户余额:"<<p->money<<endl;
outfile<<"——*——*——*——*——*——*——"<<endl;
outfile.close();
//******************money()*****************
outfile.open("Money.txt",ios::app);
outfile<<buffer<<" "<<"金库账目:"<<" "<<T_Money<<endl;
outfile<<endl;
outfile.close();
return true;}}
else p=p->next;}
//失败则返回错误信息;
cout<<"此账户不存在!!"<<endl;
return false;
}
//从账户中取出现金;
template<class T>
bool LHList<T>::Get(T item)
{//计算items的散列地址;
int x;
//X 用来表示用户想要存入的钱;
int d=HashAddress(item , m);
//得到对应单链表表头指针;
LNode* p=HT[d];
//从该单链表中顺序查找匹配的元素;
while(p!=NULL){
if(p->data==item){
while(p->check_password()){
cout<<"找到"<<' '<<"账户余额是:"<<p->money<<endl;
cout<<"请输入您要取的金额数目:"<<endl;
while(cin>>x&&p->money<x){
cout<<"您的账户余额不足!!"<<endl;
cout<<"从新输入更小的数目!"<<endl;
continue;}
p->money-=x;
T_Money-=x;
cout<<"取钱成功!"<<' '<<"账户余额是: "<<' '<<p->money<<endl;
fstream infile,outfile;
char buffer[1024];
SYSTEMTIME s;
GetLocalTime(&s);
sprintf(buffer, "%d-%d-%d %d:%d:%d", s.wYear, s.wMonth, s.wDay, s.wHour, s.wMinute, s.wSecond);
cout<<"北京时间:"<<buffer<<endl;
//**********生成用户文件************;
char *temp=new char[100];
//char *txt=".txt";
sprintf(temp,"%d",item);
strcat(temp,".txt");
outfile.open(temp,ios::app);
//outfile<<"***********账户"<<p->data<<"***************"<<endl;
outfile<<buffer<<' '<<" 用户取款:"<<' '<<x<<endl;
outfile<<"账户余额:"<<p->money<<endl;
outfile<<"——*——*——*——*——*——*——"<<endl;
outfile.close();
//******************money()*****************
outfile.open("Money.txt",ios::app);
outfile<<buffer<<" "<<"金库账目:"<<" "<<T_Money<<endl;
outfile<<endl;
outfile.close();
return true;}}
else p=p->next;}
//失败则返回错误信息;
cout<<"此账户不存在!!"<<endl;
return false;
}
template <class T>
bool LHList<T>::Check_History(T item)
{
fstream outfile,infile;
int d=HashAddress(item , m);
//得到对应单链表表头指针;
LNode* p=HT[d];
//从该单链表中顺序查找匹配的元素;
while(p!=NULL){
if(p->data==item){
while(p->check_password()){
char *temp=new char[100];
//char *