|
@@ -0,0 +1,107 @@
|
|
|
+#include "utils.h"
|
|
|
+#include <limits>
|
|
|
+#include "chap2_8.h"
|
|
|
+
|
|
|
+typedef vector<u32>* (*seq_ptr)(int) ;
|
|
|
+
|
|
|
+seq_ptr seq_array[seq_cnt] = { fibon_seq };
|
|
|
+
|
|
|
+inline bool is_size_ok(int size) {
|
|
|
+ if (size <= 0 || size > 1024) {
|
|
|
+ display_message("size is invalid");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+vector<u32>* fibon_seq(int size) {
|
|
|
+ static vector<u32> elems;
|
|
|
+
|
|
|
+ if (!is_size_ok(size)) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int current_size = elems.size();
|
|
|
+
|
|
|
+ if (size > current_size) {
|
|
|
+ for (int ix = current_size; ix < size; ++ix) {
|
|
|
+ if (ix < 2) {
|
|
|
+ elems.push_back(1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ int value_ix = elems[ix - 1] + elems[ix - 2];
|
|
|
+ elems.push_back(value_ix);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return &elems;
|
|
|
+
|
|
|
+}
|
|
|
+/*
|
|
|
+
|
|
|
+*/
|
|
|
+bool fibon_elem(int position, u32& elem) {
|
|
|
+
|
|
|
+ vector<u32>* fibon_vector = fibon_seq(position);
|
|
|
+ if (fibon_vector) {
|
|
|
+ elem = (*fibon_vector)[position - 1]; //index overflow
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ elem = 0;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+bool seq_elem(int position, u32& elem,
|
|
|
+ seq_ptr seq_func) {
|
|
|
+
|
|
|
+ if (!seq_func) {
|
|
|
+ display_message("invalid parameter seq_func=NULL_PTR!");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ vector<u32>* vec = seq_func(position);
|
|
|
+ if (vec) {
|
|
|
+ elem = (*vec)[position - 1]; //index overflow
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ elem = 0;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+int main() {
|
|
|
+ u32 elem = 0;
|
|
|
+ bool ret = fibon_elem(0, elem);
|
|
|
+ if (ret) {
|
|
|
+ cout << "elem is" << elem << endl;
|
|
|
+ }
|
|
|
+ display_message('s');
|
|
|
+ display_message("test string message", 1);
|
|
|
+ display_message("test string message", 1, 2);
|
|
|
+
|
|
|
+ string s1("test string message 1");
|
|
|
+ vector<u32>* v1 = fibon_seq(10);
|
|
|
+ display(*v1);
|
|
|
+
|
|
|
+ display_message(s1, *v1);
|
|
|
+
|
|
|
+ ret = seq_elem(100, elem, fibon_seq);
|
|
|
+
|
|
|
+ if (ret) {
|
|
|
+ cout << "elem is " << elem << endl;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|