Advertisement
Queen4

Parsint text file

Jan 19th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <unordered_set>
  5. #include <array>
  6. #include <map>
  7.  
  8. // Переменные для поиска времени
  9. struct MainTime {
  10.     int hour = 0;
  11.     int nexthour = 0;
  12.     int minute = 0;
  13.     int nextminute = 0;
  14.     float sec = 0.0;
  15.     float nextsec = 0.0;
  16.     float sum = 0.0;
  17.     float SNRs = 0.0;
  18. };
  19.  
  20. // Cкладывает по принципу XOR
  21. int XOR(const char* s) {
  22.     int c = 0;
  23.  
  24.     while (*s)
  25.         c ^= *s++;
  26.  
  27.     return c;
  28. }
  29.  
  30. /*class Avaragetime {
  31. public:
  32.     void calculate_avarage_time() {
  33.         int sum = 0;
  34.         for (int i = 0; i < count; ++i) {
  35.             sum +=
  36.         }
  37.     }
  38. private:
  39.     int count;
  40. };*/
  41.  
  42.  
  43. int main() {
  44.     int comma, key; // Переменная для подсчета запятых и проверки контрольной суммы
  45.     MainTime time; //Структура времени
  46.     // Переменные для работы со строками
  47.     char linec_h[200];
  48.     std::string line, key_s;
  49.     // Работа с ведром номеров спутника
  50.     std::unordered_set<std::string> Set; //сет номеров
  51.     std::string SatelliteNumber;
  52.     // Работа со среднем значением времени к каждому SNR
  53.     int SNRnumber;
  54.     int count = 1;
  55.     //std::array<float, 300> SNRarray;
  56.     std::map <int, std::pair<float, int>> Map;
  57.  
  58.     std::ifstream logs_("C:/Users/Ivan/Desktop/Logs.txt"); // Откуда берем данные
  59.     std::ofstream pout("C:/Users/Ivan/Desktop/SNR.txt"); // Куда загружаем данные
  60.     std::ofstream tout("C:/Users/Ivan/Desktop/Avarage Time.txt"); // Куда загружаем данные о среднем времени
  61.  
  62.     if (logs_.is_open()) {
  63.         while (getline(logs_, line)) {
  64.  
  65.             if (line.substr(0, 10) == "RE005%off%") {
  66.                 pout << "End of cycle" << std::endl;
  67.             }
  68.             else if (line.substr(0, 9) == "RE004%on%") { // Если строка с включением, то
  69.                 pout << "Time of work: " << time.nexthour + time.nextminute + time.nextsec << " sec" << std::endl; // Выводим время работы цикла
  70.                 // Обнуляем переменные времени
  71.                 time.hour = 0;
  72.                 time.minute = 0;
  73.                 time.sec = 0.0;
  74.                 time.nexthour = 0;
  75.                 time.nextminute = 0;
  76.                 time.nextsec = 0.0;
  77.                 // Очищаем сет
  78.                 Set.clear();
  79.             }
  80.             else {
  81.                 //Проверяем контрольную сумму
  82.                 key_s = line.substr(line.length() - 2, 2);
  83.                 key = strtol(key_s.c_str(), nullptr, 16);
  84.                 line = line.substr(1, line.length() - 4);
  85.  
  86.                 strcpy_s(linec_h, line.c_str());
  87.                 if (line != "E00" && key != XOR(linec_h)) pout << "Line is corrupted!" << std::endl;
  88.                 else { //Если выполняется, то
  89.                     comma = 0;
  90.                     // Находим время, переводим в секунды, находим отношение первого значения и всех следующих
  91.                     if (line.substr(0, 5) == "GPGGA") {
  92.                         if (time.hour == 0 && time.minute == 0 && time.sec == 0.0) {
  93.                             time.hour = stoi(line.substr(6, 2)) * 3600;
  94.                             time.minute = stoi(line.substr(8, 2)) * 60;
  95.                             time.sec = stof(line.substr(10, 4));
  96.                         }
  97.                         else {
  98.                             time.nexthour = stoi(line.substr(6, 2)) * 3600 - time.hour;
  99.                             time.nextminute = stoi(line.substr(8, 2)) * 60 - time.minute;
  100.                             time.nextsec = stof(line.substr(10, 4)) - time.sec;
  101.                         }
  102.                         time.sum = time.nexthour + time.nextminute + time.nextsec;
  103.                     }
  104.                     // Если строка GPGSV
  105.                     else if (line.substr(0, 5) == "GPGSV") {
  106.                         // Проходимся по строке
  107.                         for (size_t i = 0, N = 4, SNR = 7; i < line.size(); i++) {
  108.                             // Считаем количество запятых
  109.                             if (line[i] == ',') comma++;
  110.                             // При нахождении нужного количества
  111.                             if (comma == N) {
  112.                                 SatelliteNumber = line.substr(i - 1, 2);
  113.                             }
  114.                             else if (comma == SNR) {
  115.                                 if (Set.find(SatelliteNumber) == Set.end()) { // Если значение встречается в первый раз, то
  116.                                     SNRnumber = stoi(line.substr(i + 1, 2));
  117.                                     /*for (size_t j = 0; j < SNRarray.size(); j++) {
  118.                                         if (SNRarray.find(SNRnumber) == SNR.end()){
  119.                                            
  120.                                         }
  121.                                     }
  122.  
  123.                                     */
  124.                                     if (Map.find(SNRnumber) == Map.end()) {
  125.                                         time.SNRs = time.sum;
  126.                                         Map.insert(std::make_pair(SNRnumber, std::pair<float, int>(time.sum, count)));
  127.                                     }
  128.                                     else {
  129.                                         count++;
  130.                                         time.SNRs += time.sum;
  131.                                         Map.at(SNRnumber) = std::make_pair(time.SNRs, count);
  132.                                     }
  133.                                    
  134.                                     pout << "Satellite name: " << SatelliteNumber << "  " << "SNR: " << SNRnumber << "  Elapsed time : " << time.sum << " sec" << std::endl;
  135.                                     Set.insert(SatelliteNumber); // Добавляем его в сет
  136.  
  137.                                 }
  138.                                 N += 4;
  139.                                 SNR += 4;
  140.                             }
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.         logs_.close();
  147.  
  148.         for (auto & it : Map) {
  149.             tout << "SNR:  " << it.first << " Average time: " << it.second.second << " sec" << std::endl;
  150.         }
  151.  
  152.         std::cout << "Success" << std::endl;
  153.     }
  154.     else std::cout << "File is not open" << std::endl;
  155.     pout.close();
  156.     tout.close();
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement