-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcattle_reports.cpp
More file actions
91 lines (79 loc) · 2.56 KB
/
cattle_reports.cpp
File metadata and controls
91 lines (79 loc) · 2.56 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
//NAME: Ignacio Jimenez
//RED ID: 826019175
#include "cattle_tree.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main(int argc, char **argv){
if(argc != 3){
cout<< "Wrong number of arguments" <<endl;
return 1;
}
//Open skuchart
ifstream skuchart(argv[1]);
if (!skuchart) {
//If file does not exist ->
cout << "Unable to open <<" << argv[1] << ">>" << endl;
return 1;
}
//Open testfile
ifstream testfile(argv[2]);
//If file does not exist ->
if (!testfile) {
cout << "Unable to open <<" << argv[2] << ">>" << endl;
return -1;
}
//Reading internal level from 1st line
int internalLevels = 0;
string line = ""; //
getline(skuchart, line);
internalLevels = stoi(line.substr(line.find('=') + 1));//Reading number that goes after '=' then to integer
//Read max children per level from 2nd line
getline(skuchart, line);
stringstream ss(line); // Use stringstream for easy parsing
vector<int> maxChildrenPerLevel(internalLevels);
for(int i = 0; i < internalLevels; i++){
ss >> maxChildrenPerLevel[i];
}
//Create root node for our first CattlePath
CattleNode root("s_", 0, maxChildrenPerLevel);
//ReadPaths and Insert them with annotations
while(getline(skuchart, line) && !line.empty()){
// Check if line has annotation (contains [ and ])
size_t startBracket = line.find('[');
size_t endBracket = line.find(']');
string cattlePath = line;
string annotation = "";
if (startBracket != string::npos && endBracket != string::npos && endBracket > startBracket) {
// Extract cattle path (everything before [)
cattlePath = line.substr(0, startBracket);
// Remove trailing spaces
while (!cattlePath.empty() && cattlePath.back() == ' ') {
cattlePath.pop_back();
}
// Extract annotation (everything between [ and ])
annotation = line.substr(startBracket + 1, endBracket - startBracket - 1);
}
if (root.addCattle(cattlePath.c_str())) {
// Add annotation if present
if (!annotation.empty()) {
root.setAnnotation(cattlePath.c_str(), annotation.c_str());
}
}
}
//Count Cattle and display annotations
while(getline(testfile, line)){
int count = root.findCattle(line.c_str());
string annotation = root.getAnnotation(line.c_str());
cout << line << " " << count;
if (!annotation.empty()) {
cout << " [" << annotation << "]";
}
cout << "\n";
}
skuchart.close();
testfile.close();
return 0;
}