Advertisement
bartkoo

zadania funkcje

Jan 29th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. float SUMA(float a, float b);
  9. float WARTOSC_BEZWZGLEDNA(float a);
  10. bool CZY_PARZYSTA(int a);
  11. void SZLACZEK(int d, char zn);
  12. float POTEGA(float x, int y);
  13. float OBWOD_TROJKATA(float a, float b, float c);
  14. unsigned long long SILNIA(int a);
  15. int MAX(int a, int b, int c);
  16. vector <int> DZIELNIKI(int a);
  17. float ILORAZ(int a, int b);
  18. void PODZIELNOSC(int a=0, float b=0.0);
  19. int LICZBA_CYFR(int a);
  20. int SUMA_CYFR(int a);
  21. auto POLE_TROJKATA(float a, float h) -> decltype(a*h/2) {return a*h/2;}
  22. float POLE_TROJKATA(float x1, float y1, float x2, float y2, float x3, float y3);
  23.  
  24. int main()
  25. {
  26.     // ZADANIE 1
  27.  
  28.     cout<<"Zadanie 1"<<endl;
  29.     cout<<SUMA(358.23, 34.23)<<endl;
  30.     cout<<SUMA(11, 74.33)<<endl;
  31.     cout<<SUMA(547.343, 34.23)<<endl;
  32.  
  33.     // ZADANIE 2
  34.  
  35.     cout<<"Zadanie 2"<<endl;
  36.     cout<<WARTOSC_BEZWZGLEDNA(-4)<<endl;
  37.     cout<<WARTOSC_BEZWZGLEDNA(-79.34)<<endl;
  38.     cout<<WARTOSC_BEZWZGLEDNA(10)<<endl;
  39.  
  40.     // ZADANIE 3
  41.  
  42.     cout<<"Zadanie 3"<<endl;
  43.     cout<<CZY_PARZYSTA(15)<<endl;
  44.     cout<<CZY_PARZYSTA(14)<<endl;
  45.     cout<<CZY_PARZYSTA(-18)<<endl;
  46.  
  47.     // ZADANIE 4
  48.  
  49.     cout<<"Zadanie 4"<<endl;
  50.     SZLACZEK(7, 'x');
  51.     cout<<endl;
  52.     SZLACZEK(21, 'p');
  53.     cout<<endl;
  54.     SZLACZEK(-11, 'g');
  55.     cout<<endl;
  56.  
  57.     // ZADANIE 5
  58.  
  59.     cout<<"Zadanie 5"<<endl;
  60.     cout<<POTEGA(-15, 2)<<endl;
  61.     cout<<POTEGA(-17, 3)<<endl;
  62.     cout<<POTEGA(10.97, 7)<<endl;
  63.     cout<<POTEGA(2, 10)<<endl;
  64.  
  65.     // ZADANIE 6
  66.     cout<<"Zadanie 6"<<endl;
  67.     float tab[3] = {OBWOD_TROJKATA(1,3,7), OBWOD_TROJKATA(-8, 10, 12), OBWOD_TROJKATA(1,3,3)};
  68.     for(int i=0;i<(end(tab) - begin(tab));i++){
  69.         if(tab[i]==-1){
  70.             cout<<"Nie da sie utworzyc trojkata!"<<endl;
  71.         }else{
  72.             cout<<"Obwod trojkata wynosi: "<<tab[i]<<endl;
  73.         }
  74.     }
  75.  
  76.     // ZADANIE 7
  77.  
  78.     cout<<"Zadanie 7"<<endl;
  79.     cout<<SILNIA(0)<<endl;
  80.     cout<<SILNIA(6)<<endl;
  81.     cout<<SILNIA(20)<<endl;
  82.  
  83.     // ZADANIE 8
  84.  
  85.     cout<<"Zadanie 8"<<endl;
  86.     cout<<MAX(2, 7, 10)<<endl;
  87.     cout<<MAX(-2, 7, 15)<<endl;
  88.     cout<<MAX(8, 8, 9)<<endl;
  89.     cout<<MAX(8, 9, 8)<<endl;
  90.     cout<<MAX(9, 8, 8)<<endl;
  91.  
  92.     // ZADANIE 9
  93.  
  94.     cout<<"Zadanie 9"<<endl;
  95.     vector <int> tab_view;
  96.  
  97.     tab_view = DZIELNIKI(10);
  98.     for(vector<int>::const_iterator i = tab_view.begin(); i != tab_view.end(); i++){
  99.         cout<<*i<<" ";
  100.     }
  101.     cout<<endl;
  102.  
  103.     tab_view = DZIELNIKI(180);
  104.     for(vector<int>::const_iterator i = tab_view.begin(); i != tab_view.end(); i++){
  105.         cout<<*i<<" ";
  106.     }
  107.     cout<<endl;
  108.  
  109.     tab_view = DZIELNIKI(238474);
  110.     for(vector<int>::const_iterator i = tab_view.begin(); i != tab_view.end(); i++){
  111.         cout<<*i<<" ";
  112.     }
  113.     cout<<endl;
  114.  
  115.     // ZADANIE 10
  116.  
  117.     cout<<"Zadanie 10"<<endl;
  118.     float tab2[3] = {ILORAZ(0, -2), ILORAZ(2, 0), ILORAZ(3, 4)};
  119.     for(int i=0;i<(end(tab2) - begin(tab2));i++){
  120.         if(tab2[i]==-2){
  121.             cout<<"Conajmniej jedna podana liczba nie jest naturalna!"<<endl;
  122.         }else if(tab2[i]==-1){
  123.             cout<<"Nie mozna dzielic przez zero!"<<endl;
  124.         }else{
  125.             cout<<tab2[i]<<endl;
  126.         }
  127.     }
  128.  
  129.     // ZADANIE 11 - wersja tymczasowa !!!!
  130.  
  131.     cout<<"Zadanie 11"<<endl;
  132.     PODZIELNOSC();
  133.     cout<<endl;
  134.  
  135.     // ZADANIE 12
  136.  
  137.     cout<<"Zadanie 12"<<endl;
  138.     cout<<LICZBA_CYFR(123456)<<endl;
  139.     cout<<LICZBA_CYFR(123456789)<<endl;
  140.     cout<<LICZBA_CYFR(123)<<endl;
  141.  
  142.     // ZADANIE 13
  143.  
  144.     cout<<"Zadanie 13"<<endl;
  145.     cout<<SUMA_CYFR(123)<<endl;
  146.     cout<<SUMA_CYFR(123456)<<endl;
  147.     cout<<SUMA_CYFR(123456789)<<endl;
  148.  
  149.     int suma = SUMA_CYFR(1234567891);
  150.     while(true){
  151.         if (to_string(suma).length()>1){
  152.             suma = SUMA_CYFR(suma);
  153.         }else{
  154.             cout<<"Wynik jednocyforwy sumy: "<<suma<<endl;
  155.             break;
  156.         }
  157.     }
  158.  
  159.     // ZADANIE 14
  160.  
  161.     cout<<"Zadanie 14"<<endl;
  162.     cout<<POLE_TROJKATA(10, 6)<<endl;
  163.     cout<<POLE_TROJKATA(17.77, 1.23)<<endl;
  164.     cout<<POLE_TROJKATA(12, 17)<<endl;
  165.  
  166.     // ZADANIE 15
  167.  
  168.     cout<<"Zadanie 15"<<endl;
  169.     cout<<POLE_TROJKATA(1,1,4,1,1,5)<<endl;
  170.     cout<<POLE_TROJKATA(6,7,8,9,12,51)<<endl;
  171.     cout<<POLE_TROJKATA(0,1,4,7,1,5)<<endl;
  172.  
  173.  
  174.     return 0;
  175. }
  176.  
  177. float SUMA(float a, float b){
  178.     return a + b;
  179. }
  180.  
  181. float WARTOSC_BEZWZGLEDNA(float a){
  182.     if (a < 0) {
  183.         a = -a;
  184.     }
  185.     return a;
  186. }
  187.  
  188. bool CZY_PARZYSTA(int a){
  189.     if (a % 2 == 0){
  190.         return true;
  191.     }else{
  192.         return false;
  193.     }
  194. }
  195.  
  196. void SZLACZEK(int d, char zn){
  197.     for(d;d>0;d--){
  198.         cout<<zn;
  199.     }
  200. }
  201.  
  202. float POTEGA(float x, int y){
  203.     if (y > 0) return x * POTEGA(x, y-1);
  204.     else return 1;
  205. }
  206.  
  207. float OBWOD_TROJKATA(float a, float b, float c){
  208.     if((a+b>c)&&(b+c>a)&&(a+c>b)){
  209.         return a+b+c;
  210.     }else{
  211.         return -1;
  212.     }
  213. }
  214.  
  215. unsigned long long SILNIA(int a){
  216.     if(a>1){
  217.         return a * SILNIA(a-1);
  218.     }else{
  219.         return 1;
  220.     }
  221. }
  222.  
  223. int MAX(int a, int b, int c){
  224.     if((a>b)&&(a>c)){
  225.         return a;
  226.     }else if(b>c){
  227.         return b;
  228.     }else{
  229.         return c;
  230.     }
  231. }
  232.  
  233. vector <int> DZIELNIKI(int a){
  234.     vector <int> tab;
  235.     for(int i=1;i<=a;i++){
  236.         if(a%i == 0){
  237.             tab.push_back(i);
  238.         }
  239.     }
  240.     return tab;
  241. }
  242.  
  243. float ILORAZ(int a, int b){
  244.     if((a>0)&&(b>0)){
  245.         return float(a)/b;
  246.     }else if(b==0){
  247.         return -1;
  248.     }else{
  249.         return -2;
  250.     }
  251. }
  252.  
  253. void PODZIELNOSC(int a, float b){
  254.  
  255.     if(a == 0){
  256.         cout<<"Podaj ilocyfrowe liczby Cie interesuja: ";
  257.         cin>>a;
  258.     }
  259.     if (b == 0.0){
  260.         cout<<"Podaj, przez co maja byc podzielne: ";
  261.         cin>>b;
  262.     }
  263.  
  264.     if((a>0)&&(b!=0)){
  265.  
  266.         int max_c, min_c;
  267.  
  268.         if(b>0){
  269.             max_c = 9;
  270.             min_c = 1;
  271.  
  272.             for(int i=2;i<=a;i++){
  273.                 max_c = max_c * 10 + 9;
  274.                 min_c = min_c * 10;
  275.             }
  276.  
  277.             cout<<"Twoje liczby to: ";
  278.             for(int i=ceil(min_c/b);true;i++){
  279.                 if(i*b>max_c){
  280.                     break;
  281.                 }else{
  282.                     cout<<i*b<<",";
  283.                 }
  284.             }
  285.  
  286.         }else if(b<0){
  287.             max_c = -9;
  288.             min_c = -1;
  289.  
  290.             for(int i=2;i<=a;i++){
  291.                 max_c = max_c * 10 - 9;
  292.                 min_c = min_c * 10;
  293.             }
  294.  
  295.             cout<<"Twoje liczby to: ";
  296.             for(int i=ceil(min_c/b);true;i++){
  297.                 if(i*b<max_c){
  298.                     break;
  299.                 }else{
  300.                 cout<<i*b<<",";
  301.                 }
  302.             }
  303.         }
  304.     }else{
  305.         cout<<"Wprowadzone paramety sa nieprawidlowe";
  306.     }
  307. }
  308.  
  309. int LICZBA_CYFR(int a){
  310.     string b = to_string(a);
  311.     return b.length();
  312. }
  313.  
  314. int SUMA_CYFR(int a){
  315.     int suma = 0;
  316.     int breaker = to_string(a).length();
  317.     for(int i=1;i<breaker;i++){
  318.         suma = suma + a%10;
  319.         a = a/10;
  320.     }
  321.     return suma + a;
  322. }
  323.  
  324. float POLE_TROJKATA(float x1, float y1, float x2, float y2, float x3, float y3){
  325.     float a = pow(pow((x1 - x2 > 0) ? x1 - x2 : - (x1 - x2),2)+pow((y1 - y2 > 0) ? y1 - y2 : - (y1 - y2),2),0.5);
  326.     float b = pow(pow((x2 - x3 > 0) ? x2 - x3 : - (x2 - x3),2)+pow((y2 - y3 > 0) ? y2 - y3 : - (y2 - y3),2),0.5);
  327.     float c = pow(pow((x3 - x1 > 0) ? x3 - x1 : - (x3 - x1),2)+pow((y3 - y1 > 0) ? y3 - y1 : - (y3 - y1),2),0.5);
  328.  
  329.     float p = (a + b + c) / 2;
  330.  
  331.     return pow(p*(p-a)*(p-b)*(p-c),0.5);
  332. }
  333.  
  334.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement