C++, programowanie w C++, kurs C++

Ćwiczenie 1. Przeanalizuj poniższy program na zastosowanie typu danych string.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string napis;       // przechowuje ciąg znaków

 

    napis = "programowanie w C++";

    cout << napis;

 

    getch();

    return 0;

}

 

WYJśCIE

programowanie w C++

 

Ćwiczenie 2. Wyświetl na ekranie podany na wejściu ciąg znaków.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string napis;

 

    cout << "Podaj ciag znakow: ";

    getline(cin, napis);

 

    cout << endl << "Wpisales: " << napis;

 

    getch();

    return 0;

}

 

WYJśCIE

Podaj ciag znakow: to jest przyklad zastosowania typu danych o nazwie string

 

Wpisales: to jest przyklad zastosowania typu danych o nazwie string

 

 

 

Operacje na łańcuchach

 


Funkcja

lub operator                                  Opis                                                             Przykład użycia                                   Wynik

 


                        Łączy łańcuchy znaków.                             "ab" "cde""fg"              abcdefg

+                      Łączy łańcuchy znaków.                             "ab" + "cde" + "fg"          abcdefg

 

                                                   napis = "abcdefgh"

substr               Zwraca podciąg danego łańcucha.               napis.substr(0, 3)           abc

erase                Usuwa z danego łańcucha podciąg.             napis.erase(1, 2)            adefgh

insert                Wstawia podciąg do danego ciągu.             napis.insert(2, "CD")        abCDcdefgh

replace             Zastępuje określony podciąg innym.            napis.replace(2, 3, "CDE")   abCDEfgh

length               Zwraca długość łańcucha.                           napis.length()               8

find                   Zwraca numer pozycji podciągu.                 napis.find("c")              2

==                    Porównanie łańcuchów (taki sam jak).        if (napis1 == napis2)

!=                     Porównanie łańcuchów (różny od).             if (napis1 != napis2)

 

 


Ćwiczenie 3. Napisz program, który wczyta łańcuch znaków, a następnie wyświetli jego długość.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string napis;

 

    cout << "Podaj ciag znakow: ";

    getline(cin, napis);

 

    cout << endl;

    cout << "Wprowadziles ciag dlugosci " << napis.length() << " znakow.";

 

    getch();

    return 0;

}

 

WYJśCIE

Podaj ciag znakow: programowanie strukturalne

 

Wprowadziles ciag dlugosci 26 znakow.

 

 

 

Ćwiczenie 4. Odczytaj pierwszy, drugi i ostatni znak wprowadzonego z klawiatury łańcucha
                      znaków.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string napis;

 

    cout << "Podaj ciag znakow: ";

    getline(cin, napis);

 

    cout << endl;

    cout << "Pierwszy znak lancucha to: " << napis[0] << endl;

    cout << "Drugi znak lancucha to:    " << napis[1] << endl;

    cout << "Ostatni znak lancucha to:  " << napis[napis.length()-1];

 

    getch();

    return 0;

}

 

WYJśCIE

Podaj ciag znakow: Pascal

 

Pierwszy znak lancucha to: P

Drugi znak lancucha to:    a

Ostatni znak lancucha to:  l

 


 

Ćwiczenie 5. Wyświetl na ekranie znaki wprowadzonego łańcucha rozpoczynając od jego połowy.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string napis;

    int dlugosc;

 

    cout << "Podaj ciag znakow: ";

    getline(cin, napis);

 

    cout << endl;

    dlugosc = napis.length();

    for(int i=dlugosc/2; i<dlugosc; i++)

        cout << napis[i];

 

    getch();

    return 0;

}

 

WYJśCIE

Podaj ciag znakow: program

 

gram

 

 

Ćwiczenie 6. Ułóż program, który sprawdzi poprawność wprowadzonego hasła.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string haslo, mojeHaslo = "tajemnica";

 

    cout << "Podaj haslo: ";

    getline(cin, haslo);

    if (haslo == mojeHaslo)

        cout << "Brawo! Odgadles haslo.";

    else

        cout << "Nieprawidlowe haslo! Sprobuj jeszcze raz.";

 

    getch();

    return 0;

}

 

WYJśCIE

Podaj haslo: hasło

Nieprawidlowe haslo! Sprobuj jeszcze raz.

 

WYJśCIE

Podaj haslo: tajemnica

Brawo! Odgadles haslo.

 

 


Fragment na szarym prostokącie można zredagować w inny sposób – porównaj!

 

    if (haslo != mojeHaslo)

        cout << "Nieprawidlowe haslo! Sprobuj jeszcze raz.";

    else

        cout << "Brawo! Odgadles haslo.";

 


 

Ćwiczenie 7. Napisz program, który połączy dwa łańcuchy znaków.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string lancuch1, lancuch2, lancuch3;

 

    cout << "Podaj pierwszy lancuch znakow: ";

    getline(cin, lancuch1);

    cout << "Podaj drugi lancuch znakow: ";

    getline(cin, lancuch2);

 

    cout << endl;

    cout << "lancuch1 + lancuch2 = " << lancuch1+lancuch2 << endl;

    lancuch3 = lancuch1 + lancuch2;

    cout << "lancuch3 = " << lancuch3;

 

    getch();

    return 0;

}

 

WYJśCIE

Podaj pierwszy lancuch znakow: programowanie

Podaj drugi lancuch znakow:  strukturalne

 

lancuch1 + lancuch2 = programowanie strukturalne

lancuch3 = programowanie strukturalne

Ćwiczenie 8. Sprawdź działanie pozostałych procedur i funkcji tabeli.

 

KOD

#include <iostream>

#include <conio>

 

using namespace std;

 

int main()

{

    string tekst = "Programowanie strukturalne i obiektowe";

 

    cout << "tekst = \"" << tekst << "\"" << endl << endl;

 

    cout << "Zastosowane funkcje:" << endl;

    cout << "1. tekst.substr(0, 26)" << endl;

    cout << "2. tekst.erase(14, 15)" << endl;

    cout << "3. tekst.insert(14, \"strukturalne i \")" << endl;

    cout << "4. tekst.replace(29, 9, \"strukturalne\")" << endl;

    cout << "5. tekst.replace(14, 12, \"obiektowe\")" << endl;

    cout << "6. tekst.find(\"ob\")" << endl << endl;

 

    cout << "Wyniki operacji: " << endl;

    cout << "1. " << tekst.substr(0, 26) << endl;

    cout << "2. " << tekst.erase(14, 15) << endl;    

    cout << "3. " << tekst.insert(14, "strukturalne i ") << endl;

    cout << "4. " << tekst.replace(29, 9, "strukturalne") << endl;

    cout << "5. " << tekst.replace(14, 12, "obiektowe") << endl;

    cout << "6. Podciag 'ob' rozpoczyna sie na pozycji " << tekst.find("ob");

 

    getch();

    return 0;

}

 

WYJśCIE

tekst = "Programowanie strukturalne i obiektowe"

 


Zastosowane funkcje:

1. tekst.substr(0, 26)

2. tekst.erase(14, 15)

3. tekst.insert(14, "strukturalne i ")

4. tekst.replace(29, 9, "strukturalne")

5. tekst.replace(14, 12, "obiektowe")

6. tekst.find("ob")

 

Wyniki operacji:

1. Programowanie strukturalne

2. Programowanie obiektowe

3. Programowanie strukturalne i obiektowe

4. Programowanie strukturalne i strukturalne

5. Programowanie obiektowe i strukturalne

6. Podciag 'ob' rozpoczyna sie na pozycji 14

 


 

Przykład konwersji liczby typu ‘string’ do typu ‘double’

 

#include <iostream>

#include <conio>

#include <sstream>

 

using namespace std;

 

int main()

{

    string tekst = "247.12";

    double liczba = 0;

 

    stringstream ss(tekst);

    ss >> liczba;

    cout << liczba;

 

    getch();

    return 0;

}

 

WYJśCIE

247.12

 

Przykład konwersji liczby typu ‘double’ do typu ‘string’

 

#include <iostream>

#include <conio>

#include <sstream>

 

using namespace std;

 

int main()

{

    double wartosc = 863.98;

    string tekst = "";

 

    stringstream ss(tekst);

    ss << wartosc;

    tekst = ss.str();

    cout << tekst;

 

    getch();

    return 0;

}

 

WYJśCIE

863.98

 

Wróć do strony 1, 2, 3, 4, 5, przejdź do strony 7

 

Obecnie przeglądasz zagadnienia: C++, programowanie w C++, kurs C++, zobacz też: Kurs Pascala, Kurs CSS, Kurs JavaScript

 

© vj.e.pl 2007