
C/C++
Програмирование на языках С/С+=
Решить задачу более легким методом


#include <algorithm>
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
struct Note {
string name;
string phone;
short birthday[3];
bool matching(const string& surname)const {
return name.find(surname) != string::npos;
}
friend bool operator<(const Note& a, const Note& b) {
return string(a.phone.begin(), a.phone.begin() + 3)
< string(b.phone.begin(), b.phone.begin() + 3);
}
friend istream& operator>>(istream& inp, Note& n) {
cout << "Фамилия, имя: ";
getline(inp, n.name);
cout << "Номер телефона: ";
getline(inp, n.phone);
cout << "Дата рождения (д м г): ";
for (auto i = 0U; i < size(n.birthday); ++i) {
inp >> n.birthday[i];
}
inp.ignore(inp.rdbuf()->in_avail());
return inp;
}
friend ostream& operator<<(ostream& out, const Note& n) {
out << setw(24) << left << n.name << ' '
<< setw(12) << n.phone << ' ';
if (n.birthday[0] < 10) out.put('0');
out << n.birthday[0] << '.';
if (n.birthday[1] < 10) out.put('0');
out << n.birthday[1] << '.' << n.birthday[2];
return out;
}
};
int main() {
system("chcp 1251 > nul");
array<Note, 2> phonebook;
for (auto& record : phonebook) cin >> record;
sort(phonebook.begin(), phonebook.end());
system("cls");
int n = 0;
for (const auto& record : phonebook) {
cout << ++n << ". " << record << '\n';
}
cout << "\nФамилия для поиска: ";
string surname;
cin >> surname;
puts("");
n = 0;
for (const auto& record : phonebook) {
if (record.matching(surname)) {
cout << ++n << ". " << record << '\n';
}
}
if (!n) puts("Совпадения не обнаружены!");
system("pause > nul");
}
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
struct Note {
string name;
string phone;
short birthday[3];
bool matching(const string& surname)const {
return name.find(surname) != string::npos;
}
friend bool operator<(const Note& a, const Note& b) {
return string(a.phone.begin(), a.phone.begin() + 3)
< string(b.phone.begin(), b.phone.begin() + 3);
}
friend istream& operator>>(istream& inp, Note& n) {
cout << "Фамилия, имя: ";
getline(inp, n.name);
cout << "Номер телефона: ";
getline(inp, n.phone);
cout << "Дата рождения (д м г): ";
for (auto i = 0U; i < size(n.birthday); ++i) {
inp >> n.birthday[i];
}
inp.ignore(inp.rdbuf()->in_avail());
return inp;
}
friend ostream& operator<<(ostream& out, const Note& n) {
out << setw(24) << left << n.name << ' '
<< setw(12) << n.phone << ' ';
if (n.birthday[0] < 10) out.put('0');
out << n.birthday[0] << '.';
if (n.birthday[1] < 10) out.put('0');
out << n.birthday[1] << '.' << n.birthday[2];
return out;
}
};
int main() {
system("chcp 1251 > nul");
array<Note, 2> phonebook;
for (auto& record : phonebook) cin >> record;
sort(phonebook.begin(), phonebook.end());
system("cls");
int n = 0;
for (const auto& record : phonebook) {
cout << ++n << ". " << record << '\n';
}
cout << "\nФамилия для поиска: ";
string surname;
cin >> surname;
puts("");
n = 0;
for (const auto& record : phonebook) {
if (record.matching(surname)) {
cout << ++n << ". " << record << '\n';
}
}
if (!n) puts("Совпадения не обнаружены!");
system("pause > nul");
}
Ванёк Александров
Cпасибо огромное
Похожие вопросы
- Можно ли создать игру на юнити имея 0 знаний в програмировании?
- Програмирование с++ для начинающих
- ПРОГРАМИРОВАНИЕ НА С++
- Системное програмирование Помогите, пожалуйста
- Програмирование на си
- Програмирование с++,классы, дружественная функция
- Програмирования С ++ Программирование с использованием ассоциативных контейнеров STL
- Програмирование на C/C++ на основе блоксхемы
- Задачи по програмированию
- Програмирование на C