Advertisement
Talar97

[JPO] Lab11

May 28th, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. //Prototypy
  9. void WyborZadania();
  10. void Zad1();
  11. void Zad2();
  12. int Zad3a(int *a, int *b);
  13. int *Zad3b(int *a, int *b);
  14. int Zad4(int *tab, int n);
  15. int Zad5(int *tab1, int *tab2, int n);
  16.  
  17. int main(){
  18.     WyborZadania();
  19.     return EXIT_SUCCESS;
  20. }
  21.  
  22. void Zad1() {
  23.     int n, x;
  24.     cout << "Podaj n:"; cin >> n;
  25.  
  26.     int *tab = new int[n];
  27.     for (int i = 0; i < n; i++) {
  28.             cout << i + 1 << ": "; cin >> x;
  29.             if (!isdigit(x)) tab[i] = x;
  30.             else i--;
  31.     }
  32.        
  33.         for(int i = 0; i < n; i++){
  34.             cout << tab[i] << ", ";
  35.         }
  36.  
  37. }
  38.  
  39. void Zad2(){
  40.  
  41. }
  42.  
  43. int Zad3a(int *a, int *b){
  44.     if(*a < *b) return *a;
  45.     else return *b;
  46. }
  47.  
  48. int *Zad3b(int *a, int *b){
  49.     if(*a < *b) return a;
  50.     else return b;
  51. }
  52.  
  53. int Zad4(int *tab, int n){
  54.     int suma = 0;
  55.     for(int i = 0; i < n; i++){
  56.         suma += tab[i];
  57.     }
  58.    
  59.     return suma/n;
  60. }
  61.  
  62. int Zad5(int *tab1, int *tab2, int n){
  63.     int suma = 0;
  64.     for(int i = 0; i < n; i++){
  65.         cout << "(" <<tab1[i] << " - " << tab2[i] << ")^2 = " << (tab1[i] - tab2[i])*(tab1[i] - tab2[i]) << endl;
  66.         suma += (tab1[i] - tab2[i])*(tab1[i] - tab2[i]);
  67.     }
  68.    
  69.     return suma;
  70. }
  71.  
  72. class portfel{
  73. private:
  74.     double kwota;
  75. public:
  76.     portfel(){
  77.         this->inicjuj();
  78.     }
  79.    
  80.     void inicjuj(){
  81.         this->kwota = 0;
  82.     }
  83.    
  84.     void zarobki(double ilosc){
  85.         this->kwota += ilosc;
  86.     }
  87.    
  88.     void wydatki(double ilosc){
  89.         this->kwota -= ilosc;
  90.     }
  91.    
  92.     double zawartosc(){
  93.         return this->kwota;
  94.     }
  95. };
  96.  
  97. class tablica{
  98. private:
  99.     int * tab;
  100.     int n;
  101. public:
  102.     tablica(int n){
  103.         this->n = n;
  104.         tab = new int[n];
  105.         for(int i = 0; i < n; i++){
  106.             tab[i] = rand() % 100 + 1;
  107.             cout << tab[i] << ", ";
  108.         }
  109.     }
  110.    
  111.     int & at(int indeks){
  112.         if(indeks > n) {int & ref = tab[0]; return ref; }
  113.         else{
  114.             for(int i = 0; i < n; i++){
  115.                 if(i == indeks){
  116.                     int & ref = tab[i];
  117.                     return ref;
  118.                 }
  119.             }
  120.         }
  121.     }
  122.    
  123. };
  124.  
  125. class wskaznik{
  126. private:
  127.     int * wsk;
  128.    
  129. public:
  130.     wskaznik(int n){
  131.         this->utworz(n);
  132.     }
  133.    
  134.     void utworz(int n){
  135.         if(n > 0){
  136.             int * tab = new int[n];
  137.             this->wsk = tab;
  138.         }
  139.     }
  140.    
  141.     int *zwroc(){
  142.         return this->wsk;
  143.     }
  144.    
  145.     void zwolnij(){
  146.         this->wsk = NULL;
  147.     }
  148.    
  149.     void kopiuj(int* &ref){
  150.         ref = this->wsk;
  151.     }
  152. };
  153.  
  154. class czworokat{
  155. protected:
  156.     double a, b, c, d;
  157. public:
  158.     czworokat(double a, double b, double c, double d){
  159.         this->a = a;
  160.         this->b = b;
  161.         this->c = c;
  162.         this->d = d;
  163.     }
  164.    
  165.     void wypisz(){
  166.         cout << a << ", " << b << ", " << c << ", " << d << endl;
  167.     }
  168.    
  169.     virtual double pole(){
  170.         return a*b*c*d;
  171.     }
  172. };
  173.  
  174. class prostokat : public czworokat{
  175. public:
  176.     prostokat(double x1, double x2) : czworokat(x1, x1, x2, x2){}
  177.    
  178.     void wymiary(double w1, double w2){
  179.         czworokat::a = w1;
  180.         czworokat::c = w1;
  181.        
  182.         czworokat::b = w2;
  183.         czworokat::d = w2;
  184.     }
  185.    
  186.     double pole(){
  187.         return a * c;
  188.     }
  189. };
  190.  
  191. class kwadrat : public czworokat{
  192. public:
  193.     kwadrat(double x1) : czworokat(x1, x1, x1, x1){}
  194.    
  195.     void wymiary(double w1){
  196.         czworokat::a = w1;
  197.         czworokat::b = w1;
  198.         czworokat::c = w1;
  199.         czworokat::d = w1;
  200.     }
  201.    
  202.     double pole(){
  203.         return a * a;
  204.     }
  205. };
  206.  
  207. void WyborZadania() {
  208.     int zad;
  209.     cout << "Wybierz zadanie: ";
  210.     cin >> zad;
  211.     switch (zad) {
  212.     case 1:
  213.     {
  214.             Zad1();
  215.             break;
  216.     }
  217.         case 2:
  218.         {
  219.             Zad2();
  220.             break;
  221.         }
  222.         case 3:
  223.         {
  224.             int x = 10;
  225.             int y = 15;
  226.             int *pX = &x;
  227.             int *pY = &y;
  228.            
  229.             cout << Zad3a(pX, pY) << endl;
  230.             cout << *Zad3b(pX, pY);
  231.             break;
  232.         }
  233.         case 4:
  234.         {
  235.             int n = 10;
  236.             int *tab = new int[n];
  237.             int *tab2 = new int[n];
  238.            
  239.             srand(time(NULL));
  240.            
  241.             for(int i = 0; i < n; i++){
  242.                 tab[i] = rand() % 100 + 1;
  243.                 cout << tab[i] << ", ";
  244.             }
  245.             cout << endl;
  246.             for(int i = 0; i < n; i++){
  247.                 tab2[i] = rand() % 100 + 1;
  248.                 cout << tab2[i] << ", ";
  249.             }
  250.             cout << endl << "Srednia: " << Zad4(tab, n) << endl << "Srednia 2: " <<Zad4(tab2, n) << endl;
  251.             break;
  252.         }
  253.         case 5:
  254.         {
  255.             int n = 10;
  256.             int *tab = new int[n];
  257.             int *tab2 = new int[n];
  258.            
  259.             srand(time(NULL));
  260.            
  261.             for(int i = 0; i < n; i++){
  262.                 tab[i] = rand() % 100 + 1;
  263.                 cout << tab[i] << ", ";
  264.             }
  265.             cout << endl;
  266.             for(int i = 0; i < n; i++){
  267.                 tab2[i] = rand() % 100 + 1;
  268.                 cout << tab2[i] << ", ";
  269.             }
  270.            
  271.             cout << endl << Zad5(tab, tab2, n) << endl;
  272.             break;
  273.         }
  274.         case 6:
  275.         {
  276.             portfel x;
  277.             x.zarobki(500);
  278.             x.wydatki(150);
  279.             cout << x.zawartosc();
  280.             break;
  281.         }
  282.         case 7:
  283.         {
  284.             tablica t(10);
  285.             cout << endl << t.at(5) << ", " << t.at(15);
  286.             break;
  287.         }
  288.         case 8:
  289.         {
  290.             //(int*) &ref;
  291.             wskaznik x(10);
  292.             cout << x.zwroc();
  293.             //x.kopiuj(ref); //nie dziala chuj wie czemu
  294.             x.zwolnij();
  295.             break;
  296.         }
  297.         case 9:
  298.         {
  299.             //Normalnie
  300.             czworokat x(5,10,15,20);
  301.             x.wypisz();
  302.             cout << x.pole() << endl;
  303.            
  304.             prostokat y(10, 20);
  305.             y.wypisz();
  306.             cout << y.pole() << endl;
  307.            
  308.             kwadrat z(10);
  309.             z.wypisz();
  310.             cout << z.pole() << endl;
  311.            
  312.             cout << "--------" << endl;
  313.             //Na wskaznikach
  314.             czworokat * a = new czworokat(5,10,15,20);
  315.             a->wypisz();
  316.             cout << a->pole() << endl;
  317.            
  318.             prostokat * b = new prostokat(10,20);
  319.             b->wypisz();
  320.             cout << b->pole() << endl;
  321.            
  322.             kwadrat * c = new kwadrat(10);
  323.             c->wypisz();
  324.             cout << c->pole() << endl;
  325.             break;
  326.         }
  327.         case 10:
  328.         {
  329.             list <int> lista;
  330.             int el;
  331.             cout << "Ile elementow chcesz dodac do listy? "; cin >> el;
  332.             for(int i = 0; i < el; i++){
  333.                 int x;
  334.                 cout << i + 1 << ": "; cin >> x;
  335.                 lista.push_back(x);
  336.             }
  337.            
  338.            
  339.             //Wyswietlanie listy
  340.             for(list<int>::iterator i = lista.begin(); i != lista.end(); i++){
  341.                 cout << *i << ", ";
  342.             }
  343.            
  344.             cout << endl;
  345.            
  346.             //Usuwanie
  347.             int del, j;
  348.             cout << "Ktory element chcesz usunac? "; cin >> del;
  349.             for(list<int>::iterator i = lista.begin(); i != lista.end(); i++, j++){
  350.                 if(j==del%lista.size()){
  351.                     i = lista.erase(i); break;
  352.                 }
  353.             }
  354.            
  355.             //Wyswietlanie listy 2
  356.             for(list<int>::iterator i = lista.begin(); i != lista.end(); i++){
  357.                 cout << *i << ", ";
  358.             }
  359.            
  360.             break;
  361.         }
  362.         default:
  363.     {
  364.             WyborZadania();
  365.             break;
  366.     }
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement