Advertisement
myname0

практика_структура_20

Jul 2nd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <vector>
  5. #include <functional>
  6.  
  7. using namespace std;
  8.  
  9. ifstream in("input.txt");
  10. ofstream out("output.txt");
  11.  
  12. struct Person
  13. {
  14.         string name;
  15.         string secondname;
  16.         string surname;
  17.         string number;
  18. };
  19.  
  20. class compare: public unary_function<Person &, void>
  21. {
  22. private:
  23.     string tmp;
  24.     string temp;
  25. public:
  26.     compare(string a, string b): tmp(a), temp(b)
  27.     {
  28.     }
  29.     void operator()(Person &x)
  30.     {
  31.         if(x.number.substr(0, 2) == tmp)
  32.         x.number.replace(0, 2, temp);
  33.     }
  34. };
  35.  
  36. void print(Person &x)
  37. {
  38.     out << x.name << " " << x.secondname << " " << x.surname << " " << x.number << endl;
  39. }
  40.  
  41. int main()
  42.  
  43. {  
  44.         vector <Person> vec;
  45.         Person List;
  46.         int n;
  47.         in >> n;
  48.         for(int i = 0; i < n; i++)
  49.         {
  50.                 in >> List.name;
  51.                 in >> List.secondname;
  52.                 in >> List.surname;
  53.                 in >> List.number;
  54.                 vec.push_back(List);
  55.         }
  56.         string tmp, temp;
  57.         in >> tmp;
  58.         in >> temp;
  59.         in.close();
  60.         compare comp(tmp, temp);
  61.         for_each(vec.begin(), vec.end(),comp);
  62.         out << n << endl;
  63.         for_each(vec.begin(), vec.end(), print);       
  64.         out.close();
  65.         return 0;
  66. }
  67.  
  68. //2
  69. #include <fstream>
  70. #include <algorithm>
  71. #include <string>
  72. #include <vector>
  73. #include <functional>
  74.  
  75. using namespace std;
  76.  
  77. ifstream in("input.txt");
  78. ofstream out("output.txt");
  79.  
  80. struct Person
  81. {
  82.     string name;
  83.     string secondname;
  84.     string surname;
  85.     string number;
  86. };
  87.  
  88. void print(Person &x)
  89. {
  90.     out << x.name << " " << x.secondname << " " << x.surname << " " << x.number << endl;
  91. }
  92.  
  93. int main()
  94.  
  95. {
  96.     vector <Person> vec;
  97.     Person List;
  98.     int n;
  99.     in >> n;
  100.     for (int i = 0; i < n; i++)
  101.     {
  102.         in >> List.name;
  103.         in >> List.secondname;
  104.         in >> List.surname;
  105.         in >> List.number;
  106.         vec.push_back(List);
  107.     }
  108.     string tmp, temp;
  109.     in >> tmp;
  110.     in >> temp;
  111.     in.close();
  112.     for_each(vec.begin(), vec.end(), [tmp, temp](Person &x){if (x.number.substr(0, 2) == tmp)
  113.                                                                 x.number.replace(0, 2, temp);});
  114.     out << n << endl;
  115.     for_each(vec.begin(), vec.end(), print);
  116.     out.close();
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement