942 – 19 guruhi talabasi Zaripboyev Anvar 12 – tajriba ishi (mta)


Download 53.91 Kb.
bet5/9
Sana01.12.2021
Hajmi53.91 Kb.
#178642
1   2   3   4   5   6   7   8   9
Bog'liq
12 - tajriba ishi

Satr sinf sifatida

String turi. Satrlar bilan ishlash uchun standart kutubxonaga kiruvchi string sinfidan foydalanish qulay.

Bu turdan foydalanish uchun quyidagi sarlavhali faylni ulash lozim:

#include Satrlarni ta’riflashga misollar:

string st(”BAHO \n"); //sinivollar satri bilan initsiallash

string st2; // bo‘sh satr

string st3(st); shu turdagi o‘zgaruvchi bilan inltslallash


Satrlar ustida amallar. Satrlar ustida quy idagi amallar aniqlangan:

  • qiymat berish (=);

  • ikki amal ekvivalentligini tekshirish uchun (= =) va (!=);

  • konkatenatsija yoki satrlarni ulash (+);

  • qiyinat berib qo‘shish amali (- =) ;

  • indeks olish ([]).

Usullar. Satr uzunligini aniqlash uchun size() funksiyasidan foydalaniladi (uzunlik tugallovchi simvolni hisobga olmaydi).

cout << "uzunlik" << st << ":" << st.size();

Maxsus empty() usuli agar satr bo‘sh bo‘lsa true qaytaradi, aks holda false qaytaradi:

if (st.empty()) // to‘g‘ri: bo‘sh



Misol:

#include

using namespace std;

int main()

{

string str("fa.disney.com");



int size= str.size();

for (int ix= 0; ix

if (str[ix] == '.')

str[ix] ='_';

cout<

return 0;

}
Misol. Struktura ta’rifi va initsializatsiyasida string turidan foydalanish.

#include

using namespace std;

struct Student

{

string name;



int kurs;

float rating;

};

void Student_show(Student a)



{

cout <

cout <

cout <

}

int main()



{

Student s = {"Mahmudov", 1, 3.5};

Student_show (s);

return 0;

}
Satrli maydonga misol. Keyingi misolda satrli maydon string turidagi o‘zgaruvchi sifatida beriladi.

#include

#include

using namespace std;

class employee

{

public:



employee(string, long, float);

void show_employee(void);

int change_salary(float);

long get_id(void);

private:

string name;

long employee_id;

float salary;

};

employee::employee(string name, long employee_id, float salary)



{

employee::name= name;

employee::employee_id = employee_id;

if(salary < 50000.0)

employee::salary= salary;

else


employee::salary=0.0;

}

void employee::show_employee(void)



{

cout << "Ism:" << name << endl;

cout << "Nomer:" <

cout << "Maosh:" << salary<< endl;

}

int main()



{

employee worker("Happy Jamsa", 101, 10101.0);

worker.show_employee();

return 0;

}

Natija:


Ism: Happy Jamsa Nomer: 101

Maosh: 10101



Download 53.91 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling