Заполнить структуру данными из файла, данные вида
"Имя Фамилия 10.10.2020 89"
Дату рождения отдельно сохранить как массив 3 чисел
89 - оклад
Другие языки программирования и технологии
С++ работа с файлами, как достать отдельно слова из файла
#include <iostream>
#include <string>
#include <sstream>
#include <tuple>
#include <regex>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct Box {
Box() : day(0), month(0), year(0), salary(.0) {}
int day;
int month;
int year;
double salary;
string name;
string surname;
string tostring()const {
auto rub = static_cast<unsigned>(salary);
auto kop = static_cast<unsigned>(round((salary - rub) * 100));
auto str = surname + ' ' + name + " - " + to_string(day) + pos[month] + to_string(year) + " г. р. = " + to_string(rub) + '.';
if (kop < 10) str += '0';
str += to_string(kop) + " руб.";
return str;
}
friend ostream& operator<<(ostream& out, const Box& box) {
out << box.tostring();
return out;
}
friend bool operator<(const Box& a, const Box& b) {
if (a.surname < b.surname) return true;
if (a.surname == b.surname) {
if (a.name < b.name) return true;
if (a.name == b.name) {
if (a.year < b.year) return true;
if (a.year == b.year) {
if (a.month < b.month) return true;
if (a.month == b.month) return a.day <= b.day;
}
}
}
return false;
}
static string pos[13];
};
string Box::pos[13] = { "", " января ", " февраля ", " марта ", " апреля ", " мая ", " июня ", " июля ", " августа ", " сентября ", " октября ", " ноября ", " декабря " };
tuple<int, int, int> split(const string& date) {
auto tmp = date;
tmp = regex_replace(tmp, regex("[.-/]"), " ");
istringstream iss(tmp);
int d, m, y;
iss >> d >> m >> y;
return { d, m, y };
}
Box parse(const string& record) {
Box box;
string date;
istringstream iss(record);
iss >> box.name >> box.surname >> date >> box.salary;
auto [day, month, year] = split(date);
box.day = day;
box.month = month;
box.year = year;
return box;
}
int main() {
// Для файла данных в кодировке Windows 1251
system("chcp 1251 > nul");
// Сохранить строки данных из файла в вектор
vector<string> records{
"Константин Цзю 19.09.1969 340680.99",
"Фёдор Емельяненко 28.09.1976 750000.10",
"Хабиб Нурмагомедов 20.09.1988 960000.09",
};
vector<Box> table;
for (const auto& record : records) table.push_back(parse(record));
sort(table.begin(), table.end());
copy(table.begin(), table.end(), ostream_iterator<Box>(cout, "\n"));
cin.get();
}
#include <string>
#include <sstream>
#include <tuple>
#include <regex>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
struct Box {
Box() : day(0), month(0), year(0), salary(.0) {}
int day;
int month;
int year;
double salary;
string name;
string surname;
string tostring()const {
auto rub = static_cast<unsigned>(salary);
auto kop = static_cast<unsigned>(round((salary - rub) * 100));
auto str = surname + ' ' + name + " - " + to_string(day) + pos[month] + to_string(year) + " г. р. = " + to_string(rub) + '.';
if (kop < 10) str += '0';
str += to_string(kop) + " руб.";
return str;
}
friend ostream& operator<<(ostream& out, const Box& box) {
out << box.tostring();
return out;
}
friend bool operator<(const Box& a, const Box& b) {
if (a.surname < b.surname) return true;
if (a.surname == b.surname) {
if (a.name < b.name) return true;
if (a.name == b.name) {
if (a.year < b.year) return true;
if (a.year == b.year) {
if (a.month < b.month) return true;
if (a.month == b.month) return a.day <= b.day;
}
}
}
return false;
}
static string pos[13];
};
string Box::pos[13] = { "", " января ", " февраля ", " марта ", " апреля ", " мая ", " июня ", " июля ", " августа ", " сентября ", " октября ", " ноября ", " декабря " };
tuple<int, int, int> split(const string& date) {
auto tmp = date;
tmp = regex_replace(tmp, regex("[.-/]"), " ");
istringstream iss(tmp);
int d, m, y;
iss >> d >> m >> y;
return { d, m, y };
}
Box parse(const string& record) {
Box box;
string date;
istringstream iss(record);
iss >> box.name >> box.surname >> date >> box.salary;
auto [day, month, year] = split(date);
box.day = day;
box.month = month;
box.year = year;
return box;
}
int main() {
// Для файла данных в кодировке Windows 1251
system("chcp 1251 > nul");
// Сохранить строки данных из файла в вектор
vector<string> records{
"Константин Цзю 19.09.1969 340680.99",
"Фёдор Емельяненко 28.09.1976 750000.10",
"Хабиб Нурмагомедов 20.09.1988 960000.09",
};
vector<Box> table;
for (const auto& record : records) table.push_back(parse(record));
sort(table.begin(), table.end());
copy(table.begin(), table.end(), ostream_iterator<Box>(cout, "\n"));
cin.get();
}
Отдельно? Если это не файл структуры базы данных, то только считывая весь файл или известными порциями...
Похожие вопросы
- Delphi, работа с файлами
- С/C++ работа с файлами, удаление и вывод на экран заранее не известных файлов
- Программирование С++(работа с файлами) Подробное описание
- Работа с файлами. PascalABC.
- Работа с файлами в АССЕМБЛЕРЕ. Надо считывать файл, путь указывает пользователь при запуске программу. Помогите!
- Помогите пожалуйста. Язык Си работа с файлами
- Примитивная работа с файлами. PASCAL.
- С++ ПРОГРАММИРОВАНИЕ. работа с файлами
- Программирование на C++.Работа с файлами.
- Создать список из повторяющихся слов текста из файла. Первый элемент-наиб. часто повторяющееся слово/ Паскаль. (+)