soz<<"\n";
return 0;
}
Netice:
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>./prog2.exe
str_dey -in heddleri
x – 50
soz – proqramlashdirma
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>
3 - cь зalışma.
1 -ci зalışmada daxil olunan str tipli 5 elementd n ibar t strler c rg si ə ə ə ə elan edin. Bu cərgənin
hər bir elementinin ьzvlərinə istifadəзi tərəfindən daxil olunan qiymətlər mənimsədin. Daha
sonra bu qiymətləri ekranda зap edin.
// hell_8_3.cpp
#include
#include
struct str
{ int x;
char soz[30];
};
int main(int argc, char *argv[])
{ str strler[5];
int i;
for(i=1; i<=5; ++i){
std::cout<<"strlerin "<
<<" -ci elementinin x ve soz heddlerini daxil edin\n";
std::cin>>strler[i].x>>strler[i].soz;
}
std::cout<<"strler -in elementlerinin x ve soz heddleri\n";
for(i=1; i<=5; ++i)
std::cout<<"x - "<
<<"\nsoz - "<
return 0;
}
Netice:
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>./prog2.exe
strlerin 1 -ci elementinin x ve soz heddlerini daxil edin
4 ferid
strlerin 2 -ci elementinin x ve soz heddlerini daxil edin
6 ramin
strlerin 3 -ci elementinin x ve soz heddlerini daxil edin
7 elif
strlerin 4 -ci elementinin x ve soz heddlerini daxil edin
56 mithat
strlerin 5 -ci elementinin x ve soz heddlerini daxil edin
23 taleh
strler -in elementlerinin x ve soz heddleri
x - 4, soz - ferid
x - 6, soz - ramin
x - 7, soz - elif
x - 56, soz - mithat
x - 5, soz – taleh
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>
4 - cь зalışma.
Зalışma 3-ь funksiyalardan istifad etm kl h ll edin. Bu m qs dl ə ə ə ə ə ə ə 2 funksiya tərtib edin,
daxil_et və cap_et . Mьvafiq olaraq daxil_et funksiyası istifadəзidən məlumatları oxuyub,
strler cərgəsinin elementlərinə mənimsədəcək, cap_et isə strler cərgəsinin elementlərinin
qiymətlərini зap edəcək.
// hell_8_4.cpp
#include
#include
struct str
{ int x;
char soz[30];
};
void daxil_et( str strler[], int k);
void cap_et( str strler[], int k);
int main(int argc, char *argv[])
{ str strler[6];
int k=5;
daxil_et(strler,k);
cap_et(strler,k);
return 0;
}
/*==================================================*/
void daxil_et( str strler[], int k)
{ int i;
for(i=1; i<=k; ++i){
std::cout<<"strlerin "<
<<" -ci elementinin x ve soz heddlerini daxil edin\n";
std::cin>>strler[i].x>>strler[i].soz;
}} /*==================================================*/
void cap_et(struct str strler[], int k)
{ int i;
std::cout<<"strler -in elementlerinin x ve soz heddleri\n";
for(i=1; i<=k; ++i)
std::cout<<"x - "<
}
Netice:
C:\cpp\prog2\Debug>./prog2.exe
strlerin 1 -ci elementinin x ve soz heddlerini daxil edin
1 alma
strlerin 2 -ci elementinin x ve soz heddlerini daxil edin
2 heyva
strlerin 3 -ci elementinin x ve soz heddlerini daxil edin
3 nar
strlerin 4 -ci elementinin x ve soz heddlerini daxil edin
4 gilas
strlerin 5 -ci elementinin x ve soz heddlerini daxil edin
5 erik
strler -in elementlerinin x ve soz heddleri
x - 1, soz - alma
x - 2, soz - heyva
x - 3, soz - nar
x - 4, soz - gilas
x - 5, soz – erik
C:\cpp\prog2\Debug>
5 - ci зalışma
Funksiyalardan istifad etm kl el proqram qurun ki, зalışma 1-d daxil ə ə ə ə ə olunan str tipli 5
elementdən ibarət strler cərgəsi elan etsin. daxil_et funksiyası vastəsilə istifadəзidən oxunan
qiymətləri bu cərgəsinin elemntlərinə mənimsətsin. Daha sonra max_el funksiyası tərtib
edin, hansı ki, strler cərgəsinin elementləri arasında x-i ən bцyьk olanın qiymetlerini ( x və
soz ) зap etsin.
// hell_8_5.cpp
#include
#include
struct str
{ int x;
char soz[30];
};
void daxil_et( str strler[], int k);
str max_el( str strler[], int k);
int main(int argc, char *argv[])
{ str strler[6], elem;
int k=6;
daxil_et(strler,k);
elem = max_el(strler,k);
std::cout<<"max x, soz: "<
return 0;
}
/*==================================================*/
void daxil_et( str strler[], int k)
{ int i;
for(i=1; i<=k; ++i){
std::cout<<"strlerin "<
<<" -ci elementinin x ve soz heddlerini daxil edin\n";
std::cin>>strler[i].x>>strler[i].soz;
}}
/*==================================================*/
struct str max_el(struct str strler[], int k)
{ int i,j=0,dey=strler[0].x;
for(i=1; i
if (strler[i].x > dey ){
j = i;
dey = strler[i].x;
}} return strler[j];
}
Netice:
C:\cpp\prog2\Debug>./prog2.exe
strlerin 1 -ci elementinin x ve soz heddlerini daxil edin
45 baki
strlerin 2 -ci elementinin x ve soz heddlerini daxil edin
34 london
strlerin 3 -ci elementinin x ve soz heddlerini daxil edin
456 texas
strlerin 4 -ci elementinin x ve soz heddlerini daxil edin
78 istanbul
strlerin 5 -ci elementinin x ve soz heddlerini daxil edin
12 ankara
max x, soz: 456, texas
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>
$9. Siyahılar
1 - ci зalışma.
1. Proqram 2 nьmun sini el d yişin ki, siyahının obyektl ri цzьnd ə ə ə ə ə int tipli x həddindən
əlavə char tipli ad[20] həddi də saxlasın. Mьvafiq olaraq siyahi yaratma və cap_et
funksiyalarinda lazımi dəyişikliyi edin. Proqramı kompilyasiya və icra edin, siyahi yaradin ,
onun elementlərini зap edin.
// hell_9_1.cpp
#include
#include
struct syh_el
{ int x;
char ad[20];
syh_el *novb_el;};
syh_el *siyahi_yarat(syh_el *syh, int elem_say);
void siyahini_cap_et( syh_el *);
int main(int argc, char *argv[])
{
/* istifade edeceyimiz deyishenleri elan edirik */
syh_el *menim_syh;
/* siyahinin bosh oldugunu bildirmek ucun */
menim_syh = NULL;
int say;
std::cout<<"Siyahinin elementlerinin sayini daxil edin \n";
std::cin>>say;
menim_syh=siyahi_yarat(menim_syh,say);
siyahini_cap_et(menim_syh);
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
syh_el *siyahi_yarat( syh_el *syh, int elem_say)
{ syh_el *
p, *
q;
p=syh;
q=syh;
int i,dey;
char bufer[30];
for (i=1; i<=elem_say; ++i){
std::cout<<"siyahinin "<
<<" -ci elementinin x ve ad heddlerini daxil edin \n";
memset(bufer,0,30);
std::cin>>dey>>bufer;
p = new syh_el;
p->x = dey;
strncpy(p->ad,bufer,20);
*(p->ad + 20) = '\0';
p->novb_el = NULL;
if (syh==NULL){
syh=p;
q = p;
p = NULL; }
else {
q->novb_el = p;
q = p;
}} return syh;
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void siyahini_cap_et(syh_el *syh)
{ syh_el *
p;
int dey, fix = 0;
char bufer[30];
p = syh;
if (syh == NULL ) {
std::cout<<"Siyahi boshdur \n";
return;
} std::cout<<"Siyahinin elementleri \
n";
while(p!=NULL)
{ /*elementlerin capinin ekanda gozel gorunmesi ucun*/
if (fix++ != 0) std::cout<<" -> ";
dey = p->x;
memset(bufer,0,30);
strncpy(bufer, p->ad, 20);
*(bufer + 20) = '\0';
std::cout<<"(0<<"dey<<" "<
p = p->novb_el; /* novbeti elemente kec */
} std::cout<<"\n";
}
Netice:
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>./prog2.exe
Siyahinin elementlerinin sayini daxil edin
4 siyahinin 1-ci elementinin x
ve ad heddlerini daxil edin
12 kitab
siyahinin 2 -ci elementinin x ve ad heddlerini daxil edin
34 defter
siyahinin 3 -ci elementinin x ve ad heddlerini daxil edin
456 idman
siyahinin 4 -ci elementinin x ve ad heddlerini daxil edin
124 hefte
Siyahinin elementleri
(12 kitab) -> (34 defter) -> (456 idman) -> (124 hefte)
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>
2 - ci зalışma
Yuxarıdakı proqrama axtarış funksiyası lav edin. Proqram istifad зid ə ə ə ən int tipli ədəd istəyir
və siyahıda x -i bu ədədə bərabər olan obyektin ad[20] -həddini зap edir.
// hell_9_2.cpp
#include
#include
struct syh_el
{ int x;
char ad[20];
syh_el *novb_el;};
syh_el *siyahi_yarat(syh_el *syh, int elem_say);
syh_el * axtarish(syh_el *, int);
int main(int argc, char *argv[])
{ /* istifade edeceyimiz deyishenleri ilan edirik */
syh_el *menim_syh, *syh_dey = NULL;
int x;
char bufer[30];
memset(bufer,0,30);
/* siyahinin bosh oldugunu bildirmek ucun */
menim_syh = NULL;
int say;
std::cout<<"Siyahinin elementlerinin sayini daxil edin \n";
std::cin>>say;
menim_syh=siyahi_yarat(menim_syh,say);
std::cout<<"Siyahidan ad heddini tapmaq istediyiniz "
<<"elementin x -ni daxil edin \n";
std::cin>>x;
syh_dey = axtarish(menim_syh,x);
strcpy(bufer,syh_dey->ad);
if (syh_dey!=NULL)
std::cout<<"x heddi "
<
<
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
syh_el *siyahi_yarat( syh_el *syh, int elem_say)
{ syh_el *
p, *
q;
p=syh;
q=syh;
int i,dey;
char bufer[30];
for (i=1; i<=elem_say; ++i)
{
std::cout<<"siyahinin "<
<<" -ci elementinin x ve ad heddlerini daxil edin \n";
memset(bufer,0,30);
std::cin>>dey>>bufer;
p = new syh_el;
p->x = dey;
strncpy(p->ad,bufer,20);
*(p->ad + 20) = '\0';
p->novb_el = NULL;
if (syh==NULL){
syh=p;
q = p;
p = NULL; }
else {
q->novb_el = p;
q = p;
}} return syh;
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
syh_el * axtarish(syh_el *syh, int tap)
{ syh_el *
p;
p = syh;
if (syh == NULL ) {
return NULL;
} while(p!=NULL){
if (p->x==tap) return p;
p = p->novb_el; /* novbeti elemente kec */
} return NULL;
}
Netice:
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>./prog2.exe
Siyahinin elementlerinin sayini daxil edin
6
siyahinin 1-ci elementinin x ve ad heddlerini daxil edin
123 kitab
siyahinin 2 -ci elementinin x ve ad heddlerini daxil edin
34 musiqi
siyahinin 3 -ci elementinin x ve ad heddlerini daxil edin
56 ehtimal
siyahinin 4 -ci elementinin x ve ad heddlerini daxil edin
79 shebeke
siyahinin 5 -ci elementinin x ve ad heddlerini daxil edin
875 prosessor
siyahinin 6 -ci elementinin x ve ad heddlerini daxil edin
257 fizika
Siyahidan tapmaq istediyiniz elementin x -ni daxil edin
79
x heddi 79 -ye beraber elementin ad heddi shebeke –dir
C:\cpp\prog2\Debug>
3 - cь зalışma
İki siyahını birl şdir n funksiya t rtib edin. Funksiya parametr olaraq ə ə ə iki siyahi qəbul edir,
daha sonra bu siyahilardan birincini ikinci ilə birləşdirir.
// hell_9_3.cpp
#include
#include
struct syh_el
{ int x;
char ad[20];
syh_el *novb_el;};
syh_el *siyahi_yarat(syh_el *syh, int elem_say);
void birleshdir(syh_el *, syh_el *);
void siyahini_cap_et(syh_el *);
int main(int argc, char *argv[])
{ /* istifade edeceyimiz deyishenleri ilan edirik */
syh_el *syh1, *syh2 = NULL;
int x;
char bufer[30];
memset(bufer,0,30);
/* siyahilarin bosh oldugunu bildirmek ucun */
syh1 = syh2 = NULL;
int say;
std::cout<<"Birinci siyahinin elementlerinin sayini"
<<" daxil edin \n";
std::cin>>say;
syh1=siyahi_yarat(syh1,say);
std::cout<<"Ikinci siyahinin elementlerinin sayini "
<<"daxil edin \n";
std::cin>>say;
syh2=siyahi_yarat(syh2,say);
/*syh1 -i syh2 ilebirleshdiririk*/
birleshdir(syh1,syh2);
siyahini_cap_et(syh1);
return 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
syh_el *siyahi_yarat( syh_el *syh, int elem_say)
{ syh_el *
p, *
q;
p=syh;
q=syh;
int i,dey;
char bufer[30];
for (i=1; i<=elem_say; ++i){
std::cout<<"siyahinin "<
<<" -ci elementinin x ve ad heddlerini daxil edin \n";
memset(bufer,0,30);
std::cin>>dey>>bufer;
p = new syh_el;
p->x = dey;
strncpy(p->ad,bufer,20);
*(p->ad + 20) = '\0';
p->novb_el = NULL;
if (syh==NULL){
syh=p;
q = p;
p = NULL; }
else {
q->novb_el = p;
q = p;
}} return syh;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void birleshdir(syh_el *syh1, syh_el *syh2)
{ if (
syh1 == NULL )
{
syh1 = syh2;
return;
} if (
syh2 == NULL)
return;
syh_el *p;
p = syh1;
while(p->novb_el!=NULL)
p = p->novb_el;
p->novb_el = syh2;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void siyahini_cap_et(syh_el *syh)
{ syh_el *
p;
int dey, fix = 0;
char bufer[30];
p = syh;
if (syh == NULL ) {
std::cout<<"Siyahi boshdur \n";
return;
} std::cout<<"Siyahinin elementleri \
n";
while(p!=NULL)
{ /*elementlerin capinin ekanda gozel gorunmesi ucun*/
if (fix++ != 0) std::cout<<"->";
dey = p->x;
memset(bufer,0,30);
strncpy(bufer, p->ad, 20);
*(bufer + 20) = '\0';
std::cout<<"("<
p = p->novb_el; /* novbeti elemente kec */
} std::cout<<"\n";
}
Netice:
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>./prog2.exe
Birinci siyahinin elementlerinin sayini daxil edin
3
siyahinin 1-ci elementinin x ve ad heddlerini daxil edin
12 vaxt
siyahinin 2 -ci elementinin x ve ad heddlerini daxil edin
34 saniye
siyahinin 3 -ci elementinin x ve ad heddlerini daxil edin
45 elifba
Ikinci siyahinin elementlerinin sayini daxil edin
2
siyahinin 1-ci elementinin x ve ad heddlerini daxil edin
56 telim
siyahinin 2 -ci elementinin x ve ad heddlerini daxil edin
78 terbiye
Siyahinin elementleri
(12 vaxt)->(34 saniye)->(45 elifba)->(56 telim)->(78 terbiye)
C:\cpp\prog2\Debug>
C:\cpp\prog2\Debug>
4 - cь зalışma
Nьmun Proqram 3 -d daxil edilmiş silm funksiyasın el d yişdirin ə ə ə ə ə ki, siyahının verilmiş
indeksli obyektindən başlayaraq verilmiş sayda obyekti siyahıdan silsin.
// hell_9_4.cpp
#include
#include
struct syh_el
{ int x;
char ad[20];
syh_el *novb_el;
};
syh_el *siyahi_yarat(syh_el *syh, int elem_say);
syh_el *siyahi_sil(syh_el *syh, int elem, int say);
void siyahini_cap_et(syh_el *);
int main(int argc, char *argv[])
{
/* istifade edeceyimiz deyishenleri ilan edirik */
syh_el *menim_syh;
/* siyahinin bosh oldugunu bildirmek ucun */
menim_syh = NULL;
int say,elem;
std::cout<<"Siyahinin elementlerinin sayini daxil edin \n";
std::cin>>say;
menim_syh=siyahi_yarat(menim_syh,say);
siyahini_cap_et(menim_syh);
std::cout<<"Siayhidan silmek istediyiniz elementlerin \n"
<<"bashlangic indeksini ve sayini daxil edin\n";
std::cin>>elem>>say;
menim_syh = siyahi_sil(menim_syh, elem,say);
siyahini_cap_et(menim_syh);
Dostları ilə paylaş: