x2311

Untitled

Jun 5th, 2022
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <sstream>
  5. #include <string>
  6.  
  7. /** Файл містить дані: прізвище, адреса, телефон. Відсортувати прізвищами в
  8. алфавітному порядку, використавши список, а при наявності однакових прізвищ
  9. відсортувати адреси теж в алфавітному порядку. Здійснити пошук абонента за
  10. номером телефону..**/
  11.  
  12. using namespace std;
  13. struct UserInformation {
  14.     string name;
  15.     string address;
  16.     int phone;
  17. };
  18.  
  19. void conclusion(UserInformation &information);
  20.  
  21. int main() {
  22.     //database
  23.     vector<UserInformation> base;
  24.     UserInformation temporaryStructure;
  25.     //import file
  26.     ifstream file;
  27.     file.open("..\\tess.txt");
  28.     //File open check
  29.     if (!file.is_open()) {
  30.         cout << "ERROR\n\n";
  31.         return 1;
  32.     }
  33.     //read line by line and write to database
  34.     string line;
  35.  
  36.     while (getline(file, line)) {
  37.         stringstream word(line);
  38.         word >> temporaryStructure.name;
  39.         word >> temporaryStructure.address;
  40.         word >> temporaryStructure.phone;
  41.         base.push_back(temporaryStructure);
  42.     }
  43.  
  44.     for (int g = 0; g < base.size() - 1; ++g) {
  45.         for (int i = 0; i < base.size() - 1; ++i) {
  46.             for (int j = 0; j < base[i].name.length(); ++j) {
  47.                 cout << int(base[i].name[j]) << " > " << int(base[i + 1].name[j]) << endl;
  48.                 if (base[i].name[j] > base[i + 1].name[j]) {
  49.                     cout << "[1]" << endl; //?????
  50.                     swap(base[i], base[i + 1]);
  51.                     break;
  52.                 } else if (base[i].name[j] < base[i + 1].name[j]) {
  53.                     break;
  54.                 }
  55.             }
  56.         }
  57.     }
  58.  
  59.     for (int g = 0; g < base.size() - 1; ++g) {
  60.         for (int i = 0; i < base.size() - 1; ++i) {
  61.             if (base[i].name == base[i + 1].name) {
  62.                 for (int j = 0; j < base[i].address.length(); ++j) {
  63.                     cout << int(base[i].address[j]) << " > " << int(base[i + 1].address[j]) << endl;
  64.                     if (base[i].address[j] > base[i + 1].address[j]) {
  65.                         cout << "[1]" << endl; //?????
  66.                         swap(base[i], base[i + 1]);
  67.                         break;
  68.                     } else if (base[i].address[j] < base[i + 1].address[j]) {
  69.                         break;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75.  
  76.  
  77.     for (int i = 0; i < base.size(); ++i) {
  78.         conclusion(base[i]);
  79.     }
  80. }
  81.  
  82. void conclusion(UserInformation &information) {
  83.     cout << information.name << " "
  84.          << information.address << " "
  85.          << information.phone << " "
  86.          << endl;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment