⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 银行atm机模拟程序 实现简单的atm机操作 单机版的
💻 C
📖 第 1 页 / 共 2 页
字号:
//输入密码是backsapce   用户名输入长度需要判断。//在转帐函数里回滚操作没有实现//注册完成后不能直接进行login 操作,必须的重启系统后才能操作#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <string.h>   
#include <fcntl.h>#include<termios.h>  /* for tcgetattr, tcsetattr, ECHO, etc */#include <unistd.h>   /* for STDIN_FILENO */#include <math.h>#include <time.h>#define MAXINFO 10 //用户名、密码、和用户余额最大长度//#define MAXOPER 80 //一条操作日志的最大长度。struct userInfo{	//保存用户信息	char *name;		//用户名	char *pwd;		//密码	char *total;	//用户余额	char *userData;	//保存用户所有信息的字符串,注册和写操作日志时用到};struct operation{	//保存操作信息	char* type;		//1、存钱 2、取钱 3、转帐(转出)4、转帐(转入) 	char* time;		//操作时间	char* amount;	//用户存或取钱金额	char* prev;		//执行操作前用户余额	char* post;		//执行操作后用户余额	char* userName;	//只有在转帐时用到,记录转入帐号的用户名	char* operData;	//保存一次操作的所有信息,写操作日志是用到};int writeUser(struct userInfo* user);//创建一文件用来保存用户的注册信息,文件名为用户名inline int writeLog(struct userInfo* user,struct operation* op);//打开用户文件把操作日志写入,并更新用户信息。int operate(struct userInfo* user,struct operation* op);int login(struct userInfo* user,struct operation* op);int registNewUser(struct userInfo* user);//注册新用户int deposit(struct userInfo* user,struct operation* op);//存钱int drawMoney(struct userInfo* user,struct operation* op);//取钱int transfer(struct userInfo* user,struct operation* op);//转帐int getMoney(char* money);int stringToInteger(char* str,int *value);int integerToString(int value,char* str);int getCurTime(char* operTime);int file_exist(char* filename);int getUserinfo(char* filename,struct userInfo* user);int Getch(void);int getPwd(char* str);void registInfo(void);//注册成功后的选择信息//int isNumAlpha(char ch);int main(){	int choose1;		//初始化user变量	struct userInfo* user;	user = malloc(sizeof(struct userInfo));	(*user).name = malloc(sizeof(char)*MAXINFO);	(*user).pwd = malloc(sizeof(char)*MAXINFO);	(*user).total = malloc(sizeof(char)*MAXINFO);	(*user).userData = malloc(sizeof(char)*40);		//初始化op变量	struct operation* op;	op = malloc(sizeof(struct operation));	(*op).type = malloc(sizeof(char));	(*op).time = malloc(sizeof(char)*30);	(*op).amount = malloc(sizeof(char)*MAXINFO);	(*op).userName = malloc(sizeof(char)*MAXINFO);	(*op).prev = malloc(sizeof(char)*MAXINFO);	(*op).post = malloc(sizeof(char)*MAXINFO);	(*op).operData = malloc(sizeof(char)*80);		printf("Welocme to our system.\n\n");	while(1){		printf("1、Login。\n");		printf("2、Register。\n");		printf("3、Cancle。\n");				printf("Your choice:");		scanf("%d",&choose1);				switch(choose1){			case 1 : if(login(user,op) == 0)return 0;					 return -1;			case 2 : if(registNewUser(user) == 1) break;					 if(registNewUser(user) == 0) return 0;					 if(registNewUser(user) == -1) return -1;			case 3 : return 0;			default: printf("Wrong input ..."); break;		}				}				return 0;}//用户登录方法,登录后可以执行存、取、转账等操作。int login(struct userInfo* user,struct operation* op){		char *loginname = malloc(sizeof(char)*MAXINFO);	char *pwd = malloc(sizeof(char)*MAXINFO);		while(1)	{		printf("Login Name:");		scanf("%s",loginname);				printf("Password:");		if(getPwd(pwd) == -1){}//用户输入密码格式错误,重新输入用户名和密码			else{	//else1   					printf("\n");										if(getUserinfo(loginname,user) == -1)//					{						printf("error in getUserinfo,return value -1...\n");						return(-1);//用户名不存在,或者文件丢失					}										if((strcmp(loginname,(*user).name) == 0) && (strcmp(pwd,(*user).pwd)) == 0 )					{												operate(user,op);//进行重复的存取款操作,当出现错误或用户退出时退出。						break;					}else{						printf("userName or password wrong,retry...\n");						}						}//end else1	}//end while	return 0;}int operate(struct userInfo* user,struct operation* op){	int choose;	int choose1;	//clrscr();	while(1){		printf("\n1、存钱。\n");		printf("2、取钱。\n");		printf("3、转帐。\n");		printf("4、查询。\n");		printf("5、退出。\n");		printf("Your choice:");		scanf("%d",&choose);		switch(choose){			case 1 : if(deposit(user,op) == 0){//成功存钱												registInfo();						printf("Your choice:");						scanf("%d",&choose1);						if(choose1 == 1) break;						if(choose1 == 2) return 0;						printf("wrong input,exiting...");						return 0;					 }					return -1;//存钱失败,变量user和op里保存的内容可能已经和事实不符合,需要退出系统重新初始化								case 2 : if(drawMoney(user,op) == 0){//成功取钱						registInfo();						printf("Your choice:");						scanf("%d",&choose1);						if(choose1 == 1) break;						if(choose1 == 2) return 0;						printf("wrong input,exiting...");						return 0;					 }					return -1;//取钱失败,变量user和op里保存的内容可能已经和事实不符合,需要退出系统重新初始化			case 3 : if(transfer(user,op) == 0){//成功转帐						registInfo();						printf("Your choice:");						scanf("%d",&choose1);						if(choose1 == 1) break;						if(choose1 == 2) return 0;						printf("wrong input,exiting...");						return 0;					 }					 return -1;//转帐失败,变量user和op里保存的内容可能已经和事实不符合,需要退出系统重新初始化			case 4 : printf("chaxun");					 return 0;					 			case 5 : return 0;						default: printf("wrong input..\n");					 break;		}//end switch	}//end while		return 0;}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////int getUserinfo(char* filename,struct userInfo* user){	FILE* f;	int i = 0;	int j = 0;	int k = 0;	int count = 0;		f = fopen(filename,"r");	if(f == NULL){		printf("File do not exist,Be sure the login name is right...\n");		return -1;//用户还没有注册或者文件丢失,异常退出。	}	fseek(f,0,SEEK_SET);	fgets((*user).userData,40,f);	fclose(f);		while((*user).userData[count] != ' '){		(*user).name[i++] = (*user).userData[count++];		if(i >= MAXINFO) return -1;//文件储存格式错误	}	(*user).name[i]='\0';		while((*user).userData[count] == ' ')count++;		(*user).pwd[j] = (*user).userData[count];	while((*user).userData[count] != ' '){		(*user).pwd[j++] = (*user).userData[count++];		if(j >= MAXINFO ||count>=40) return -1;//文件储存格式错误	}	(*user).pwd[j] = '\0';		while((*user).userData[count] == ' ')count++;		(*user).total[k] = (*user).userData[count];		while((*user).userData[count] != ' '){		/*if((*user).userData[count] == '\n'||!isNumAlpha((*user).userData[count])){			(*user).total[k] = '\0';			break;		}*/		(*user).total[k++] = (*user).userData[count++];		if(k >= MAXINFO ||count>=40) return -1;//文件储存格式错误	}	(*user).total[k] = '\0';	return 0;}int getPwd(char* str)	//To get the password in a security way.{    char c;    int i = 0;    //c=Getch();s    while (1)    {		c=Getch();		if (c=='\n'){			if(i==0){}			else break;		}else{			*(str++) = c;			i++;			if (i>=MAXINFO-1) {				printf("To many characters!!!\n");				return -1;			}		}    }    *(str++) = '\0';    return 0;}int Getch (void){    int ch;    struct termios oldt, newt;     tcgetattr(STDIN_FILENO, &oldt);    newt = oldt;    newt.c_lflag &= ~(ECHO|ICANON);    tcsetattr(STDIN_FILENO, TCSANOW, &newt);    ch = getchar();    tcsetattr(STDIN_FILENO, TCSANOW, &oldt);      return ch;}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int registNewUser(struct userInfo* user){	char* rePwd = malloc(sizeof(char)*MAXINFO);	while(1)	{		printf("Login Name:");		scanf("%s",(*user).name);		if(file_exist((*user).name))//用户名已经存在		{			printf("Login name is not valide.Use a new name to login...\n");		}else //else1		{			printf("Password:");			if(getPwd((*user).pwd) == -1){}//密码输入时出现错误			else //else2			{				printf("\nConfirm password:");				if(getPwd(rePwd) == -1){}//确认密码输入时出现错误				else //else3				{					printf("\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -