chore: Lab04

This commit is contained in:
Fándly Gergő 2020-03-28 15:04:49 +02:00
parent 430a0aa45e
commit f5645fca66
4 changed files with 353 additions and 0 deletions

23
lab03/01.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <iostream>
#include <string>
#include <sstream>
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<<word<<"\n";
}
cout.flush();
return 0;
}

192
lab04/01.cpp Normal file
View File

@ -0,0 +1,192 @@
#include <iostream>
#include <vector>
#include <cstdlib>
#include <chrono>
using namespace std;
#define SIZE 300
void benchmark(void(*f)(vector<int>* v), vector<int>* v);
void sort_insert(vector<int>* v);
void sort_bininsert(vector<int>* v);
void sort_select(vector<int>* v);
void sort_bubble(vector<int>* v);
void sort_shake(vector<int>* 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<int> v;
vector<int> work;
while(true){
char command;
cout<<"> ";
cin>>command;
switch(command){
case 'X':
case 'x':
cout<<"Exiting..."<<endl;
return 0;
case 'C':
case 'c':
cout<<"Initializing vector...\n";
v.clear();
for(int i=0; i<SIZE; i++){
v.push_back(rand()%1000);
}
work = v;
cout<<"Done\n";
break;
case 'V':
case 'v':
cout<<"The vector:\n";
for(vector<int>::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<int>* v), vector<int>* v){
auto t1 = chrono::high_resolution_clock::now();
(*f)(v);
auto t2 = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::nanoseconds>(t2 - t1).count();
cout<<"Execution time: "<<duration<<" ns\n";
}
void sort_insert(vector<int>* v){
for(int i=2; i<v->size(); 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<int>* v){
for(int i=1; i<v->size(); 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<int>* v){
for(int i=0; i<v->size()-1; i++){
int minpos = i;
int minval = v->at(i);
for(int j=i; j<v->size(); 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<int>* v){
bool done = false;
while(!done){
done = true;
for(vector<int>::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<int>* 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);
}

47
lab04/02.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <vector>
#include <numeric>
#include <cstdlib>
#include <ctime>
using namespace std;
void sort_select(vector<int>* v){
for(int i=0; i<v->size()-1; i++){
int minpos = i;
int minval = v->at(i);
for(int j=i; j<v->size(); 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<int> v(49);
iota(v.begin(), v.end(), 1);
vector<int> res;
for(int i=0; i<6; i++){
res.push_back(v[rand()%50]);
}
// sort
sort_select(&res);
cout<<"Elements in order: ";
for(vector<int>::iterator it = res.begin(); it < res.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
return 0;
}

91
lab04/03.cpp Normal file
View File

@ -0,0 +1,91 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <cmath>
#include <iomanip>
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<<setw(6)<<x<<setw(6)<<y<<setw(6)<<z;
}
};
int main(){
ios::sync_with_stdio(false);
cout<<"Planets to generate: ";
int n;
cin>>n;
cout<<"Planets:\n";
vector<Planet> v;
for(int i=0; i<n; i++){
v.push_back(Planet());
cout<<"Planet "<<setw(3)<<i<<": ";
v[i].print();
cout<<"\n";
}
cout<<"\n";
int closeA = 0;
int closeB = 1;
double closeDist = v[0].distanceTo(v[1]);
int farA = 0;
int farB = 1;
double farDist = v[0].distanceTo(v[1]);
for(int i=0; i<v.size(); i++){
for(int j=i+1; j<v.size(); j++){
double dist = v[i].distanceTo(v[j]);
if(dist < closeDist){
closeDist = dist;
closeA = i;
closeB = j;
}
if(dist > farDist){
farDist = dist;
farA = i;
farB = j;
}
}
}
cout<<"The planets closest to each other:\n";
cout<<"Planet "<<closeA<<"(";
v[closeA].print();
cout<<") and Planet "<<closeB<<"(";
v[closeB].print();
cout<<")\n";
cout<<"Distance: "<<closeDist<<"\n\n";
cout<<"The planets farthest to each other:\n";
cout<<"Planet "<<farA<<"(";
v[farA].print();
cout<<") and Planet "<<farB<<"(";
v[farB].print();
cout<<")\n";
cout<<"Distance: "<<farDist<<"\n\n";
return 0;
}