-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbLib.cpp
executable file
·156 lines (140 loc) · 4.89 KB
/
dbLib.cpp
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
/*
* =========================================================================================
* Name : dbLib.cpp
* Author : Duc Dung Nguyen, Nguyen Hoang Minh
* Email : [email protected]
* Copyright : Faculty of Computer Science and Engineering - Bach Khoa University
* Description : library for Assignment 1 - Data structures and Algorithms - Fall 2017
* This file implements functions used for database management
* =========================================================================================
*/
#include "dbLib.h"
#include <time.h>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <iomanip>
#define pi 3.14159265358979323846
#define earthRadiusKm 6371.0
using namespace std;
void strPrintTime(char* des, time_t& t) {
tm *pTime = localtime(&t);
strftime(des, 26, "%Y-%m-%d %H:%M:%S", pTime);
}
void loadNinjaDB(char* fName, L1List<NinjaInfo_t> &db) {
// TODO: write code to load information from file into db
L1List<NinjaInfo_t> db_temp;
fstream filein(fName, ios::in | ios::out);
string temp, timestamp, id, longitude, latitude;
getline(filein, temp);
NinjaInfo_t nj;
while (getline(filein, temp, ','))
{
getline(filein, timestamp, ',');
struct tm tm;
istringstream ss(timestamp);
ss >> get_time(&tm, "%m/%d/%Y %H:%M:%S");
nj.timestamp = mktime(&tm);
getline(filein, id, ',');
while (id.length() < 4)
{
id = "0" + id;
}
strcpy(nj.id, id.c_str());
getline(filein, longitude, ',');
istringstream(longitude) >> nj.longitude;
getline(filein, latitude, ',');
istringstream(latitude) >> nj.latitude;
getline(filein, temp, '\n');
db_temp.push_back(nj);
}
filein.close();
strcpy(db.request2nd, db_temp._tail->data.id);
//Convert to old linkedlist
while (db_temp._head)
{
L1Item<NinjaInfo_t> *pCur = db_temp._head;
L1Item<NinjaInfo_t> *pNext = pCur->next;
NinjaInfo_t nj;
nj = db_temp._head->data;
db.push_back(nj);
while (pNext)
{
if (strcmp(pNext->data.id, nj.id) == 0)
{
nj = pNext->data;
db.push_back(nj);
pCur->next = pNext->next;
L1Item<NinjaInfo_t> *pTemp = pNext;
pNext = pNext->next;
delete pTemp;
pTemp = NULL;
db_temp._size--;
}
else
{
pCur = pNext;
pNext = pNext->next;
}
}
db_temp.removeHead();
}
}
bool parseNinjaInfo(char* pBuf, NinjaInfo_t& nInfo) {
// TODO: write code to parse information from a string buffer, ignore if you don't use it
return true;
}
void process(L1List<ninjaEvent_t>& eventList, L1List<NinjaInfo_t>& bList) {
void* pGdata = NULL;
//static_cast <L1List<ninjaEvent>*> (pGdata = &eventList);
pGdata = &eventList;
L1List<ninjaEvent_t>*eList = static_cast<L1List<ninjaEvent_t>*>(pGdata);
initNinjaGlobalData(&pGdata);
while (!eventList.isEmpty()) {
if(!processEvent(eventList[0], bList, pGdata))
cout << eventList[0].code << " is an invalid event\n";
eventList.removeHead();
}
releaseNinjaGlobalData(pGdata);
}
bool initNinjaGlobalData(void** pGData) {
/// TODO: You should define this function if you would like to use some extra data
/// the data should be allocated and pass the address into pGData
return true;
}
void releaseNinjaGlobalData(void* pGData) {
/// TODO: You should define this function if you allocated extra data at initialization stage
/// The data pointed by pGData should be released
}
void printNinjaInfo(NinjaInfo_t& b) {
printf("%s: (%0.5f, %0.5f), %s\n", b.id, b.longitude, b.latitude, ctime(&b.timestamp));
}
/// This function converts decimal degrees to radians
double deg2rad(double deg) {
return (deg * pi / 180);
}
/// This function converts radians to decimal degrees
double rad2deg(double rad) {
return (rad * 180 / pi);
}
/**
* Returns the distance between two points on the Earth.
* Direct translation from http://en.wikipedia.org/wiki/Haversine_formula
* @param lat1d Latitude of the first point in degrees
* @param lon1d Longitude of the first point in degrees
* @param lat2d Latitude of the second point in degrees
* @param lon2d Longitude of the second point in degrees
* @return The distance between the two points in kilometers
*/
double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = deg2rad(lat1d);
lon1r = deg2rad(lon1d);
lat2r = deg2rad(lat2d);
lon2r = deg2rad(lon2d);
u = sin((lat2r - lat1r)/2);
v = sin((lon2r - lon1r)/2);
return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}