Skip to content

Instantly share code, notes, and snippets.

@IvanLuchkin
Created May 11, 2019 11:19
Show Gist options
  • Save IvanLuchkin/9dbd7f116203c828653fdda080498a01 to your computer and use it in GitHub Desktop.
Save IvanLuchkin/9dbd7f116203c828653fdda080498a01 to your computer and use it in GitHub Desktop.
LabThreeTA
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/LabThree.iml" filepath="$PROJECT_DIR$/.idea/LabThree.iml" />
</modules>
</component>
</project>
cmake_minimum_required(VERSION 3.14)
project(LabThree)
set(CMAKE_CXX_STANDARD 14)
add_executable(LabThree main.cpp)
#include<bits/stdc++.h>
using namespace std;
int ln(int x)
{
int res = 0;
while(x != 0)
{
res++;
x /= 10;
}
return res;
}
int to_int(const string& s)
{
stringstream ss;
ss << s;
int ans;
ss >> ans;
return ans;
}
string to_str(int x)
{
string ans;
stringstream ss;
ss << x;
ss >> ans;
while(ans.size() != 5)
ans = '0' + ans;
return ans;
}
void add(int key, string s, bool need = 0, int pos = -1)
{
fstream memory("memory.txt", std::fstream::in | std::fstream::out);
memory.seekg(0, std::fstream::end);
if(pos == -1)
pos = memory.tellg();
if(need == 0)
memory << key << ' ' << s << '\n';
int step = 0;
int block = 5000;
int line_ln = 12;
fstream ind("/home/ivan/CLionProjects/LabThree/index.txt", std::fstream::in | std::fstream::out);
ind >> step;
ind.seekg(ln(step)+1, std::fstream::beg);
ind.seekg(key/block*step*line_ln, std::fstream::cur);
vector<string> a(step);
for(int i = 0; i < step; i++)
getline(ind, a[i]);
if(a[step-1][10] != '=')
{
ind.seekg(ln(step)+1, std::fstream::beg);
vector<string> q(4*step);
for(int i = 0; i < 4; i++)
for(int j = 0; j < step; j++)
getline(ind, q[step*i+j]);
ofstream fout;
fout.open("/home/ivan/CLionProjects/LabThree/index.txt");
fout << step+5 << '\n';
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < step; j++)
fout << q[step*i+j] << '\n';
for(int j = 0; j < 6; j++)
fout << "============\n";
}
fout.close();
step += 5;
ind.close();
memory.close();
add(key, s, true, pos);
return;
}
vector<int> b(step);
for(int i = 0; i < step; i++)
b[i] = to_int(a[i]);
int l = 0;
int next = step/2;
while(next != 0)
if(l+next < step && a[l+next][0] != '=' && b[l+next] <= key)
l += next;
else
next /= 2;
if(b[l] == key)
{
cout << "Existing elment\n";
return;
}
if(l+1 < step && a[l][0] != '=' && b[l] <= key)
l++;
if(b[l] == key)
{
cout << "Existing elment\n";
return;
}
ind.seekg(ln(step)+1+key/block*step*line_ln, std::fstream::beg);
for(int i = 0; i < l; i++)
ind << a[i] << '\n';
ind << to_str(key) + ' ' + to_str(pos) << '\n';
for(int i = l; i < step-1; i++)
ind << a[i] << '\n';
ind.close();
memory.close();
}
int cnt = 0;
string request(int key)
{
int step = 0;
int block = 5000;
int line_ln = 12;
fstream ind("/home/ivan/CLionProjects/LabThree/index.txt");
ind >> step;
ind.seekg(ln(step)+1, ind.beg);
ind.seekg(key/block*step*line_ln, ind.cur);
vector<string> a(step);
for(int i = 0; i < step; i++)
getline(ind, a[i]);
vector<int> b(step);
for(int i = 0; i < step; i++)
b[i] = to_int(a[i]);
int l = 0;
int next = step/2;
while(next != 0)
if(l+next < step && a[l+next][0] != '=' && b[l+next] <= key)
cnt++,
l += next;
else
cnt++,
next /= 2;
stringstream ss;
ss << a[l];
int res;
ss >> res >> res;
ifstream fin;
fin.open("/home/ivan/CLionProjects/LabThree/memory.txt");
fin.seekg(res, fin.beg);
string s;
getline(fin, s);
return s;
}
void erase(int key)
{
int step = 0;
int block = 5000;
int line_ln = 12;
fstream ind("/home/ivan/CLionProjects/LabThree/index.txt");
ind >> step;
ind.seekg(ln(step)+1, ind.beg);
ind.seekg(key/block*step*line_ln, ind.cur);
vector<string> a(step);
for(int i = 0; i < step; i++)
getline(ind, a[i]);
vector<int> b(step);
for(int i = 0; i < step; i++)
b[i] = to_int(a[i]);
int l = 0;
int next = step/2;
while(next != 0)
if(l+next < step && a[l+next][0] != '=' && b[l+next] <= key)
l += next;
else
next /= 2;
ind.seekg(ln(step)+1, ind.beg);
ind.seekg(key/block*step*line_ln,ind.cur);
for(int i = 0; i < l; i++)
ind << a[i] << '\n';
for(int i = l+1; i < step; i++)
ind << a[i] << '\n';
ind << "===========\n";
}
void edit(int key, string s)
{
erase(key);
add(key, s);
}
int main()
{
bool completeOperation = false;
srand(time(0));
while(!completeOperation)
{
cout << "Choose the operation to be performed:\n";
cout << "1 -- add an element\n";
cout << "2 -- add several elements\n";
cout << "3 -- look for the element\n";
cout << "4 -- delete the element\n";
cout << "5 -- change the data\n";
int x;
cin >> x;
int y;
string s;
switch(x)
{
case 1:
cout << "Enter the key and the data\n";
cin >> y >> s;
add(y, s);
completeOperation = true;
break;
case 2:
cout << "Enter the amount of elements to be inserted\n";
cin >> y;
for(int i = 0; i < y; i++) {
add(rand()%10000, "tttttt");
}
completeOperation = true;
break;
case 3:
cout << "Enter the key you are looking for\n";
cin >> y;
cout << request(y) << ' ';
cout << cnt << '\n';
cnt = 0;
completeOperation = true;
break;
case 4:
cout << "Enter the key of the element to be deleted\n";
cin >> y;
erase(y);
completeOperation = true;
break;
case 5:
cout << "Enter the element to be modified\n";
cin >> y >> s;
edit(y, s);
completeOperation = true;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment