-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
125 lines (98 loc) · 3.34 KB
/
main.cpp
File metadata and controls
125 lines (98 loc) · 3.34 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
#include <fstream>
#include <string>
#include <map>
#include <sstream>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
string int128_to_string(__int128 value) {
if (value == 0) return "0";
bool negative = value < 0;
if (negative) value = -value;
string str;
while (value > 0) {
str += '0' + static_cast<char>(value % 10);
value /= 10;
}
if (negative) str += '-';
reverse(str.begin(), str.end());
return str;
}
class TradeProcessor {
private:
// 每个交易品种的统计信息(这里可以补充)
struct SymbolStats {
int64_t last_timestamp = -1; // 上次交易时间戳
int64_t max_time_gap = 0; // 最大时间间隔
__int128 total_volume = 0; // 总交易量(使用128位防止溢出)
__int128 sum_price = 0; // 总交易额(数量×价格累加)
int64_t max_price = 0; // 最高交易价格
};
map<std::string, SymbolStats> symbol_data; // 使用map以排序
public:
void process(const std::string& input_file, const std::string& output_file) {
process_input(input_file);
write_output(output_file);
}
private:
void process_input(const std::string& filename) {
ifstream file(filename);
string line;
while (std::getline(file, line)) {
process_line(line);
}
}
void process_line(const std::string& line) {
stringstream ss(line);
string part;
vector<std::string> tokens;
while (std::getline(ss, part, ',')) {
tokens.push_back(part);
}
if (tokens.size() != 4) return; // 忽略格式错误行
int64_t timestamp = stoll(tokens[0]);
string symbol = tokens[1];
__int128 quantity = static_cast<__int128>(stoll(tokens[2]));
int64_t price = stoll(tokens[3]);
SymbolStats& stats = symbol_data[symbol];
if (stats.last_timestamp != -1) {
int64_t gap = timestamp - stats.last_timestamp;
stats.max_time_gap = std::max(stats.max_time_gap, gap);
} else {
stats.max_time_gap = 0; // 首次交易时间间隔为0
}
stats.last_timestamp = timestamp;
stats.total_volume += quantity;
stats.sum_price += quantity * static_cast<__int128>(price);
stats.max_price = max(stats.max_price, price);
}
void write_output(const std::string& filename) {
std::ofstream file(filename);
for (const auto& [symbol, stats] : symbol_data) {
__int128 weighted_avg = stats.total_volume > 0
? stats.sum_price / stats.total_volume
: 0;
file << symbol << ","
<< stats.max_time_gap << ","
<< int128_to_string(stats.total_volume) << ","
<< int128_to_string(weighted_avg) << ","
<< stats.max_price << "\n";
}
}
};
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " input.csv output.csv\n";
return 1;
}
TradeProcessor processor;
processor.process(argv[1], argv[2]);
return 0;
}
/*
int main(int argc, char* argv[]) {
TradeProcessor processor;
processor.process("testdata.csv","output.csv");
return 0;
}*/