-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
76 lines (62 loc) · 2.2 KB
/
test.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "stone_pair.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];
std::pair<int, int> except(data[2], data[3]);
std::vector<double> stone_weight(data.begin() + 4, data.end());
std::pair<int, int> stone_pair = find_stone_pair(stone_weight, D); // Find the stone pair
if ((stone_pair.first == except.first && stone_pair.second == except.second)
|| stone_pair.first == except.second && stone_pair.second == except.first) {
std::cout << count << " OK" << std::endl;
success++;
} else {
std::cerr << count << " ERROR :"
<< "the expeted pair (" << except.first << "," << except.second << ") is not the same as ("
<< stone_pair.first << "," << stone_pair.second << ")." << 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;
}