设为首页 加入收藏

TOP

C语言 实现ATM系统(一)
2015-01-21 11:08:57 来源: 作者: 【 】 浏览:29
Tags:语言 实现 ATM 系统

主要用到了指针、结构体、数组、链表、文件读取

由于平时用java,所以感觉自己的代码还是可以看看的,尽量用面向对象的思想去写,这也使得原本近千行代码的程序只用了不到一半的代码完成。

User.h //用户对象

struct User
{
	char UserAccount[100];
	char UserPassword[100];
	int Money;
	struct User *next;
};

UI.h //就是一个简单的菜单显示

int showMenu(char MenuItem[][20], int itemCount){
	system("cls");
	for (int i = 0; i < 10;i++){
		printf("\n");
	}
	printf("***************************************************\n");
	for (int i = 0; i < itemCount; i++){
		printf("*               ");
		printf("%d.", i+1);
		int Str_size = 20;
		for (int j = 0; j < 10; j++){
			if (MenuItem[i][j] != '\0'){
				putchar(MenuItem[i][j]);
			}
			else{
				putchar(' ');
			}
		}
		printf("                      *\n");
	}
	printf("***************************************************");
	return 0;
}

FileContrller.h用于文件相关的操作,还有初始化

void writeIntoFIle(FILE *fp, char path[]){
	fopen_s(&fp, path, "w+");
	struct User *temp = (struct User *)malloc(sizeof(struct User));
	temp = head;
	for (int i = 0; i < 5; i++){
		//	printf("%s %s %d\n", temp->UserAccount, temp->UserPassword, temp->Money);
		fprintf(fp, "%s %s %d\n", temp->UserAccount, temp->UserPassword, temp->Money);
		if (temp->next != NULL)
			temp = temp->next;
	}

}


void initFile(FILE *fp, char path[]){
	//fopen_s(&fp, path, "w+");
	if (feof(fp)){
		fprintf(fp, "%s %s %d\n", "6210300022352323", "123123", 10000);
		fprintf(fp, "%s %s %d\n", "6210300022352527", "132132", 1000000);
		fprintf(fp, "%s %s %d\n", "6210303002352323", "123123", 10000);
		fprintf(fp, "%s %s %d\n", "6210300202235233", "123123", 10000);
		fprintf(fp, "%s %s %d\n", "6213002323523233", "123123", 10000);
	}
	//	fclose(fp);
}
void initdata(FILE *fp, char path[]){
	/*printf("%d", fopen_s(&fp, path, "r+"));
	printf("%d", fopen_s(&fp, path, "r+"));
	printf("%d",fgetc(fp)==EOF);*/
	if (fopen_s(&fp, path, "r+") == 0 && fgetc(fp) == EOF){
		initFile(fp, path);
	}
	rewind(fp);
	struct User *user;
	Current_user = user = (struct User *)malloc(sizeof(struct User));
	char temp[100];
	//_getch();
	while (fscanf_s(fp, "%s", &user->UserAccount, 100) != EOF){
		fscanf_s(fp, "%s", &user->UserPassword, 100);
		fscanf_s(fp, "%d\n", &user->Money, 100);
		struct User *nextuser;
		nextuser = (struct User *)malloc(sizeof(struct User));
		printf("%s %s %d\n", user->UserAccount, user->UserPassword, user->Money);
		if (head == NULL)
			head = user;
		user->next = nextuser;
		user = nextuser;

	}
	fclose(fp);
}

AccountController.h//与账户相关的操作,查找账户,验证密码什么的

bool checkAccount(char account[],struct User *u){

	struct User *temp = (struct User *)malloc(sizeof(struct User));
	struct User *headCopy = (struct User *)malloc(sizeof(struct User));
	temp = head;
	*headCopy =* head;
	while (temp != NULL){
		bool isExist = true;
		/*printf("first%s %d\n", head->UserAccount, head->Money);
		printf("temp%s %d\n", temp->UserAccount, temp->Money);*/
		for (int i = 0; i < 16; i++)
		{	
			if (account[i]!=temp->UserAccount[i])
			{
				isExist = false;
				break;
			}
		} 
		if (isExist)
		{ 
			u=temp;
			Current_user = u;
	       //*head =* headCopy;
		/*	printf("findout%s\n",u->UserAccount);
			printf("afterchange%s %s\n", Current_user->UserAccount, head->UserAccount);
			printf("headcopy%s %s\n", headCopy->UserAccount, headCopy->UserPassword);
			_getch();*/
			return true;
		}
		temp = (temp->next);
	}
	

	return false;
}
bool checkPassword(char Userpwd[], char Inputpwd[]){
	for (int i = 0; i<6; i++){
		if (Userpwd[i] != Inputpwd[i]){
			return false;
		}
	}
	return true;
}
void showAccount(char account[]){
	int length = strlen(account);
	if (length>16)
		length=16;
	for (int i = 0; i < length; i++){
		putchar(account[i]);
		if ((i+1) % 4 == 0&&i!=0){
			putchar(' ');
		}
	}
}
void showPassword(int length){
	if (length>6)
		length = 6;
	for (int i = 0; i < length; i++){
		_putch('*');
	}

}
void InputAccount(char UserAccount[]){
	int i = 0;
	system("cls");
	printf("请输入用户名:");
	while ((UserAccount[i] = _getch()) != '\r'&&i<16){
		if (UserAccount[i] == 8&&i>0){
			UserAccount[i] = '\0';
			UserAccount[i-1] = '\0';
			i--;
		}
		else if (UserAccount[i] >= '0' && UserAccount[i] <= '9'){
			if (i <= 15)
				i++;
		}
		system("cls");
		printf("请输入用户名:");
		showAccount(UserAccount);
	}
}
void InputPassword(char account[],char pwd[]){
	int i = 0;
	system("cls");
	printf("用户名:");
	showAccount(account);
	printf("\n请输入密码:");
	while ((pwd[i] = _getch()) != '\r'){
	 	if (pwd[i] == 8){
			pwd[i] = '\0';
			pwd[i - 1] = '\0';
			i--;
		}
		else if (pwd[i] >= '0' && pwd[i] <= '9'){
			if (i <= 5) 
				i++;
		}
		system("cls");
		printf("用户名:");
		showAccount(account);
		printf("\n请输入密码:");
		showPassword(strlen(pwd));
	}
}
void WithDraw(int Count){
	if (Count > Current_user->Money){
		printf("取款金额超出存款");
		return;
	}
	else {
		system("cls");
		printf("取款金额%d\n按任意键返回上一菜单",Count);

		Current_user->Money -= Count;
	}
}
void Deposit(int Count){
		system("cls");
	
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言动态规划-Sumsets(Hdu 2709) 下一篇C指针编程之道 ---第五次笔记

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: