Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include"l_files.h"
  2.  
  3. int globalCounter = 0;
  4.  
  5.  
  6. int countElements(int *array, int k, int length)
  7. {
  8.     int counter = 0;
  9.     int ip = 0;
  10.     int ik = length - 1;
  11.     int is = (ip + ik) / 2;
  12.     int counter2 = 1;
  13.  
  14.     while (ip <= ik)
  15.     {
  16.         is = (ip + ik) / 2;
  17.  
  18.         if (array[is] == k)
  19.         {
  20.             break;
  21.         }
  22.  
  23.         if (k > array[is])
  24.         {
  25.             ip = is + 1;
  26.         }
  27.         else
  28.         {
  29.             ik = is - 1;
  30.         }
  31.     }
  32.  
  33.     if (array[is] == k)
  34.     {
  35.         counter++;
  36.  
  37.         while (array[is - counter2] == k && (is - counter2) >=0)
  38.         {
  39.             counter2++;
  40.             counter++;
  41.         }
  42.  
  43.         counter2 = 1;
  44.         while (array[is + counter2] == k && (is + counter2) < length)
  45.         {
  46.             counter2++;
  47.             counter++;
  48.         }
  49.     }
  50.     return counter;
  51.  
  52. }
  53.  
  54. int interpolationSerch(int *array,int k, int length)
  55. {
  56.     int ip=0;//element poczatkowy
  57.     int ik=length-1;//element koncowy
  58.     int is=(ip+ik)/2;//index srodkowego elemenetu
  59.     int indexK;//index klucza
  60.  
  61.  
  62.     while ( (k >= array[ip]) && (k <= array[ik])&& (array[ik] != array[ip])) {
  63.  
  64.         is = ip + ((k - array[ip]) * (ik - is) / (array[ik] - array[ip]));
  65.  
  66.         if (array[is] == k)
  67.         {
  68.             indexK = is;
  69.             return indexK;
  70.             break;
  71.            
  72.         }
  73.         else if (k < array[is])
  74.             ik = is - 1;
  75.         else
  76.             ip = is + 1;
  77.     }
  78.  
  79.     if (k == array[ip])
  80.         return ip;
  81.     else
  82.     {
  83.         return -1;
  84.     }
  85. }
  86.  
  87.  
  88.  
  89. string* readFromFile(string fileName)
  90. {
  91.     ifstream file;
  92.     string line;
  93.     string *array = new string[1000];
  94.     int failCounter = 1;
  95.     do
  96.     {
  97.         file.open(fileName, ios::in);
  98.         if (file.good() == true)
  99.         {
  100.             break;
  101.         }
  102.         else
  103.         {
  104.             cout << "File of this name does not exist! Enter it again " << endl;
  105.             cin >> fileName;
  106.             if (failCounter > 1)
  107.             {
  108.                 cout << "Invalid number of tries";
  109.                     exit(0);
  110.             }
  111.            
  112.         }
  113.         failCounter++;
  114.     } while (true);
  115.  
  116.  
  117.     while (!file.eof())
  118.     {
  119.         getline(file, array[globalCounter]);
  120.             globalCounter++;
  121.     }
  122.  
  123.     file.close();
  124.     return array;
  125. }
  126.  
  127.  
  128. void saveToFile(string namOfFile)
  129. {
  130.     ofstream plik;
  131.     string*sData = readFromFile(namOfFile);
  132.     stringstream ss[1000];
  133.  
  134.     for (int i = 0; i <globalCounter+1 ; i++)
  135.         ss[i] << sData[i];
  136.  
  137.         int numberOfSets;
  138.         int numberOfAsks;
  139.         int *array=new int [1000];
  140.         int *arSerchForValue=new int [1000];
  141.         int length;
  142.         int counter = 0;
  143.  
  144.         ss[0] >> numberOfSets;
  145.  
  146.         if (numberOfSets > 32768 || numberOfSets < 1)
  147.         {
  148.             cout << "! Invalid number of sets";
  149.             exit(0);
  150.         }
  151.  
  152.  
  153.     plik.open("out."+namOfFile, ios::out);
  154.  
  155.     if (plik.good() == true)
  156.     {
  157.        
  158.         for (int j = 0; j < numberOfSets; j++)
  159.         {
  160.  
  161.             ss[1 + (counter * 4)] >> length;
  162.  
  163.             ss[3 + (counter * 4)] >> numberOfAsks;
  164.             if (numberOfAsks > 32768 || numberOfAsks < -32768)
  165.             {
  166.                 cout << "Invalid number of requests ";
  167.                 exit(0);
  168.             }
  169.  
  170.             for (int k = 0; k < length; k++)
  171.             {
  172.                 if (array[k] > pow(2, 48) || array[k] < (-1)*pow(2, 48))
  173.                 {
  174.                     cout << "Invalid elements in array !";
  175.                         exit(0);
  176.                 }
  177.                 ss[2 + (counter * 4)] >> array[k];
  178.  
  179.             }
  180.  
  181.             for (int z = 0; z < numberOfAsks; z++)
  182.             {
  183.                 if (arSerchForValue[z] > pow(2, 48) || arSerchForValue[z] < (-1)*pow(2, 48))
  184.                 {
  185.                     cout << "Invalid elements in array of request !";
  186.                     exit(0);
  187.                 }
  188.                 ss[4 + (counter * 4)] >> arSerchForValue[z];
  189.             }
  190.  
  191.             for (int i = 0; i < numberOfAsks; i++)
  192.             {
  193.                 plik << "(" << countElements(array, arSerchForValue[i], length) << " " << interpolationSerch(array, arSerchForValue[i], length) << ")";
  194.             }
  195.  
  196.             int prev = array[0];//wczesniejszy
  197.             int current;//terazniejszy
  198.  
  199.             plik << prev << " ";
  200.             for (int p = 1; p < length; p++)
  201.             {
  202.                 current = array[p];
  203.                 if (current != prev)
  204.                 {
  205.                     plik << current << " ";
  206.                 }
  207.                 prev = array[p];
  208.             }
  209.             counter++;
  210.             plik << endl;
  211.  
  212.         }
  213.  
  214.     }
  215.         plik.close();
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement