diff --git a/lab03/01.cpp b/lab03/01.cpp new file mode 100644 index 0000000..b2fe849 --- /dev/null +++ b/lab03/01.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +using namespace std; + +int main(){ + ios::sync_with_stdio(false); + + cout<<"Introduce a line: "; + string line; + getline(cin, line); + + cout<<"Line broken into separate words:\n"; + istringstream sstream(line); + string word; + while(getline(sstream, word, ' ')){ + cout< +#include +#include +#include +using namespace std; + +#define SIZE 300 + +void benchmark(void(*f)(vector* v), vector* v); +void sort_insert(vector* v); +void sort_bininsert(vector* v); +void sort_select(vector* v); +void sort_bubble(vector* v); +void sort_shake(vector* v); + +int main(){ + ios::sync_with_stdio(false); + + cout<<"----- Testing sort algorithms -----\n\n\n"; + cout<<"Usage: e[X]it, [C]reate vector, print [V]ector, [R]eset vector, [I]nsertion sort, [B]inary insertion sort, [S]elect sort, bubble sor[T], s[H]ake sort\n\n"; + + srand(time(NULL)); + + vector v; + vector work; + + while(true){ + char command; + cout<<"> "; + cin>>command; + + switch(command){ + case 'X': + case 'x': + cout<<"Exiting..."<::iterator it = work.begin(); it < work.end(); it++){ + cout<<*it<<" "; + } + cout<<"\n"; + break; + case 'R': + case 'r': + cout<<"Reinitializing working copy...\n"; + work = v; + cout<<"Done\n"; + break; + case 'I': + case 'i': + cout<<"# Insertion sort\n"; + benchmark(sort_insert, &work); + break; + case 'B': + case 'b': + cout<<"# Binary insertion sort\n"; + benchmark(sort_bininsert, &work); + break; + case 'S': + case 's': + cout<<"# Select sort\n"; + benchmark(sort_select, &work); + break; + case 'T': + case 't': + cout<<"# Bubble sort\n"; + benchmark(sort_bubble, &work); + break; + case 'H': + case 'h': + cout<<"# Shake sort\n"; + benchmark(sort_shake, &work); + break; + } + } + + return 0; +} + + + +void benchmark(void(*f)(vector* v), vector* v){ + auto t1 = chrono::high_resolution_clock::now(); + (*f)(v); + auto t2 = chrono::high_resolution_clock::now(); + auto duration = chrono::duration_cast(t2 - t1).count(); + cout<<"Execution time: "<* v){ + for(int i=2; isize(); i++){ + int x = v->at(i); + int j = i-1; + while(j>=0 && v->at(j)>x){ + v->at(j+1) = v->at(j); + j--; + } + v->at(j+1) = x; + } +} + +void sort_bininsert(vector* v){ + for(int i=1; isize(); i++){ + int x = v->at(i); + int start = 0; + int dest = i-1; + while(start <= dest){ + int middle = (start + dest) / 2; + if(v->at(middle) > x){ + dest = middle-1; + } + else{ + start = middle+1; + } + + for(int j=i-1; j>=start; j--){ + v->at(j+1) = v->at(j); + } + + v->at(start) = x; + } + } +} + +void sort_select(vector* v){ + for(int i=0; isize()-1; i++){ + int minpos = i; + int minval = v->at(i); + for(int j=i; jsize(); j++){ + if(v->at(j) < minval){ + minval = v->at(j); + minpos = j; + } + } + + v->at(minpos) = v->at(i); + v->at(i) = minval; + } +} + +void sort_bubble(vector* v){ + bool done = false; + while(!done){ + done = true; + for(vector::iterator it = v->begin(); it < v->end()-1; it++){ + if(*it > *(it+1)){ + int tmp = *it; + *it = *(it+1); + *(it+1) = tmp; + done = false; + } + } + } +} + +void sort_shake(vector* v){ + int left = 2; + int right = v->size() - 1; + int k = v->size() - 1; + do{ + for(int j=right; j>=left; j--){ + if(v->at(j-1) > v->at(j)){ + int tmp = v->at(j-1); + v->at(j-1) = v->at(j); + v->at(j) = tmp; + k = j; + } + } + left = k+1; + for(int j=1; j<=right; j++){ + if(v->at(j-1) > v->at(j)){ + int tmp = v->at(j-1); + v->at(j-1) = v->at(j); + v->at(j) = tmp; + k = j; + } + } + right = k-1; + }while(left <= right); +} diff --git a/lab04/02.cpp b/lab04/02.cpp new file mode 100644 index 0000000..05e6df6 --- /dev/null +++ b/lab04/02.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +using namespace std; + +void sort_select(vector* v){ + for(int i=0; isize()-1; i++){ + int minpos = i; + int minval = v->at(i); + for(int j=i; jsize(); j++){ + if(v->at(j) < minval){ + minval = v->at(j); + minpos = j; + } + } + + v->at(minpos) = v->at(i); + v->at(i) = minval; + } +} + +int main(){ + ios::sync_with_stdio(false); + + srand(time(NULL)); + + vector v(49); + iota(v.begin(), v.end(), 1); + vector res; + + for(int i=0; i<6; i++){ + res.push_back(v[rand()%50]); + } + + // sort + sort_select(&res); + + cout<<"Elements in order: "; + for(vector::iterator it = res.begin(); it < res.end(); it++){ + cout<<*it<<" "; + } + cout< +#include +#include +#include +#include +#include +using namespace std; + +class Planet { +public: + int x,y,z; + Planet(int x, int y, int z){ + this->x = x; + this->y = y; + this->z = z; + } + Planet(){ + x = rand()%2000 - 1000; + y = rand()%2000 - 1000; + z = rand()%2000 - 1000; + } + + double distanceTo(Planet p){ + return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2) + pow(p.z - z, 2)); + } + + void print(){ + cout<>n; + + cout<<"Planets:\n"; + vector v; + for(int i=0; i farDist){ + farDist = dist; + farA = i; + farB = j; + } + } + } + + cout<<"The planets closest to each other:\n"; + cout<<"Planet "<