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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct person
{
string fname, lname, state;
int ss_number, dob;
bool isstate;
person(string a, string b, string c, int d, int e)
{
fname=a;
lname=b;
state=c;
ss_number=d;
dob=e;
isstate=false;
}
void print()
{
cout<<dob<<" "<<ss_number<< " "<<fname<<" "<<lname<<" "<<state<<endl;
}
bool compare(person *p)
{
if(fname<p->fname && lname<p->lname)
{
return true;
}
else
return false;
}
};
template<typename T>
struct node
{
T *data;
node<T>*left, *right;
node(T*d,node<T>*l=NULL, node<T>*r=NULL)
{
data=d;
left=l;
right=r;
}
};
template<typename T>
struct tree
{
node<T>*root;
tree()
{
root=NULL;
}
void insert(T*object, node<T> * n)
{
if (n==NULL)
{
n=new node<T>(object);
}
if(object->compare(n->data)) //compare is to organize states by name and people by either first or last name.
insert(object,n->left);
else
insert(object,n->right);
}
void insert(T*object)
{
if(root!=NULL)
insert(object,root);
}
void print_states(node<T> * temp)
{
if (temp!=NULL)
{
print_states(temp->left);
cout<<temp->data->sname;
print_states(temp->right);
}
}
};
struct state
{
string sname;
tree<person>*P;
bool isstate;
state()
{
// P=new person[3000];
}
state(string a,tree<person> *x)
{sname=a;
P=x;
isstate=true;}
void print()
{
cout<<sname<<endl;
}
bool compare(state*s)
{
if(sname<s->sname)
{
return true;
}
else
return false;
}
};
tree<state> * tree_from_file(string filename)
{
tree<state>* StateTree=new tree<state>();
//node<state> * root=NULL;
ifstream fin (filename.c_str());
if (fin.fail())
cerr << "file not found\n";
if(!fin.fail())
{
while(true)
{
string a,b,sn;
int d,e;
fin >> a >> b >> sn >> d>>e;
if(fin.fail()) break;
person * p = new person(a,b,sn,d,e); //create person
node<state>*temp=StateTree->root; //go to first state node
while(true)
{
if(temp==NULL) //if state doesnt exist in tree yet
{
state*s=new state(sn,new tree<person>()); //create state
StateTree->insert(s); //insert state into tree
temp->data->P->insert(p); //insert person to state
break;
}
if(temp->data->sname == sn) //if state already exists
{
temp->data->P->insert(p);
break;
}
temp=temp; //not sure how to move through data
}
}
}
else
cout<<"Can't Open!";
fin.close();
return StateTree;
}
int main()
{
tree<state>*B=tree_from_file("data.txt");
node<state>*A;
B->print_states(A);
return 0;
}
|