Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ОПАМ.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
326.66 Кб
Скачать

Int main()

{

cout<<"Function pointers"<<endl;

cout<<"Enter lower,upper bounds and step"<<endl;

cin>>lower>>upper>>step;

Print(lower,upper,step,Polynom,"x^3-x+1");

Print(lower,upper,step,Exponent,"e^(x-3)");

Print(lower,upper,step,sin,"sin(x)");

system ("pause");

}

Приклад 5.6

/* Упорядкувати два числа */

#include <iostream>

using namespace std;

int n,m;

//================= упорядкування двох значень за спаданням =============

void swap(int &a, int &b)

{

if (a<b) a^=b^=a^=b;

}

//================= упорядкування двох значень за зростанням ============

void swap(int *a, int *b)

{

if (a<b) {int tmp=*a; *a=*b; *b=tmp;}

}

//========================== головна функція ===========================

Int main()

{

cout<<"Using pointer and reference parameters of function"<<endl;

cout<<"Enter integer n="; cin>>n;

cout<<"Enter integer m="; cin>>m;

swap(n,m);

cout<<"Descending ordering: n="<<n<<" m="<<m<<endl;

swap(&n,&m);

cout<<"Ascending ordering: n="<<n<<" m="<<m<<endl;

system("pause");

}

Приклад 5.7

/* Неконстантні покажчики на константні дані */

#include <iostream>

using namespace std;

//======================== виведення символів рядка ======================

void PrintString(const char* PtrStr)

{

for ( ;*PtrStr != 0; PtrStr++)

cout<< *PtrStr;

}

Int main ()

{

cout<<"Not const pointer to data const"<<endl;

char *string="C++ programming languare";

cout<<" String is:\"";

PrintString (string);

cout<<"\""<<endl;

system("pause");

}

Приклад 5.8

/* Пошук корення рівняння методом ділення відрізка навпіл */

#include <iostream>

#include <math.h>

using namespace std;

double precision=0.00001,x;

//============= обчислити значення функції в точці =================

double а(double param)

{

if (param==0)

param+=precision;

return exp(param)-1/param;

}

//=========== реалізація алгоритму половиного ділення ================

void solution(double* const a,double* const b)

{

do

{

x=(*a+*b)/2;

if (f(x)*f(*a)<0)

*b=x;

else *a=x;

}

while((*b-*a)>=precision);

}

//======================= головна функція ============================

Int main()

{

double left,right;

cout<<"const pointer to not const data"<<endl;

cout<<"Define root of e^x=1/x"<<endl;

cout<<"Input a,b"<<endl;

cin>>left>>right;

double* const pl=&left;

double* const pr=&right;

solution(pl,pr);

cout<<"root = "<<x<<endl;

system("pause");

}

Приклад 5.9

/* Константні покажчики на константні дані */

#include <iostream>

using namespace std;

int f(const int* const ptr)

{

return *ptr;

}

Int main ()

{

int x;

cout<<"Input integer value"<<endl; cin>>x;

const int* const ptrx = &x;

cout<<"Value "<<f(ptrx)<<" is saved at adress "<<ptrx<<endl;

system("pause");

}

Приклад 5.12

/* Таблиці степенів числа 2 та довжин введених рядків */

#include <iostream>

#include <string.h>

using namespace std;

char s[100];

const int n=10;

int x, len;

//============== визначення довжин рядка =============

int& value(char* p)

{

return len;

}

//============= доступ до елемента масиву ============

int& setx (int index)

{

return x;

}

//================== головна функція =================