rikardoricz

Tomasz Świątek 3BTI funkcje

Jan 25th, 2021 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.72 KB | None | 0 0
  1. //zad1
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int SUMA (int a, int b)
  7. {
  8.     int s=a+b;
  9.     return s;
  10. }
  11.  
  12. int main()
  13. {
  14.     int a,b;
  15.     cout << "Podaj a i b: ";
  16.     cin >> a >> b;
  17.     cout << SUMA(a,b) << endl;
  18.     return 0;
  19. }
  20.  
  21. //zad2
  22.  
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. int WARTOSC_BEZWZGLEDNA (int x)
  27. {
  28.     if (x<0)
  29.     {
  30.         x=x*-1;
  31.     }
  32.     return x;
  33. }
  34.  
  35. int main()
  36. {
  37.     int x;
  38.     cout << "Podaj liczbe: ";
  39.     cin >> x;
  40.     cout << WARTOSC_BEZWZGLEDNA (x) << endl;
  41.  
  42.     return 0;
  43. }
  44.  
  45. //zad3
  46.  
  47. #include <iostream>
  48. using namespace std;
  49.  
  50. bool CZY_PARZYSTA (int x)
  51. {
  52.     if (x%2==0)
  53.     {
  54.         return true;
  55.     }
  56.     else
  57.     {
  58.         return false;
  59.     }
  60. }
  61.  
  62. int main()
  63. {
  64.     int x;
  65.     cout << "Podaj liczbe zeby sprawdzic czy jest parzysta: ";
  66.     cin >> x;
  67.     cout << CZY_PARZYSTA (x) << endl;
  68.  
  69.     return 0;
  70. }
  71.  
  72. //zad4
  73.  
  74. #include <iostream>
  75. using namespace std;
  76.  
  77. void SZLACZEK (int x, char y)
  78. {
  79.     for (int i=0;i<x;i++)
  80.     {
  81.         cout << y;
  82.     }
  83. }
  84.  
  85. int main()
  86. {
  87.     int x;
  88.     char y;
  89.     cout << "Podaj dlugosc szlaczka: ";
  90.     cin >> x;
  91.     cout << "Podaj znak: ";
  92.     cin >> y;
  93.     SZLACZEK (x,y);
  94.  
  95.     return 0;
  96. }
  97.  
  98. //zad5
  99.  
  100. #include <iostream>
  101. using namespace std;
  102.  
  103. int x,y,s;
  104.  
  105. int POTEGA (int x, int y)
  106. {
  107.     int s=1;
  108.     for (int i=0;i<y;i++)
  109.     {
  110.         s=s*x;
  111.     }
  112.     return s;
  113. }
  114.  
  115. int main()
  116. {
  117.     cout << "Podaj podstawe potegi: ";
  118.     cin >> x;
  119.     cout << "Podaj wykladnik potegi: ";
  120.     cin >> y;
  121.     cout << POTEGA (x,y);
  122.  
  123.     return 0;
  124. }
  125.  
  126. //zad6
  127.  
  128. #include <iostream>
  129. using namespace std;
  130.  
  131. int a,b,c,ob;
  132.  
  133. int OBWOD_TROJKATA (int a, int b, int c)
  134. {
  135.     if (a<=0 || b<=0 || c<=0)
  136.     {
  137.         cout << "Nie da sie zbudowac trojkata, wartosc(i) sa ujemne";
  138.         return -1;
  139.     }
  140.     else if (a+b>c&&a+c>b&&b+c>a)
  141.     {
  142.         int ob=a+b+c;
  143.         return ob;
  144.     }
  145.     else
  146.     {
  147.         cout << "Nie da sie zbudowac trojkata";
  148.         return -1;
  149.     }
  150. }
  151.  
  152. int main()
  153. {
  154.     cout << "Podaj dlugosci bokow trojkata: ";
  155.     cin >> a >> b >> c;
  156.     cout << OBWOD_TROJKATA(a,b,c) <<endl;
  157.     return 0;
  158. }
  159.  
  160. //zad7
  161.  
  162. #include <iostream>
  163. using namespace std;
  164.  
  165. int n;
  166.  
  167. int SILNIA (int n)
  168. {
  169.     if (n<0)
  170.     {
  171.         cout << "Blad, niewlasciwe dane";
  172.         return 0;
  173.     }
  174.     else if (n>=0&&n<2)
  175.     {
  176.         return 1;
  177.     }
  178.     else
  179.     {
  180.         return n*SILNIA(n-1);
  181.     }
  182.  
  183. }
  184.  
  185. int main()
  186. {
  187.     cout << "Podaj liczbe zeby obliczyc silnie: ";
  188.     cin >> n;
  189.     cout << SILNIA(n);
  190.     return 0;
  191. }
  192.  
  193. //zad8
  194.  
  195. #include <iostream>
  196. using namespace std;
  197.  
  198. int a,b,c;
  199.  
  200. /*void MAX (int a,int b,int c)
  201. {
  202.     if (a>b)
  203.     {
  204.         if (b>c)
  205.         {
  206.             cout << "a: " << a << " b: " << b << " c: " << c;
  207.         }
  208.         else
  209.         {
  210.             cout << "a: " << a << " c: "<< c << " b: " <<b;
  211.         }
  212.  
  213.     }
  214.     else if (b>c)
  215.     {
  216.  
  217.         if (a>c)
  218.         {
  219.             cout << "b: " << b << " a: " << a << " c: " << c;
  220.         }
  221.         else
  222.         {
  223.             cout << "b: " << b << " c: " << c << " a: " << a;
  224.         }
  225.  
  226.     }
  227.     else
  228.     {
  229.         if (a>b)
  230.         {
  231.             cout << "c: " << c << " a: " << a << " b: " << b;
  232.         }
  233.         else
  234.         {
  235.             cout << "c: " << c << " b: " << b << " a: " << a;
  236.         }
  237.  
  238.     }
  239. }
  240. */
  241. int MAX(int a,int b,int c)
  242. {
  243.     if (a>b)
  244.     {
  245.         if (a>c)
  246.         {
  247.             return a;
  248.         }
  249.         else if (b>c)
  250.         {
  251.             return b;
  252.         }
  253.         else
  254.         {
  255.             return c;
  256.         }
  257.     }
  258.     else if (b>c)
  259.     {
  260.         return b;
  261.     }
  262.     else
  263.     {
  264.         return c;
  265.     }
  266. }
  267.  
  268. int main()
  269. {
  270.     cout << "Podaj 3 liczby: ";
  271.     cin >> a >> b >> c;
  272.     cout << MAX(a,b,c);
  273.     //MAX(a,b,c);
  274.  
  275.     return 0;
  276. }
  277.  
  278. //zad9
  279.  
  280. #include <iostream>
  281. using namespace std;
  282.  
  283. int n;
  284.  
  285. void DZIELNIKI (int n)
  286. {
  287.     if (n<0)
  288.     {
  289.         cout << "Podana liczba " << n << " nie jest liczba naturalna";
  290.     }
  291.     else
  292.     {
  293.     cout << "Dzielniki liczby " << n << ": ";
  294.     for (int i=n;i>0;i--)
  295.     {
  296.         if (n%i==0)
  297.         {
  298.             cout << i << " ";
  299.         }
  300.     }
  301.     }
  302. }
  303.  
  304. int main()
  305. {
  306.     cout << "Podaj liczbe zeby znalezc dzielniki: ";
  307.     cin >> n;
  308.     DZIELNIKI (n);
  309.  
  310.     return 0;
  311. }
  312.  
  313. //zad10
  314.  
  315. #include <iostream>
  316. using namespace std;
  317.  
  318. int a,b,x;
  319.  
  320. int ILORAZ(int a, int b)
  321. {
  322.     if (a<0||b<0)
  323.     {
  324.         x=-2;
  325.         return -2;
  326.     }
  327.     else if (b==0)
  328.     {
  329.         x=-1;
  330.         return -1;
  331.     }
  332.     else
  333.     {
  334.         x=a*b;
  335.         return x;
  336.     }
  337. }
  338.  
  339. int main()
  340. {
  341.     cout << "Podaj liczby zeby obliczyc ich iloraz: ";
  342.     cin >> a >> b;
  343.     cout << ILORAZ(a,b) << endl;
  344.     if (x==-1)
  345.     {
  346.         cout << "Nie dziel przez 0";
  347.     }
  348.     else if (x==-2)
  349.     {
  350.         cout << "Ktoras z podanych liczb nie jest liczba naturalna";
  351.     }
  352.  
  353.     return 0;
  354. }
  355.  
  356. //zad11
  357.  
  358. #include <iostream>
  359. #include <math.h>
  360. using namespace std;
  361.  
  362. int a,b,x;
  363.  
  364. void PODZIELNOSC(int a, int b)
  365. {
  366.     x=1;
  367.     for(int i=0; i<a-1; i++)
  368.     {
  369.         x=x*10;
  370.     }
  371.     cout << "Liczby " << a << " cyfrowe podzielne przez " << b << " to: ";
  372.     for (int i=x;i<x*10-1;i++)
  373.     {
  374.         if (i%b==0)
  375.         {
  376.             cout << i << " ";
  377.         }
  378.     }
  379.  
  380. }
  381.  
  382. int main()
  383. {
  384.     cout << "Podaj ilocyfrowe liczby cie interesuja: ";
  385.     cin >> a;
  386.     cout << "Podaj przez co maja byc podzielne: ";
  387.     cin >> b;
  388.     PODZIELNOSC(a,b);
  389.  
  390.     return 0;
  391. }
  392.  
  393. //zad12
  394.  
  395. #include <iostream>
  396. using namespace std;
  397.  
  398. int x;
  399.  
  400. int LICZBA_CYFR(int x)
  401. {
  402.     int z=x;
  403.     int i=0;
  404.     if (x<0)
  405.     {
  406.         cout << "Podana liczba nie jest liczba naturalna";
  407.     }
  408.     else
  409.     {
  410.         for (i;x>0;i++)
  411.         {
  412.             x=x/10;
  413.         }
  414.         cout << "Ilosc cyfr liczby " << z <<": ";
  415.         return i;
  416.     }
  417. }
  418.  
  419. int main()
  420. {
  421.     cout << "Podaj liczbe naturalna: ";
  422.     cin >> x;
  423.     cout << LICZBA_CYFR(x);
  424.     return 0;
  425. }
  426.  
  427. //zad13
  428.  
  429. #include <iostream>
  430. using namespace std;
  431.  
  432. int x;
  433.  
  434. void SUMA_CYFR(int x)
  435. {
  436.     /*if(x>0)
  437.         return x%10 + SUMA_CYFR(x/10);
  438.  
  439.     return 0;
  440.     */
  441.     if (x<0)
  442.     {
  443.         cout << "Podana liczba nie jest liczba naturalna" << endl;
  444.     }
  445.     else
  446.     {
  447.         do
  448.         {
  449.             int s=0;
  450.             while (x>0)
  451.             {
  452.                 s+=x%10;
  453.                 x/=10;
  454.             };
  455.             x=s;
  456.             cout << "Suma cyfr wynosi: " << x << endl;
  457.         }
  458.         while (x>9);
  459.         cout << "Suma ostateczna wynosi: " << x << endl;
  460.     }
  461. }
  462.  
  463. int main()
  464. {
  465.     cout << "Podaj liczbe: ";
  466.     cin >> x;
  467.     SUMA_CYFR(x);
  468.  
  469.     return 0;
  470. }
  471.  
  472. //zad14,15
  473.  
  474. #include <iostream>
  475. #include <math.h>
  476. using namespace std;
  477.  
  478. float POLE_TROJKATA(float a,float h)
  479. {
  480.     if (a<=0||h<=0)
  481.     {
  482.         cout << "Niewlasciwa dlugosc podstawy lub wysokosci, ";
  483.         return 0;
  484.     }
  485.     else
  486.     {
  487.         float p=(a*h)/2;
  488.         cout << "Pole trojkata wynosi: ";
  489.         return p;
  490.     }
  491. }
  492.  
  493. float POLE_TROJKATA(float x1,float y1,float x2,float y2,float x3,float y3)
  494. {
  495.     float a,b,c,p,s;
  496.     a=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
  497.     b=sqrt(pow(x2-x3,2)+pow(y2-y3,2));
  498.     c=sqrt(pow(x1-x3,2)+pow(y1-y3,2));
  499.  
  500.     if (a<0||b<0||c<0||a+b>c||a+c>b||b+c>a)
  501.     {
  502.         p=(a+b+c)*0.5;
  503.         s=sqrt(p*(p-a)*(p-b)*(p-c));
  504.         //s = sqrt(p * (p-a) * (p-b) * (p-c));
  505.         cout << "Pole trojkata wynosi: " << s;
  506.     }
  507.     else
  508.     {
  509.         cout << "Nie da sie zbudowac trojkata";
  510.     }
  511. }
  512.  
  513. int main()
  514. {
  515.     int f;
  516.     float a,h,x1,x2,x3,y1,y2,y3;
  517.     cout << "Wywolane funkcje dla przykladowych wartosci: " << endl;
  518.     POLE_TROJKATA(-1,0,1,2,1,-1);
  519.     cout << endl;
  520.     POLE_TROJKATA(5,1,-2,3,0,-2);
  521.     cout << endl;
  522.     cout << POLE_TROJKATA(10,5) << endl;
  523.     cout << POLE_TROJKATA(0,2) << endl;
  524.     cout << POLE_TROJKATA(9,3) << endl;
  525.     cout << POLE_TROJKATA(5,-2) << endl;
  526.     cout << endl << "--------------------------------------" << endl;
  527.     cout << "Liczenie pola trojkata z podanej wysokosci i podstawy - wpisz 1" << endl << "Liczenie pola trojkata z wspolrzednych - wpisz 2" << endl;
  528.     cin >> f;
  529.     if (f==1)
  530.     {
  531.         cout << "Podaj dlugosc podstawy i wysokosc: ";
  532.         cin >> a >> h;
  533.         cout << POLE_TROJKATA(a,h);
  534.     }
  535.     else if (f==2)
  536.     {
  537.         cout << "Podaj wspolrzedne:" << endl;
  538.         cout << "punktu A, x i y: ";
  539.         cin >> x1 >> y1;
  540.         cout << "punktu B, x i y: ";
  541.         cin >> x2 >> y2;
  542.         cout << "punktu C, x i y: ";
  543.         cin >> x3 >> y3;
  544.         cout << POLE_TROJKATA(x1,y1,x2,y2,x3,y3);
  545.     }
  546.     else
  547.     {
  548.         cout << "Podano niewlasciwa wartosc";
  549.     }
  550.  
  551.     return 0;
  552. }
  553.  
Add Comment
Please, Sign In to add comment