ATM 1.0

这是atm的第一个版本,实现atm的最基本功能。

主要采用c/s的方式,一个服务端,一个客户端。
c客户端功能:
开户、销户、存钱、取钱、转账、查询。
客户端接收指令,发送消息给s服务端。

s服务端:
负责处理客户端送过来的业务。
所有处理在服务端完成,不在客户端处理。
每项服务都有一个进程。

以下附上代码:
服务端

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"

//开户
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT|00644);
	if(0>msgid1)
	{
		perror("msgget");
		return -1;
	}
	int x=190000000;
	while(1)
	{
		Msg msg={};
		msgrcv(msgid1,&msg,sizeof(Msg),1192,0);
		printf("s_build_error\n");
		msg.acc.card_id=x++;
		msg.acc.is_suc=0;
		msg.acc.is_lock=0;
		msg.acc.login_times=3;
		char buf[255]={};
		sprintf(buf,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
		int fd=open(buf,O_CREAT|O_RDWR,0644);
		if(0>fd)
		{
			perror("build:open");
			return -1;
		}
		write(fd,&msg,sizeof(Msg));
		msg.typ=1193;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		close(fd);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"

//修改密码
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
		Msg msg={};
		char filename[255]={};
		msgrcv(msgid1,&msg,sizeof(Msg),9999,0);
		puts(msg.acc.password);
		sprintf(filename,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
		int fd=open(filename,O_RDWR,0644);
		if(fd<0)
		{
			perror("pass:open");
			return -1;
		}
		Msg msg1={};		
		read(fd,&msg1,sizeof(Msg));
		strcpy(msg1.acc.password,msg.acc.password);
		puts(msg1.acc.password);
		lseek(fd,0,SEEK_SET);
		write(fd,&msg1,sizeof(Msg));
		msg.typ=9998;
		msg.acc.is_suc=1;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);		
		close(fd);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//销户
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
		Msg msg={};
		msgrcv(msgid1,&msg,sizeof(Msg),2222,0);
		printf("s_del_error\n");
		char buf[255]={};
		sprintf(buf,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);	
		if(-1==access(buf,F_OK))
		{
			msg.acc.is_suc=-1;
//			msg.typ=2223;
			printf("%d",msg.typ);
			printf("%d\n",msg.acc.is_suc);
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
			continue;
		}
		int fd=open(buf,O_RDWR|O_EXCL,0644);
		if(fd<0)
		{
			perror("del:open");
			return -1;
		}
		Msg msg1={};
		read(fd,&msg1,sizeof(Msg));
		if(0!=strcmp(msg1.acc.password,msg.acc.password))
		{
			msg.acc.is_suc=-2;
			msg.typ=2223;
			printf("%d",msg.typ);
			printf("%d\n",msg.acc.is_suc);
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		}
		else if(msg1.acc.money>0.01)
		{
			msg.acc.is_suc=-3;
			msg.typ=2223;
			printf("%d",msg.typ);
			printf("%d\n",msg.acc.is_suc);
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		}
		else
		{
			char del[255]={};
			sprintf(del,"/home/zhizhen/atmproject/del/%d",msg.acc.card_id);
			int fd=open(del,O_CREAT|O_RDWR,0644);
			if(0>fd)
			{
				perror("del1:open");
				return -1;
			}
			write(fd,&msg1,sizeof(Msg));
			close(fd);
			remove(buf);
			msg.acc.is_suc=1;
			msg.typ=2223;
			printf("%d",msg.typ);
			printf("%d\n",msg.acc.is_suc);
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		}
		close(fd);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//取钱
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
		Msg msg={};
		msgrcv(msgid1,&msg,sizeof(Msg),5555,0);
		char buf[255]={};
		sprintf(buf,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
		int fd=open(buf,O_RDWR|O_EXCL,0644);
		if(fd<0)
		{
			perror("del:open");
			return -1;
		}
		Msg msg1={};
		read(fd,&msg1,sizeof(Msg));
		if(msg1.acc.money>msg.acc.money)
		{
			msg1.acc.money-=msg.acc.money;
			lseek(fd,0,SEEK_SET);
			write(fd,&msg1,sizeof(Msg));
			msg.acc.money=msg1.acc.money;
			msg.acc.is_suc=1;
			msg.typ=5556;
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);	
		}
		else
		{
			msg.acc.is_suc=-1;
			msg.typ=5556;
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);	
		}
		close(fd);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//登录
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{		
	Msg msg={};
	msgrcv(msgid1,&msg,sizeof(Msg),3333,0);
	printf("card_id:%d\n",msg.acc.card_id);
	printf("is_lock:%d\n",msg.acc.is_lock);
	printf("s_login_error\n");
	char buf[255]={};
	sprintf(buf,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
	if(-1==access(buf,F_OK))
	{
		msg.acc.is_suc=-1;
		msg.typ=3334;
		printf("-1:%d\n",msg.acc.is_suc);
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		continue;
	}

	int fd=open(buf,O_RDWR|O_EXCL,0644);
	if(0>fd)
	{
		perror("login:open");
		return -1;
	}
	Msg msg1={};
	read(fd,&msg1,sizeof(Msg));

	if(1==msg1.acc.is_lock)
	{
		msg.acc.is_lock=1;
		msg.typ=3334;
		printf("is_lock:suc=:%d\n",msg.acc.is_suc);
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		continue;
	}
	if(0==strcmp(msg1.acc.password,msg.acc.password))
	{
		msg.acc.is_suc=1;
		printf("1:%d\n",msg.acc.is_suc);
		msg.typ=3334;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	}
	else
	{
		if(msg1.acc.login_times>1)
		{
			msg1.acc.login_times--;
			msg.acc.login_times=msg1.acc.login_times;
			lseek(fd,0,SEEK_SET);
			write(fd,&msg1,sizeof(Msg));
			msg.acc.is_suc=-2;
			printf("-2:%d\n",msg.acc.is_suc);
			msg.typ=3334;
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		}
		else
		{
			msg1.acc.is_lock=1;
			lseek(fd,0,SEEK_SET);
			write(fd,&msg1,sizeof(Msg));
			msg.typ=3334;
			msg.acc.is_lock=1;
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		}
	}
	close(fd);
	printf("end\n");
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//查询
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
		Msg msg={};
		msgrcv(msgid1,&msg,sizeof(Msg),8888,0);
		char filename[255]={};
		sprintf(filename,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
		int fd=open(filename,O_RDWR,0644);
		if(0>fd)
		{
			perror("query:open");
			return -1;
		}
		read(fd,&msg,sizeof(Msg));
		msg.typ=8889;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		close(fd);		
	}

}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//存钱
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
	Msg msg={};
	msgrcv(msgid1,&msg,sizeof(Msg),4444,0);
	char buf[255]={};
	sprintf(buf,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
	int fd=open(buf,O_RDWR,0644);
	if(0>fd)
	{
		perror("save:open");
		return -1;
	}
	Msg msg1={};
	read(fd,&msg1,sizeof(Msg));
	{
		msg1.acc.money+=msg.acc.money;
		lseek(fd,0,SEEK_SET);
		write(fd,&msg1,sizeof(Msg));
		msg.acc.money=msg1.acc.money;
		msg.typ=4445;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	}
	close(fd);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//转账
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
		Msg msg={};
		Msg msg1={};
		msgrcv(msgid1,&msg,sizeof(Msg),6666,0);
		msgrcv(msgid1,&msg1,sizeof(Msg),7777,0);
		
		char getter[255]={};
		char sender[255]={};
		sprintf(getter,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
		sprintf(sender,"/home/zhizhen/atmproject/inf/%d.dat",msg1.acc.card_id);
		if(-1==access(getter,F_OK))
		{
			msg.acc.is_suc=-1;
			msg.typ=6667;
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0); 
			continue;
		}
		
		int fd=open(sender,O_RDWR,0644);
		if(fd<0)
		{
			perror("trans:open");
			return -1;
		}
		Msg msg2={};
		read(fd,&msg2,sizeof(Msg));
		if(msg2.acc.money>=msg.acc.money)
		{
			msg2.acc.money-=msg.acc.money;
			msg2.typ=6667;
			msgsnd(msgid1,&msg2,sizeof(Msg)-4,0);
			msg2.acc.is_suc=1;
			lseek(fd,0,SEEK_SET);
			write(fd,&msg2,sizeof(Msg));
		}
		else
		{
			msg.acc.is_suc=-2;
			msg.typ=6667;
			msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
			continue;
		}
		fd=open(getter,O_RDWR,sizeof(Msg));
		if(fd<0)
		{
			perror("trans:open");
			return -1;
		}
		read(fd,&msg2,sizeof(Msg));
		msg2.acc.money+=msg.acc.money;
		lseek(fd,0,SEEK_SET);
		write(fd,&msg2,sizeof(Msg));
		close(fd);
	}
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include "struct.h"


//解锁账号
int main()
{
	key_t key=ftok(".",111);
	int msgid1=msgget(key,IPC_CREAT);
	while(1)
	{
	Msg msg={};
	msgrcv(msgid1,&msg,sizeof(Msg),3560,0);
	char buf[255]={};
	sprintf(buf,"/home/zhizhen/atmproject/inf/%d.dat",msg.acc.card_id);
	if(-1==access(buf,F_OK))
	{
		msg.acc.is_suc=-1;
		msg.typ=3566;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
		continue;
	}
	int fd=open(buf,O_RDWR,0644);
	if(0>fd)
	{
		perror("unlock:open");
		return -1;
	}
	Msg msg1={};
	read(fd,&msg1,sizeof(Msg));
	printf("msg%lld msg1%lld\n",msg.acc.id,msg1.acc.id);
	if(msg.acc.id==msg1.acc.id)
	{
		msg1.acc.is_lock=0;
		msg1.acc.login_times=3;
		lseek(fd,0,SEEK_SET);
		write(fd,&msg1,sizeof(Msg));
		msg.acc.is_suc=1;
		msg.typ=3566;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	}
	else
	{	
		msg.typ=3566;
		msg.acc.is_suc=-2;
		msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	}
		close(fd);
	}
}
//服务端开进程
#include "tools.h"
#include "struct.h"
#include "server.h"

int arr[10]={};
void sigint(int signum)
{
	key_t key=ftok("/home/zhizhen/atmproject/server",111);
	int msgid1=msgget(key,IPC_CREAT);
	 printf("服务器正在升级\n");
	 if(msgctl(msgid1,IPC_RMID,NULL) == -1)
	 {
		perror("消息队列1删除失败!\n");
		exit(-1);
	 }
	 else
	 {
		for(int i=0;i<9;i++)
		{	
			kill(arr[i],9);
		}
	 	printf("消息队列1删除成功!\n");
	 }
	printf("服务器成功关闭!\n");
	exit(0);
}

int main()
{
	printf("退出服务器请按CTRL+C\n");
	signal(SIGINT,sigint);
	//开户
	pid_t pid0=vfork();
	if(0==pid0)
	{
		execl("s_build","s_build",NULL);
		exit(0);
	}
	else
	{
		perror("s_build");
		arr[0]=pid0;
	}
	//销户
	pid_t pid1=vfork();
	if(0==pid1)
	{
		execl("s_del","s_del",NULL);
		exit(0);
	}
	else
	{
		perror("s_del");
		arr[1]=pid1;
	}
	
	//登录
	pid_t pid2=vfork();
	if(0==pid2)
	{
		execl("s_login","s_login",NULL);
		exit(0);
	}
	else
	{
		perror("s_login");
		arr[2]=pid2;
	}
	
	//存款
	pid_t pid3=vfork();
	if(0==pid3)
	{
		execl("s_save","s_save",NULL);
		exit(0);
	}
	else
	{
		perror("s_save");
		arr[3]=pid3;
	}

	//取款
	pid_t pid4=vfork();
	if(0==pid4)
	{
		execl("s_get","s_get",NULL);
		exit(0);
	}
	else
	{
		perror("s_get");
		arr[4]=pid4;
	}

	//转账
	pid_t pid5=vfork();
	if(0==pid5)
	{
		execl("s_transfer","s_transfer",NULL);
		exit(0);
	}
	else
	{
		perror("s_transfer");
		arr[5]=pid5;
	}

	//查询
	pid_t pid6=vfork();
	if(0==pid6)
	{
		execl("s_query","s_query",NULL);
		exit(0);
	}
	else
	{
		perror("s_query");
		arr[6]=pid6;
	}
	
	//修改密码
	pid_t pid7=vfork();
	if(0==pid7)
	{
		execl("s_change_pass","s_change_pass",NULL);
		exit(0);
	}
	else
	{
		perror("s_change_pass");
		arr[7]=pid7;
	}
	
	//解锁账号
	pid_t pid8=vfork();
	if(0==pid8)
	{
		execl("s_unlock","s_unlock",NULL);
		exit(0);
	}
	else
	{
		perror("s_unlock");
		arr[8]=pid8;
	}
	while(-1!=wait(0));
}

客户端

#include "cfunc.h"

int msgid1;
int send_msg(void)
{
	key_t key=ftok(".",111);
	msgid1=msgget(key,IPC_CREAT);
	if(0>msgid1)
	{
		perror("msgget1");
		return -1;
	}
}



//开户申请
int c_build(void)
{
	Account acc={};
	printf("请输入身份证号:");
	scanf("%lld",&acc.id);
	printf("请输入密码:");
	get_pd(acc.password,10);
	printf("请存入金额:");
	scanf("%lf",&acc.money);
	Msg msg={1192,acc};
	send_msg();
	if(0>msgsnd(msgid1,&msg,sizeof(Msg)-4,0))
	{	
		show_msg("服务器正在维护中...",1);
		return -1;
	}
	msgrcv(msgid1,&msg,sizeof(Msg),1193,0);
		printf("开户成功!\n");
		printf("ID: %d\n",msg.acc.card_id);
		printf("余额: %.2lf\n",msg.acc.money);
		printf("身份证:%lld\n",msg.acc.id);
		clear_stdin();
		anykey_continue();
}

//销户申请
int c_del(void)
{
	send_msg();
	Account acc={};
	printf("请输入需要销毁账户的ID:");
	scanf("%d",&acc.card_id);
	printf("请输入密码:");
	get_pd(acc.password,20);
	Msg msg={2222,acc};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),2223,0);
	if((msg.acc.is_suc>0))
	{
		show_msg("销户成功!",1);
	}
	else if(-2==msg.acc.is_suc)
	{
		show_msg("销毁失败,密码错误!",1);
	}
	else if(-1==msg.acc.is_suc)
	{
		show_msg("销毁失败,账号错误!",1);
	}
	else if(-3==msg.acc.is_suc)
	{
		show_msg("销毁失败,账户还有余额!",1);
	}
}

//登录申请
int c_login(void)
{
	send_msg();
	Account acc={};
	printf("请输入账号:");
	scanf("%d",&acc.card_id);
	printf("请输入密码:");
	get_pd(acc.password,20);
	Msg msg={3333,acc};
	printf("%d\n",acc.is_lock);
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),3334,0);
	printf("is_suc%d\n",msg.acc.is_suc);
	printf("card_id%d\n",msg.acc.card_id);
	printf("is_lock%d\n",acc.is_lock);
	if(1==msg.acc.is_lock)
	{
		show_msg("账号已锁定,请解锁!",1);
		return -1;
	}
	if(msg.acc.is_suc>0)
	{
		show_msg("登录成功!",1);
		interface2(msg.acc.card_id);
	}
	else if(-1==msg.acc.is_suc)
	{
		show_msg("登录失败,账号错误!",1);
	}
	else if(-2==msg.acc.is_suc)
	{	
	/*	if(1==msg.acc.login_times)
		{
			show_msg("账号已锁定,请使用身份证解锁!",1);
			break;
		}
		else
		{*/
			printf("登录失败,密码错误!(剩余%d次机会)\n",msg.acc.login_times);	
			sleep(1);
	//	}
	}
}

//解锁账号申请
int c_unlock(void)
{
	send_msg();
	Account acc={};
	printf("请输入账号:");
	scanf("%d",&acc.card_id);
	printf("请输入身份证号:");
	scanf("%lld",&acc.id);
	Msg msg={3560,acc};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),3566,0);
	if(msg.acc.is_suc>0)
	{
		show_msg("解锁成功!",1);
		return -1;	
	}
	else if(-1==msg.acc.is_suc)
	{
		show_msg("账号错误!",1);
		return -1;	
	}
	else if(-2==msg.acc.is_suc)
	show_msg("身份证不匹配!",1);
}

//存钱申请
int c_save(int num)
{
//	send_msg();
printf("%d\n",num);
	Account acc;
	printf("请存入金额:");
	scanf("%lf",&acc.money);
	if(acc.money<=0)
	{
		show_msg("请输入正确的面额!\n",1);
		return -1;
	}
	acc.card_id=num;
	Msg msg={4444,acc};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),4445,0);
	printf("您的余额为:%.2lf\n",msg.acc.money);
	sleep(1);
}

//取钱申请
int c_get(int num)
{
	//send_msg();
	Account acc;
	printf("请输入取款金额:");
	scanf("%lf",&acc.money);
	if(acc.money<=0)
	{
		show_msg("请输入正确的面额!\n",1);
		return -1;
	}
	acc.card_id=num;
	Msg msg={5555,acc};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),5556,0);
	if(msg.acc.is_suc>0)
	{
		printf("您的余额:%.2lf\n",msg.acc.money);
		sleep(1);
	}
	else
	show_msg("余额不足!",1);
}

//转账申请
int c_transfer(int num)
{
	//send_msg();
	Account acc;
	Account acc1;
	printf("请输入收款人账号:\n");
	scanf("%d",&acc.card_id);
	if(acc.card_id==num)
	{
		show_msg("不能转账给该账号!\n",1);
		return -1;
	}
	printf("请输入转账金额:\n");
	scanf("%lf",&acc.money);
	if(acc.money<=0)
	{
		show_msg("请输入正确的面额!\n",1);
		return -1;
	}
	acc1.card_id=num;
	Msg msg={6666,acc};
	Msg msg1={7777,acc1};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgsnd(msgid1,&msg1,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),6667,0);
	if(-1==msg.acc.is_suc)
	{
		show_msg("账号不存在!",1);
		return -1;
	}
	else if(-2==msg.acc.is_suc)
	{
		show_msg("余额不足!",1);
		return -1;
	}
	printf("转账成功! 余额:%.2lf\n",msg.acc.money);
	sleep(1);
}

//查询申请
int c_query(int num)
{
	Account acc={};
	acc.card_id=num;
	Msg msg={8888,acc};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),8889,0);
	printf("余额:%.2lf\n",msg.acc.money);
	clear_stdin();
	anykey_continue();
}

//修改密码申请
int c_change_pass(int num)
{
	Account acc={};
	acc.card_id=num;
	printf("请输入新的密码:");
	get_pd(acc.password,20);
	Msg msg={9999,acc};
	msgsnd(msgid1,&msg,sizeof(Msg)-4,0);
	msgrcv(msgid1,&msg,sizeof(Msg),9998,0);
	if(msg.acc.is_suc>0)
	show_msg("修改成功!",1);
}

//客户端主程序
#include "struct.h"
#include "tools.h"
#include "show.h"
#include "cfunc.h"

int main()
{
	interface1();	
}
//客户端显示界面
#include "tools.h"
#include "struct.h"
//#include "client.h"
#include "cfunc.h"

void interface1(void)
{
	while(1)
	{
		show1();
		switch(get_cmd('1','4'))
		{
			case '1':c_build();break;
			case '2':c_del();break;
			case '3':c_login();break;
			case '4':c_unlock();break;
		}
	}
}

void interface2(int num)
{
	printf("%d\n",num);
	while(1)
	{
		show2();
		switch(get_cmd('1','6'))
		{
			case '1':c_save(num);break;
			case '2':c_get(num);break;
			case '3':c_transfer(num);break;
			case '4':c_query(num);break;
			case '5':c_change_pass(num);break;
			case '6':return;
		}
	}
}
//客户端工具
#include <get_keyboard.h>
#include <string.h>
#include "tools.h"

//清屏
void clear_scr(void)
{
	system("clear");
}

//清理输入缓冲区
void clear_stdin(void)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
}

//获取用户名
char* get_user(char* str,uint8_t size)
{
	clear_stdin();

	uint8_t index = 0; 
	while(index < size -1)
	{
		uint8_t val = get_keyboard();
		if(val == KEY_ESC)
		{
			return NULL;
		}
		else if(val == KEY_ENTER)
		{
			break;
		}
		else if(val == KEY_BACKSPACE)
		{
			if(index > 0)
			{
				printf("\b \b");
				index--;
			}
			continue;
		}
		str[index++] = val;
		printf("%c",val);
	}

	clear_stdin();
	str[index] = '\0';
	printf("\n");
	return str;
}

//获取字符串
char* get_str(char* str,uint8_t size)
{
	clear_stdin();

	fgets(str,size,stdin);
	uint8_t len = strlen(str);

	if(str[len-1] == '\n') 
	{
		str[len-1] = '\0';
	}

	clear_stdin();
}

//获取密码
char* get_pd(char* pd,uint8_t size)
{
	clear_stdin();

	uint8_t index = 0;
	while(index < size-1)
	{
		uint8_t val = get_keyboard();
		if(KEY_ENTER == val)
		{
			break;
		}
		else if(KEY_BACKSPACE == val)
		{
			if(index > 0)
			{
				printf("\b \b");
				index--;
			}
			continue;
		}
		pd[index++] = val;
		printf("*");
	}

	clear_stdin();	
	pd[index] = '\0';
	printf("\n");
	return pd;
}

//获取指令
char get_cmd(char start,char end)
{
	clear_stdin();

	while(1)
	{
		uint8_t val = get_keyboard();
		if(val >= start && val <= end)
		{
			printf("%c\n",val);
			return val;
		}
	}
}

//输入任意键继续
void anykey_continue(void)
{
	printf("请输入任意键继续...");
	get_keyboard();
	printf("\n");
}

//显示提示信息
void show_msg(char* msg,uint8_t sec)
{
	printf("\33[;34m");
	printf("*** %s *** \n",msg);
	sleep(sec);
	printf("\33[0m");
}

void show1(void)
{
	system("clear");
	printf("1.开户\n");
	printf("2.销户\n");
	printf("3.登录\n");
	printf("4.解锁\n");	
	printf("请输入指令:\n");
}

void show2(void)
{
	clear_scr();
	printf("1.存钱\n");
	printf("2.取钱\n");
	printf("3.转账\n");
	printf("4.查询\n");
	printf("5.修改密码\n");
	printf("6.退出\n");
	printf("请输入指令:\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值