-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.cpp
More file actions
184 lines (160 loc) · 4.45 KB
/
p1.cpp
File metadata and controls
184 lines (160 loc) · 4.45 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
int removeDuplicates(string arr[], int n)
{
//int j = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (arr[i] == arr[j] || arr[i] == "")
{
n--;
for (int k = i; k < n; k++)
{
arr[k] = arr[k + 1];
}
i--;
}
}
}
return n;
}
int main(int argc, char *argv[])
{
string source[3000];
string destination[3000];
int bandwith[3000];
int final[2000];
string temp[30000];
string origsource[3000];
string origdestination[3000];
ifstream inFS;
int done = 0;
string line;
int row = 0;
int index = 0;
int col = 0;
string total[20000];
int num = 0;
int leaving = 0;
int to = 0;
inFS.open(argv[1]);
while (!inFS.eof())
{
getline(inFS, line);
stringstream ss(line);
ss >> source[row] >> destination[row] >> bandwith[row]; //read in values and put in three diff arrays
total[col] = source[row];
col++;
total[col] = destination[row];
col++;
row++;
index++;
ss.clear();
}
inFS.close();
for (int i = 0; i < col; i++)
{
for (int j = 0; j < total[i].length(); j++)
{
total[i][j] = toupper(total[i][j]);
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < source[i].length(); j++)
{
source[i][j] = toupper(source[i][j]);
} // convert source to uppercase
origsource[i] = source[i];
for (int j = 0; j < destination[i].length(); j++)
{ // keep copy of original source and destination (with repeats)
destination[i][j] = toupper(destination[i][j]);
}
origdestination[i] = destination[i];
}
int origColumn = col;
int origRow = row;
int origIndex = index;
col = removeDuplicates(total, col); // remove duplicates
row = removeDuplicates(source, row);
index = removeDuplicates(destination, index);
int size = 0;
if (row > col)
{ // find which list has more and use
size = row;
}
else if (col >= row)
{
size = row;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < index; j++)
{
if (source[i] == destination[j])
{ //find all non-leaf identifiers
temp[num] = source[i];
//cout << temp[num] << endl;
num++;
}
}
}
int order = 0;
string ordered[10000];
num = removeDuplicates(temp, num);
for (int i = 0; i < col; i++)
{
for (int j = 0; j < num; j++)
{ //order 2nd half to first
if (total[i] == temp[j])
{
//cout << total[i];
ordered[order] = total[i];
//cout << total[i] << endl;
order++;
}
}
}
ofstream outPut;
outPut.open("output.txt");
for (int i = 0; i < col; i++)
{
outPut << total[i] << endl; //print out top half
}
outPut << endl;
for (int i = 0; i <= order; i++)
{
if (ordered[i] == "")
{
break;
}
for (int j = 0; j < origRow; j++)
{
if (ordered[i] == origsource[j])
{ // FIX ME, adding up to/from values, copying to array. Need to copy outside of for loop, but not working?
leaving += bandwith[j]; // need reset before i increments
}
if (ordered[i] == origdestination[j])
{
to += bandwith[j];
// cout << "&" << to;
}
}
final[done] = leaving;
done++;
outPut << ordered[i] << " " << leaving << " " << to << " " << fixed << setprecision(2) << (double(to) / leaving) << endl;
to = 0;
leaving = 0;
}
//for(int i=0; i<order; i++) { // print out bottom (REMEMBER PRINT TO FILE NOT SCREEN)
// cout << ordered[i] << " " << final[i]<< endl;
//}
return 0;
}