-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.cpp
More file actions
122 lines (105 loc) · 3.84 KB
/
Copy pathtest2.cpp
File metadata and controls
122 lines (105 loc) · 3.84 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include "stone_pair2.hpp"
void displayUsage(const std::string& programName) {
std::cerr << "Usage: " << programName << " <TXT file>" << std::endl;
}
std::vector<double> split(const std::string& str, char delimiter) {
std::vector<double> tokens;
size_t pos = 0;
size_t found = 0;
while ((found = str.find(delimiter, pos)) != std::string::npos) {
tokens.push_back(std::stod(str.substr(pos,found - pos)));
pos = found + 1;
}
tokens.push_back(std::stod(str.substr(pos)));
return tokens;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
displayUsage(argv[0]);
return 1;
}
const std::string fileName = argv[1];
std::ifstream file(fileName);
if (!file.is_open()) {
std::cerr << "Unable to open TXT file: " << fileName << std::endl;
return 1;
}
std::vector<double> data;
std::string line;
unsigned int count = 0;
unsigned int success = 0;
while (std::getline(file, line)) {
count++;
if (line[0] != '1') {
continue;
}
std::vector<double> data = split(line, ',');
double D = data[1];
int pair_num = data[2];
int possible_res_count = data[3];
std::vector<std::set<std::pair<int, int>>> cadidate;
for (int k = 0; k < possible_res_count; k++) {
std::set<std::pair<int, int>> tmp;
for (int i = 4 + k * pair_num * 2; i < pair_num * (k+1) * 2 + 4; i += 2) {
tmp.insert(std::make_pair(data[i], data[i + 1]));
tmp.insert(std::make_pair(data[i + 1], data[i]));
}
cadidate.push_back(tmp);
}
// std::cout << "==============" << std::endl;
// for (auto& item: cadidate) {
// for (auto& pair : item) {
// std::cout << pair.first << "," << pair.second << ";";
// }
// std::cout << std::endl;
// }
std::vector<double> stone_weight(data.begin() + pair_num * possible_res_count * 2 + 4, data.end());
std::vector<std::pair<int, int>> stone_pair = find_all_stone_pairs(stone_weight, D); // Find the stone pair
bool succ = false;
std::vector<std::pair<int, int>> err_pairs;
if (stone_pair.size() == pair_num && pair_num == 0) {
std::cout << count << " OK" << std::endl;
success++;
succ = true;
} else if (stone_pair.size() != pair_num) {
std::cerr << count << " ERROR :" << pair_num << " pairs are expected, but " << stone_pair.size() << " pairs are found." << std::endl;
} else {
for(auto& pairs : cadidate) {
for (auto& item : stone_pair) {
if (pairs.find(item) != pairs.end()) {
pair_num--;
pairs.erase(item);
} else {
err_pairs.push_back(item);
}
}
if (pair_num == 0) {
std::cout << count << " OK" << std::endl;
success++;
succ = true;
break;
}
}
}
if (!succ) {
std::cerr << count << " ERROR :"
<< "the pair [";
for (auto& item : err_pairs) {
std::cout << "(" << item.first << "," << item.second << ");";
}
std::cout << "] is not in the candidate." << std::endl;
}
data.clear();
}
std::cout << "Total cases: " << count << std::endl;
std::cout << "Success cases: " << success << std::endl;
std::cout << "error cases: " << count - success << std::endl;
file.close();
return 0;
}