Code:
#include<stdio.h>
#include<string.h>
struct node {
int acctnum;
char lastName[15];
char firstName[10];
float balance;
node* left;
node* right;
node(int c = 0, char f[10] = "", char l[15] = "", float b = 0.0) {
acctnum = c;
strcpy(lastName, l);
strcpy(firstName, f);
balance = b;
left=right=NULL;
}
};
class BST{
private:
node *root,*ptr,*iterator;
void _insert(node *s, int x, char fn[], char ln[],float ba){
if(x==root->acctnum)
return;
if(x<s->acctnum){
if(s->left==NULL)
{
ptr=new node(x,fn,ln,ba);
s->left=ptr;
return;
}
else _insert(s->left,x,fn,ln,ba);
return;}
if(x>s->acctnum){
if(s->right==NULL){
ptr=new node(x,fn,ln,ba);
s->right=ptr; return;}
else _insert(s->right,x,fn,ln,ba);
return; }
}
void _inorder(node *s,FILE *x){
if(s){
_inorder(s->left,x);
fprintf(x,"%-6d%-16s%-11s%10.2f\n",s->acctnum,s->lastName,s->firstName,s->balance);
_inorder(s->right,x);
}
}
void _preorder(node *s,FILE *x){
if(s){
fprintf(x,"%-6d%-16s%-11s%10.2f\n",s->acctnum,s->lastName,s->firstName,s->balance);
_preorder(s->left,x);
_preorder(s->right,x);}
}
void _postorder(node *s){
if(s){
_postorder(s->left);
_postorder(s->right);
delete s;}
}
node* findParent(int x){
node *parent, *tmp= root;
if (x == tmp->acctnum)
return NULL;
while (tmp != NULL)
{
if (x == tmp->acctnum)
break;
parent = tmp;
if (x < tmp->acctnum)
tmp = tmp->left;
else tmp = tmp->right;
}
return parent;
}
node* findMinnode(node *s) {
while (s->left != NULL){
s = s->left;
}
return s;
}
node* findMaxnode(node *s) {
while (s->right != NULL) {
s = s->right;
}
return s;
}
node* find(int x){
ptr=root;
while(ptr){
if(x==ptr->acctnum)
return ptr;
if (x>ptr->acctnum)
ptr=ptr->right;
else ptr=ptr->left;
} return NULL;
}
public:
BST(){
root=NULL;
ptr=iterator=root;}
~BST(){
postorder();}
void insert(int x,char f[],char l[],float b){
if(root)
_insert(root,x,f,l,b);
else
root=new node(x,f,l,b);}
void inorder(FILE *s){
_inorder(root,s);
}
void preorder(){
//_preorder(root);
}
void postorder(){
_postorder(root);
}
bool search(int x){
ptr=root;
while(ptr){
if(x==ptr->acctnum)
return true;
if (x>ptr->acctnum)
ptr=ptr->right;
else ptr=ptr->left;
} return false;
}
bool remove(int x) {
node* parent, *minnode;
if (!root)
return false;
if (!find(x))
return false;
parent = findParent(x);
if (parent)
{
if ((ptr->left == NULL) && (ptr->right == NULL))
{
if (parent->left == ptr)
parent->left = NULL;
else
parent->right = NULL;
delete ptr;
return true;
}
if (ptr->left == NULL)
{
if (parent->left == ptr)
parent->left = ptr->right;
else
parent->right = ptr->right;
delete ptr;
return true;
}
if (ptr->right == NULL)
{
if (parent->left == ptr)
parent->left = ptr->left;
else
parent->right = ptr->left;
delete ptr;
return true;
}
minnode = findMinnode(ptr->right);
parent = findParent(minnode->acctnum);
if(parent == ptr)
{
parent = findParent(ptr->acctnum);
minnode->left = ptr->left;
if( ptr == parent->left)
parent->left =minnode;
else
parent->right =minnode;
delete ptr;
}
else
{
ptr->acctnum = minnode->acctnum;
parent->left = minnode->right;
delete minnode;
}
return true;
}
else
minnode = findMinnode(root->right);
parent = findParent(minnode->acctnum);
if(parent == root)
{
minnode->left = root->left;
root = minnode;
}
else
{
root->acctnum= minnode->acctnum;
parent->left = minnode->right;
delete minnode;
}
return true;
}
void withdraw(int account, float am){
node *p=find(account);
if((p->balance) >= am){
p->balance -= am;
printf("%-6d%-16s%-11s%10.2f\n",p->acctnum,p->lastName,p->firstName,p->balance);}
else
printf("you cannot withdraw %.2f$ your balance contains only %.2f$\n",am, p->balance );
}
void deposit(int account,float am){
node *p;
p=find(account);
p->balance += am;
printf("%-6d%-16s%-11s%10.2f\n",p->acctnum,p->lastName,p->firstName,p->balance);}
};
struct Bank {
private:
FILE *fp;
BST a;
void readfile() {
int acc;
char fn[10];
char ln[15];
float bal;
if ( ( fp = fopen( "bank.txt", "r" ) ) != NULL ){
while(!feof(fp)){
fscanf(fp,"%d",&acc);
fscanf(fp,"%s",ln);
fscanf(fp,"%s",fn);
fscanf(fp,"%f",&bal);
a.insert(acc,fn,ln,bal); }
}
menu();
return;
}
int enterChoice() {
int choice;
printf("\nEnter your choice\n"
"1 - save information in the text file \n"
" \"bank.txt\" for printing\n"
"2 - update an account\n"
"3 - add a new account\n"
"4 - delete an account\n"
"5 - end program\n"
"(Note:Befor Ending save your information in a file by choosing first selection)\nYour choice: " );
scanf( "%d", &choice );
return choice;
}
void menu() {
int choice;
while ( ( choice = enterChoice() ) != 5 ) {
switch ( choice ) {
case 1:
saveinfo();
break;
case 2:
updateaccount();
break;
case 3:
newaccount();
break;
case 4:
deleteaccount();
break;
default: printf("\nPlease select from 1-5\n\n");
}
}
}
void newaccount(){
int acct;
char f[10];
char l[15];
float b;
printf("Enter number of account u want to create from :\n");
scanf("%d",&acct);
if(!(a.search(acct))&& ( acct>0)){
printf("Enter your firstname, lastname, balance: ");
scanf("%s%s%f",&f,&l,&b);
a.insert(acct,f,l,b);}
else
printf("the number u entered is in use or it doesn't exist \n");
}
void updateaccount(){
int acct;
int i;
float amount;
printf("enter ur account number :\n");
scanf("%d",&acct);
if(a.search(acct)){
printf("enter 1 if you want to deposit or 2 for withdrawaing: ");
scanf("%d",&i);
printf("Enter the amount of money: ");
scanf("%f",&amount);
if(i==2)
a.withdraw(acct,amount);
else if(i==1)
a.deposit(acct,amount);
else
printf("you didn't enter the right # for depositing or withdrawing\n");
}
else
printf("The account number doesn't exist\n");
}
void saveinfo() {FILE *w;
if ( ( w = fopen( "bank.txt", "w" ) ) == NULL )
printf( "File could not be opened.\n" );
else {
fprintf( w, "%-6s%-16s%-11s%10s\n","Acct", "Last Name", "First Name","Balance" );
a.inorder(w);}
}
void deleteaccount(){
int j;
printf("enter number of account u want to delete: ");
scanf ("%d",&j);
if(a.search(j)){
a.remove(j);
printf("account %d was removed \n",j);}
else
printf("This account doesn't exist\n");
}
public:
Bank() {
readfile();
}
~Bank() {
fclose(fp);
}
};
int main() {
Bank a;
return 0;
}