Advertisement
cyberjab

Untitled

Jun 21st, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. // Дан текс1товый файл, содержащий список фамилий. В списке
  2. //содержатся мужские и женские фамилии (признаком женской фамилии
  3. //является наличие окончания – а). Разработать программу, которая сначала
  4. //выводила мужские фамилии, отсортированные в алфавитном порядке, затем
  5. //женские, отсортированные в обратном порядке. Результаты работы
  6. //программы выводятся в файл
  7. #include "pch.h"
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <Windows.h>
  14.  
  15. using namespace std;
  16.  
  17. bool isFemale(string surname) {
  18. return (surname.back() == 'а');
  19. }
  20.  
  21. bool compare(string a, string b) {
  22. return (a < b);
  23. }
  24.  
  25. void check(ifstream &file, vector<string> &male_surnames, vector<string> &female_surnames) {
  26. string surname;
  27. while (file >> surname) {
  28. if (isFemale(surname))
  29. female_surnames.push_back(surname);
  30. else
  31. male_surnames.push_back(surname);
  32. }
  33. }
  34.  
  35. void sort_all(vector<string>& male_surnames, vector<string>& female_surnames) {
  36. sort(male_surnames.begin(), male_surnames.end(), compare);
  37. sort(female_surnames.begin(), female_surnames.end(), compare);
  38. reverse(female_surnames.begin(), female_surnames.end());
  39. }
  40.  
  41. void print_all(vector<string>& male_surnames, vector<string>& female_surnames) {
  42. cout << "Мужские фамилии:" << endl;
  43. for (string name : male_surnames)
  44. {
  45. cout << name << endl;
  46. }
  47. cout << "Женские фамилии:" << endl;
  48. for (string name : female_surnames)
  49. cout << name << endl;
  50. }
  51.  
  52. void save(vector<string>& male_surnames, vector<string>& female_surnames) {
  53. ofstream out("C:\\Users\\User128\\source\\repos\\ConsoleApplication13\\output.txt");
  54. for (string name : male_surnames)
  55. out << name << endl;
  56. for (string name : female_surnames)
  57. out << name << endl;
  58. out.close();
  59. }
  60.  
  61. vector<string> male_surnames, female_surnames;
  62.  
  63. int main() {
  64. SetConsoleCP(1251);
  65. SetConsoleOutputCP(1251);
  66.  
  67. ifstream file("C:\\Users\\User128\\source\\repos\\ConsoleApplication13\\input.txt");
  68. if (!file.is_open()) {
  69. cout << "Error opening file!" << endl;
  70. return 0;
  71. }
  72.  
  73. check(file, male_surnames, female_surnames);
  74. sort_all(male_surnames, female_surnames);
  75. print_all(male_surnames, female_surnames);
  76. save(male_surnames, female_surnames);
  77.  
  78. file.close();
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement