-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcriminal.cpp
More file actions
133 lines (121 loc) · 3.87 KB
/
criminal.cpp
File metadata and controls
133 lines (121 loc) · 3.87 KB
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
/******************************************************************************
Name: criminal.cpp
Des: This file builds a criminal database from criminal.txt and uses
casefile.txt to process and prune the tree. The suspects that match the case
file are output in alphabetical in criminal.out file. The criminal database is
constructed using an alphabetically sorted binary search tree, and criminals
not matching the casefile are pruned from the tree.
Author: Matthew Thompson
Date: 4/25/2017
******************************************************************************/
#include"bst.h"
#include"vld.h"
#include<fstream>
#include<iostream>
// Function Prototypes
void constructDatabase(Tree & crimimalDatabase);
void main()
{
ifstream fin("casefile.txt");
ofstream fout("criminal.out");
string inputLine;
int caseNumber = 1;
fin >> inputLine;
// Read each inputline from casefile.txt until reaching "FINISHED" or the end of the file.
while (inputLine != "FINISHED" && !fin.eof())
{
Tree criminalDatabase;
constructDatabase(criminalDatabase);
// Process each group of lines starting with the word "CASE".
if (inputLine == "CASE")
{
// Output directly to criminal.out.
fout << "CASE NUMBER " << caseNumber << endl;
fout << "PRIME SUSPECTS ARE..." << endl;
// Reads "TIP" before reading the actual tip content.
fin >> inputLine;
while (inputLine != "CASE" && inputLine != "FINISHED")
{
if (inputLine == "TIP")
{
fin >> inputLine;
}
// Trim the database down to only the criminals who match the tip.
criminalDatabase.searchAndPrune(inputLine);
fin >> inputLine;
}
// Output the criminals in alphabetical order.
criminalDatabase.output(fout);
fout << endl;
caseNumber++;
}
}
fin.close();
Tree criminalDatabase;
constructDatabase(criminalDatabase);
Node * foundNode = criminalDatabase.find("ERIN-TAFT");
if (foundNode != nullptr)
{
cout << foundNode->name << endl;
}
else
{
cout << "Node not found." << endl;
}
criminalDatabase.insert("MATTHEW-THOMPSON");
criminalDatabase.insert("RAYNA-DEYOUNG");
criminalDatabase.insert("LEW-KNAPP");
criminalDatabase.insert("BETHANY-SIMMONS");
criminalDatabase.insert("EMMA-ESSLINGER");
criminalDatabase.insert("EMILY-MORRIS");
criminalDatabase.insert("EMILY-NICKS");
criminalDatabase.insert("EMILY-WIDEMAN");
criminalDatabase.insert("EMILY-GREEN");
criminalDatabase.insert("JACOB-MASON");
criminalDatabase.insert("COLBERT-LEHR");
criminalDatabase.insert("OLIVIA-LANUM");
criminalDatabase.insert("ZEPH-LONG");
criminalDatabase.output(fout);
fout.close();
}
/******************************************************************************
Name: constructDatabase
Des: This function reads input from criminal.txt and constructs an alphabetical
binary search tree of criminals. Each node of the tree contains a name, up to
eight criminal attributes, and an attribute count.
******************************************************************************/
void constructDatabase(Tree & criminalDatabase)
{
ifstream fin("criminal.txt");
string inputLine;
fin >> inputLine;
while (!fin.eof())
{
// Read the group of lines starting with "SUSPECT" which give name
// and multiple criminal attributes.
if (inputLine == "SUSPECT")
{
string name;
string attributes[8];
int attributeCount = 0;
fin >> inputLine;
// Ensure that the suspect block actually has content.
if (inputLine != "SUSPECT" && inputLine != "FINISHED")
{
name = inputLine;
fin >> inputLine;
// Read multiple attributes listed below name.
while (inputLine != "SUSPECT" && inputLine != "FINISHED")
{
attributes[attributeCount] = inputLine;
attributeCount++;
fin >> inputLine;
}
// This dynamically creates a new node in the tree which recieves
// the processed information.
criminalDatabase.insert(name, attributes, attributeCount);
}
}
}
fin.close();
}