Задан текстовый файл input.txt. Требуется определить строки этого
файла, содержащие максимальную по длине подстроку, состоящую только из
цифр. Если таких строк несколько, найти первые 10. Результат вывести на
консоль в форме, удобной для чтения.
C/C++
Программирование C++. Работа с файлами.
#include <algorithm>
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <fstream>
using namespace std;
class DS {
public:
DS(const string& digits) : digits(digits) {}
size_t length() const {
return digits.length();
}
private:
string digits;
friend bool operator<(const DS& a, const DS& b) {
return a.digits.length() < b.digits.length();
}
};
class BS {
public:
vector<pair<DS, string>> get()const {
return bs;
}
void add(const string& line) {
auto str = line;
vector<DS> box;
smatch sm;
const regex re(R"(\d+)");
while (regex_search(str, sm, re)) {
box.emplace_back(DS{ sm.str() });
str = sm.suffix();
}
auto max = *max_element(box.begin(), box.end());
bs.push_back({ max, line });
}
private:
vector<pair<DS, string>> bs;
};
class Source {
public:
bool load(const string& path) {
ifstream inp{ path };
if (!inp.is_open()) return false;
string line;
while (getline(inp, line)) bs.add(line);
inp.close();
return true;
}
vector<string> get(const unsigned ml)const {
vector<string> box;
auto ds = bs.get();
auto& [max, str] = *max_element(ds.begin(), ds.end());
auto i = 0U;
for (const auto& [key, value] : ds) {
if (key.length() == max.length()) {
box.push_back(value);
if (++i == ml) break;
}
}
return box;
}
private:
BS bs;
};
int main() {
Source src;
if (src.load("input.txt")) {
auto lines = src.get(10);
for (const auto& line : lines) cout << line << '\n';
} else {
puts("File input.txt not found!");
}
}
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <fstream>
using namespace std;
class DS {
public:
DS(const string& digits) : digits(digits) {}
size_t length() const {
return digits.length();
}
private:
string digits;
friend bool operator<(const DS& a, const DS& b) {
return a.digits.length() < b.digits.length();
}
};
class BS {
public:
vector<pair<DS, string>> get()const {
return bs;
}
void add(const string& line) {
auto str = line;
vector<DS> box;
smatch sm;
const regex re(R"(\d+)");
while (regex_search(str, sm, re)) {
box.emplace_back(DS{ sm.str() });
str = sm.suffix();
}
auto max = *max_element(box.begin(), box.end());
bs.push_back({ max, line });
}
private:
vector<pair<DS, string>> bs;
};
class Source {
public:
bool load(const string& path) {
ifstream inp{ path };
if (!inp.is_open()) return false;
string line;
while (getline(inp, line)) bs.add(line);
inp.close();
return true;
}
vector<string> get(const unsigned ml)const {
vector<string> box;
auto ds = bs.get();
auto& [max, str] = *max_element(ds.begin(), ds.end());
auto i = 0U;
for (const auto& [key, value] : ds) {
if (key.length() == max.length()) {
box.push_back(value);
if (++i == ml) break;
}
}
return box;
}
private:
BS bs;
};
int main() {
Source src;
if (src.load("input.txt")) {
auto lines = src.get(10);
for (const auto& line : lines) cout << line << '\n';
} else {
puts("File input.txt not found!");
}
}
Похожие вопросы
- C++ Работа с файлами.
- Помогите решить задачку по теме работа с файлами C++
- Задача по программированию C++
- Программирование c++ программирование и работа со структурами
- Программирование C++ ПРОШУ ПОМОЧЬ!
- Что такое #include <iostream>, std using namespace std В языке программирования C++?
- Контрольная по алгоритмизации и программированию! C++
- Язык программирования c++
- Нормальный ли язык программирования C для новичка ,что бы в дальнейшем перейти на C# ,C++,И в чем различие между ними?
- Задача по работе со строками на языке программировании c++