Aleksandra Stępniewska
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Zajęcia zerowe ### Plan Zajęć #### Część pierwsza: - omówienie szablonu programu w C++ (w Code::Blocks) - co znaczy #include <iostream>, a co using namespace std? - jakie są typy zmiennych (int, float, string)? * czym różni się cout << "a"; od cout << a;? * jak działają operatory arytmetyczne na różnych typach? (dlaczego 5/2=2 a 5.0/2.0=2.5?) - przydatne skróty w zapisie stringów "\n" "\b" - warunki - if i else oraz operatory logiczne &&(i), ||(lub), !(nie) - pętle: for, while, do while i czym różni się i++ od ++i? - zakresy zmiennych - czym są zmienne lokalne, a czym globalne? #### Część druga: - jak działają tablice i jak je tworzymy? - dlaczego trzeba uważać na zakresy (od 0 do n-1)? - jak odczytywać i zapisywać dane do tablicy? - dlaczego czasami tablica ma w sobie same zera, a czasami przypadkowe wartości? - funkcje wbudowane (max, min, abs) - jak napisać je samemu? --- ### Omówienie szablonu - **#include <nazwa_biblioteki>** - import odpowiednich bibliotek, dzięki którym możemy korzystać z gotowych funkcji i struktur #include <iostream> - najbardziej podstawowe funkcje takie jak cout, cin #include <string> - obsługa zmiennych tekstowych #include <cmath> - funkcje takie jak min, max, sqrt (pierwiastek) **#include <bits/stdc++.h>** - importuje wszystkie podstawowe biblioteki - **using namespace std** określa przestrzeń nazw, w której się znajdujemy oraz pozwala na pisanie **cout**, **cin**, **endl** zamiast **std::cout**, **std::cin**, **std::endl** - **int main()** funkcja główna, w której wywołujemy pozostałe funkcje i piszemy główną część kodu. **main** zwraca **int**, dlatego na koniec main'a piszemy **return 0;**" (zero oznacza, że program wykonał się poprawnie. Jest to pewna konwencja). :::warning Uwaga - niektórzy mogli spotkać się z zapisem void main(), wówczas funcja kończy się bez zwrócenia wartości. (Nie ma "return 0;"). Jest to jednak sposób archaiczny, obecnie zaleca się używanie int main(). ::: ### Typy zmiennych ###### Zmienne i ich typy Zmienna - to jest taki obiekt, który przechowuje dane w programie. Zmienną tworzymy w następujący sposób: ```cpp= typ nazwa; ``` ###### Typy liczbowe ```cpp= int liczba; \\ liczba całkowita float liczba2; \\ liczba zmienno-przecinkowa 6-7 cyfr po przecinku double liczba3; \\ tzw. podwojna precyzja 15-16 cyfr po przecinku unsigned int liczba; \\ liczba calkowita bez znaku, tylko dodatnie, podwojony prawy zakres long int liczba; ``` ###### Typ logiczny ```cpp= bool a = true; bool b = false; // LUB bool a = 1; bool b = 0; ``` ###### Typy znakowe ```cpp= char znak = 'A'; char znak = 65; ``` ###### Napisy - stringi ```cpp = string napis = "abcd"; //'a' 'b' 'c' 'd' '\0' // 0 1 2 3 4 // gdzie '\0' to znak specjalny, który oznacza koniec napisu cout << napis.size(); cout << napis.length(); cout << napis[0]; cout << napis[1]; napis[2] = 'x'; for(int i = 0; i < napis.size(); i++) { napis[i] = 'x'; } string napis2 = "efg"; string napis3 = napis + napis2; string napis3 += "hij"; // do uzupelnia string a = "abcdef"; cout << a.length(); // 6 a = "abcdef\0"; cout << a.length() << endl; // 6 a = "abc\0def"; cout << a.length() << endl; // 3 ``` NIEPOPRAWNE NAZWY ZMIENNYCH: ```cpp= int moja-zmienna; int moja zmienna; int 12zmienna; // ale zmiennna123 JEST POPRAWNE int ąę; int max; int return; ``` Nie używamy w nazwach zmiennych spacji, myślników(jest odbierany przez kompilator jako odejmowanie), nie zaczynamy od cyfry (w środku i na końcu już mogą być), nie używamy również polskich znaków, ani np. słów kluczowych (takich jak return, auto, max, int - jeśli nam podkresla to znaczy ze cos jest nie tak). ###### Operatory + - * / % ``` cpp = int l1= 2; int l2= 5; cout << l1 + l2 << endl cout << 1.2 + 3.4 << endl; cout << l1 - 3 << endl; // analogicznie mnozenie, dzielenie, reszta z dzielenia // -1 mod 3 = 2 cout << -1 % 3 << endl; ``` DZIAŁANIA MAJĄ PRIORYTETY: np. mnożenie potem dodawanie ```cpp= cout << 2 * 3 + 1 << endl; cout << 1 + 2 * 3 << endl; ``` ```cpp = int l1 = 2; int l2 = 5; l1 = l1 + l2; l1 += l2; // do tego co mamy po lewej stronie dodajemy to co jest po prawej // Analogicznie jest zdefiniowane -=, *=, /= l1++; // l1 += 1; l1 = l1 + 1; l1--; // l1 -= 1; // DLATEGO NIE: l1+2; // nie dziala // TYLKO l1 += 2; ``` ###### Dzielenie liczb całkowitych vs liczb zmiennoprzecinkowe ```cpp= int a = 5; int b = 2; cout << a/b; //tutaj wynik byłby 2, bo dzielimy przez siebie dwie liczby całkowite, więc wynik mamy tutaj dzieleni całkowite ``` ```cpp= float a = 5; float b = 2; cout << a/b; //tutaj wynik byłby 2.5, bo dzielimy przez siebie dwie liczby zmiennoprzecinkowe, więc mamy tutaj zwykłe dzielenie ``` ```cpp= int a = 5; float b = 2; cout << a/b; //tutaj wynik byłby 2.5, bo program automatycznie dostosuje się do typu o większym zakresie ``` ###### Preinkrementacja i postinkrementacja ``` cpp = int i1 = 2; int i2 = 2; cout << ++i1 << " " << i1 << endl; cout << i2++ << " " << i2 << endl; ``` ###### KOD ASCII: Jest to system kodowania znaków, działa w ten sposób, że każdemu znakowi przyporządkowana jest liczba od 0-127. Warto zapamiętać: 65 - 90 (26 wielkich liter) 97 - 122 (26 malych liter) 48 - 57 (10 cyfr) ![](https://i.imgur.com/VOuEx04.png) ![](https://i.imgur.com/jyXRgrW.png) ### Skróty w zapisie stringów \n - new line - skrót na wstawienie entera \b - backspace - usuwa znak poprzedząjacy \t - tab - wstawia przerwę horyzontalną ###### Przykład użycia - wypisywanie tablicy ```cpp= cout<<"["; for(int i=0; i<10; i++) { cout<<i<<", "; } cout<<"\b\b]"; ``` ### Instrukcje warunkowe #### Operatory porownania ``` cpp = <, >, <=, >=, ==, != ``` // POROWNYWAC MOZEMY TEZ ZNAKI ```cpp = 'a' < 'b' // prawda 'A' < 'a' // prawda bo A jest wczesniej w kodzie ASCII ``` if (warunek) {kod1} else {kod2} jeżeli warunek jest prawdziwy to wykona się fragment kod1, a w przeciwnym wypadku kod2 :::warning 0 - false - fałsz 1 - true - prawda ::: **&&** logiczne "i" | p | q | p&&q | | - | - | -----| | 0 | 0 | 0 | | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 1 | **||** logiczne "lub" | p | q | p\|\|q | | - | - | -----| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 | **!** logiczne "nie" | p | !p | | - | - | | 0 | 1 | | 1 | 0 | ###### Przykład ```cpp= int liczba = 5; if(liczba > 1 && liczba < 6) { ... } if(!(liczba>1 && liczba < 6)) { } // TO OZNACZA ZE if(!(liczba >1) || !(liczba < 6)) // czyli if(liczba <= 1 || liczba >= 6) if(!(liczba > 1)) // oznacza liczba <= 1 { } if(liczba < 7 || liczba > 10) ``` ###### Uwaga! Nawiasy mają znaczenie: ```cpp= int liczba = 1; if(liczba > 3) liczba++; cout << liczba << endl; // ta linijka wykonuje sie poza if'em ``` ### Pętle ```cpp= for(int i = 0; i < 5; i++) { cout << i << " "; } cout << endl; for(int i = 4; i >=0; i--) { cout << i << " "; } cout << endl; for(int i = 2; i <= 10; i+=2) { cout << i << " "; } cout << endl; //można wykorzystać pętle for rownież w następujący posób (ale nie jest to wskazane, bo do tego służy pętla while) int i = 0; for(;i<10;) { cout << i << endl; i++; } ``` ```cpp= int i = 0; while(i < 10) { if(i == 5) { continue; } cout << i << " "; i++; } cout << endl; int i = 0; while(i < 10) { if(i == 5) { i++; continue; } cout << i << " "; i++; } cout << endl; ``` ``` int i = 0; while(i > 5) { cout << "jestem w while" << endl; } do { cout << "jestem w do...while" << endl; }while(i > 5); ``` #### Zakres ważności zycia obiektu i zaslanianie nazw ```cpp= int x = 1; if(x == 1) { int y = 4; cout << y << endl; } cout << y << endl; // blad, bo zmienna y poza if'em nie istnieje ``` ```cpp= int x = 1; if(x == 1) { int x = 4; // tutaj zmienna x zostaje zasłoniona cout << x << endl; } cout << x << endl; ``` #### Tablice Zmienna przechowuje jedną wartość, tablice przechowują wiele wartości. Czyli jest to po prostu pojemnik na dane. W tablicy przechowujemy tylko wartości jednego typu. Poczatek tablicy to tak naprawdę wskaźnik na pierwszy element tablicy; Tablice muszą mieć z góry określony rozmiar. Tablice w C++ numerowane są od 0 do n-1. Jeśli nie przypiszemy wartości do tablicy, to będzie ona w sobie zawierać śmieci (chyba, że zadeklarujemy ją globalnie tzn. nad funkcją main - wtedy jest wypełniona zerami). :::warning Deklarowanie zmiennych globalnie nie jest dobrą praktyką. ::: ``` int tab[5]; ``` ``` int tab[5] = {1,2,3,4,5}; ``` ``` int x = 4; int tab[x]; ``` ``` tab[ilosc_wierszy][ilosc_kolumn]; tab[3][2] = {{1,1}, {2, 2}, {3, 3}}; tab[][2] = {{1,1}, {2, 2}, {3, 3}}; for(int i = 0; i < 3; i++) { for(int j = 0; j < 2; j++) { cout << tab[i][j] << " "; } cout << endl; } ``` Przy tworzeniu tablicy dwuwymiarowej możemy pominąć wartość w pierwszym nawiasie kwadratowym, ale nie w drugim! (Jeśli ktoś jest ciekawy dlaczego, to można wygooglować lub nas dopytać ;) ### Funkcje :::success O funkcjach będzie jeszcze wykład. Teraz przekazujemy Wam w skrócie kilka informacji, żebyście lepiej zrozumieli, jak używać funkcji wbudowanych, o których chcemy dziś powiedzieć. ::: ``` cpp = typ_wartosci_zwracanej nazwa_funkcji(typ argumentu) { } int zwieksz_o_1(int liczba) { return liczba + 1; } ``` ```cpp= int abs(int x) { if(x < 0) { return -x; // TO RETURN KONCZY DZIALANIE FUNKCI } return x; } ``` ```cpp= void funkcja1() { cout << "hello" << endl; } void funkcja2() { funkcja1(); } ``` Przy takim wywołaniu funkcji ważna jest kolejność definicji funkcji, jeśli umieścimy funkcja2() przed funkcja1(), to wywali nam to błąd. Widzimy więc, że funkcja moze wywolac inna funkcje, ale nie mozna napisac funkcji w funkcji. Nie tworzymy też funkcji w mainie - jedynie wywolujemy. ```cpp= void funkcja(int tab[]) { for(int i = 0; i < 5; i++) { tab[i] = 1; } } int main() { int tab[5] = {0, 0, 0, 0, 0}; funkcja(tab); for(int i = 0; i < 5; i++) { cout << tab[i] << " "; } return 0; } ``` ### Przydatne funkcje wbudowane - min() - minimum - max() - maksimum - abs() - wartość bezwzględna - moduł liczby - sort() - sortowanie tablicy - swap() - zamiana wartości dwóch zmiennych - __gcd() - największy wspólny dzielnik ``` cpp= bool porownaj(int i, int j) { return i > j; } int main() { int ar[5] = {2,5,1,2,4}; sort(ar, ar + 5, porownaj); for(int i = 0; i < 5; i++) { cout << ar[i] << " "; } return 0; } ----------------------------------------------- cout << max(1, 2) << endl; cout << min(1, 2) << endl; int a[5] = {1, 2, 3, 4, 5}; swap(a[0], a[1]); for(int i = 0; i < 5; i++) { cout << a[i] << " "; } cout << endl; cout << abs(-5) << endl; cout << __gcd(4, 10) << endl; ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully