linux ATM自定取款机简单实现

首先是在linux地下实现的,创建了四个文件,主要实现流程:

注册-登陆-存款-取款-转账-更改密码-查询个人信息-显示全部账户-退出系统

废话不多说,直接看代码:

Blank.h

#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <pthread.h>
using namespace std;

#define MAX_SIZE 65535
//互斥锁的初始化
static pthread_mutex_t mutex_lock=PTHREAD_MUTEX_INITIALIZER;

//定义用户的两种状态,在线和已注销
enum STATUS{ONLINE,LOGOUT};

//有一个缺点注销后的账号不能再使用

struct user
{
    string _name; //用户名
    string _id;  //身份证
    string _block_name; //卡号
    string _passwd;    //密码
    int _money;     //存的钱
    int _total_money; // 总共余额
//    STATUS status;   //用户的状态

    void insert_user(const string &name="",const string &id="",const string &block_name="",	    const string &passwd="",int money=0)
    {
	_name=name;
	_id=id;
	_block_name=block_name;
	_passwd=passwd;
	_money=money;
	_total_money=money;
	//status=ONLINE;
    }
    bool operator !=(const user &s)
    {
	return (strcmp(_block_name.c_str(),(s._block_name).c_str())!=0);
    }
};
//注册--》登陆--》存款--》取款--》查询--》显示全部信息--》更改密码--》注销用户--》退出系统
class Bank
{
    private:
	user value_user;
	vector<struct user> vector_user;
    private:
	Bank(const Bank &b);
	Bank& operator=(const Bank &b);
    public:
	Bank()
	{}
	~Bank() {}
	bool operator !=(const user &s)
	{
	    return value_user.operator!=(s);
	}
    public:
	//转账
	bool transfer_account(const string& block_name,const int number)
	{
	    string tmp_block_name;
	    cout<<"please enter block_name:";
	    fflush(stdout);
	    cin>>tmp_block_name;
	    string passwd;
	    cout<<"please enter passwd:";
	    fflush(stdout);
	    cin>>passwd;
	    vector<struct user>::iterator tmp=Find(tmp_block_name,passwd);
	    if(tmp !=vector_user.end())
	    {
		if(tmp->_total_money <number)
		{
		    cout<<"余额不足"<<endl;
		    return false;
		}
		vector<struct user>::iterator it=Find(block_name);
		pthread_mutex_lock(&mutex_lock); //加锁
		tmp->_total_money-=number;
		it->_total_money+=number;
		pthread_mutex_unlock(&mutex_lock);//解锁
		return true;
	    }
	    return false;
	}
	//注销用户  注销后的账号不能再次使用
	bool logout(const string &block_name,const string &passwd)
	{
	    vector<struct user>::iterator tmp=Find(block_name,passwd);
	    if(tmp !=vector_user.end())
	    {
		vector_user.erase(tmp);
		return true;
	    }
	    return false;
	}
	//更改密码
	bool change_passwd(const string &block_name,const string &passwd)
	{
	    vector<struct user>::iterator tmp=Find(block_name,passwd);
	    if(tmp==vector_user.end())
		return false;
	    cout<<"please enter old passwd :";
	    fflush(stdout);
	    string old_passwd="";
	    cin>>old_passwd;
	    if(strcmp(old_passwd.c_str(),passwd.c_str())==0)
	    {
		cout<<"please enter new passwd:";
		fflush(stdout);
		string new_passwd="";
		cin>>new_passwd;

		pthread_mutex_lock(&mutex_lock); //加锁
		tmp->_passwd=new_passwd;
		pthread_mutex_unlock(&mutex_lock);//解锁
	    }
	    return true;
	}
	//用户登陆
	bool log_in(const string &block_name,const string &passwd)
	{
	    vector<struct user>::iterator tmp=Find(block_name,passwd);
	    if(tmp !=vector_user.end())
		return true;
	    return false;
	}
	//判断身份证是否有效,可以查看这张身份证开零几张卡,但遗憾的是我没有实现
	bool effective(const string &id)
	{
	    vector<struct user>::iterator it=vector_user.begin();
	    for(; it!=vector_user.end(); ++it)
	    {
		if(strcmp((it->_id).c_str(),id.c_str())==0)
		    return true;
	    }
	    return true;
	}
	//将整数转换成字符串
	string get_string(int number)
	{
	    char arr[MAX_SIZE]; //因为最大的字符串值为MAX_SIZE
	    memset(arr,'\0',sizeof(arr));
	    string name="";
	    sprintf(arr,"%d",number);//将整数转换成字符串后存在arr数组中
	    name=arr;
	    return name;
	}
	//系统为用户分配一个账号
	string get_name()
	{
	    static int tmp=1;
	    if(tmp >MAX_SIZE)
		return "";
	    string name="";
	    pthread_mutex_lock(&mutex_lock); //加锁
	    name+=get_string(tmp);
	    tmp++;
	    pthread_mutex_unlock(&mutex_lock);//解锁
	    return name;
	}
	//系统分配的账号都以622123开始
	string get_block_name()
	{
	    string name="622123";
	    pthread_mutex_lock(&mutex_lock); //加锁
	    name+=get_name();
	    pthread_mutex_unlock(&mutex_lock);//解锁
	    return name;
	}
	//用户注册函数
	bool Register(const string &name,const string &id)//用户注册
	{
	    if(!effective(id))//判断身份证是否有效
	    {
		cout<<"已经存在此身份证"<<endl;
		return false;
	    }
	    string passwd; //初始状态的密码
	    string certain_passwd; //确定密码

	    cout<<"please enter passwd:";
	    fflush(stdout);
	    cin>>passwd;

	    cout<<"please again enter passwd:";
	    fflush(stdout);
	    cin>>certain_passwd;

	    //两次密码一致才注册成功
	    //然后把结构体保持在vector数组中
	    if(strcmp(passwd.c_str(),certain_passwd.c_str())==0)
	    {
		//////////加锁
		pthread_mutex_lock(&mutex_lock); //加锁
		value_user.insert_user(name,id,get_block_name(),passwd);
		vector_user.push_back(value_user);
		pthread_mutex_unlock(&mutex_lock);//解锁
		return true;
	    }
	    else
		return false;
	}
	//寻找已经在vector数组存在的用户
	vector<struct user>::iterator Find(const string &block_name,const string &passwd)
	{
	    vector<struct user>::iterator it=Find(block_name);
	    if(it !=vector_user.end())
	    {
	        if(strcmp(it->_passwd.c_str(),passwd.c_str())==0)
		    return it;
	    }
	    return vector_user.end();

	}
	///重载Find
	vector<struct user>::iterator Find(const string &block_name)
	{
	    vector<struct user>::iterator it=vector_user.begin();
	    for(; it!=vector_user.end(); ++it)
	    {
		if(strcmp(it->_block_name.c_str(),block_name.c_str())==0)
		    return it;
	    }
	    return vector_user.end();
	}
	//存款,存款之前保证用户已经登陆
	bool deposit(const string &block_name,const string &passwd,const int money)//存款
	{
	    if(log_in(block_name,passwd))
	    {
		vector<struct user>::iterator tmp=Find(block_name,passwd);
		if(tmp !=vector_user.end())
		{
		    pthread_mutex_lock(&mutex_lock); //加锁
		    tmp->_money=money;
		    tmp->_total_money+=money;
		    pthread_mutex_unlock(&mutex_lock);//解锁
		}
		return true;
	    }
	    return false;
	}
	//取款
	bool withdraw_money(const string &block_name,const string &passwd,const int &money)
	{
	    vector<struct user>::iterator tmp=Find(block_name,passwd);
	    if(tmp !=vector_user.end())
	    {
		if(money > tmp->_total_money)
		{
		    cout<<"余额不足"<<endl;
		    return false;
		}
		//////////加锁
		pthread_mutex_lock(&mutex_lock); //加锁
		tmp->_money-=money;
		tmp->_total_money-=money;
		pthread_mutex_unlock(&mutex_lock);//解锁
		///////////
		return true;
	    }
	    return false;
	}
	////////////////////////////
	//
	bool check(const string &block_name,const string &passwd)//用卡号查找
	{
	    vector<struct user>::iterator it=Find(block_name,passwd);
	    if(it !=vector_user.end())
	    {
		cout<<"用户:"<<it->_name<<"\t身份证:"<<it->_id;
         	cout<<"\t卡号:"<<it->_block_name;
	  	cout<<"\t余额:"<<it->_total_money<<endl;
		return true;
	    }
	    return false;
	}
	//显示所有信息
	void show()
	{
	    vector<struct user>::iterator it=vector_user.begin();
	    int i=1;
	    for(; it !=vector_user.end(); ++it)
	    {
		cout<<"开户第 "<<i++<<" 人"<<endl;
		cout<<"用户:"<<it->_name<<"\t身份证:"<<it->_id<<"\t";
		cout<<"卡号:"<<it->_block_name<<"\t密码:"<<it->_passwd<<"\t";
		cout<<"余额:"<<it->_total_money<<endl;
	    }
	    cout<<"开户总人数:"<<i-1<<endl;
	    i=0;
	}
};

Blank.cpp

#include "Bank.h"

static string name;//用户
static string id;  //身份证
static string block_name;//卡号
static string passwd;  //密码

int output()
{
    cout<<"******************************************"<<endl;
    cout<<"*                                        *"<<endl;
    cout<<"* [1]:注册                     [2]:登陆  *"<<endl;
    cout<<"* [3]:存款                     [4]:取款  *"<<endl;
    cout<<"* [5]:查询                     [6]:改密  *"<<endl;
    cout<<"* [7]:注销                     [8]:转账  *"<<endl;
    cout<<"* [9]:显示                     [0]:退出  *"<<endl;
    cout<<"*                                        *"<<endl;
    cout<<"******************************************"<<endl;
    char key;
    cout<<" 请选择:";
    fflush(stdout);
    cin>>key;
    if(key >='0' && key <='9')
        return key;
    while(key <'0' || key >'9')
    {
	cout<<"  请重新选择:";
	fflush(stdout);
	cin>>key;
    }
    return key;
}
void input()
{
    cout<<" please enter block_name: ";
    fflush(stdout);
    cin>>block_name;

    cout<<" please enter passwd: ";
    fflush(stdout);
    cin>>passwd;
}
void register_user()
{
    cout<<" please enter name: ";
    fflush(stdout);
    cin>>name;

    cout<<" please enter id: ";
    fflush(stdout);
    cin>>id;
}
void bank_function()
{
    Bank atm;
    size_t  money=0;
    bool flag=false;
    while(1)
    {
      char res=output();
      switch(res)
      {
          case '1':
	      register_user();
              flag=atm.Register(name,id);//用户注册
              if(flag)
          	cout<<" 【 注册成功 】"<<endl;
              else
          	cout<<" 【 注册失败 】"<<endl;
              break;
          case '2':
	      {
		  int count=3;
	    	  while(1)
	    	  {
	    	      if(count==0)
	    	          break;
	    	      input();
	    	      flag=atm.log_in(block_name,passwd);
	    	      if(flag)
		      {
	    	        cout<<" 【 登陆成功 】"<<endl;
			break;
		      }
	    	      else
	    	      {
	    	        cout<<" 【 登陆失败 】"<<endl;
	    	        cout<<"【 你还有** "<<--count<<" **次机会 】"<<endl;
	    	      }
	    	  }
	      }
              break;
          case '3':
	      input();
	      cout<<" please enter money: ";
	      fflush(stdout);
	      cout<<" 【请输入小于 5000 的整数!】"<<endl;
	      cin>>money;
	      while(money > 5000)
	      {
		  cout<<"【 请输入小于 5000 的整数!】"<<endl;
		  cin>>money;
	      }

	      if(atm.deposit(block_name,passwd,money))//存款
		  cout<<"【 存款成功 】"<<endl;
	      else
		  cout<<"【 存款失败 】"<<endl;
              break;
          case '4':
	      input();
	      cout<<" please enter money: ";
	      fflush(stdout);
	      cout<<"【 请输入小于 5000 的整数!】"<<endl;
	      cin>>money;
	      while(money > 5000)
	      {
		  cout<<"【 请输入小于 5000 的整数!】"<<endl;
		  cin>>money;
	      }

	      flag=atm.withdraw_money(block_name,passwd,money);
	      if(flag)
		  cout<<"【 取款成功 】"<<endl;
	      else
		  cout<<"【 取款不成功 】"<<endl;
              break;
          case '5':
	      input();
	      flag=atm.check(block_name,passwd);//用卡号查找
	      if(flag)
		  cout<<"【 查询成功 】"<<endl;
	      else
		  cout<<"【 查询失败 】"<<endl;
              break;
          case '6':
	      input();
	      flag=atm.change_passwd(block_name,passwd);
	      if(flag)
	        cout<<"【 更改密码成功 】"<<endl;
	      else
	        cout<<"【 更改密码失败 】"<<endl;
              break;
          case '7':
	      input();
	      flag=atm.logout(block_name,passwd);
	      if(flag)
		  cout<<"【 注销成功 】"<<endl;
	      else
		  cout<<"【 注销失败 】"<<endl;
              break;
          case '8':
	      cout<<" please enter card_number: ";
	      fflush(stdout);
	      cin>>block_name;
	      cout<<" please enter money: ";
	      fflush(stdout);
	      cout<<"【 请输入小于 5000 的整数!】"<<endl;
	      cin>>money;
	      flag=atm.transfer_account(block_name,money);
	      if(flag)
		  cout<<"【 转账成功 】"<<endl;
	      else
		  cout<<"【 转账失败 】"<<endl;
              break;
          case '9':
	      atm.show();
              break;
          case '0':
	      cout<<"【 欢迎使用ATM自能系统 】"<<endl;
              return;
          default:
	      system("clear");
              break;
      }
    }
}

int main()
{
    bank_function();
    return 0;
}

makefile

DES=Bank.cpp
CER=Bank

$(CER):$(DES)
	g++ -o [email protected] $^ -g

.PHONY:out
out:
	mkdir out
	mv $(CER) ./out
	chmod 755 table.sh
	cp table.sh ./out
	cd out
	sh table.sh

.PHONY:clean
clean:
	rm -rf out
	chmod 644 table.sh

table.sh

#/bin/bash
./out/Bank

时间: 2024-08-04 14:42:40

linux ATM自定取款机简单实现的相关文章

Linux 用C语言实现简单的shell(2)

不知不觉两周没有发文了,因为“一万美金的福特奖学金答辩”,ACM比赛,网络论文阅读和网络大作业一大堆事把时间冲散了,所以先写一篇博文补上之前一坑. 之前发了一篇关于linux 用C语言实现简单shell的博文,当时因为刚刚接触linux,只是处理了: 1)外部命令 2)pwd,cd,exit内置命令 3)输入输出重定向 并且代码相比较而言是一步一步添加的,代码相对来讲比较丑QAQ,所以在学完管道之后,相信不得不重新写代码才能实现了. 相比较之前的版本我对代码进行了相关的修改: 1)对于shell

Linux下禁止ping最简单的方法

LINUX下禁止ping命令的使用 以root进入Linux系统,然后编辑文件icmp_echo_ignore_allvi /proc/sys/net/ipv4/icmp_echo_ignore_all将其值改为1后为禁止PING将其值改为0后为解除禁止PING 直接修改会提示错误: WARNING: The file has been changed since reading it!!!Do you really want to write to it (y/n)?y"icmp_echo_i

【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间

打算学习一下Linux,这两天先看了一下gcc的简单用法以及makefile的写法,今天是周末,天气闷热超市,早晨突然发现住处的冰箱可以用了,于是先出去吃了点东西,然后去超市买了一坨冰棍,老冰棍居多,5毛钱一根,还有几根1.5的. 嗯 接着说gcc的事 先把源代码贴上来 //gettime.h #ifndef _GET_TIME_H_ #define _GET_TIME_H_ void PrintCurrentTime(); #endif //gettime.c #include <stdio.

GNU/Linux平台上正则表达式的简单使用

友情提醒:本博文涉及的内容中涉及到的系统实践操作在Centos6.5上实现,GNU/Linux简称为linux,GNU/grep简称为grep,GNU/sed简称为sed,GNU/gawk简称为awk. -------------------------------------------------楔子--------------------------------------------- 小酒馆一角落,一胖三瘦围着方桌坐定,大快耳颐后正在唠嗑. 瘦子甲:"胖子,为什么要把正则表达式和lin

shell 脚本实战笔记(11)--Mysql在linux下的安装和简单运维

前言: linux中安装mysql以及配置的管理, 基础的运维和管理还是需要会一些的. 这边作下笔记, 以求天天向上(^_^). 安装流程:*). 安装mysql-server1). 借助yum检索相关的mysql rpm包yum search mysqlmysql-server.x86_64 正是我们想要的 2). 安装mysql-serveryum install mysql-server.x86_64 -y默认mysql-client也安装好 3). 启动mysql服务/etc/init.

Linux命令行及Vim简单学习记录

Linux命令行 1.打开命令行 Ctrl+Alt+t 2.目录 显示当前目录的文件列表 ls 跳转至当前目录中的x文件夹 cd x 返回根目录 cd 3.文件 新建文件1.cpp touch ./1.cpp 用vim打开文件1.cpp vim ./1.cpp 用g++编译文件1.cpp g++ ./1.cpp (编译后生成文件名为"1.out"的可执行文件) 用g++将文件1.cpp编译为文件名为"1"的可执行文件 g++ ./1.cpp -o ./1 -g 在使

Linux下好用的简单实用命令

1.你是否为在输入了一大串命令之后发现第一个字符打错了而苦恼?只能删除重来嘛?或者一步步左移光标? NO,一个组合键轻松搞定 Ctrl+A -----到命令行首 Ctrl+E ------到命令行末 Ctrl+W ------删除光标处向前一个单词(到下一个空格键处) 和文本编辑中的home和end键一样好用(^o^)/~ 2.每次需要下载日志都得打开sftp 好麻烦,环境加固之后,更是愁...有快捷简便的方法吗?答案肯定是yes了.首先得查看你是否安装了一个rpm包“rpm -aq | gre

linux中make命令的简单使用以及Makefile文件的书写

Makefile 会不会写makele,从一个侧面说明了一个是否具备完成大型工程的能力. 一个工程中的源件不计数,其按类型.功能.模块分别放在若干个目录中,makele定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makele就像一个Shell脚本一样,其中也可以执行操作系统的命令.makele带来的好处就是"自动化编译",一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率. ma

学习Linux系统中命令的简单方法

如果说如何快速学习.了解Linux的话,我的答案是学命令.背命令!为何呢?对于一名新手来说,去学习Linux的思想.了解Linux的架构.明白Linux中"一切皆文件"概念虽然说是没有错,是对的.但是个人认为去学习这些"高大上"的东西不是一时半会的事儿,它需要一定的时间和经验去沉淀才能掌握.那么如何最快速了解Linux并使用呢?我依然觉得学命令.背命令,掌握命令是比较笨但却是比较快的方式. 我开始学习Linux的时候,问了前辈:我入门Linux需要掌握哪些命令呢?前