-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
196 lines (164 loc) · 5.13 KB
/
Copy pathmain.cpp
File metadata and controls
196 lines (164 loc) · 5.13 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
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <math.h>
#include <algorithm>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
bool cmp(const pair<string, int>& p1, const pair<string, int>& p2)
{
return p1.second > p2.second;
}
bool cmp2(const pair<string, double>& p1, const pair<string, double>& p2)
{
return p1.second > p2.second;
}
// Terminal: maxValue minValue
int main(int argc, char** argv)
{
cout << "---------- 选股小程序 by Ma Xin ----------" << endl << endl;
cout << "使用指南: *.exe(当前程序) Stock.txt maxValue(基金十大持仓中第一位持仓权重) minValue(最后一位持仓权重)" << endl;
cout << "注意:将Stock.txt与 *.exe 可执行放在同一目录下" << endl;
cout << "注意:Stock.txt有中文字符,如果这些字符由excel复制而来,则默认格式为UTF8,不符合。因此需要打开该txt文件选择另存为,选择格式“ANSI”,另存为新的Stock.txt文件。"
<< endl << endl;
if (argc != 4)
{
cout << "Error: incorrect input." << endl;
cout << "Usage: *.exe Stock.txt maxValue minValue" << endl;
return -1;
}
string filename = string(argv[1]);
// 当前路径位于.vcxproj文件的同目录
// string filename = ".\\Stock.txt";
double maxValue = stod(argv[2]);
double minValue = stod(argv[3]);
ifstream infile;
infile.open(filename);
if (!infile.is_open())
{
cerr << "Error: file open failed." << endl;
return -1;
}
string s;
getline(infile, s);
getline(infile, s);
// 第三行开始有基金信息
vector<string> jiNames;
while (!infile.eof())
{
getline(infile, s);
if (s == "")
break;
istringstream ss(s);
int a;
string name;
ss >> a >> name;
jiNames.push_back(name);
}
int num = jiNames.size();
cout << "totally find " << num << " funds." << endl;
vector<string> guNames;
while (!infile.eof())
{
getline(infile, s);
if (s == "")
continue;
guNames.push_back(s);
}
infile.close();
if (jiNames.size() != guNames.size())
cerr << "Error: different size." << endl;
map<string, vector<string>> jiAndCang;
for (int i = 0; i < jiNames.size(); i++)
{
string s = guNames[i];
istringstream ss(s);
vector<string> cang;
for (int j = 0; j < 10; j++)
{
string guname;
ss >> guname;
cang.push_back(guname);
}
string jiname = jiNames[i];
jiAndCang.insert(pair<string, vector<string>>(jiname, cang));
}
multiset<string> allGu_multi;
set<string> allGu;
for (int i = 0; i < guNames.size(); i++)
{
string s = guNames[i];
istringstream ss(s);
string name;
for (int a = 0; a < 10; a++)
{
ss >> name;
allGu.insert(name);
allGu_multi.insert(name);
}
}
cout << "totally find " << allGu.size() << " stocks." << endl;
map<string, int> guAndCount;
map<string, double> guAndScore;
for (auto it = allGu.begin(); it != allGu.end(); it++)
{
string name = *it;
int cnt = allGu_multi.count(name);
guAndCount[name] = cnt;
guAndScore[name] = 0;
}
// map不能使用sort排序,转换为vector
vector< pair<string, int>> vGuAndCount(guAndCount.begin(), guAndCount.end());
sort(vGuAndCount.begin(), vGuAndCount.end(), cmp); // 从大到小
cout << endl << "-------" << endl;
for (int i = 0; i < vGuAndCount.size(); i++)
cout << "Count: " << vGuAndCount[i].second << ", Stock name: " << vGuAndCount[i].first << endl;
// 计算各股票权值
double values[10];
for (int i = 0; i < 10; i++)
{
values[i] = maxValue - (maxValue - minValue)*i / 9;
}
for (auto it = jiAndCang.begin(); it != jiAndCang.end(); it++)
{
vector<string> vNames = it->second;
if (vNames.size() != 10)
cerr << "Error: incorrct cang size." << endl;
for (int i = 0; i < vNames.size(); i++)
{
string name = vNames[i];
double value = values[i];
guAndScore[name] += value;
}
}
// sort
vector<pair<string, double> > vGuAndScore(guAndScore.begin(), guAndScore.end());
sort(vGuAndScore.begin(), vGuAndScore.end(), cmp2);
cout << endl << "-------" << endl;
cout << fixed;
for (int i = 0; i < vGuAndScore.size(); i++)
cout << setprecision(3) << "Score: " << vGuAndScore[i].second << ", Stock name: " << vGuAndScore[i].first << endl;
// 输出文件
//string out = filename.substr(0, filename.find("\\"));
string out = "./Stock_Score.txt";
ofstream outfile(out);
if (!outfile.is_open())
cerr << "outfile open failed." << endl;
outfile << "# 股票在所有持仓中出现次数 " << endl << endl;
for (int i = 0; i < vGuAndCount.size(); i++)
outfile << "Count: " << vGuAndCount[i].second << ", Stock name: " << vGuAndCount[i].first << endl;
outfile << endl;
outfile << "# 股票在所有持仓中得分" << endl;
outfile << fixed;
for (int i = 0; i < vGuAndScore.size(); i++)
outfile << setprecision(3) << "Score: " << vGuAndScore[i].second << ", Stock name: " << vGuAndScore[i].first << endl;
outfile.close();
cout << "Stock_Score.txt has been saved in " << out << endl;
return 0;
}