Skip to content

Commit

Permalink
Added C++ exercise folder
Browse files Browse the repository at this point in the history
Some small C++ problems
  • Loading branch information
AlexHodgson committed Nov 5, 2020
1 parent e1d8488 commit e2cef60
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
67 changes: 67 additions & 0 deletions cpp-exercises/BinarySearchTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <stdexcept>
#include <string>
#include <iostream>

class Node
{
public:
Node(int value, Node* left, Node* right)
{
this->value = value;
this->left = left;
this->right = right;
}

int getValue() const
{
return value;
}

Node* getLeft() const
{
return left;
}

Node* getRight() const
{
return right;
}

private:
int value;
Node* left;
Node* right;
};

class BinarySearchTree
{
public:
static bool contains(const Node& root, int value)
{
int node_value = root.getValue();
if (value == node_value){
return true;
}
else if (value > node_value && root.getRight() != NULL){

return contains(*root.getRight(), value);
}
else if (value < node_value && root.getLeft() != NULL){
return contains(*root.getLeft(), value);
}
else{
return false;
}
}
};

#ifndef RunTests
int main()
{
Node n1(1, NULL, NULL);
Node n3(3, NULL, NULL);
Node n2(2, &n1, &n3);

std::cout << BinarySearchTree::contains(n2, 3);
}
#endif
27 changes: 27 additions & 0 deletions cpp-exercises/QuadraticEquation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <tuple>
#include <stdexcept>
#include <iostream>
#include <string>
#include <cmath>

std::pair<double, double> findRoots(double a, double b, double c)
{
double discriminant = std::pow(b,2) - 4*a*c;

std::pair<double, double> roots;

roots.first = (-b + std::sqrt(discriminant))/(2*a);
roots.second = (-b - std::sqrt(discriminant))/(2*a);

return roots;


}

#ifndef RunTests
int main()
{
std::pair<double,double> roots = findRoots(2, 10, 8);
std::cout << "Roots: " + std::to_string(roots.first) + ", " + std::to_string(roots.second);
}
#endif
Empty file added cpp-exercises/README.md
Empty file.
48 changes: 48 additions & 0 deletions cpp-exercises/train_composition_deque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdexcept>
#include <iostream>
#include <deque>
#include <chrono>

class TrainComposition
{
private:
std::deque <int> train;

public:
void attachWagonFromLeft(int wagonId)
{
this->train.push_back(wagonId);
}

void attachWagonFromRight(int wagonId)
{
this->train.push_front(wagonId);
}

int detachWagonFromLeft()
{
int wagonID = this->train.back();
this->train.pop_back();

return wagonID;
}

int detachWagonFromRight()
{
int wagonID = this->train.front();
this->train.pop_front();

return wagonID;
}
};

#ifndef RunTests
int main()
{
TrainComposition train;
train.attachWagonFromLeft(7);
train.attachWagonFromLeft(13);
std::cout << train.detachWagonFromRight() << "\n"; // 7
std::cout << train.detachWagonFromLeft(); // 13
}
#endif

0 comments on commit e2cef60

Please sign in to comment.