-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (86 loc) · 2.74 KB
/
main.cpp
File metadata and controls
114 lines (86 loc) · 2.74 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
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <chrono>
#include "Classes/Array.h"
#include "Classes/SortedArray.h"
#include "Classes/BST.h"
#include "Classes/AVL.h"
#include "Classes/HashTable.h"
#include "Funcs.h"
using namespace std;
void createQ(string* Q, int size, char* file_name);
template<typename T> string PrintTime(T&, const char*);
int main()
{
srand((unsigned)time(0));
char* file_name;
ifstream ifs;
bool FILE_EXIST=true;
file_name = (char*)"test.txt"; // name of the file
ifs.open(file_name);
if(!ifs.good()){
cout<<"!!!Default Filename does not correspond to any File in this directory!!!\nThe Program will now terminate\n";
exit(1);
}
/* Reads the name of the file from the keyboard
cout<<"Insert File Name:";
do{
if(!FILE_EXIST) cout<<"File Name does not exist\nEnter File Name that exists:";
cin >> file_name;
ifs.open(file_name);
FILE_EXIST = ifs.good();
}while(!FILE_EXIST);
*/
cout<<"Program has started\nConstruction of the Data Structures may take a while \n";
Array a(file_name);
SortedArray b(file_name);
BST c(file_name);
AVL d(file_name);
HashTable e(file_name);
const int size =rand() % a.GetNum() % 10001;
string * Q;
Q = new string[size];
createQ(Q, size,file_name);
ofstream ofs("OUT.txt");
for(int i=0; i<size; i++){
ofs << "++++++++++++"<<Q[i]<<"++++++++++++\n";
ofs << " Array : "<<PrintTime(a, Q[i].c_str());
ofs << " S.Array: "<<PrintTime(b, Q[i].c_str());
ofs << " BST : "<<PrintTime(c, Q[i].c_str());
ofs << " AVLTree: "<<PrintTime(d, Q[i].c_str());
ofs << " HashTable: "<<PrintTime(e, Q[i].c_str());
ofs<<"\n\n";
}
ofs.close();
cout<<"The procedure was successful. Check the OUT.txt file to see the results.\n";
}
void createQ(string* Q, int size,char * file_name)
{
int i = 0;
ifstream ifs = My::ClearFile(file_name);
string a;
while (ifs >> a&& i<size)
{
if ((rand()) % 5 == 1)
{
Q[i] = My::better2low(a.c_str());
i++;
}
}
ifs.close();
remove("Temp.txt");
}
template<typename T>
string PrintTime(T& a, const char* keyword){
string OUT; // Output String
auto start = chrono::high_resolution_clock::now(); // Start Time
auto i = a.search(keyword);
auto end = chrono::high_resolution_clock::now(); // End Time
chrono::duration<double, milli> fp_ms = end - start; // Duration (End - Start)
double time = fp_ms.count(); // Duration to double (milliseconds)
int Fz = a.GetFrequency(i); // Frequency
OUT = to_string(time) + " milliseconds ||| Frequency:" + to_string(Fz) + '\n'; // Example: "0.006 milliseconds Frequency: 9"
return OUT.c_str();
}