CMakeLists.txt
project(aaa) add_library(aaa SHARED aaa.cpp) add_executable(bbb bbb.cpp) target_link_libraries(bbb aaa)aaa.h
#pragma once #ifndef AAA_AAA_H #define AAA_AAA_H #endif class father { private: public: int *fabonacci(int n); double *wMA(double *array, int *fabonacci, int arrayLen, int wMALen); };aaa.cpp
#include "aaa.h" #include <iostream> using namespace std; int *father::fabonacci(int n) { if(30 >= n > 0) { int *array = new int[n]; switch(n) { case 1: { array[0] = 1; cout << array[0] << endl; break; } case 2: { array[0] = 1; array[1] = 1; cout << array[0] << endl; cout << array[1] << endl; break; } default: { array[0] = 1; array[1] = 1; cout << array[0] << endl; cout << array[1] << endl; for(int i = 2; i < n; i++) { array[i] = array[i - 1] + array[i - 2]; cout << array[i] << endl; } } } return array; } else { cout << "Error!\n" << endl; return NULL; } } double *father::wMA(double *array, int *fabonacci, int arrayLen, int wMALen) { int n = wMALen; double *wma = new double[arrayLen]; if(arrayLen >= wMALen >0) { int *pF = father::fabonacci(arrayLen); while(arrayLen >= wMALen) { double sum = 0; for(int i = wMALen - n; i < wMALen; i++) { sum = sum + array[i] * (*(pF + i)); } wma[wMALen - 1] = sum / pF[arrayLen]; cout << "wma[" << wMALen - 1 << "] = " << wma[wMALen - 1] << endl; wMALen++; } delete(pF); return wma; } else { cout << "Error!\n" << endl; return NULL; } }bbb.cpp
#include "aaa.h" #include <iostream> using namespace std; template <typename t> int getArrayLen(t &array) { return sizeof(array) / sizeof(array[0]); } int main() { double array[] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 9, 11, 12}; father child; int len = getArrayLen(array); int *pF = child.fabonacci(len); double *pWMA = child.wMA(array, pF, len, 3); delete(pF); delete(pWMA); return 0; }
时间: 2024-11-04 20:00:04