From 40bb7d2dc2cce378ba64bfef109da11115f1f0cb Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 25 May 2017 16:50:31 +0200 Subject: binary search in c++ (with classes) --- cpp/ricercaBinaria.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 cpp/ricercaBinaria.cpp 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 +#include +#include + +using namespace std; + +class Bar{ + protected: + int x; + vector 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 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; +} -- cgit v1.2.3-18-g5258