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

📄 402126589.txt

📁 一个简单的银行系统C语言代码
💻 TXT
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ACCOUNTS 100

typedef struct a_info { 
	char last_name[30]; 
	char first_name[30]; 
	char ssn[12]; 
	char acct_n[6]; 
	char addr[60]; 
	float balance; 
} A_INFO; 

enum sort_mode {
	ACCOUNT_NO = 1,
	FIRSTNAME,
	LASTNAME,
	BALANCE
};

char *toUpper(const char *);
void add(A_INFO [], A_INFO [], int *);
void sort(A_INFO [], int, enum sort_mode);
void swap(A_INFO *, A_INFO *);
int binSearch(A_INFO [], int, char *, A_INFO *);
int linearSearchName(A_INFO [], int, char *, char *, A_INFO []);
int linearSearchBalance(A_INFO [], int, float, A_INFO [], int);
void generateAC(char []);

int ac_no = 0;

void main()
{
	enum sort_mode mode = ACCOUNT_NO;
	A_INFO accounts[MAX_ACCOUNTS], sorted[MAX_ACCOUNTS], result, found[MAX_ACCOUNTS];
	int i, last = 0, numResult = 0, choice;
	float amount;
	char command[10], ac_no[6], firstname[30], lastname[30];
	system("cls");
	printf("Welcome to Min Chuan Banking System\n"
		   "===================================\n"
		   "\ntype 'HELP' or '?' for the commands.\n");
	do {
		printf("\n>");
		gets(command);

		if (_stricmp(command, "HELP") == 0 || strcmp(command, "?") == 0)
		{
			printf("\nEnter the below command to make transaction\n\n"
				   " ADD\t\tAdd a new account.\n"
				   " RAA\t\tReport all of the account.\n"
				   " RANUM\t\tReport a particular account by number.\n"
				   " RANAME\t\tReport a particular account by first name and last name.\n"
				   " RABAL\t\tReport a particular account by balance.\n"
				   " DEP\t\tDeposit an amount into a particular account.\n"
				   " WIT\t\tWithdraw money from a particular account.\n"
				   " SORT\t\tSort the account(s) according to a criteria,\n\t\teither account number, name or balance.\n"
				   " CLS\t\tClear the current screen.\n"
				   " HELP / ?\tDisplay this help menu.\n"
				   " EXIT\t\tExit this program.\n");
		}
		else if (_stricmp(command, "ADD") == 0)
		{
			add(accounts, sorted, &last);
			sort(accounts, last, ACCOUNT_NO);
			sort(sorted, last, mode);
		}
		else if (_stricmp(command, "RAA") == 0)
		{
			printf("A/C No.\t\tFirst Name\t\tLast Name\t\tBalance\n"
				   "-------\t\t----------\t\t---------\t\t-------\n");
			for (i = 0; i < last; i++)
				printf("%-5s\t\t%-24s%-24s%7.2f\n",
					    sorted[i].acct_n, sorted[i].first_name, sorted[i].last_name,
						sorted[i].balance);
		}
		else if (_stricmp(command, "RANUM") == 0)
		{
			printf("Please enter an account number: ");
			gets(ac_no);
			if (binSearch(sorted, last, ac_no, &result) != -1)
				printf("\nAccount Number\t: %s\n"
					   "First Name\t: %s\nLast Name\t: %s\n"
					   "SSN\t\t: %s\nAddress\t\t: %s\nBalance\t\t: %.2f\n",
					   result.acct_n, result.first_name, result.last_name,
					   result.ssn, result.addr, result.balance);
			else
				printf("No valid account for this number.\n");
		}
		else if (_stricmp(command, "RANAME") == 0)
		{
			printf("Please enter your first name: ");
			gets(firstname);
			printf("Please enter your last name: ");
			gets(lastname);
			numResult = linearSearchName(sorted, last, firstname, lastname, found);
			if (numResult > 0)
			{
				printf("A/C No.\t\tFirst Name\t\tLast Name\t\tBalance\n"
					   "-------\t\t----------\t\t---------\t\t-------\n");
				for (i = 0; i < numResult; i++)
					printf("%-5s\t\t%-24s%-24s%7.2f\n",
							sorted[i].acct_n, sorted[i].first_name, sorted[i].last_name,
							sorted[i].balance);
			}
			else
				printf("No valid account under this name.\n");
		}
		else if (_stricmp(command, "RABAL") == 0)
		{
			printf("Please enter an amount: ");
			scanf("%f", &amount); fflush(stdin);
			printf("\tYou may choose those record which are\n\n"
				   "\t1. Less than %.2f\n"
				   "\t2. Greater than %.2f\n"
				   "\t3. Equals to %.2f\n"
				   "\nPlease enter your choice: "
				   , amount, amount, amount);
			scanf("%d", &choice); fflush(stdin);
			numResult = linearSearchBalance(sorted, last, amount, found, choice);
			if (numResult > 0)
			{
				printf("A/C No.\t\tFirst Name\t\tLast Name\t\tBalance\n"
					   "-------\t\t----------\t\t---------\t\t-------\n");
				for (i = 0; i < numResult; i++)
					printf("%-5s\t\t%-24s%-24s%7.2f\n",
							sorted[i].acct_n, sorted[i].first_name, sorted[i].last_name,
							sorted[i].balance);
			}
			else
				printf("No account found.\n");
		}
		else if (_stricmp(command, "DEP") == 0)
		{
			printf("Please enter an account number: ");
			gets(ac_no);
			i = binSearch(sorted, last, ac_no, &result);
			if (i != -1)
			{
				printf("\nAccount No. : %s\nName : %s %s\n", result.acct_n, result.first_name, result.last_name);
				printf("\nHow much do you want to deposit? ");
				scanf("%f", &amount); fflush(stdin);
				sorted[i].balance += amount;
				i = binSearch(accounts, last, ac_no, &result);
				accounts[i].balance += amount;
			}
			else
				printf("No valid account for this number.\n");
		}
		else if (_stricmp(command, "WIT") == 0)
		{
			printf("Please enter an account number: ");
			gets(ac_no);
			i = binSearch(sorted, last, ac_no, &result);
			if (i != -1)
			{
				printf("\nAccount No. : %s\nName : %s %s\nBalance : %.2f\n",
						result.acct_n, result.first_name, result.last_name, result.balance);
				printf("\nHow much do you want to withdraw? ");
				scanf("%f", &amount); fflush(stdin);
				if (result.balance >= amount)
				{
					sorted[i].balance -= amount;
					i = binSearch(accounts, last, ac_no, &result);
					accounts[i].balance -= amount;
				}
				else
					printf("You wanna rob the bank???\n");
			}
			else
				printf("No valid account for this number.\n");
		}
		else if (_stricmp(command, "SORT") == 0)
		{
			printf("\tYou may choose to sort by\n\n"
				   "\t1. Account Number\n"
				   "\t2. First Name\n"
				   "\t3. Last Name\n"
				   "\t4. Balance\n"	
				   "\nPlease enter your choice: ");
			scanf("%d", &choice); fflush(stdin);
			mode = choice;
			sort(sorted, last, mode);
		}
		else if (_stricmp(command, "EXIT") == 0);
		else if (_stricmp(command, "") == 0);
		else if (_stricmp(command, "cls") == 0)
			system("exit");
		else
			printf("Bad command or file name\n");

	} while (strcmp(toUpper(command), "EXIT") != 0);
}

void add(A_INFO ac[], A_INFO s[], int *tail)
{
	char account_number[6];
	printf("\nPlease enter your first name: ");
	gets(ac[*tail].first_name);
	printf("Please enter your last name: ");
	gets(ac[*tail].last_name);
	printf("Please enter ssn: ");
	gets(ac[*tail].ssn);
	printf("Please enter your address: ");
	gets(ac[*tail].addr);
	ac[*tail].balance = 0.0;
	generateAC(account_number);
	strcpy(ac[*tail].acct_n, account_number);
	printf("\nYour account number is %s.\n", account_number);
	s[*tail] = ac[*tail];
	(*tail)++;
}

void generateAC(char num[])
{
	unsigned i;
	char temp[5];
	_itoa(++ac_no, temp, 10);
	num[0] = 'A';
	for (i = 1; i <= 4 - strlen(temp); i++)
		num[i] = '0';
	num[i] = '\0';
	strcat(num, temp);
}

void sort(A_INFO ac[], int size, enum sort_mode mode)
{
	int i, j;
	for (i = 0; i < size; i++)
		for (j = i + 1; j < size; j++)
		{
			switch (mode)
			{
				case ACCOUNT_NO:
					if (_stricmp(ac[i].acct_n, ac[j].acct_n) > 0)
						swap(&ac[i], &ac[j]);
					break;
				case FIRSTNAME:
					if (_stricmp(ac[i].first_name, ac[j].first_name) > 0)
						swap(&ac[i], &ac[j]);
					break;
				case LASTNAME:
					if (_stricmp(ac[i].last_name, ac[j].last_name) > 0)
						swap(&ac[i], &ac[j]);
					break;
				case BALANCE:
					if (ac[i].balance > ac[j].balance)
						swap(&ac[i], &ac[j]);
					break;
			}	
		}
}

int binSearch(A_INFO ac[], int last, char *element, A_INFO *found)
{
	int low = 0, high = last - 1, mid = (last - 1) / 2;
	while (high >= low)
	{
		mid = (low + high) / 2;
		if (_stricmp(ac[mid].acct_n, element) > 0)
			high = mid - 1;
		else if (_stricmp(ac[mid].acct_n, element) < 0)
			low = mid + 1;
		else if (_stricmp(ac[mid].acct_n, element) == 0)
		{
			*found = ac[mid];
			return mid;
		}
	}
	return -1;
}

int linearSearchName(A_INFO ac[], int size, char *f, char *l, A_INFO result[])
{
	int i, numResult = 0;
	for (i = 0; i < size; i++)
		if (_stricmp(ac[i].first_name, f) == 0 && _stricmp(ac[i].last_name, l) == 0)
		{
			result[numResult] = ac[i];
			numResult++;
		}
	return numResult;
}

int linearSearchBalance(A_INFO ac[], int size, float balance, A_INFO result[], int mode)
{
	int i, numResult = 0;
	for (i = 0; i < size; i++)
	{
		switch (mode)
		{
			case 1:
				if (ac[i].balance < balance)
				{
					result[numResult] = ac[i];
					numResult++;
				}
				break;
			case 2:
				if (ac[i].balance > balance )
				{
					result[numResult] = ac[i];
					numResult++;
				}
				break;
			case 3:
				if (ac[i].balance == balance )
				{
					result[numResult] = ac[i];
					numResult++;
				}
				break;
			default:
				break;
		}
	}
	return numResult;
}

void swap(A_INFO *a, A_INFO *b)
{
	A_INFO temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

char *toUpper(const char *str)
{
	char *temp;
	int i;
	temp = malloc(strlen(str) + 1);
	for (i = 0; str[i] != '\0'; i++)
		temp[i] = toupper(str[i]);
	temp[i] = '\0';
	return temp;
}

⌨️ 快捷键说明

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