Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef L_FILES_H
- #define L_FILES_H
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <string>
- #include <sstream>
- #include <cmath>
- using namespace std;
- /*Funkcja liczy podobne elementy w tablicy
- @ parametr array jest to tablica po ktorej sie porszuamy
- @ zmienna k odpowiada za klucz , czyli szukany element
- @ parametr length jest to wielkosc tablicy
- @ return ilosc podobnych elementow*/
- int countElements(int *array, int k, int length);
- /*Funkcja zczytujaca dane z pliku
- @zmienna fileName jest to string ktory przechowuje nazwe pliku
- @return zwraca tablice stringów ktore przechowuja dane w linijkach pliku */
- string* readFromFile(string fileName);
- /*Funkcja odpowiedzialna za szukanie interpolacyjne.Szuka indexu tablicy pytanej wartosci.
- @ parametr array to tablica po ktorej porusza sie algorytm
- @ parametr k oznacza klucz szukanego elementu
- @ parametr length jest to wielkosc tablicy
- @ return numer indexu tablicy dla klucza k*/
- int interpolationSerch(int *array, int k, int lenght);
- /*Funckja zapisuje dane do pliku wykorzystuje biblioteke sstream ,
- wykorzystujê stringstream by przepisac dane ze stringow w wartosci typu int, na koncu "usuwam "duplikaty
- @parametr numberOfAsks to inaczej liczba oznaczaja ilosc zapytan o szukana dla nas wartosc
- @parametr namOfFile to nazwa pliku
- @parametr array to tablica po ktorej sie poruszam
- @parametr arSerchValue to tablica kluczy o ktore sie pytam
- @parametr length to wielkosc tablicy */
- void saveToFile(string namOfFile);
- #endif // !L_FILES_H
- -----------------------------------
- #include"l_files.h"
- int globalCounter = 0;
- int countElements(int *array, int k, int length)
- {
- int counter = 0;
- int ip = 0;
- int ik = length - 1;
- int is = (ip + ik) / 2;
- int counter2 = 1;
- while (ip <= ik)
- {
- is = (ip + ik) / 2;
- if (array[is] == k)
- {
- break;
- }
- if (k > array[is])
- {
- ip = is + 1;
- }
- else
- {
- ik = is - 1;
- }
- }
- if (array[is] == k)
- {
- counter++;
- while (array[is - counter2] == k && (is - counter2) >=0)
- {
- counter2++;
- counter++;
- }
- counter2 = 1;
- while (array[is + counter2] == k && (is + counter2) < length)
- {
- counter2++;
- counter++;
- }
- }
- return counter;
- }
- int interpolationSerch(int *array,int k, int length)
- {
- int ip=0;//element poczatkowy
- int ik=length-1;//element koncowy
- int is=(ip+ik)/2;//index srodkowego elemenetu
- int indexK;//index klucza
- while ( (k >= array[ip]) && (k <= array[ik])&& (array[ik] != array[ip])) {
- is = ip + ((k - array[ip]) * (ik - is) / (array[ik] - array[ip]));
- if (array[is] == k)
- {
- indexK = is;
- return indexK;
- break;
- }
- else if (k < array[is])
- ik = is - 1;
- else
- ip = is + 1;
- }
- if (k == array[ip])
- return ip;
- else
- {
- return -1;
- }
- }
- string* readFromFile(string fileName)
- {
- ifstream file;
- string line;
- string *array = new string[1000];
- int failCounter = 1;
- do
- {
- file.open(fileName, ios::in);
- if (file.good() == true)
- {
- break;
- }
- else
- {
- cout << "File of this name does not exist! Enter it again " << endl;
- cin >> fileName;
- if (failCounter > 1)
- {
- cout << "Invalid number of tries";
- exit(0);
- }
- }
- failCounter++;
- } while (true);
- while (!file.eof())
- {
- getline(file, array[globalCounter]);
- globalCounter++;
- }
- file.close();
- return array;
- }
- void saveToFile(string namOfFile)
- {
- ofstream plik;
- string*sData = readFromFile(namOfFile);
- stringstream ss[1000];
- for (int i = 0; i <globalCounter+1 ; i++)
- ss[i] << sData[i];
- int numberOfSets;
- int numberOfAsks;
- int *array=new int [1000];
- int *arSerchForValue=new int [1000];
- int length;
- int counter = 0;
- ss[0] >> numberOfSets;
- if (numberOfSets > 32768 || numberOfSets < 1)
- {
- cout << "! Invalid number of sets";
- exit(0);
- }
- plik.open("out."+namOfFile, ios::out);
- if (plik.good() == true)
- {
- for (int j = 0; j < numberOfSets; j++)
- {
- ss[1 + (counter * 4)] >> length;
- ss[3 + (counter * 4)] >> numberOfAsks;
- if (numberOfAsks > 32768 || numberOfAsks < -32768)
- {
- cout << "Invalid number of requests ";
- exit(0);
- }
- for (int k = 0; k < length; k++)
- {
- if (array[k] > pow(2, 48) || array[k] < (-1)*pow(2, 48))
- {
- cout << "Invalid elements in array !";
- exit(0);
- }
- ss[2 + (counter * 4)] >> array[k];
- }
- for (int z = 0; z < numberOfAsks; z++)
- {
- if (arSerchForValue[z] > pow(2, 48) || arSerchForValue[z] < (-1)*pow(2, 48))
- {
- cout << "Invalid elements in array of request !";
- exit(0);
- }
- ss[4 + (counter * 4)] >> arSerchForValue[z];
- }
- for (int i = 0; i < numberOfAsks; i++)
- {
- plik << "(" << countElements(array, arSerchForValue[i], length) << " " << interpolationSerch(array, arSerchForValue[i], length) << ")";
- }
- int prev = array[0];//wczesniejszy
- int current;//terazniejszy
- plik << prev << " ";
- for (int p = 1; p < length; p++)
- {
- current = array[p];
- if (current != prev)
- {
- plik << current << " ";
- }
- prev = array[p];
- }
- counter++;
- plik << endl;
- }
- }
- plik.close();
- }
- ------------------------------
- #include "l_files.h"
- int main()
- {
- string fileName;
- cout << "Enter your file name , it has to be .txt extension(type in1.txt for example)" << endl;
- cin >> fileName;
- saveToFile(fileName);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement