-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1109 lines (948 loc) · 33.1 KB
/
main.cpp
File metadata and controls
1109 lines (948 loc) · 33.1 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************************************************
* Array and Linked List Implementations of a City Database
* Program Designed and Documented by Mathew Jackson
* Project 1 for Data Structures
*
* // ARRAY:
* // Functionality
* // Insert
* // Delete by name or coordinates
* // Search by name or coordinates
* // Function to print out the names of cities in a given distance
* // Print all listings
*
* // LINKED LIST:
* // Functionality
* // Insert
* // Search by name or coordinates
* // Delete by name or coordinates
* // Function to print out the names of cities in a given distance
* // Print all listings
*
*******************************************************************************************************************/
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <sstream>
#include <fstream>
#include <math.h>
#include <cmath>
#include <time.h>
#include <string>
#include <algorithm>
using namespace std;
//Constants
const int MAX = 1000; // Change this value to 100 for larger set of data
const float radiusMiles = 3963.191;
//Main Function Prototypes
//-------------------- //
int printUserInterface();
void arrayDataBase();
void linkedlistDataBase();
float deg_to_radians(float);
float rad2deg(float);
//-------------------- //
//File variables //
//----------------------//
ifstream oldtestData;
ifstream linkedTestData;
ofstream newtestData;
//----------------------//
//Array Class//
class ArrayImplementation{
public:
ArrayImplementation(){
sizeOfArray = 0;
spacesLeft = MAX;
}
string cityName[MAX];
float cityLongitude[MAX];
float cityLatitude[MAX];
int sizeOfArray;
int spacesLeft; // how many more spots in the array are left
int lastPos;
int curPosition;
//On Start up when choosing array-implementation
void fillArray(string[]);// parameter takes an array of data from TestFile.txt
// To rearrange array after deletions
void arrangeArray();
// Basic functionality
void insertCity(); //COMPLETE -- error handling and duplicate detection
int searchByName(bool, bool); // COMPLETE -- I made it so it is not case-sensitive
int searchByCoord(bool);// COMPLETE -- Fixed comparison-of-floats problem with error-handling fabs() functions
void deleteCityByName();// COMPLETE -- TODO::May need to check through for error-handling
void deleteCityByCoordinates();// COMPLETE
void printCitiesInDestination();
void printAll();// COMPLETE -- TODO:: Need to get rid of the empty entries
};
// Node Structure
class Node{
private:
string cityName;
float cityLatitude;
float cityLongitude;
string fromFileStream;
public:
friend class LinkedList;
Node* nextN;
Node(string name, float latitude, float longitude){
cityName = name;
cityLatitude = latitude;
cityLongitude = longitude;
}
};
// Linked List Class
class LinkedList{
public:
LinkedList(){
root = NULL;
head = NULL;
filled = false;
}
~LinkedList(){
delete root;
delete head;
}
bool filled; // Used for determining if the linked list has been filled from previous data
//Pointers
Node* root;
Node* head;
// Functions
void insertCity(); // COMPLETE : TODO: Verify city does not already exist
void insertCity(string); // COMPLETE -- Used for importing previous data
void searchByName(); // COMPLETE
void searchByCoord(); // COMPLETE
void deleteCityByName(); // COMPLETE
void deleteCityByCoordinates(); // COMPLETE
void printCitiesInDestination(); // COMPLETE
void printAll();// COMPLETE
};
int main(void)
{
//Program Starter
while(printUserInterface()!= 0);
return 0;
}
int printUserInterface(){
char usersChoiceChar; // Choosing array or linked list
cout<<"*************************************\n"
<<"Implementation Options:\n"
<<"A.Use an array-based list implementation\n"
<<"B.Use a linked-list implementation\n"
<<endl;
cout<<"Your Implementation: ";
bool valid = false;
while(!valid){
cin>>usersChoiceChar;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a A or B only only." << endl;
valid = false;
}else{
valid = true;
}
}
usersChoiceChar = toupper(usersChoiceChar);
switch(usersChoiceChar){
case 'A':
arrayDataBase();
return 0;
break;
case 'B':
linkedlistDataBase();
return 0;
break;
default:
cout<<"Incorrect Choice. Please Select again"<<endl;
return 1;
}
}
/**********************************************ARRAY SECTION********************/
void arrayDataBase(){
string line;
// to arrange the file
string lineArray[MAX];
//actual db of cities and their coordinates *City/Latitude/Longitude
string cityDB[MAX];
ArrayImplementation citiesDB;
//Open files
oldtestData.open("TestData.txt");
newtestData.open("newTestData.txt");
int i = 0; //to count the index of cities in the string array
if(oldtestData.is_open()){
while(getline(oldtestData, line)){
if(!line.empty()){
lineArray[i++] = line;
}
}
}else{
cout<<"Cannot open file"<<endl;
}
//Pump the removed blank line data into a temp array
for(int n = 1; n < 20; n++ ){
newtestData << lineArray[n] << "\n";
}
//Filling the cityDB and removing header from datafile
for(int j = 1; j <= 20; j++){
cityDB[j-1] = lineArray[j];
}
//Initialize the current set of data
citiesDB.fillArray(cityDB);
int usersChoiceInt;
cout<<"Operation Options:\n"
<<"1. Insert a record\n"
<<"2. Search for a record by name\n"
<<"3. Search for a record by coordinate\n"
<<"4. Delete a record by name\n"
<<"5. Delete a record by coordinate\n"
<<"6. Print records within a given distance of a specified location\n"
<<"7. Print all records\n"
<<"8. Exit"
<<endl;
while(usersChoiceInt != 8){
cout<<"Your Operation: ";
bool valid = false;
while(!valid){
cin>>usersChoiceInt;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a single number only" << endl;
valid = false;
}else{
valid = true;
}
}
switch(usersChoiceInt){
case 1:
clock_t c1, c2;
c1 = clock();
citiesDB.insertCity();
c2 = clock();
long double diff;
diff = (long double)c2 - (long double)c1;
long double secs;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 2:
c1 = clock();
citiesDB.searchByName(false, false); // false because we don't want to delete the city
c2 = clock();
diff = 0;
diff = (long double)c2 - (long double)c1;
secs = 0;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 3:
c1 = clock();
citiesDB.searchByCoord(false);// false because we don't want to delete the city
c2 = clock();
diff = 0;
diff = (long double)c2 - (long double)c1;
secs = 0;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 4:
c1 = clock();
citiesDB.deleteCityByName();
c2 = clock();
diff = 0;
diff = (long double)c2 - (long double)c1;
secs = 0;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 5:
c1 = clock();
citiesDB.deleteCityByCoordinates();
c2 = clock();
diff = 0;
diff = (long double)c2 - (long double)c1;
secs = 0;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 6:
c1 = clock();
citiesDB.printCitiesInDestination();
c2 = clock();
diff = 0;
diff = (long double)c2 - (long double)c1;
secs = 0;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 7:
c1 = clock();
citiesDB.printAll();
c2 = clock();
diff = 0;
diff = (long double)c2 - (long double)c1;
secs = 0;
secs = diff/CLOCKS_PER_SEC;
cout<<"Execution time: "<<secs<<endl;
break;
case 8:
cout<<"Bye Bye"<<endl;
break;
default:
cout<<"Non-supported choice, try again"<<endl;
}
}
/*/DEBUG AREA---------------------------------------------//
for(int j = 0; j < MAX; j++){
cout<<citiesDB.cityName[j]<<endl;;
}
for(int j = 0; j < MAX; j++){
cout<<citiesDB.cityLatitude[j]<<endl;;
}
for(int k = 0; k < MAX; k++){
cout<<citiesDB.cityLongitude[k]<<endl;
}
cout<<citiesDB.sizeOfArray<<endl;
cout<<citiesDB.spacesLeft<<endl;
//DEBUG END ----------------------------------------------/*/
//close file
newtestData.close();
oldtestData.close();
}
/**********************************************END ARRAY SECTION********************/
/**********************************************LINKED LIST SECTION********************/
void linkedlistDataBase(){
LinkedList* citiesDB = new LinkedList();
string line;
string lineArray[MAX];
//Open files
oldtestData.open("TestData.txt");
newtestData.open("newTestData.txt");
int i = 0; //to count the index of cities in the string array
if(oldtestData.is_open()){
while(getline(oldtestData, line)){
if(!line.empty()){
lineArray[i++] = line;
}
}
}else{
cout<<"Cannot open file"<<endl;
}
//Pump the removed blank line data into a temp array
for(int n = 1; n < 20; n++ ){
newtestData << lineArray[n] << "\n";
}
newtestData.close();
linkedTestData.open("newTestData.txt");
if(linkedTestData.is_open()){
while(getline(linkedTestData, line)){
if(!line.empty()){
citiesDB->insertCity(line);
}
}
}
int usersChoiceInt;
cout<<"Operation Options:\n"
<<"1. Insert a record\n"
<<"2. Search for a record by name\n"
<<"3. Search for a record by coordinate\n"
<<"4. Delete a record by name\n"
<<"5. Delete a record by coordinate\n"
<<"6. Print records within a given distance of a specified location\n"
<<"7. Print all records\n"
<<"8. Exit"
<<endl;
while(usersChoiceInt != 8){
cout<<"Your Operation: ";
bool valid = false;
while(!valid){
cin>>usersChoiceInt;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a single number only" << endl;
valid = false;
}else{
valid = true;
}
}
switch(usersChoiceInt){
case 1:
citiesDB->insertCity();
break;
case 2:
citiesDB->searchByName();
break;
case 3:
citiesDB->searchByCoord();
break;
case 4:
citiesDB->deleteCityByName();
break;
case 5:
citiesDB->deleteCityByCoordinates();
break;
case 6:
citiesDB->printCitiesInDestination();
break;
case 7:
citiesDB->printAll();
break;
case 8:
cout<<"Bye Bye"<<endl;
break;
default:
cout<<"Non-supported choice, try again"<<endl;
}
}
//close file
oldtestData.close();
}
/***********************************************END LINKED LIST SECTION*****************/
/*LINK-LIST FUNCTIONS */
void LinkedList::insertCity(){
string usersName;
float usersLat;
float usersLong;
cout<<"Enter name of city: ";
cin>>usersName;
cout<<"Enter x-coordinate of "<<usersName<<": ";
cin>>usersLat;
cout<<"Enter y-coordinate of "<<usersName<<": ";
cin>>usersLong;
// If the linked list is empty
if(this->root == NULL){
root = new Node(usersName, usersLat, usersLong);
root->nextN = NULL;
head = root;
head->cityName = usersName;
head->cityLatitude = usersLat;
head->cityLongitude = usersLong;
return;
}
head = root;
//traverse through the list until you get to a node with a NULL pointer for nextN
if(head != NULL){
while(head->nextN != NULL){
head = head->nextN;
}
}
// add a new node to the list
head->nextN = new Node(usersName, usersLat, usersLong);
head = head->nextN;
head->nextN = NULL;
// point back to the beginning
head = root;
cout<<"Entry Successfully added to Linked-List DataBase"<<endl;
/*
// Make sure the conductor is pointing to something
if(head != 0){
// Print the first node
cout<<head->getData()<<endl;
//print the linked list
while(head->nextN != 0){
head = head->nextN;
cout<<head->getData()<<endl;
}
}
delete conductor;
delete root;
*/
}
void LinkedList::insertCity(string line){
// initialize the root node and the head...the head is the "pointer" to the current node
string usersName;
string debug;
float usersLat;
float usersLong;
// Extract information
std::string::size_type colonPos = line.find(":");
std::string::size_type commaPos = line.find(",");
usersName = line.substr(0, colonPos);
usersLat = atof(line.substr(colonPos+1, 10).c_str());
usersLong = atof(line.substr(commaPos + 1).c_str());
// If the linked list is empty
if(this->root == NULL){
root = new Node(usersName, usersLat, usersLong);
root->nextN = NULL;
head = root;
return;
}
//traverse through the list until you get to a node
if(head != NULL){
while(head->nextN != NULL){
head = head->nextN;
}
}
// add a new node to the list
head->nextN = new Node(usersName, usersLat, usersLong);
head = head->nextN;
head->nextN = NULL;
// point back to the beginning
head = root;
}
void LinkedList::searchByName(){
string usersCityName;
cout<<"Enter name of city: "<<endl;
cin>>usersCityName;
head = root;
while(head->nextN != NULL){
if(head->cityName == usersCityName){
cout<<"Found City!\n"<<"x-coordinates: "<<head->cityLatitude<<"\n"<<"y-coordinates: "<<head->cityLongitude<<endl;
return;
}
head = head->nextN;
}
cout<<"Could not find city. Try another name."<<endl;
}
void LinkedList::searchByCoord(){
float usersCityLatitude;
float usersCityLongitude;
cout<<"Enter x-coordinate of city: "<<endl;
cin>>usersCityLatitude;
cout<<"Enter y-coordinate of city: "<<endl;
cin>>usersCityLongitude;
head = root;
while(head->nextN != NULL){
if(fabs(head->cityLatitude - usersCityLatitude) < 0.0001 && fabs(head->cityLongitude - usersCityLongitude) < 0.0001){
cout<<"Found City! It is "<<head->cityName<<endl;
return;
}
head = head->nextN;
}
cout<<"Could not find city. Check your coordinates!"<<endl;
}
void LinkedList::deleteCityByName(){
string usersCityName;
Node* tempNode; // to hold address of previous node of deletion
Node* prevNode;
cout<<"Enter name of city you want to delete: "<<endl;
cin>>usersCityName;
head = root;
tempNode = NULL;
prevNode = root;
while(head != NULL && prevNode->nextN != NULL){
if(head->cityName == usersCityName){
while(prevNode->nextN != NULL && prevNode->nextN != head){
prevNode = prevNode->nextN;
}
tempNode = head;
head = tempNode->nextN;
prevNode->nextN = head;
delete tempNode;
cout<<"Deletion Successful!"<<endl;
return;
}
head = head->nextN;
}
tempNode = NULL;
prevNode = NULL;
free(tempNode);
free(prevNode);
cout<<"Could not find city. Try another name."<<endl;
}
void LinkedList::deleteCityByCoordinates(){
float usersCityLat;
float usersCityLong;
Node* tempNode;
Node* prevNode;
cout<<"Enter x-coordinate of city you want to delete: ";
cin>>usersCityLat;
cout<<"Enter y-coordinate of city you want to delete: ";
cin>>usersCityLong;
head = root;
tempNode = NULL;
prevNode = root;
while(head != NULL && prevNode->nextN != NULL){
//If deletion is at the top of the list
if(fabs(usersCityLat - head->cityLatitude) < 0.0001 && fabs(usersCityLong - head->cityLongitude) < 0.0001){
if(head == root){
tempNode = root;
root = root->nextN;
delete tempNode;
cout<<"Deletion Successful!"<<endl;
return;
}else
{
while(prevNode->nextN != NULL && prevNode->nextN != head){
prevNode = prevNode->nextN;
}
tempNode = head;
head = tempNode->nextN;
prevNode->nextN = head;
cout<<tempNode->cityName<<endl;
delete tempNode;
cout<<"Deletion Successful!"<<endl;
return;
}
}
head = head->nextN;
}
tempNode = NULL;
prevNode = NULL;
free(tempNode);
free(prevNode);
cout<<"Could not find city. Try another name."<<endl;
}
void LinkedList::printCitiesInDestination(){
/// TODO: convert this to linked list...then you're done!
string usersCity;
bool foundCity = false;
float usersLatitude;
float usersLongitude;
float usersDistance;
cout<<"Enter the city you want to find cities around: ";
cin>>usersCity;
head = root;
while(head != NULL){
if(head->cityName == usersCity){
cout<<"Found city!"<<endl;
usersLongitude = head->cityLongitude;
usersLatitude = head->cityLatitude;
foundCity = true;
}
head = head->nextN;
}
if(!foundCity){
cout<<"Could not find city"<<endl;
return;
}
cout<<"What is the distance? ";
cin>>usersDistance;
if(usersDistance == 0){
cout<<"That would be the city's location. Please try again."<<endl;
return;
}
float distance = 0;
float e;
foundCity = false;
head = root;
// Formula to calculate cities in a given distance on Earth
while(head != NULL){
e = acos(sin(deg_to_radians(usersLatitude))*sin(deg_to_radians(head->cityLatitude)) + cos(deg_to_radians(usersLatitude))*cos(deg_to_radians(head->cityLatitude))*cos(deg_to_radians(head->cityLongitude) - deg_to_radians(usersLongitude)));
distance = e*radiusMiles;
if(distance <= usersDistance && usersDistance != 0 && distance > 0){
cout<<"Name: "<<head->cityName<<" | Distance: "<<distance<<endl;
foundCity = true;
}
head = head->nextN;
}
if(foundCity != true){
cout<<"No cities found within that given distance"<<endl;
cout<<"Distance: "<<distance<<" | "<<"Users chosen distance: "<<usersDistance<<endl;
}
}
void LinkedList::printAll(){
head = root;
cout<<"City | "<<"Latitude | "<<"Longitude"<<endl;
while(head->nextN != NULL){
cout<<head->cityName<<": "<<head->cityLatitude<<", "<<head->cityLongitude<<endl;
head = head->nextN;
}
cout<<head->cityName<<": "<<head->cityLatitude<<", "<<head->cityLongitude<<endl;
head = root;
}
/* ARRAY CLASS FUNCTIONS */
void ArrayImplementation::fillArray(string dataArray[MAX]){
for(int i = 0; i < MAX; i++){
std::string::size_type colonPos = dataArray[i].find(":");
std::string::size_type commaPos = dataArray[i].find(",");
cityName[i] = dataArray[i].substr(0, colonPos);
cityLatitude[i] = atof(dataArray[i].substr(colonPos+1, 10).c_str());
cityLongitude[i] = atof(dataArray[i].substr(commaPos + 1).c_str());
}
}
void ArrayImplementation::arrangeArray(){
//TODO: shift an unbalanced array back to normal
for(int i = 0; i < MAX - 1; i++){
if(cityName[i] == "" && cityName[i+1] != ""){
cityName[i] = cityName[i+1];
cityLongitude[i] = cityLongitude[i+1];
cityLatitude[i] = cityLatitude[i+1];
}
}
}
void ArrayImplementation::insertCity(){
int counter = 0;
int i = 0; //cursor for while loop below
while(this->cityLatitude[i] != 0){
counter++;
this->sizeOfArray++;
i++;
}
this->lastPos = counter;
cout<<"Last position is: "<<lastPos<<endl;
string usersCityName;
float usersLat;
float usersLong;
//Enter city name
cout<<"Enter name of the city: ";
cin>>usersCityName;
// Check if the city name already exists
for(int i = 0; i < MAX; i++){
if(usersCityName == this->cityName[i]){
cout<<"City already exists in this Database. No need to enter it again."<<endl;
return;
}
}
this->cityName[this->lastPos] = usersCityName;
//Enter city latitude
cout<<"Enter x-coordinate of the city: ";
bool valid = false;
while(!valid){
cin>>usersLat;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a float/integer only." << endl;
valid = false;
}else{
valid = true;
}
}
this->cityLatitude[this->lastPos] = usersLat;
//Enter city longitude
cout<<"Enter y-coordinate of the city: ";
valid = false;
while(!valid){
cin>>usersLong;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a float/integer only." << endl;
valid = false;
}else{
valid = true;
}
}
// Make sure coordinates do not already exist
for(int j = 0; j < MAX; j++){
if(usersLong == this->cityLongitude[j] && usersLat == this->cityLongitude[j]){
cout<<"City already exists in that location in the Database. Perhaps you have the wrong coordinates?"<<endl;
return;
}
}
this->cityLongitude[this->lastPos] = usersLong;
cout<<"Successfully added entry."<<endl;
// Update everything
this->sizeOfArray++;
this->spacesLeft = MAX - this->sizeOfArray;
this->lastPos++;
}
int ArrayImplementation::searchByName(bool isDeleting, bool distanceSearch){
string usersCity;
string UPPERCASENAME[MAX];
if(!isDeleting)
cout<<"Enter name of the city to be Searched: ";
else if(distanceSearch){
cout<<"Enter name of the specified location: ";
cin>>usersCity;
}
bool valid = false;
while(!valid){
cin>>usersCity;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a city name only." << endl;
valid = false;
}else{
valid = true;
}
}
// convert all the letters in the string to upper case
transform(usersCity.begin(), usersCity.end(),usersCity.begin(), ::toupper );
// now set all of the characters in the strings of the cityName array to a temporary array for comparison
for(int h = 0; h < MAX; h++){
UPPERCASENAME[h]= cityName[h];
transform(UPPERCASENAME[h].begin(), UPPERCASENAME[h].end(), UPPERCASENAME[h].begin(), ::toupper);
}
// Linear search
bool found = false;
for(int i = 0; i < MAX; i++){
if(usersCity == UPPERCASENAME[i]){
cout<<"Found the city!\nIt is at index: "<<i<<" of Data Array!"<<endl;
cout<<"coordinates are: x = "<<cityLatitude[i]<<" | y = "<<cityLongitude[i]<<endl;
return i;
}
}
if(!found){
cout<<"Could not retrieve city! Check your spelling!"<<endl;
return 55;
}
return 56;
}
int ArrayImplementation::searchByCoord(bool deleteCity){
// check which functionality the search will do
if(deleteCity){
cout<<"Enter the x-coordinate of the city you want to delete: ";
}else{
cout<<"Enter the x-coordinate of the city you want to find: ";
}
float usersCityX;
bool valid = false;
while(!valid){
cin>>usersCityX;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a float/int only." << endl;
valid = false;
}else{
valid = true;
}
}
bool xTrue = false;
for(int i = 0; i < MAX; i++){
if(fabs(cityLatitude[i] - usersCityX) < 0.0001){
xTrue = true;
cout<<"HIT!!!!"<<endl;
}
}
if(deleteCity){
cout<<"Enter the y-coordinate of the city you want to delete: ";
}else{
cout<<"Enter the y-coordinate of the city you want to find: ";
}
float usersCityY;
valid = false;
while(!valid){
cin>>usersCityY;
if(cin.fail()) //cin.fail() checks to see if the value in the cin
//stream is the correct type, if not it returns true,
//false otherwise.
{
cin.clear(); //This corrects the stream.
cin.ignore(); //This skips the left over stream data.
cout << "Please enter a float/int only." << endl;
valid = false;
}else{
valid = true;
}
}
bool yTrue = false;
int index;
for(int n = 0; n < MAX - 1; n++){
if(fabs(usersCityY - cityLongitude[n]) < 0.0001){
cout<<"HIT!!!!"<<endl;
index = n;
yTrue = true;
}