#/* * highorder.cpp * * Created on: 10.01.2013 * Author: trifon */ #include #include using namespace std; int f(int x) { return x + 2; } void swapi(int& x, int& y) { int tmp = x; x = y; y = tmp; } double plus_op(double x, double y) { return x + y; } double mult_op(double x, double y) { return x * y; } typedef int(*next_function)(int); typedef double(*term_function)(double); typedef double(*operation)(double,double); // plus1, mult2, mult_op, plus_op double accumulate(operation op, double base_value, double a, double b, term_function f, next_function next) { double s = base_value; for(int i = a; i <= b; i = next(i)) s = op(s, f(i)); return s; } int main1() { typedef int number; typedef number new_number; number x = 2; for(number i = 0; i < x; i++) cout << i << endl; new_number y = 3; //swapi(x,y); cout << x << ' ' << y << endl; cout << x / y << endl; return 0; } typedef int matrix[10][10]; int g(matrix m) { return m[0][0] + m[1][1]; } int main2() { matrix m = { { 1, 2, 3}, {4, 5, 6} }; cout << m[1][2] << endl; typedef int **dpi; int* x = &m[0][1]; dpi p = &x; } void h() { cout << "h()\n"; } int plus1(int i) { return i + 1; } int mult2(int i) { return i * 2; } double sum(int n, double(*f)(double), int(*next)(int)) { double s = 0; for(int i = 1; i <= n; i = next(i)) s += f(i); return s; } double sum2(int n, double(*f)(double), int(*next)(int)) { return accumulate(plus_op, 0, 1, n, f, next); } double product(int n, double(*f)(double), int(*next)(int)) { double s = 1; for(int i = 1; i <= n; i = next(i)) s *= f(i); return s; } double product2(int n, double(*f)(double), int(*next)(int)) { return accumulate(mult_op, 1, 1, n, f, next); } // не можем да подадем на expterm x // като параметър // затова използваме глобална променлива double X; double id(double x) { return x; } double fact(double n) { return accumulate(mult_op, 1, 1, n, id, plus1); } double expterm(double i) { return pow(X,i)/fact(i); } double myexp(int n, double x) { X = x; return accumulate(plus_op, 0, 0, n, expterm, plus1); } double N; double K; double binom_term(double i) { return (N-K+i)/i; } double binom(int n, int k) { N = n; K = k; return accumulate(mult_op, 1, 1, k, binom_term, plus1); } term_function choose_function(int n) { switch(n) { case 1: return sin; case 2: return cos; case 3: return exp; case 4: return log; default: return NULL; } } const double EPS = 1E-10; term_function F; double derive_result(double x) { return (F(x+EPS)-F(x))/EPS; } term_function derive(term_function f) { F = f; return derive_result; } int main() { cout << (void*)main << endl; cout << (void*)f << endl; void (*p)(int&,int&); p = swapi; cout << (void*)p << endl; cout << (void*)swapi << endl; int x = 1, y = 2; swapi(x, y); cout << x << ' ' << y << endl; p(x, y); cout << x << ' ' << y << endl; double (*op)(double) = sin; // func op; cout << (void*)op << endl; cout << op(0) << endl; op = cos; cout << (void*)op << endl; cout << op(0) << endl; op = NULL; cout << (void*)op << endl; typedef double (*math_fun)(double); math_fun op2 = sin; // !!! op = swapi; // !!! p = sin; typedef void (*simple_function)(); simple_function q = h; q(); void (*r)() = q; r(); simple_function* qq = &q; (*qq)(); void (**rr)() = &r; cout << sum(10, sin, plus1) << endl; cout << sum(10, cos, mult2) << endl; cout << accumulate(plus_op, 0, 1, 10, sin, plus1) << endl; cout << accumulate(plus_op, 0, 1, 10, cos, mult2) << endl; cout << fact(7) << endl; cout << myexp(20, 1) << endl; cout << binom(10, 3) << endl; int n; cout << "Изберете функция:"; cin >> n; /* term_function f = choose_function(n); cout << f(1) << endl; */ cout << choose_function(n)(1) << endl; cout << exp(2) << endl; /* term_function f = derive(exp); cout << f(2) << endl; */ cout << derive(exp)(2) << endl; return 0; }