C/C++

Помогите решить задачу пожалуйста, в C++

Помогите пожалуйста какой нужен код для этого в C++
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <cctype>
#include <regex>
using namespace std;
bool match(const string& token) {
regex re{ R"((\d{2,3}-\d{2}-\d{2})|(\d{3}-\d{3}))" };
return regex_match(token.cbegin(), token.cend(), re);
}
void select(const string& line, vector<string>& tokens) {
auto tmp = line + '.';
string token;
for (auto sign : tmp) {
if (sign > 0 && isdigit(sign) || sign == '-') token += sign;
else if (!token.empty()) {
if (match(token)) tokens.emplace_back(token);
token.clear();
}
}
}
vector<string> load(const string& path) {
vector<string> text;
ifstream inp{ path };
if (inp.is_open()) {
string line;
while (getline(inp, line)) text.emplace_back(line);
inp.close();
}
return text;
}
void save(const string& path, const vector<string>& phones) {
ofstream out{ path };
if (out.is_open()) {
for (const auto phone : phones) out << phone << '\n';
out.close();
}
}
int main() {
auto text = load("input.txt");
vector<string> phones;
for (const auto& line : text) select(line, phones);
save("output.txt", phones);
}
Баглан Утепов
Баглан Утепов
68 209
Лучший ответ
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
bool temp1(string str)
{
if(isdigit(str[0]) && isdigit(str[1])
&& str[2] == '-' && isdigit(str[3])
&& isdigit(str[4]) && str[5] == '-'
&& isdigit(str[6]) && isdigit(str[7]))
return true;
return false;
}
bool temp2(string str)
{
if(isdigit(str[0]) && isdigit(str[1])
&& isdigit(str[2]) && str[3] == '-'
&& isdigit(str[4]) && isdigit(str[5])
&& isdigit(str[6]))
return true;
return false;
}
bool temp3(string str)
{
if(isdigit(str[0]) && isdigit(str[1])
&& isdigit(str[2]) && str[3] == '-'
&& isdigit(str[4]) && isdigit(str[5])
&& str[6] == '-' && isdigit(str[7])
&& isdigit(str[8]))
return true;
return false;
}
bool test(string str)
{
int len = str.length();
if(len >= 7)
{
if(temp1(str))
return true;
else if(temp2(str))
return true;
else if(temp3(str))
return true;
else return false;
}
else
return false;
}
int main()
{
string text,tmp, s, old;
ifstream i;
ofstream o;
i.open("input.txt");
o.open("output.txt");
if(i.is_open() && o.is_open())
{
while(!i.eof())
{
i >> tmp;
if(test(tmp))
{
if(temp1(tmp))
{
s = "";
for(int x = 0; x < 8; x++)
s += tmp[x];
}
else if(temp2(tmp))
{
s = "";
for(int x = 0; x < 7; x++)
s += tmp[x];
}
else
{
s = "";
for(int x = 0; x < 9; x++)
s += tmp[x];
}
if(s != old) o << s << endl;
old = s;
}
}
o.close();
i.close();
}
return 0;
}
регулярными выражениями нельзя? я их не шарю прадва
Павел Вяткин
Павел Вяткин
17 648
Гафиль Мамедов нет, нельзя не то будет