Piratux

Untitled

Dec 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <ctype.h>
  6.  
  7. using namespace std;
  8.  
  9. struct list {
  10.     string orgName;
  11.     vector<string> chName;
  12.     vector<int> chNum;
  13. };
  14.  
  15. void Read(vector<list>& l)
  16. {
  17.     int counter = 10;
  18.     bool readingVar;
  19.     string line, varName = "";
  20.     ifstream fin("input.txt");
  21.     while (true)
  22.     {
  23.         counter++;
  24.         readingVar = false;
  25.         getline(fin, line);
  26.         for (auto x : line)
  27.         {
  28.             if (x == ']')
  29.                 return;
  30.             if (x == '{')
  31.             {
  32.                 l.push_back(list());
  33.                 counter = 0;
  34.                 break;
  35.             }
  36.             if (isalpha(x) || isdigit(x))
  37.             {
  38.                 if(readingVar)
  39.                     varName += x;
  40.             }
  41.             else
  42.             {
  43.                 if(x == ':')
  44.                     readingVar = true;
  45.             }
  46.         }
  47.         switch (counter)
  48.         {
  49.         case 1:
  50.             l[l.size() - 1].orgName = varName;
  51.             break;
  52.         case 2:
  53.             l[l.size() - 1].chName.push_back(varName);
  54.             break;
  55.         case 3:
  56.             l[l.size() - 1].chNum.push_back(stoi(varName));
  57.             break;
  58.         default:
  59.             break;
  60.         }
  61.         varName = "";
  62.     }
  63. }
  64. void createList(vector<list>& l)
  65. {
  66.     for (int i = 0; i < l.size(); i++)
  67.     {
  68.         for (int j = 0; j < i; j++)
  69.         {
  70.             if (l[j].orgName == l[i].orgName)
  71.             {
  72.                 l[j].chName.push_back(l[i].chName[0]);
  73.                 l[j].chNum.push_back(l[i].chNum[0]);
  74.                 l.erase(l.begin() + i);
  75.             }
  76.         }
  77.     }
  78. }
  79. void Output(vector<list>& l)
  80. {
  81.     ofstream fout("output.txt");
  82.     char c = '"';
  83.     fout << "[" << endl;
  84.     for (int i = 0; i < l.size(); i++)
  85.     {
  86.         fout << "{ " << c << l[i].orgName << c << " : [";
  87.         for (int j = 0; j < l[i].chName.size(); j++)
  88.         {
  89.             fout << "{";
  90.             fout << c << l[i].chName[j] << c << " : ";
  91.             fout << c << l[i].chNum[j] << c;
  92.             fout << "}";
  93.             if (j + 1 != l[i].chName.size())
  94.                 fout << ", ";
  95.         }
  96.         fout << "]}";
  97.         if (i + 1 != l.size())
  98.             fout << ",";
  99.         fout << endl;
  100.     }
  101.     fout << "]";
  102. }
  103. int main()
  104. {
  105.     vector<list> l;
  106.     Read(l);
  107.     createList(l);
  108.     Output(l);
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment