Facebook
From ja, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 291
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5.  
  6. bool FileOpen(std::string& sFileName, std::ifstream& plik);
  7. long int* CreateTab(const int& iTabSize);
  8. void ReadDates(long int* TabWithDates, const int& iSize, int& iCountLookingNumbers, std::ifstream& plik);
  9. void CheckDataCorrect(const int& iSize, const int& iCount);
  10. void CheckLookingNumbersCountCorrect(const int& iCountLookingNumbers, const int& iSize);
  11. long int LookNumberOfInstance(long int* TabWithDates, const long int& LookingValue, const int& iSize);
  12. void WriteToFile(const std::string& sFileName, const int& iCountOccurance, const long int& LookingValue);
  13.  
  14.  
  15. int main()
  16. {
  17.         std::string sFileName;
  18.         std::ifstream plik;
  19.         int iSize, iCount, iCountLookingNumbers;
  20.  
  21.         do
  22.         {
  23.                 std::cin >> sFileName;
  24.  
  25.         } while (!FileOpen(sFileName, plik));
  26.  
  27.         plik >> iCount;
  28.  
  29.         int j = 0;
  30.         while (j < iCount)
  31.         {
  32.                 plik >> iSize;
  33.  
  34.                 CheckDataCorrect(iSize, iCount);
  35.  
  36.                 long int * lTabWithDates = CreateTab(iSize);
  37.  
  38.                 ReadDates(lTabWithDates, iSize, iCountLookingNumbers, plik);
  39.  
  40.                 int i = 0;
  41.                 while (i < iCountLookingNumbers)
  42.                 {
  43.                         long int LookingValue;
  44.                         int iCountOccurance;
  45.                         plik >> LookingValue;
  46.                         iCountOccurance = LookNumberOfInstance(lTabWithDates, LookingValue, iSize);
  47.                         WriteToFile(sFileName, iCountOccurance, LookingValue);
  48.                         ++i;
  49.                 }
  50.  
  51.                 delete[] lTabWithDates;
  52.                 ++j;
  53.         }
  54.         plik.close();
  55.         std::cin.get();
  56.         std::cin.get();
  57. }
  58.  
  59. bool FileOpen(std::string& sFileName, std::ifstream & plik)
  60. {
  61.         try
  62.         {
  63.                 if (sFileName.find(".txt") != std::string::npos)
  64.                 {
  65.                         plik.open(sFileName.c_str());
  66.                 }
  67.                 else
  68.                 {
  69.                         sFileName += ".txt";
  70.                         plik.open(sFileName.c_str());
  71.                 }
  72.  
  73.                 if (!plik)
  74.                 {
  75.                         throw "Nie znaleziono pliku. Podaj jeszcze raz: ";
  76.                 }
  77.         }
  78.  
  79.         catch (char * cWhatWrong)
  80.         {
  81.                 plik.clear();
  82.                 std::cout << cWhatWrong;
  83.                 return false;
  84.         }
  85.         return true;
  86. }
  87.  
  88. long int* CreateTab(const int& iTabSize)
  89. {
  90.         long int* lTabForDates;
  91.         try
  92.         {
  93.                 lTabForDates = new long int[iTabSize];
  94.                 return lTabForDates;
  95.         }
  96.         catch (std::bad_alloc & bad)
  97.         {
  98.                 std::cout << "Wystapil problem z przydzialem pamieci. Program zostanie zakonczony.";
  99.                 exit(EXIT_FAILURE);
  100.         }
  101.  
  102. }
  103.  
  104. void CheckDataCorrect(const int& iSize, const int& iCount)
  105. {
  106.         if (iCount < 1 || iSize < 1 || iSize > 1000000)
  107.         {
  108.                 std::cout << "Bledne dane w pliku. Aplikacja zostala zatrzymana !";
  109.                 exit(EXIT_FAILURE);
  110.         }
  111.  
  112. }
  113.  
  114. void ReadDates(long int* lTabWithDates, const int& iSize, int& iCountLookingNumbers, std::ifstream & plik)
  115. {
  116.  
  117.         for (int i = 0; i < iSize; ++i)
  118.         {
  119.                 plik >> lTabWithDates[i];
  120.         }
  121.         plik >> iCountLookingNumbers;
  122.         CheckLookingNumbersCountCorrect(iCountLookingNumbers, iSize);
  123. }
  124.  
  125. void CheckLookingNumbersCountCorrect(const int& iCountLookingNumbers, const int& iSize)
  126. {
  127.         if (iCountLookingNumbers < 1 || iCountLookingNumbers > iSize)
  128.         {
  129.                 std::cout << "Bledne dane.";
  130.                 exit(EXIT_FAILURE);
  131.         }
  132.  
  133. }
  134.  
  135. long int LookNumberOfInstance(long int* lTabWithDates, const long int& lLookingValue, const int& iSize)
  136. {
  137.  
  138.         int iTabCenter = (iSize - 1) / 2;
  139.         int ValueCount = 0;
  140.         int iPointer = iTabCenter;
  141.         int iFirst = 0;
  142.         int iLast = iSize - 1;
  143.         bool NotFind = false;
  144.         bool IfFind = false;
  145.  
  146.         while (!NotFind && !IfFind)
  147.         {
  148.                 if (lTabWithDates[iTabCenter] == lLookingValue)
  149.                 {
  150.                         iPointer = iTabCenter;
  151.                         ++ValueCount;
  152.                         ++iPointer;
  153.                         while (lTabWithDates[iPointer] == lLookingValue && iPointer < iSize)
  154.                         {
  155.                                 ++ValueCount;
  156.                                 ++iPointer;
  157.                         }
  158.                         iPointer = iTabCenter - 1;
  159.                         while (lTabWithDates[iPointer] == lLookingValue)
  160.                         {
  161.                                 ++ValueCount;
  162.                                 --iPointer;
  163.                         }
  164.                         IfFind = true;
  165.                 }
  166.  
  167.                 if (lTabWithDates[iTabCenter] < lLookingValue)
  168.                 {
  169.                         iFirst = iTabCenter;
  170.                         iTabCenter = iLast - (iLast - iFirst) / 2;
  171.                 }
  172.                 else
  173.                         if (lTabWithDates[iTabCenter] > lLookingValue)
  174.                         {
  175.                                 iLast = iTabCenter;
  176.                                 iTabCenter = iFirst + (iLast - iFirst) / 2;
  177.                         }
  178.  
  179.                 if (iLast == iFirst)
  180.                 {
  181.                         NotFind = true;
  182.                 }
  183.         }
  184.         return ValueCount;
  185. }
  186.  
  187. void WriteToFile(const std::string& sFileName, const int& iCountOccurance, const long int& LookingValue)
  188. {
  189.         std::ofstream outFile;
  190.         outFile.open(sFileName, std::ios::app);
  191.         outFile << std::endl << LookingValue << " " << iCountOccurance;
  192.         outFile.close();
  193. }