Advertisement
AdrianMadajewski

Untitled

Jan 4th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <vector>
  4. #include <map>
  5. #include <cmath>
  6.  
  7. int main()
  8. {
  9.     // Typy ifstream oraz ofstream nie potrzebuja zamykania
  10.     // Recznego, poniewaz maja wlasny destruktor
  11.     // Przy skonczeniu programu
  12.  
  13.     std::ifstream input("sygnaly.txt");
  14.     std::ofstream output("wyniki4.txt");
  15.     std::vector<std::string> dane;
  16.  
  17.     std::string temp;
  18.  
  19.     while (input >> temp)
  20.     {
  21.         dane.emplace_back(temp);
  22.     }
  23.  
  24.     // Zadanie 4.1
  25.     std::string przeslanie;
  26.     for (int i = 39; i < dane.size(); i += 40)
  27.         przeslanie += dane[i][9];
  28.  
  29.     output << "Zadanie 4.1:\n" << przeslanie << std::endl;
  30.  
  31.     // Zadanie 4.2
  32.     std::vector<std::map<char, int>> liczniki;
  33.  
  34.     for (int i = 0; i < dane.size(); i++)
  35.     {
  36.         // Dodanie pustej mapy do licznika
  37.         liczniki.emplace_back();
  38.  
  39.         for (int j = 0; j < dane[i].size(); j++)
  40.         {
  41.             // Najpierw odwolanie do licznika tablicy - licznik[i]
  42.             // Potem odwolanie do danego napisu - dane[i]
  43.             // A nastepnie do kazdej litery - dane[i][j]
  44.             liczniki[i][dane[i][j]]++;
  45.         }
  46.     }
  47.  
  48.     // Szukanie wartosci maksymalnej
  49.     int max = 0;
  50.     int index = -1;
  51.  
  52.     for (int i = 0; i < liczniki.size(); i++)
  53.     {
  54.         if (liczniki[i].size() > max)
  55.         {
  56.             max = liczniki[i].size();
  57.             index = i;
  58.         }
  59.     }
  60.  
  61.     output << "Zadanie 4.2:\n" << dane[index] << " oraz liczba roznych znakow: " << max << std::endl;
  62.  
  63.     // Zadanie 4.3
  64.     output << "Zadanie 4.3:\n";
  65.  
  66.     for (auto& wyraz : dane)
  67.     {
  68.         bool czySpelniaWarunek = true;
  69.  
  70.         for (auto &litera1 : wyraz)
  71.         {
  72.             for (auto &litera2 : wyraz)
  73.             {
  74.                 if (abs(litera1 - litera2) > 10)
  75.                 {
  76.                     czySpelniaWarunek = false;
  77.                     break;
  78.                 }
  79.             }
  80.  
  81.             if (!czySpelniaWarunek)
  82.             {
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         if (czySpelniaWarunek) output << wyraz << std::endl;
  88.     }
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement