1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
using namespace std;
const int NAMESIZE = 11;
typedef double Acctid_t;
typedef char Fname_t[NAMESIZE];
typedef char Lname_t[NAMESIZE];
typedef double balance_t;
const int FILENAMESIZE = 80;
typedef char Filename_t[FILENAMESIZE];
typedef fstream bankfile_t;
struct Bank_t
{
Fname_t Name;
Lname_t LName;
Acctid_t Acctid;
balance_t balance;
};
typedef int Addr_t;
const Addr_t MAXNODES = 20;
struct Node_t
{
Bank_t Bank;
Addr_t Left;
Addr_t Right;
};//Nod_t
typedef Node_t Tree_t[MAXNODES];
const Addr_t NIL = -1;
bool search(Tree_t Tree,Addr_t Root, Addr_t &Parent, Bank_t &TheBank)
{
bool found = false;
if(Root != NIL)
{
if(strcmp(TheBank.Name,Tree[Root].Bank.Name)<0)
{
Parent = Root;
found = search(Tree,Tree[Root].Left,Parent,TheBank);
}
else if(strcmp(TheBank.Name,Tree[Root].Bank.Name)>0)
{
Parent = Root;
found = search(Tree,Tree[Root].Right,Parent,TheBank);
}
else
{
found = true;
TheBank = Tree[Root].Bank;
}
}
return found;
}//search
void insert(Tree_t &Tree, Addr_t &Root, Addr_t Parent,
Addr_t &Avail, Bank_t TheBank)
{
Addr_t Curr;
Curr = Avail;
Avail = Avail + 1;
Tree[Curr].Bank = TheBank;
Tree[Curr].Left = NIL;
Tree[Curr].Right = NIL;
if( Parent != NIL)
{
if(strcmp(TheBank.Name,Tree[Parent].Bank.Name)<0)
{
Tree[Parent].Left = Curr;
}
else
{
Tree[Parent].Right = Curr;
}
}
else
{
Root = Curr;
}
}//insert
void Add(Tree_t &Tree,
Addr_t &Root,
Addr_t &Avail, Bank_t Thebank)
{
Addr_t Parent;
Parent = NIL;
bool found;
found = search(Tree,Root,Parent,Thebank);
if(!found)
{
insert(Tree,Root,Parent,Avail,Thebank);
}
else
{
cout << "Duplicate" << "Student not inserted" << endl;
}
}//Add
void Load(Tree_t &Tree, Addr_t Root, Addr_t Avail)
{
Acctid_t LAcctid;
Fname_t LFname;
Lname_t LLname;
balance_t lbalance;
bankfile_t bankfile;
cout << "Enter name of file to open: " << endl;
Filename_t filename;
cin >> filename;
bankfile.open(filename,ios::in);
bankfile >> LAcctid;
bankfile >> LFname;
bankfile >> LLname;
bankfile >> lbalance;
Bank_t TheBank;
insert(Tree,Root,Parent,Avail,LFname);
while(!bankfile.eof())
{
Add(Tree,Root,Avail,TheBank);
insert(Tree,Root,Parent,Avail,LFname);
}//while
bankfile.close();
}//Load1I
main()
{
Tree_t Bankhill;
Addr_t Parent;
Addr_t Root = NIL;
Addr_t Avail = 0;
Load(Bankhill,Root,Avail);
}
|