数据结构课程设计

注意:请新建“bank.txt”且在bank.txt中输入“x x 1”

main.cpp

/***********************************************************
* 版权所有 (C)2016, ZhouGuoliang。
* *
文件名称: main.cpp
* 文件标识: 无

* 其它说明: 无
* 当前版本: V1.0
* 作 者: 周国亮
* 完成日期: 20161228
* *
**********************************************************/


#include"bank.h"
Bank bank;
int main()
{
   bank.readAccountput();
	while(1)
{
	system("color 1A");
      system("cls");
		show();
		int n;
		cin>>n;
		if(n == 1)
		{
		    bank.append();
		}
		if (n == 2)
		{
			bank.del();
		}

		if (n == 3)
		{
			bank.query();
		}
		if (n == 4)
		{
		bank.cunqv();
		}
		if (n == 5)
		{
			bank.shuchu();
		}
        if(n==6)
        {
             system("cls");
             cout<<"Bye-bye";
            break;
        }


}


	return 0;
}

bank.h

/***********************************************************
* 版权所有 (C)2016, ZhouGuoliang。
* *
文件名称: bank.h
* 文件标识: 无
* 内容摘要: 
class Account
{
public:
	Account();
	Account(string a,string b,double bal);
	void saving(double a);
	void getOutMoney(double a);
    void showme();
	string getname();
	string getId();
	double getmoney();
	Account *next;
	string Account_name;
	string name;
	d
* 其它说明: 无
* 当前版本: V1.0
* 作 者: 周国亮
* 完成日期: 20161228
* *
**********************************************************/


#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED

#include <iostream>
#include <string>
using namespace std;
#include<iomanip>
#include<stdlib.h>
#include<stdio.h>
class Account
{
public:
	Account();
	Account(string a,string b,double bal);
	void saving(double a);
	void getOutMoney(double a);
    void showme();
	string getname();
	string getId();
	double getmoney();
	Account *next;
	string Account_name;
	string name;
	double balance ;
};

class Bank
{
public:
	Bank();
	void append();
	bool baocun();
	bool duqv;
	void del();
	void query();
	void cunqv();
	void shuchu();
	void addlist(Account *acc);
    void saveAccountFile() ;
    void readAccountput();
	Account *accountList;
};

void show();

#endif // BANK_H_INCLUDED

bank.cpp

/***********************************************************
* 版权所有 (C)2016, ZhouGuoliang。
* *
文件名称: bank.cpp
* 文件标识: 无
* 内容摘要: 
void Bank::append()
{
     system("cls");
	string str1,str2;
	cout << "请输入用户账号" << endl;
	cin>>str1;
	cout << "请输入开户人姓名" << endl;
	cin>>str2;
	Account *acc = new Account(str1,str2,0);
	cout<<"增加账户成功"<<endl;
	addlist(acc);
saveAccountFile();
system("pause");
}
* 其它说明: 无
* 当前版本: V1.0
* 作 者: 周国亮
* 完成日期: 20161228
* *
**********************************************************/


#include"bank.h"
Account::Account()
{
	balance = 0;
}
Account::Account(string a,string b,double bal)
{
	Account_name = a;
	name = b;
	balance = bal;
}
void Account::saving(double a)
{
	balance = balance + a;
}
void Account::getOutMoney(double a)
{
	if(a > balance)
		cout<<"余额不足"<<endl;
	else
	{
		balance = balance - a;
		cout << "已取出" << a << "元" <<endl;
	}
}
void Account::showme()
{
	cout << "用户账号为" << Account_name << endl;
	cout << "开户人姓名" << name <<endl;
	cout << setiosflags(ios::fixed)<<setprecision(6)<<"账户余额为" << balance <<endl;
}
string Account::getId()
{
	return Account_name;
}
string Account::getname()
{
	return name;
}
double Account::getmoney()
{
	return balance;
}
void Bank::addlist(Account *acc)
{
	Account *p;
	if(accountList==NULL)
	{
		accountList = acc;
		acc->next =NULL;
	}
	else
	{
		p=accountList;
		while(p)
		{
			if(p->next==NULL)
			{
				p->next=acc;
				acc->next =NULL;
			}
			p= p->next;
		}
	}
}
Bank::Bank()
{
	accountList = NULL;
}

void Bank::append()
{
     system("cls");
	string str1,str2;
	cout << "请输入用户账号" << endl;
	cin>>str1;
	cout << "请输入开户人姓名" << endl;
	cin>>str2;
	Account *acc = new Account(str1,str2,0);
	cout<<"增加账户成功"<<endl;
	addlist(acc);
saveAccountFile();
system("pause");
}

void Bank::del()
{
    system("cls");
	string n;
	cout << "请输入要注销的用户账号" << endl;
	cin>>n;
Account *p=accountList;
Account *a=NULL;
Account *b=accountList;
int j=0,i=0;
while(p)
{
		if(p->getId() == n)
		{
			j=1;
			if(i==0)
			{
				a=accountList;
				accountList=accountList->next;
			}
			else
			{
				a=p;
				b->next=p->next;
			}
			cout<<"注销成功"<<endl;
		}
		b=p;
		p=p->next;
		i=1;
}
	if(j==0)
		cout<<"没有这个账号"<<endl;
	delete a;
saveAccountFile();
 system("pause");
}

void Bank::query()
{
    system("cls");
	string n;
	cout <<"请输入您要查询的用户账号" << endl;
	cin>>n;
Account *p=accountList;
int j=0;
while(p)
{
	if(p->getId()== n )
	{j=1;p->showme();break;}
	p=p->next;
}
	if(j==0)
	{cout<<"没有这个账号"<<endl;}
	 system("pause");
}
void Bank::cunqv()
{
     system("cls");
		string n;
			cout<<"请输入您要存取款的账号"<<endl;
			cin>>n;
			Account *p=accountList;
			int j=0;
			while(p)
			{
						if(p->getId() == n)
				{
							j=1;
					int choice;
					cout<<"1.取款"<<endl;
					cout<<"2.存款"<<endl;
					cout<<"3.返回"<<endl;
					cout<<"请选择"<<endl;
					cin>>choice;
					if(choice == 1)
					{
						double jine;
						cout<<"请输入取款金额"<<endl;
						cin >> jine;
						p->getOutMoney(jine);
						p->showme();
					}
					if(choice == 2)
					{
						double qkuan;
						cout<<"请输入存款金额"<<endl;
						cin>>qkuan;
						p->saving(qkuan);
						p->showme();
					}
					if(choice==3)
					{break;}
				}
						p=p->next;
			}
				if(j==0)
					cout<<"账号输入错误"<<endl;
saveAccountFile();
 system("pause");
}
void Bank::shuchu()
{
    system("cls");
	Account *p=accountList;
	if(p)
	{
			while(p)
		{
			p->showme();
			p=p->next;
		}
	}
		else
	{
		cout<<"请先建立帐号!"<<endl;
	}
	 system("pause");
}
void show()
{
    cout<<"----------------------------------"<<endl;
	cout << "          1.增加账户"<< endl;
	cout << "          2.删除账户" <<endl;
	cout << "          3.查询账户" <<endl;
	cout << "          4.取款和存款" <<endl;
	cout << "          5.查询所有账户" <<endl;
	cout << "          6.退出" <<endl;
	cout << "          请选择" <<endl;
	cout<<"----------------------------------"<<endl;
}
void Bank::saveAccountFile()                //保存用户信息到文件
{
	FILE *fp;
   	Account *p=accountList;
   	char str1[32],str2[32],str[32];
	if((fp=fopen("bank.txt","w"))==NULL)
	{
		printf("没有创建!");
		exit(1);
	}
	while(p)
	{
	     int i;
    for( i=0;i<p->Account_name.length();i++)
        str1[i] = p->Account_name[i];
    str1[i] = '\0';
    for( i=0;i<p->name.length();i++)
        str2[i] = p->name[i];
    str2[i] = '\0';
    sprintf((char*)str,"%f",p->getmoney());
    fprintf(fp,"%s %s %s \n",str1,str2,str);
		p=p->next;
    }
	fclose(fp);
}
void Bank::readAccountput()          //运行前把文件内容读取到电脑内存
{
    FILE *fp;
    char x[32];
    fp=fopen("bank.txt","rb"); //以只读方式打开当前目录下的.txt
    if(fp==NULL)
    {
        printf("无法打开文件\n");
        exit(0);                   //-------- 终止程序 ---------
    }

    int i=0;
  	while(!feof(fp))
	{
        fscanf(fp,"%s %s %s",x,x,x);
		i++;
	}
	fclose(fp);
    FILE *FP;
    FP=fopen("bank.txt","rb"); //以只读方式打开当前目录下的.txt
       	char str2[32],str1[32],str3[32];
       	double str;
    FP=fopen("bank.txt","rb"); //以只读方式打开当前目录下的.txt
    if(FP==NULL)
    {
        printf("无法打开文件\n");
        exit(0);                   //-------- 终止程序 ---------
    }
	int j=1;
	int b=i-1;
		while(!feof(FP))
		{
            fscanf(FP,"%s %s %s",str1,str2,str3);
            str=atoi(str3);
            Account *p = new Account(str1,str2,str);
            addlist(p);
            p=p->next;
        if(j==b)
		break;
	    j++;
		}
    fclose(FP);    //----关闭文件-----
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值