Strukturalar ha’m birlespeler


Download 150.4 Kb.
Sana16.01.2022
Hajmi150.4 Kb.
#359908
Bog'liq
structura
Kurs ishi, Mustaqil ish, 7-seminar, Internetda ishlashni, sotsiologiya, MASSIV1

Strukturalar ha’m birlespeler


Strukturalar

Structura – bul bir neshe structuralardi, maydanlardi ha’m har qiyli tiplerdi biriktiriwshi tip).

qasiyeti:
    • avtor (qatar)
    • ati (qatar)
    • jili (putin san)
    • Betler sani (putin san)

masele: usilardin barligin bir putin etip birlestiriw

struct Book {

char author[40];

char title[80];

int year;

int pages;

};

Stucturaga berilgen magliwmatlardi kiritiw?

Yad ajiratilmaydi!

!

structura



ati

maydan

Struktura sitaksisi

struct

;

;

;

}; 

Bul jerde - struktura ko'rinisinde jarati'li'p ati'rg'an jan'a tu’rdin’ ati', i> i> - strukturani'n' i-maydani'ni'n' (ati') dag'azasi'.


Strukturalar menen islew

Dagazalaw:

Book b; // Yad ajiratiladi!

Book b1 = { "А.С. Пушкин",

"Полтава", 1998, 223 };

Toliqtirilg’an maydanlar:

strcpy ( b.author, "А.С. Пушкин" );

strcpy ( b.title, "Полтава" );

b.year = 1998;

b.pages = 223;

Klaviyaturada kiritiw

printf ( “avtor " );

gets ( b.author );

printf ( “kitap ti " );

gets ( b.title );

printf ( “jil betler sani " );

scanf ( "%d%d", &b.year, &b.pages );

Strukturalardi kopiyalaw

Elementlerin kopiyalaw:

Book b1, b2;

... // b1 di kiritiw

strcpy ( b2.author, b1.author );

strcpy ( b2.title, b1.title );

b2.year = b1.year;

b2.pages = b1.pages;

Masele : b1 di b2 ge kopiyalaw.

Kopiyalaw «bit ti bitke»:

#include

...

memcpy ( &b2, &b1, sizeof(Book) );



Yamasa :

b2 = b1;


qayaqqa

qayaqtan

Neshe bayt

Structuralar massivi

Dagazalaw:

Book B[10];



Maydanlar menen islew:

for ( i = 0; i < 10; i ++ )

B[i].year = 2008;


B[0] ... B[9]

author

title

year

pages

Faylg’a jaziw:

Fayldan oqiw:

FILE *f;


f = fopen("input.dat", "wb" );

fwrite ( B, sizeof(Book), 10, f );



f = fopen("input.dat", "rb" );

n = fread ( B, sizeof(Book), 10, f );

printf ( “oqilg’anlar sani %d ", n );

Massiv adresi

Blok o’lshemi

Bloklar sani

Faylg’a krsetiw

Book

write binary

Programmaliq ulgiler

Ma’sele: books.dat fayilinda kitap haqqindag’ berilgenler keltirilgen bolip, ol Book tipindegi ( 100 den kishi) structura korinisinde berilgen. Olardin’ barliginin’ shiqqan jilina 2008 di ornatip qaytadan usi faylg’a jazin.

#include

struct Book { … };

void main()

{

Book B[100];



int i, n;

FILE *f;


f = fopen ( "books.dat", "rb" );

n = fread ( B, sizeof(Book), 100, f );

fclose(f);

for ( i = 0; i < n; i ++ ) B[i].year = 2008;

fp = fopen("books.dat", "wb" );

fwrite ( B, sizeof(Book), n, f );

fclose ( f );

}

struct Book { … };



f = fopen ( "books.dat", "rb" );

n = fread ( B, sizeof(Book), 100, f );

fclose ( f );

fp = fopen("books.dat", "wb" );

fwrite ( B, sizeof(Book), n, f );

fclose ( f );

structuranin’ toliq jaziliwi

Massivti oqiw (≤ 100 struktura), n o’zgeriwshige massiv uzinlig’i jaziladi

Massivke (n strukturani) jaziw

Structurag’a yad ajiratiw

Book *p;

p = new Book;

printf ( “avtor " );

gets ( p->author );

printf ( “kitap ati " );

gets ( p->title );

printf ( “betler sani " );

scanf ( "%d", &p->pages );

p->year = 2008;

...

delete p;

Maydang’a murajat etiw ushin -> dan paydalaniladi!

!

p = new Book;



Structurag’a yad ajiratip P o’zgeriwshige jazadi

p->author

delete p;

Yatti bosatiw

Strukturalardin’ dinamikaliq massivi

Book *B;

int n;

printf ( “Sizde qansha kitap bar? " );

scanf ( "%d", &n );

B = new Book[n];

... // Bul jerde massiv toliqtiriladi

for ( i = 0; i < n; i++ )

printf ( "%s. %s. %d.\n",

B[i].author, B[i].title,

B[i].year);

delete B;

Ma’sele: Programma o’rinlaniwinda strukturalar massivine yadtan orin ajiratiw.

B = new Book[n];

Book *B;

delete B;

Mina ko’rsetkish massiv adresine jaziladi

выделяем память

освобождаем память

Strukturalar massivin sortlaw

Gilt(Ключ) ( gilt maydani) – bul strukturalar sortlanatugin maydan.

Mashqala(Проблема): Sortlawda strukturalardin’ nusqalaniwinan qalay qashiw mumkin?

Shehimi: Orin almastiriw ushin qosimsha massiv ko’rsetkishinen paydalaniw kerek.


5

1

3

2

4

5

1

3

2

4

p[0]

p[1]

p[2]

p[3]

p[4]

p[4]

p[0]

p[2]

p[1]

p[3]

Sortlawg’a shekem:

Sortlawdan keyin:

Sortlawdan keyin:

for ( i = 0; i < 5; i ++ )

printf("%d %s", p[i]->year, p[i]->title);

p[i]

p[i]

Programmasi

const N = 10;

Book B[N];

Book *p[N], *temp;

int i, j;

... // bul jerde strukturani toliqtiramiz

for ( i = 0; i < N; i++ )

p[i] = &B[i];

for ( i = 0; i < n-1; i++ )

for ( j = n-2; j >= i; j-- )

if ( p[j+1]->year < p[j]->year ) {

temp = p[j];

p[j] = p[j+1];

p[j+1] = temp;

}

for ( i = 0; i < 5; i ++ )

printf("%d %s", p[i]->year, p[i]->title);

Book *p[N], *temp;

for ( i = 0; i < N; i++ )

p[i] = &B[i];

for ( i = 0; i < n-1; i++ )

for ( j = n-2; j >= i; j-- )

if ( p[j+1]->year < p[j]->year ) {

temp = p[j];

p[j] = p[j+1];

p[j+1] = temp;

}

Ja’rdemshi ko’rsetkish

Korsetkishtin’ dastlepki ornalasiwi

Ko’bikli sortlaw metodi ja’rdeminde sortlaw, bunda strukturalar o’z orninda qaladi tek korsetkishler o’zgeredi

Strukturalarda ko'rsetkish

Struktura elementlerine ko'rsetkishler arqali' murajat yetiw mu'mkin. Woni'n' ushi'n strukturag'a ko'rsetkish wo'zgeriwshisi dag'aza yetiliwi kerek. Mi'sali', joqari'da keltirilgen mi'salda Student strukturasi'na ko'rsetkish to'mendegishe dag'aza yetiledi:

Student * k_talaba;

 

Ko'rsetkish arqali' ani'qlang'an struktura elementlerine murajat «.» menen yemes, ba'lki -> jardeminde a'melge asi'ri'ladi':



cout<FISh;

Strukturalarda ko'rsetkish

Strukturalardi ko'rsetkish ha'm murajat (&) jardeminde funksiya argumenti si'pati'nda uzati'w mu'mkin. To'mende keltirilgen programma bo’leginde strukturani' Student_Kiritiw() funksiyasi'na ko'rsetkish arqali', Student_FISh() funksiyasi'na murajat arqali' uzati'wg'a mi'sal keltirilgen.



Strukturalarda ko'rsetkish (misal)

...


void Student_Kirgiziw(Student *t) ;

void Studentler_FISh(Student & t) ;

int main( ) 

{

Student * k_student;



k_talaba=(Student*) malloc(n*sizeof(Student) ) ;

Student_Kirgiziw(k_student) ;

Studentler_FISh(*k_student) ;

return 0;

}

Strukturalarda ko'rsetkish (misal)

void Studentler_FISh(Student & t) ;

{

for(int i=0; i

cout<<(&t+i). ->FISh<

}

void Student_Kirgiziw(Student *t) ;



{

for(int i=0; i

{

cout<

cout<<” Student FISh :”;

cin.getline(t+i]->FISh, 30) ;

cout<<” Kurs:”;

cin>>(t+i) ->Kurs;



...



}
Download 150.4 Kb.

Do'stlaringiz bilan baham:




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