-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
158 lines (137 loc) · 2.89 KB
/
Copy pathtimer.cpp
File metadata and controls
158 lines (137 loc) · 2.89 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
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
#include <iostream>
#include <cstdio>
#include <ctime>
#include <fstream>
using namespace std;
struct Person
{
string SSN;
string name;
};
int main(int argc, char* argv[])
{
fstream input;
input.open(argv[1]);
char IDR;
string Name;
string SSN;
string First;
string Last;
int insertionCount = 0;
int deletionCount = 0;
int retrievalCount = 0;
int ArraySize = 1000;
int numPersons = 0;
Person *People = new Person[ArraySize];
clock_t start, end;
double duration;
start = clock();
while(!input.eof())
{
input>>IDR;
input>>SSN;
input>>First;
input>>Last;
Name = First + " " + Last;
Person p = {SSN, Name};
switch(IDR)
{
case 'i':
{
bool matchfound = false;
for (int i = 0; i < numPersons; i++)
{
if ((p.SSN).compare(People[i].SSN) ==0)
{
matchfound = true;
break;
}
}
if (!matchfound)
{
if (numPersons == ArraySize)
{
// The current array is full, we need to create double sized array
ArraySize *=2;
Person *New_People = new Person[ArraySize];
// Copy People elements to shadow
for (int i = 0; i<numPersons; i++)
{
New_People[i] = People[i];
}
// Delete People
delete [] People;
People = New_People;
}
else
{
// increment insertionCount, and append element to end of array
People[numPersons].name = p.name;
People[numPersons].SSN = p.SSN;
insertionCount++;
numPersons++;
}
}
break;
}
case 'd':
{
for (int i = 0; i < numPersons; i++)
{
if (((p.name).compare(People[i].name)) == 0 && ((p.SSN).compare(People[i].SSN) == 0 ))
{
if(numPersons < (ArraySize/4))
{
ArraySize /=2;
Person *New_People = new Person[ArraySize];
for(int i = 0; i<numPersons; i++)
{
New_People[i] = People[i];
}
delete [] People;
People = New_People;
for(int pos = i+1; pos<numPersons; pos++)
{
People[pos-1] = People[pos];
}
numPersons--;
deletionCount++;
break;
}
else
{
for(int pos = i+1; pos<numPersons; pos++)
{
People[pos-1] = People[pos];
}
numPersons--;
deletionCount++;
break;
}
}
}
break;
}
case 'r':
{
for (int i = 0; i < numPersons; i++)
{
if ((p.name).compare(People[i].name) == 0 && (p.SSN).compare(People[i].SSN) == 0 )
{
retrievalCount++;
break;
}
}
break;
}
}
}
end = clock();
duration = ( end - start ) / (double) CLOCKS_PER_SEC;
cout<< "The Number of Valid Insertation: " <<insertionCount<<endl;
cout<< "The Number of Valid Deletion: "<<deletionCount<<endl;
cout<< "The Number of Valid Retrieval: "<<retrievalCount<<endl;
cout<<"Item numbers in the array: "<<numPersons<<endl;
cout<<"Array Size is: "<< ArraySize<<endl;
cout<<"Time elapsed: "<<duration<<endl;
}