Advertisement
piotrek32110

ostatnie

Dec 8th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. // labkiostatnie.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8. #include <iomanip>
  9. #include <map>
  10.  
  11. using namespace std;
  12.  
  13. class Person {
  14. private:
  15.     int pesel;
  16.     string imie;
  17.     string nazwisko;
  18. public:
  19.     Person(int pesell = 0, string imiee = "null", string nazwiskoo = "null") : pesel(pesell), imie(imiee), nazwisko(nazwiskoo) {
  20.         cout << "Test1";
  21.     }
  22.     virtual string tostring()
  23.     {
  24.         return (imie + " " + nazwisko + " Pesel:" + to_string(pesel) + "\n");
  25.     }
  26.     int get_pesel()
  27.     {
  28.         return pesel;
  29.     }
  30.     string get_imie()
  31.     {
  32.         return imie;
  33.     }
  34.     string get_nazwisko()
  35.     {
  36.         return nazwisko;
  37.     }
  38.  
  39.  
  40. };
  41. class Student : public Person {
  42. private:
  43.     int nr;
  44.     string kierunek;
  45. public:
  46.     Student(int pesell = 0, string imiee = "null", string nazwiskoo = "null", int nrr = 0, string kierunekk = "null") : Person(pesell, imiee, nazwiskoo), nr(nrr), kierunek(kierunekk){
  47.         cout << "Test2";
  48.     }
  49.     string tostring()
  50.     {
  51.         return (get_imie() + " " + get_nazwisko() + " Pesel:" + to_string(get_pesel()) + " Nr indeksu:" + to_string(nr) + " Kierunek:" + kierunek + "\n");
  52.     }
  53.  
  54.  
  55. };
  56. class InvalidValue {
  57. public:
  58.     const char* what() {
  59.         return "Value error";
  60.     }
  61. };
  62. class InvalidIndex {
  63. public:
  64.     const char* what() {
  65.         return "Index error";
  66.     }
  67. };
  68. class StaticArray {
  69. private:
  70.     vector <int> values;
  71. public:
  72.     StaticArray(vector <int> _values) : values(_values) {
  73.         try {
  74.             for (int i = 0; i < values.size(); i++)
  75.                 if(values[i] < 0)
  76.                     throw InvalidValue();
  77.         }
  78.         catch(InvalidValue &ex){
  79.             cerr << ex.what() << endl;
  80.         }
  81.  
  82.     };
  83.     int at(int index)
  84.     {
  85.         try {
  86.             if (index >= 16)
  87.             {
  88.                 throw InvalidIndex();
  89.             }
  90.             return values[index];
  91.         }
  92.         catch (InvalidIndex &ex)
  93.         {
  94.             cerr << ex.what() << endl;
  95.         }
  96.         return 0;
  97.     }
  98.  
  99. };
  100.  
  101. int main()
  102. {
  103.     cout << "Hello World!\n";
  104.     Person osoba1(1010, "Pawel", "Janecki");
  105.     cout << "EMD";
  106.     //cout << osoba1.tostring();
  107.     Student osoba2(100, "Ania", "Kowalska", 300, "Informatyka");
  108.     cout << "END2";
  109.     //cout << osoba2.tostring();
  110.     vector <Person*> dane;
  111.     dane.push_back(&osoba1);
  112.     dane.push_back(&osoba2);
  113.     Student* osoba3 = new Student(1001, "Ania2", "Kowalsk2a", 300, "Informaty2ka");
  114.     for (int i = 0; i < dane.size(); i++)
  115.     {
  116.         cout << 1;
  117.         cout << dane[i]->tostring();
  118.     }
  119.  
  120.     try {
  121.         cout << "Throwing an integer exception...\n";
  122.         throw 42;
  123.     }
  124.     catch (int i) {
  125.         cout << " the integer exception was caught, with value: " << i << '\n';
  126.     }
  127.  
  128.     try {
  129.         cout << "Creating a vector of size 5... \n";
  130.         vector<int> v(5);
  131.         cout << "Accessing the 11th element of the vector...\n";
  132.         cout << v.at(10); // vector::at() throws out_of_range
  133.     }
  134.     catch (const std::exception & e) { // caught by reference to base
  135.         cout << " a standard exception was caught, with message '"
  136.             << e.what() << "'\n";
  137.     }
  138.  
  139.     double a, b, c;
  140.     cout << "Podaj po spacji a b c " << endl;
  141.     cin >> a >> b >> c;
  142.     double delta = pow(b, 2) - 4 * a * c;
  143.     try {
  144.         if (delta < 0)
  145.         {
  146.             throw "Negative value, error";
  147.         }
  148.         double deltasqrt = sqrt(delta);
  149.         double x1 = (-b - deltasqrt) / (2 * a);
  150.         double x2 = (-b + deltasqrt) / (2 * a);
  151.         cout << "x1 to " << x1 << " x2 to " << x2 << endl;
  152.     }
  153.     catch (const char *ex)
  154.     {
  155.         cout << ex << endl;
  156.     }
  157.     std::stringstream sstream;
  158.     int d;
  159.     cin >> d;
  160.     sstream << std::hex << d;
  161.     std::string result = sstream.str();
  162.     cout << result;
  163.     vector <int> Test1;
  164.     for (int i = 0; i < 16; i++)
  165.     {
  166.         Test1.push_back(i - 100);
  167.     }
  168.     cout << "Test111" << endl;
  169.     StaticArray Array1(Test1);
  170.     cout << "Test222" << endl;
  171.     cout << Array1.at(14)<< endl;
  172.  
  173.     string teststring = "asdd";
  174.     map<char, int> test2;
  175.     for (int i = 0; i < teststring.size(); i++)
  176.     {
  177.         test2[teststring[i]]++;
  178.     }
  179.     for (map<char, int>::iterator x = test2.begin(); x != test2.end(); ++x)
  180.     {
  181.         cout << x->first << " " << x->second << endl;
  182.     }
  183.  
  184. }
  185.  
  186. // Uruchomienie programu: Ctrl + F5 lub menu Debugowanie > Uruchom bez debugowania
  187. // Debugowanie programu: F5 lub menu Debugowanie > Rozpocznij debugowanie
  188.  
  189. // Porady dotyczące rozpoczynania pracy:
  190. //   1. Użyj okna Eksploratora rozwiązań, aby dodać pliki i zarządzać nimi
  191. //   2. Użyj okna programu Team Explorer, aby nawiązać połączenie z kontrolą źródła
  192. //   3. Użyj okna Dane wyjściowe, aby sprawdzić dane wyjściowe kompilacji i inne komunikaty
  193. //   4. Użyj okna Lista błędów, aby zobaczyć błędy
  194. //   5. Wybierz pozycję Projekt > Dodaj nowy element, aby utworzyć nowe pliki kodu, lub wybierz pozycję Projekt > Dodaj istniejący element, aby dodać istniejące pliku kodu do projektu
  195. //   6. Aby w przyszłości ponownie otworzyć ten projekt, przejdź do pozycji Plik > Otwórz > Projekt i wybierz plik sln
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement