-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsorting.cpp
More file actions
150 lines (102 loc) · 2.82 KB
/
sorting.cpp
File metadata and controls
150 lines (102 loc) · 2.82 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
#include <iostream>
#include <stdio.h>
#include <string>
#include "linked_list.cpp"
#include "compare.cpp"
#include <fstream>
using std::string;
int partition(linked_list list,int low_index,int high_index){
string pivot = list.get(high_index);
int i,j;
j = low_index;
for(i=low_index;i<high_index;i++){
if(lt(pivot,list.get(i))){
list.swap(i,j);
j++;
}
}
list.swap(j,high_index);
return j;
}
void quicksort(linked_list list,int low_index= 0,int high_index= -2){
if(high_index == -2){
high_index = list.len-1;
}
if(low_index < high_index){
int i = partition(list,low_index,high_index);
quicksort(list,low_index,i-1);
quicksort(list,i+1,high_index);
}
}
linked_list read_new_adresses(linked_list list){
std::ifstream new_block;
new_block.open("new_block.csv");
if(new_block.is_open()){
string header;
std::getline(new_block,header);
int no_adresses = std::stoi(header);
int i;
string buffer;
for(i=0;i<no_adresses;i++){
std::getline(new_block,buffer);
list.append(buffer);
}
return list;
} else {
std::cerr << "Missing input file new_block.csv" << std::endl;
throw;
}
}
void store(linked_list list){
std::ifstream adress_database_old;
adress_database_old.open("adress_database.csv",std::ios::in);
if(!adress_database_old.is_open()){
//There is no file yet, so just dumping everything.
std::ofstream adress_database;
adress_database.open("adress_database.csv",std::ios::out);
int i = 0;
for (i=0;i<list.len;i++){
adress_database << list.get(i) << std::endl;
}
adress_database.close();
} else {
//There is already a file so a new file is created, that holds the updated content.
//Old File is delted afterwards.
std::ofstream adress_database_new;
adress_database_new.open("adress_database.csv.tmp",std::ios::out|std::ios::trunc);
if(adress_database_new.is_open()){
int i = 0;
string adress;
while(!adress_database_old.eof() && i<list.len){
std::getline(adress_database_old,adress);
if(list.get(i) == adress) {
i++;
adress_database_new << adress << std::endl;
} else if (lt(adress,list.get(i))){
adress_database_new << list.get(i) << std::endl;
adress_database_new << adress << std::endl;
i++;
} else {
adress_database_new << adress << std::endl;
}
}
adress_database_new.close();
adress_database_old.close();
remove("adress_database.csv");
rename("adress_database.csv.tmp","adress_database.csv");
} else {
std::cerr << "Opening of new temporary database failed." << std::endl;
}
}
}
int main() {
//Reading the file
linked_list address_list;
address_list = read_new_adresses(address_list);
//sorting the new adresses
quicksort(address_list);
std::cout << "Sorted." << std::endl;
store(address_list);
std::cout << "Stored in Database." << std::endl;
return 0;
}