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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
| #include <iostream>
#include <string>
#include <stdlib.h> //atoi))
#include <vector>
#include <fstream>
#include <windows.h>
using namespace std;
template<typename T>
void show_vec( vector<T> &vec, string name="")
{
if(name!="")cout<<name<<endl;
if(vec.size()>0)
for(size_t i=0; i<vec.size(); ++i)cout<<vec[i]<<' ';
cout<<endl;
}
template<typename T>
void show_vec( vector<pair<T,T>> &vec, string name="")
{
if(name!="")cout<<name<<endl;
if(vec.size()>0)
for(size_t i=0; i<vec.size(); ++i)cout<<vec[i].first<<' '<<vec[i].second<<endl;
cout<<endl;
}
void save_inp_tofile(string filename, vector<string> inp_vec)
{
ofstream out_file(filename.c_str(), ios::out);
vector<string>::const_iterator it = inp_vec.begin();
unsigned long long counter=1;
while(it!= inp_vec.end())
{
out_file<<(*it);
if(!(counter%5))out_file<<'\n';
//
it++;
counter++;
}
}
void test_inp_file(string filename)
{
vector<string> inp_vec;
ifstream in_file(filename.c_str(), ios::in);
string str;
while(in_file>>str) inp_vec.push_back(str);
show_vec(inp_vec, filename);
}
void split(string str, vector<string> &arr, string delim)//честно стащил из сетки (мог сам написать конечно, но и так хватило))
{
arr.clear();
size_t prev = 0;
size_t next;
size_t delta = delim.length();
while( ( next = str.find( delim, prev ) ) != string::npos ){
string tmp = str.substr( prev, next-prev );
arr.push_back( str.substr( prev, next-prev ) );
prev = next + delta;
}
string tmp = str.substr( prev );
arr.push_back( str.substr( prev ) );
}
void make_res_file
(
string filename,
string filename1,
string filename2,
vector<pair<int, int>> &res_file_positions
)
{
ofstream of_file(filename.c_str(), ios::out);
ifstream in_file1(filename1.c_str(), ios::in);
ifstream in_file2(filename2.c_str(), ios::in);
vector<pair<int, int>>::const_iterator it=res_file_positions.begin();
vector<string> strings;
int pos1(1), pos2(1);
string str_res, str1,str2;
for(;it!=res_file_positions.end(); it++)
{
int cnt(0);
pos2=1;
while(getline(in_file1, str1, '\n'))
{
if(pos1==it->first)
{
split(str1, strings, ";");
str1=strings[0]+";";
}
else
{
pos1++;
continue;
}
in_file2.seekg(0L);
pos2=1;
while(getline(in_file2, str2, '\n'))
{
if(pos2==it->second)
{
split(str2, strings, ";");
str2=strings[2];
pos2++;
pos1++;
goto hodu;
}
pos2++;
}//while
pos1++;
}//while
hodu:;//почему без кавычек не работает :D !!!
of_file<<str1<<str2<<endl;
}//for
}
void read_to_vec_pairs(string filename, vector<pair<int, pair<int, int>>> &inp_vec, bool first_unp_file=true)
{
inp_vec.clear();
vector<string> strings;
ifstream in_file(filename.c_str(), ios::in);
string str;
unsigned long counter=1;
int first,second;
while(getline(in_file, str, '\n'))
{
split(str, strings, ";");
if(first_unp_file)
{
first=atoi(strings[1].c_str());
second=atoi(strings[2].c_str());
inp_vec.push_back(make_pair(counter, make_pair(first,second)));
}
else
{
first=atoi(strings[0].c_str());
second=atoi(strings[1].c_str());
inp_vec.push_back(make_pair(counter, make_pair(first,second)));
}
counter++;
}
}
void first_inp_file_positions
(
vector<pair<int, pair<int, int>>> &inp_vec_pairs1,
vector<pair<int, pair<int, int>>> &inp_vec_pairs2,
vector<pair<int, int>> &res_file_positions
)
{
vector<pair<int, pair<int, int>>>::const_iterator
it1=inp_vec_pairs1.begin(), it2;
while(it1!=inp_vec_pairs1.end())
{
it2=inp_vec_pairs2.begin();
while(it2!=inp_vec_pairs2.end())
{
if(it1->second.first==it2->second.first && it1->second.second==it2->second.second)
res_file_positions.push_back(make_pair(it1->first,it2->first));
it2++;
}
it1++;
}
}
int main(int argc, char* argv[])
{
SetConsoleCP (1251); SetConsoleOutputCP (1251);
string
filename1="filename1",
filename2="filename2",
str_arr1[]=
{
"Андрей", ";", "123", ";", "789",
"Марина", ";", "7250", ";", "924",
"Люба", ";", "7250", ";", "924",
"Светлана", ";", "7251", ";", "924",
"Игорь", ";", "756758",";", "7538"
},
str_arr2[]=
{
"123", ";", "789", ";", "AAA",
"7250", ";", "924", ";", "CCC",
"756758",";", "7538" ";", "IGP"
};
vector<string>
inp_vec1(str_arr1, str_arr1+sizeof(str_arr1)/sizeof(str_arr1[0])),
inp_vec2(str_arr2, str_arr2+sizeof(str_arr2)/sizeof(str_arr2[0]));
save_inp_tofile(filename1,inp_vec1);
save_inp_tofile(filename2,inp_vec2);
test_inp_file(filename1);
test_inp_file(filename2);
cout<<endl;
vector<pair<int, pair<int, int>>> inp_vec_pairs1,inp_vec_pairs2;
read_to_vec_pairs(filename1, inp_vec_pairs1);
cout<<endl;
read_to_vec_pairs(filename2, inp_vec_pairs2, false);
vector<pair<int, int>> res_file_positions;
first_inp_file_positions(inp_vec_pairs1, inp_vec_pairs2, res_file_positions);
show_vec(res_file_positions,"res_file_positions");
string filename3="resulting_file";
make_res_file(filename3,filename1,filename2,res_file_positions);
test_inp_file(filename3);
cout<<endl;
system("pause");
return 0;
} |