summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <sancn@live.com>2017-05-25 16:50:31 +0200
committerSanto Cariotti <sancn@live.com>2017-05-25 16:50:31 +0200
commit40bb7d2dc2cce378ba64bfef109da11115f1f0cb (patch)
treee8a7c4dc218a1d639d6f705bd37db98a575d4040
parent67c2cce031d3836f4cd45ec46cd6ba81217de427 (diff)
binary search in c++ (with classes)
-rw-r--r--cpp/ricercaBinaria.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/cpp/ricercaBinaria.cpp b/cpp/ricercaBinaria.cpp
new file mode 100644
index 0000000..a3e510b
--- /dev/null
+++ b/cpp/ricercaBinaria.cpp
@@ -0,0 +1,103 @@
+#include <iostream>
+#include <vector>
+#include <string.h>
+
+using namespace std;
+
+class Bar{
+ protected:
+ int x;
+ vector<long long> f;
+
+ public:
+ virtual void foobar() = 0;
+
+ auto setX(const int x) -> void{
+ f.erase(f.begin(), f.end());
+
+ this->x = x;
+
+ fib();
+ };
+
+ auto fib() -> void {
+ f.push_back(1);
+ f.push_back(1);
+
+ for(int i = 2; i < x; i++)
+ f.push_back(f.at(i-1) + f.at(i-2));
+
+ };
+
+ auto getFib() const -> void {
+ for(auto i : f)
+ cout << i << ' ';
+
+ cout << '\n';
+ };
+
+ auto ricerca(long long n) const -> bool {
+ vector<long long> f2(f);
+ bool s = false;
+ int met_div;
+ do {
+ met_div = f2.size() / 2;
+
+ if(n == f2.at(met_div)) {
+ s = true;
+ break;
+ }else if(n > f2.at(met_div)) {
+ f2.erase(f2.begin(), f2.begin()+met_div);
+ } else {
+ f2.erase(f2.begin()+met_div, f2.end());
+ }
+
+ if(f2.size() == 1) {
+ if(f.at(0) == n)
+ s = true;
+ else
+ break;
+ }
+
+ } while(!s);
+
+ return s;
+ };
+};
+
+class Foo : public Bar {
+ public:
+ void foobar() override {
+ cout << "FOOBAR!!" << endl;
+ }
+
+ Foo(int x) {
+ this->x = x;
+ fib();
+ }
+
+};
+
+int main(int argc, char** argv)
+{
+ Foo foo(10);
+ int sx;
+ long long valRic;
+ foo.getFib();
+
+ cin >> valRic;
+
+ cout << (foo.ricerca(valRic) ? "Presente" : "Non presente") << endl;
+
+ cin >> sx;
+ foo.setX(sx);
+
+ foo.getFib();
+
+ cin >> valRic;
+ cout << (foo.ricerca(valRic) ? "Presente" : "Non presente") << endl;
+
+ foo.foobar();
+
+ return 0;
+}