Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.49 KB | None | 0 0
  1. #ifndef L_FILES_H
  2. #define L_FILES_H
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <sstream>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11. /*Funkcja liczy podobne elementy w tablicy
  12. @ parametr array jest to tablica po ktorej sie porszuamy  
  13. @ zmienna k odpowiada za klucz , czyli szukany element
  14. @ parametr length jest to wielkosc tablicy
  15. @ return ilosc podobnych elementow*/
  16. int countElements(int *array, int k, int length);
  17.  
  18. /*Funkcja zczytujaca dane z pliku
  19. @zmienna fileName jest to string ktory przechowuje nazwe pliku
  20. @return zwraca tablice stringów ktore przechowuja dane w linijkach pliku */
  21. string* readFromFile(string fileName);
  22.  
  23. /*Funkcja odpowiedzialna za szukanie interpolacyjne.Szuka indexu tablicy pytanej wartosci.
  24. @ parametr array to tablica po ktorej porusza sie algorytm
  25. @ parametr k oznacza klucz szukanego elementu
  26. @ parametr length jest to wielkosc tablicy
  27. @ return numer indexu tablicy dla klucza k*/
  28. int interpolationSerch(int *array, int k, int lenght);
  29.  
  30. /*Funckja zapisuje dane do pliku wykorzystuje biblioteke sstream ,
  31. wykorzystujê stringstream by przepisac dane ze stringow w wartosci typu int, na koncu "usuwam "duplikaty
  32.  
  33. @parametr numberOfAsks to inaczej liczba oznaczaja ilosc zapytan o szukana dla nas wartosc
  34. @parametr namOfFile to nazwa pliku
  35. @parametr array to tablica po ktorej sie poruszam
  36. @parametr arSerchValue to tablica kluczy o ktore sie pytam
  37. @parametr length to wielkosc tablicy */
  38. void saveToFile(string namOfFile);
  39.  
  40.  
  41.  
  42.    
  43.  
  44.  
  45. #endif // !L_FILES_H
  46.  
  47.  
  48.  
  49. -----------------------------------
  50. #include"l_files.h"
  51.  
  52. int globalCounter = 0;
  53.  
  54.  
  55. int countElements(int *array, int k, int length)
  56. {
  57.     int counter = 0;
  58.     int ip = 0;
  59.     int ik = length - 1;
  60.     int is = (ip + ik) / 2;
  61.     int counter2 = 1;
  62.  
  63.     while (ip <= ik)
  64.     {
  65.         is = (ip + ik) / 2;
  66.  
  67.         if (array[is] == k)
  68.         {
  69.             break;
  70.         }
  71.  
  72.         if (k > array[is])
  73.         {
  74.             ip = is + 1;
  75.         }
  76.         else
  77.         {
  78.             ik = is - 1;
  79.         }
  80.     }
  81.  
  82.     if (array[is] == k)
  83.     {
  84.         counter++;
  85.  
  86.         while (array[is - counter2] == k && (is - counter2) >=0)
  87.         {
  88.             counter2++;
  89.             counter++;
  90.         }
  91.  
  92.         counter2 = 1;
  93.         while (array[is + counter2] == k && (is + counter2) < length)
  94.         {
  95.             counter2++;
  96.             counter++;
  97.         }
  98.     }
  99.     return counter;
  100.  
  101. }
  102.  
  103. int interpolationSerch(int *array,int k, int length)
  104. {
  105.     int ip=0;//element poczatkowy
  106.     int ik=length-1;//element koncowy
  107.     int is=(ip+ik)/2;//index srodkowego elemenetu
  108.     int indexK;//index klucza
  109.  
  110.  
  111.     while ( (k >= array[ip]) && (k <= array[ik])&& (array[ik] != array[ip])) {
  112.  
  113.         is = ip + ((k - array[ip]) * (ik - is) / (array[ik] - array[ip]));
  114.  
  115.         if (array[is] == k)
  116.         {
  117.             indexK = is;
  118.             return indexK;
  119.             break;
  120.            
  121.         }
  122.         else if (k < array[is])
  123.             ik = is - 1;
  124.         else
  125.             ip = is + 1;
  126.     }
  127.  
  128.     if (k == array[ip])
  129.         return ip;
  130.     else
  131.     {
  132.         return -1;
  133.     }
  134. }
  135.  
  136.  
  137.  
  138. string* readFromFile(string fileName)
  139. {
  140.     ifstream file;
  141.     string line;
  142.     string *array = new string[1000];
  143.     int failCounter = 1;
  144.     do
  145.     {
  146.         file.open(fileName, ios::in);
  147.         if (file.good() == true)
  148.         {
  149.             break;
  150.         }
  151.         else
  152.         {
  153.             cout << "File of this name does not exist! Enter it again " << endl;
  154.             cin >> fileName;
  155.             if (failCounter > 1)
  156.             {
  157.                 cout << "Invalid number of tries";
  158.                     exit(0);
  159.             }
  160.            
  161.         }
  162.         failCounter++;
  163.     } while (true);
  164.  
  165.  
  166.     while (!file.eof())
  167.     {
  168.         getline(file, array[globalCounter]);
  169.             globalCounter++;
  170.     }
  171.  
  172.     file.close();
  173.     return array;
  174. }
  175.  
  176.  
  177. void saveToFile(string namOfFile)
  178. {
  179.     ofstream plik;
  180.     string*sData = readFromFile(namOfFile);
  181.     stringstream ss[1000];
  182.  
  183.     for (int i = 0; i <globalCounter+1 ; i++)
  184.         ss[i] << sData[i];
  185.  
  186.         int numberOfSets;
  187.         int numberOfAsks;
  188.         int *array=new int [1000];
  189.         int *arSerchForValue=new int [1000];
  190.         int length;
  191.         int counter = 0;
  192.  
  193.         ss[0] >> numberOfSets;
  194.  
  195.         if (numberOfSets > 32768 || numberOfSets < 1)
  196.         {
  197.             cout << "! Invalid number of sets";
  198.             exit(0);
  199.         }
  200.  
  201.  
  202.     plik.open("out."+namOfFile, ios::out);
  203.  
  204.     if (plik.good() == true)
  205.     {
  206.        
  207.         for (int j = 0; j < numberOfSets; j++)
  208.         {
  209.  
  210.             ss[1 + (counter * 4)] >> length;
  211.  
  212.             ss[3 + (counter * 4)] >> numberOfAsks;
  213.             if (numberOfAsks > 32768 || numberOfAsks < -32768)
  214.             {
  215.                 cout << "Invalid number of requests ";
  216.                 exit(0);
  217.             }
  218.  
  219.             for (int k = 0; k < length; k++)
  220.             {
  221.                 if (array[k] > pow(2, 48) || array[k] < (-1)*pow(2, 48))
  222.                 {
  223.                     cout << "Invalid elements in array !";
  224.                         exit(0);
  225.                 }
  226.                 ss[2 + (counter * 4)] >> array[k];
  227.  
  228.             }
  229.  
  230.             for (int z = 0; z < numberOfAsks; z++)
  231.             {
  232.                 if (arSerchForValue[z] > pow(2, 48) || arSerchForValue[z] < (-1)*pow(2, 48))
  233.                 {
  234.                     cout << "Invalid elements in array of request !";
  235.                     exit(0);
  236.                 }
  237.                 ss[4 + (counter * 4)] >> arSerchForValue[z];
  238.             }
  239.  
  240.             for (int i = 0; i < numberOfAsks; i++)
  241.             {
  242.                 plik << "(" << countElements(array, arSerchForValue[i], length) << " " << interpolationSerch(array, arSerchForValue[i], length) << ")";
  243.             }
  244.  
  245.             int prev = array[0];//wczesniejszy
  246.             int current;//terazniejszy
  247.  
  248.             plik << prev << " ";
  249.             for (int p = 1; p < length; p++)
  250.             {
  251.                 current = array[p];
  252.                 if (current != prev)
  253.                 {
  254.                     plik << current << " ";
  255.                 }
  256.                 prev = array[p];
  257.             }
  258.             counter++;
  259.             plik << endl;
  260.  
  261.         }
  262.  
  263.     }
  264.         plik.close();
  265.  
  266. }
  267. ------------------------------
  268.  
  269. #include "l_files.h"
  270.  
  271.  
  272.  
  273.  
  274. int main()
  275. {
  276.     string fileName;
  277.     cout << "Enter your file name , it has to be .txt extension(type in1.txt for example)" << endl;
  278.     cin >> fileName;
  279.     saveToFile(fileName);
  280.  
  281.     return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement