limun11

PRII - Pokazivaci na funkcije - Vjezbe 4

Mar 17th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #pragma warning (disable:4996)
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void uvecaj(int* a, int *b)
  6. {
  7.     (*a)++;
  8.     (*b)++;
  9.  
  10.     cout << "U funkciji uvecaj vrijednosti varijabli su: " << endl;
  11.     cout << "a: " << *a << endl;
  12.     cout << "b: " << *b << endl;
  13.     cout << "-------------------------" << endl;
  14.  
  15. }
  16. int main()
  17. {
  18.     int a = 6;
  19.     int b = 7;
  20.     cout << "Main prije poziva funkcije uvecaj: " << endl;
  21.     cout << "a: " << a << endl;
  22.     cout << "b: " << b << endl;
  23.     cout << "-------------" << endl;
  24.  
  25.     uvecaj(&a, &b);
  26.  
  27.     cout << "Main proslije poziva funkcije uvecaj: " << endl;
  28.     cout << "a: " << a << endl;
  29.     cout << "b: " << b << endl;
  30.     system("PAUSE");
  31.     return 0;
  32. }
  33.  
  34. /////
  35.  
  36. #pragma warning (disable:4996)
  37. #include <iostream>
  38. using namespace std;
  39.  
  40. int main()
  41. {
  42.     int a = 15;
  43.     int & refA = a;
  44.  
  45.     cout << "Vrijednost varijable a: " << a << endl;
  46.     cout << "Vrijednost reference ref: " << refA << endl;
  47.  
  48.     cout << "---------------------------------" << endl;
  49.  
  50.     refA = 10;
  51.     cout << "refA=10" << endl;
  52.  
  53.     cout << "-----------------------------" << endl;
  54.  
  55.     cout << "Vrijednost varijable a je: " << a << endl;
  56.     cout << "Vrijednost reference ref je: " << refA << endl;
  57.  
  58.     cout << "-----------------------------------" << endl;
  59.  
  60.     a++;
  61.     cout << "a++ " << endl;
  62.  
  63.     cout << "---------------------------------" << endl;
  64.  
  65.     cout << "Vrijednost varijable a je: " << a << endl;
  66.     cout << "Vrijednost reference ref je: " << refA << endl;
  67.  
  68.     cout << "-----------------------------------" << endl;
  69.  
  70.     system("PAUSE");
  71.     return 0;
  72. }
  73.  
  74. /////
  75.  
  76. #pragma warning (disable:4996)
  77. #include <iostream>
  78. using namespace std;
  79.  
  80. void uvecaj(int & ref)
  81. {
  82.     cout << "Unutar funkcije uvećaj: " << endl;
  83.     ref++;
  84.  
  85.     cout << "ref++" << endl;
  86.     cout << "adresa varijable ref: " << &ref << endl;
  87.     cout << "Vrijednost varijable ref: " << ref << endl;
  88.     cout << "-----------------------" << endl;
  89. }
  90. int main()
  91. {
  92.     int a = 5;
  93.     cout << "Main prije poziva funkcije uvecaj: " << endl;
  94.     cout << "Adresa varijable a: " << &a << endl;
  95.     cout << "Vrijednost varijable a: " << a << endl;
  96.  
  97.     cout << "-----------------------------" << endl;
  98.  
  99.     uvecaj(a);
  100.  
  101.     cout << "main poslije poziva funckije uvecaj: " << endl;
  102.     cout << "adresa varijable a: " << &a << endl;
  103.     cout << "vrijednost varijable a: " << a << endl;
  104.  
  105.     cout << "------------------------" << endl;
  106.  
  107.  
  108.     system("PAUSE");
  109.     return 0;
  110. }
  111.  
  112. /////
  113.  
  114. #pragma warning (disable:4996)
  115. #include <iostream>
  116. using namespace std;
  117.  
  118. bool provjeri(int broj)
  119. {
  120.     if (broj >= 10)
  121.         return true;
  122.     else
  123.         return false;
  124. }
  125. int main()
  126. {
  127.     int broj = 0;
  128.     bool(*pFunkacija)(int) = nullptr;
  129.     pFunkacija = &provjeri;
  130.  
  131.     cout << "Unesite neki broj: ";
  132.     cin >> broj;
  133.  
  134.     bool odgovor = (*pFunkacija)(broj);
  135.  
  136.     if (odgovor == true)
  137.         cout << "Broj je veci ili jednak 10!" << endl;
  138.     else
  139.         cout << "Broj je manje od 10!" << endl;
  140.     system("PAUSE");
  141.     return 0;
  142. }
  143.  
  144.  
  145. /////
  146.  
  147. #pragma warning (disable:4996)
  148. #include <iostream>
  149. using namespace std;
  150.  
  151. int saberi(int a, int b)
  152. {
  153.     return a + b;
  154.    
  155. }
  156. int main()
  157. {
  158.     int a = 6;
  159.     int b = 7;
  160.  
  161.     int(*pFunkcija)(int, int) = &saberi;
  162.     if (pFunkcija == &saberi)
  163.         cout << "Zbir brojeva je: " << (*pFunkcija)(a, b) << endl;
  164.     else
  165.         cout << "Greska pri pozivanju programa!" << endl;
  166.     system("PAUSE");
  167.     return 0;
  168. }
  169.  
  170.  
  171. /////
  172.  
  173. #pragma warning (disable:4996)
  174. #include <iostream>
  175. using namespace std;
  176.  
  177. int naKvadrat(int broj)
  178. {
  179.     return broj*broj;
  180. }
  181. int naKub(int broj)
  182. {
  183.     return broj*broj*broj;
  184. }
  185. void centralna(int broj, int(*pFunkcija)(int))
  186. {
  187.     int rezultat = (*pFunkcija)(broj);
  188.     cout << "Rezultat je: " << rezultat << endl;
  189. }
  190. int main()
  191. {
  192.     int broj = 0;
  193.     int(*pFunkcija)(int) = nullptr;
  194.  
  195.     cout << "Unesite neki broj: ";
  196.     cin >> broj;
  197.  
  198.     if (broj < 10)
  199.         pFunkcija = &naKub;
  200.     else
  201.         pFunkcija = &naKvadrat;
  202.  
  203.     centralna(broj, pFunkcija);
  204.     system("PAUSE");
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment