Problema 1.2. V LMbWV MM VL WV MM ZM VVMVMV WV @ MV. iMM M WV VVMVMV g V bV LVbL WV VMM. bV WMVV LVbL VVMVMVV VLMM g b bV bLV MML WV MbVV MMM VVMVM M VL bV ML WM@ @V WV MbVVq VLM b MM WM.
Rezolvare:
#include<stdio.h> #include<conio.h> typedef int vector[20]; vector a; int n;
3 Structuri de date Culegere de probleme n C++ VL g MLV
void sortare(vector x,int n){ int temp; for(int i=0;i<n-1;i++) for(int j=i+1;j<n;j++) if (x[i]>x[j]){ temp=x[i]; x[i]=x[j]; x[j]=temp; } } void tiparire(vector x,int n) { for(int i=0;i<n;i++) printf("%5d",x[i]); } int inserare(vector x,int n) { int poz; int elem; if (n==19) printf("\nVector plin!"); else { printf("\nDati elem. de inserat: "); scanf("%d",&elem); if (elem<=x[0]) poz=0; else if (elem>=x[n-1]) poz=n; else for(int i=0;i<n-1;i++) if ((x[i]<=elem)&&(elem<x[i+1])) poz=i+1; } for(int i=n;i>=poz;i--) x[i]=x[i-1]; x[poz]=elem; n++; return n; }
4 Structuri de date Culegere de probleme n C++ VL g MLV
Problema 1.3. V LMbWV MM VL WV VVMVMV WV @ MV WV @VM @VLVWVM . bV bVV LVbL VVMVMVV LVbM VL g b bV bLV Z MV WV gVVV MMM VVMVM WM VL` - MWLMW @ LVbM M VL - WM@ V L WV VMM.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef int vect[20]; vect x; int elem,n,poz; void sortare(int x[20],int n) { int temp;
5 Structuri de date Culegere de probleme n C++ VL g MLV
printf("\nDati elem. pe care doriti sa-l stergeti: "); scanf("%d",&elem); //verificam daca val. citita este sau nu in vector poz=-1; for(i=0;i<n;i++) if(x[i]==elem) poz=i; if (poz==-1) printf("\nNu exista in vector aceasta val.! "); else { for(i=poz;i<n-1;i++) 6 Structuri de date Culegere de probleme n C++ VL g MLV
8 Structuri de date Culegere de probleme n C++ VL g MLV
Problema 1.6. V LMbWV MLV [q M LM MMMV M bVL MMV 1i g MLV [HqH MVM @VbV M WV MVMV @V LV LM@ MLV . bV LLMVV bMM VVMVMV de pe diagonala principala a matricei b.
Rezolvare:
#include<stdio.h> #include<conio.h> union zona{ int a[5][5]; int b[4][4]; }z; int suma(){ unsigned char i; int sum=0,j,k; int mat[5][5]={{1,2,3,4,5},{6,7,8,9,10}, {11,12,13,14,15},{16,17,18,19,20},{7,1,1,4,2}}; for(j=0;j<5;j++) for(k=0;k<5;k++) z.a[j][k]=mat[j][k]; for(i=0;i<4;i++){ printf("\n%d ",z.b[i][i]); sum+=z.b[i][i]; } return sum; }
void main(){ int s; clrscr(); s=suma(); printf("\nSuma elementelor de pe diagonala principala"); printf("\na matricei b este: %d",s); getch(); }
9 Structuri de date Culegere de probleme n C++ VL g MLV
Problema 1.7. 1VLM LM@MMVV MLV WM @MM WV M _b MMW bVM WV @LM V WV @VMLV.
11 Structuri de date Culegere de probleme n C++ VL g MLV
-1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1
Problema 1.9. 1@M MMV LVL VbV LLV @M M@V WV b q LV WV @VbMM g categorii de salarizare. Astfel, a i,j,k V@VM MMM M WV MW WV M@ WV b q MW LV WV @VbV _ g bM WV LV W. LV @MM LV LLMV `
- MMM M WV MW WM LVLV - MMM M WV MW @V M@ WV b - MMM M WV MW WM VLV M@ WV b g LVV WV profesie;
Rezolvare:
#include<stdio.h> #include<conio.h> const int a[3][3][3]={ { {1,2,3},{4,5,6},{7,8,9} }, { {0,0,0},{1,2,1},{2,2,2} }, { {3,2,4},{1,2,3},{5,4,3} } }; int x=0; int xx[3];; int xxx[3][3]; void main() { clrscr(); for(int i=0;i<3;i++) { xx[i]=0;
12 Structuri de date Culegere de probleme n C++ VL g MLV
15 Structuri de date Culegere de probleme n C++ VL g MLV
void main() { clrscr(); creare_matrice(b,a); printf("\n\nMatricea a este: "); tipar(a); transp(a,c); printf("\n\nMatricea b este: "); tipar(c); suma(a,c,d); printf("\n\nMatricea c este: "); tipar(d); getch(); }
Problema 1.13. S bV MVV MLV MMMM 1 WV WMVMbMMV [q LM VVMVMVV MVMMV bM WM @ML@ q LM LV dintr-un vector B de 15 elemente.
Rezolvare:
#include<stdio.h> #include<conio.h> typedef int vec[15]; typedef int mat[5][5]; const vec b={ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; int k=0; mat a; void main() { clrscr(); for(int i=0;i<5;i++) for(int j=0;j<5;j++) if (i>=j) a[i][j]=b[k++]; else a[i][j]=0; for(i=0;i<5;i++) { printf("\n"); for(int j=0;j<5;j++) 16 Structuri de date Culegere de probleme n C++ VL g MLV
printf("%2d ",a[i][j]); } getch();}
Problema 1.14. S bV VLV WL MLV [q WV @ MVq VbV bM MM MLV bMVL .
Rezolvare:
#include<stdio.h> #include<conio.h> typedef int mat[3][3]; int vb; mat a; int simetric(mat a) { int vb=1,test; for(int i=0;i<3;i++) for(int j=2;j>i-1;j--) { test=0; while ( (test==0)&&(vb) ) if ( a[i][j]!=a[j][i] ) vb=0; else test=1; } return vb; }
void main() { clrscr(); for(int i=0;i<3;i++) for(int j=0;j<3;j++) { printf("a(%d)(%d)=",i,j); scanf("%d",&a[i][j]); }; vb=simetric(a); if (!vb) printf("\nMatricea nu este simetrica! "); else printf("\nMatricea este simetrica! "); getch();} 17 Structuri de date - Culegere de probleme n C++ Articole
2. Articole
Problema 2.1 Explica MWM WV WVMVq M V g VVV MMV structuri de date de tip articol.
Rezolvare: 11 _1\@1111 1 1111 >L LL111Lg1L 1>1N11 11L11L1>1\11 11 i,j 1L L 111 elemente sunt date de tip articol. Articolul are la rndul sau printre membrii
19 Structuri de date - Culegere de probleme n C++ Articole
un masiv bidimensional d i,j . L1L111L 1111 L1L1L11 L1 L\\1L\11L1L g1 ] 1 N11111L1 11 >L 1L11ZLZ _111 LN111L L7_1L>1L1. Art i,j .d k,j
#include<stdio.h> #include<conio.h> typedef struct articol{ char a[20]; int b; char c; short int d[5][5]; }; articol art[4][4]; void main(){ clrscr(); for(int i=0;i<4;i++) for(int j=0;j<4;j++) for(int k=0;k<5;k++) for(int l=0;l<5;l++) art[i][j].d[k][l]=i+j+k+l; printf("\nValoarea ultimului element este: %d ",art[3][3].d[4][4]); getch(); }
Problema 2.5 Documentele care nso VbL MVVV LM M LM@MV` LWMq WVMMMVq MMV WV M bM g WV L L V LMV g @V Mq V LMV VbVM M bLq M g Vgq V MMM V MVMM. LV @MM LV WVMVgV bMLM WV @ articol g VV M V LVbM.
Rezolvare:
#include<conio.h> #include<stdio.h>
20 Structuri de date - Culegere de probleme n C++ Articole
struct capr{ int cant; int pret;}; struct sies{ int stoc_init; int intrari; int iesiri;}; union alfa{ capr cp; sies io; int val;}; typedef struct { int cod,alf; char den[30],um[5]; union alfa ala; }recvar; recvar articol; void main(){ clrscr(); printf("\ncod material: "); scanf("%d",&articol.cod); printf("\ndenumire material: "); fflush(stdin); gets(articol.den); printf("\nunitatea de masura: "); gets(articol.um); printf("\nintroduceti tipul articolului(1,2,3): "); scanf("%d",&articol.alf); switch(articol.alf){ case 1: printf("\narticol 1:\n"); printf("\ncantitate:"); scanf("%d",&articol.ala.cp.cant); printf("\npret:"); scanf("%d",&articol.ala.cp.cant); break; case 2: printf("\narticol 2:\n"); printf("\nstoc initial:"); scanf("%d",&articol.ala.io.stoc_init); 21 Structuri de date - Culegere de probleme n C++ Articole
Problema 2.6 1VM MLV ____ MM VL _U_ g bMLM WV @ L Lq MW L MVMM MM VL LM U LM@MVMV 1L ` __ g ______L.W__q b bV bLV @MM LV gV VVMVMVV _____ g VMVM L.W__ bMLM WV @ L
Rezolvare:
#include<stdio.h> #include<conio.h>
typedef struct{ int d[9]; }c;
union zona{ int a[3][3]; int b[9]; c e; }z;
void main(){ clrscr(); for(int i=0;i<9;i++) z.b[i]=(i+1)*(i+1); printf("\nElementele matricei a sunt: "); for(i=0;i<3;i++){ printf("\n"); 22 Structuri de date - Culegere de probleme n C++ Articole
void main(){ clrscr(); strcpy(zo.un1.x,"1234567890"); printf("\n%s",zo.un1.x); strcpy(zo.un2.y,"abcde"); printf("\n%s",zo.un2.y); 23 Structuri de date - Culegere de probleme n C++ Articole
Problema 2.8 Despre fiecare student dintr-o grup bV LMMbL MMMVV LVbMq MVV MMV bVMMq MMM M WV @VVM V bVMM. se memoreze ntr- bMLM WV WV LVb@MM V V LVbV MM g b bV bLV MML V g @LVWMV @VMM MVV MM V bM `
- gV bMWVM M WMV VL - gV MM WVb@V MM MMM bMWVM - MbLVV MMM MM bMWVM M M@ - gVVV MMM bMWVM WM M@
student a[20]; 24 Structuri de date - Culegere de probleme n C++ Articole
unsigned char sir[20],r; int i,j,n;
int creare(student a[20]){ //returneaza nr de studenti introdusi int n; printf("Numarul de studenti: "); scanf("%d",&n); for(i=0;i<n;i++){ printf("\nNumele si prenumele: "); fflush(stdin); gets(a[i].nume); printf("\nnote la seminarii<=10: "); for(j=0;j<5;j++) scanf("%d",&a[i].notas[j]); printf("\nprezenta: "); scanf("%d",&a[i].prez); } return n; }
void sortnume(student a[20],int n){ //sorteaza studentii n ordine alfabetica student s; for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++) if((strcmpi(a[i].nume,a[j].nume))>0){ s=a[i]; a[i]=a[j]; a[j]=s; } } }
void listare(student a[20],int n){ puts("\n"); for(i=0;i<n;i++){ printf("\n%s, note: ",a[i].nume); for(j=0;j<5;j++) printf(" %d ",a[i].notas[j]); printf(", prezenta: %d ",a[i].prez); } puts("\n"); } 25 Structuri de date - Culegere de probleme n C++ Articole
int gasit(student a[20],int n,char nume[20]){ if (!n){printf("\nnume= %s!",nume);return 0;} else { for(i=0;i<n;i++) if(!(strcmpi(a[i].nume,nume))) return 1; return 0; } }
void cauta(student a[20],int n){ char nume[20]; printf("\nDati numele studentului! "); fflush(stdin); gets(nume); if(gasit(a,n,nume)){ i=pozitie(a,nume); printf("\nNume si prenume: %s ",a[i].nume); printf("\nNote la seminar:\n"); for(j=0;j<5;j++) printf(" %d ",a[i].notas[j]); printf("\nPrezenta= %d ",a[i].prez); } else printf("\nStudentul nu se afla in aceasta grupa! "); }
void inserare(student a[20], int *pn){ student s; int poz; if(*pn>20)printf("\ngrupa are deja 20 de studenti!");
26 Structuri de date - Culegere de probleme n C++ Articole
else { poz=-1; printf("\ndati numele studentului ce trebuie inserat: "); fflush(stdin); gets(s.nume); printf("\ndati notele de la seminar: "); for(i=0;i<5;i++) scanf("%d",&s.notas[i]); printf("\ndati prezenta: "); scanf("%d",&s.prez); if((strcmpi(s.nume,a[(*pn)-1].nume))>0){ a[(*pn)]=s; (*pn)++; } else{ if(!(*pn)) a[(*pn)++]=s; else {if((strcmpi(a[0].nume,s.nume))>0) poz=0; else{poz++; for(i=0;i<((*pn)-1);i++)
void stergere(student a[20],int *pm){ char nume[20]; int k,i; if(!(*pm))printf("\nNu este nici un student!"); else{ printf("\nDati numele studentului pe care doriti sa-l stergeti! "); fflush(stdin); gets(nume); 27 Structuri de date - Culegere de probleme n C++ Articole
if (gasit(a,*pm,nume)){ k=pozitie(a,nume); for(i=k;i<(*pm)-1;i++) a[i]=a[i+1]; (*pm)--; } else printf("\nstudentul nu se afla in lista!"); } }
Problema 3.3 1VM@L WVMV g M V V b@V @MV g Vb@VL @MV b@V @MV b@V @Mer spre ntreg.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<alloc.h> int intreg=22; int *p_int;//pointer spre intreg int **pp_int;//pointer spre pointer void main(){ clrscr(); p_int=(int *)malloc(sizeof(int)); p_int=&intreg; pp_int=(int **)malloc(sizeof(int*)); pp_int=&p_int; printf("%d",**pp_int); getch(); }
30 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.4 S bV bLV MM @M LV MV @MV b@V pointeri spre pointeri.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<alloc.h> int ***ppp_int; int **pp_int; int *p_int; int intreg=44; void main(){ clrscr(); p_int=(int *)malloc(sizeof(int)); pp_int=(int **)malloc(sizeof(int*)); ppp_int=(int ***)malloc(sizeof(int**)); p_int=&intreg; pp_int=&p_int; ppp_int=&pp_int; printf("%d",***ppp_int); getch(); }
Problema 3.5 1MWL LV g @MM M MM VVLM V`
#include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct articol{ int camp1; char camp2[80]; unsigned char camp3; float camp4; }; int *p_int; int **p_mat; articol *p_art; int mat[3][3]={{1,2,3},{4,5,6},{7,8,9}}; articol art[1]={{100,"sir1",a,12.3},}; void main(){ 31 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.6 1VM @M WV WV @VMM MbV MM g WMVMbMVq VVLM LV WMML q M g MbMM VVMVMVV g M@M bMMVV.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<alloc.h> int **pa; int *pb; int s1,s2; void main(){ clrscr(); 32 Structuri de date - Culegere de probleme n C++ Pointeri
#include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct { int c1; }c; int a[1]={10},*pa; int b[1][1]={{20},},(*pb)[1][1]; c cc,*pc; int d=40,*pd;
33 Structuri de date - Culegere de probleme n C++ Pointeri
#include<stdio.h> #include<conio.h> typedef struct{ int * b; int c; }a; a aa,*pa; void main(){ clrscr(); aa.c=7; 34 Structuri de date - Culegere de probleme n C++ Pointeri
#include<stdio.h> #include<conio.h> typedef struct { int c[2]; int *d[2];//vector de pointeri }b; b bb[2]; //vector de articole b *pbc[2];//vector de pointeri la articole b (*ppp)[2];//pointer la vector de articole void main(){ clrscr(); bb[0].c[0]=11; bb[0].d[0]=&bb[0].c[0]; bb[0].c[1]=22; bb[0].d[1]=&bb[0].c[1]; bb[1].c[0]=11; bb[1].d[0]=&bb[1].c[0]; pbc[0]=&bb[0]; printf("\n *pbc[0]->d[1] = %d",*pbc[0]->d[1]); ppp=&bb; printf("\n *ppp[0]->d[1] = %d",*ppp[0]->d[1]); getch(); } 35 Structuri de date - Culegere de probleme n C++ Pointeri
Rezolvare:
Se va afig.
*pbc[0]->d[1] = 22 *ppp[0]->d[1] = 22
Problema 3.10 V LMbWV VV VVMVMV` q q L g W g MM VL M LM @M VVMVMV. LV @MM LV MbMMV VVMVMVV q q Lq W M b g VVMVMVV VLMM M M bZq M LWM aceleiag bMLM V@VV. bV @MM M VWVM LMMV MV VL WV @MV g @MV b@V VL.
Rezolvare:
`L LL111Lg1L 11 NLL1\1 LL _\111L11 >_1L 111L@ 11111 7 L1L >L 111 111ZLZ L1 L1L>L1L 111 1 L g1 L. `L LN1LL1 1Z definirea pointerului spre un vector pm.
#include<stdio.h> #include<conio.h> int a=7,b=10,c=13,d=20; int m[4]={7,10,13,20}; int (*pm)[4];//un pointer la vector int *x[4]; //un vector de pointeri int i,s1,s2; void main(){ x[0]=&a; x[1]=&b; x[2]=&c; x[3]=&d; pm=&m; s1=s2=0; for(i=0;i<4;i++){ s1+=*x[i]; s2+=(*pm)[i]; } 36 Structuri de date - Culegere de probleme n C++ Pointeri
printf("\ns1= %d s2=%d",s1,s2); getch(); }
Problema 3.11 V LMbWV @M MMMVV MVMV M MV bLV MM V VVMVMV. LV @MM LV @VMV V acestora ca elemente ale unui masiv bidimensional.
void main(){ clrscr(); z.pa=&a; int s=0; for(i=0;i<5;i++){ s+=(*z.pa)[i][i]; printf(" %d ",(*z.pa)[i][i]); } printf("\nSuma pentru a[1..5][1..5]= %d\n\n",s); printf("\nSuma pentru a[1..5][1..5]= %d\n\n",suma(z.pb,5)); printf("\nSuma pentru a[1..4][1..4]= %d\n\n",suma(z.pb,4)); getch(); }
Rezolvare:
T1\@1111 11gLZ .
1 7 13 19 2 Suma pentru a[1..5][1..5] = 42
1 6 11 16 7 Suma pentru a[1..5][1..5] = 41
1 6 11 16 Suma pentru a[1..4][1..4] = 34
38 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.13 1g VVMVMVV MMM vector de tip ntreg, accesnd elementele acestuia prin intermediul unui vector de pointeri spre ntreg g MMM @MV b@V VL WV @MV b@V MV.
Rezolvare:
#include<stdio.h> #include<conio.h> int a[5]={1,2,3,4,5}; int *p[5];//vector de pointeri int *(*pp)[5];
Problema 3.14 bV LLMVV bMMVV V MLV WV LVVg dimensiune, accesnd elementele matricelor prin intermediul unui vector de pointeri spre matrice.
Rezolvare:
#include<stdio.h> #include<conio.h> int (*vp[3])[2][3]; unsigned char i,j,k; int s[3]={0,0,0}; int a[2][3]={{1,1,1},{2,2,2}}; int b[2][3]={{3,3,3},{4,4,4}}; int c[2][3]={{5,5,5},{6,6,6}}; void main(){ clrscr(); 39 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.15 bV LLMVV bMMVV V MLV WV LVVg dimensiune accesnd elementele matricelor prin intermediul unui vector de pointeri spre pointeri spre matrice.
Rezolvare:
#include<stdio.h> #include<conio.h> int (*(*vpp[3]))[2][3]; int (*vp[3])[2][3]; unsigned char i,j,k; int s[3]={0,0,0}; int a[2][3]={{1,1,1},{2,2,2}}; int b[2][3]={{3,3,3},{4,4,4}}; int c[2][3]={{5,5,5},{6,6,6}}; void main(){ clrscr(); unsigned char var=a; vp[0]=&a; vp[1]=&b; vp[2]=&c; for(i=0;i<3;i++) vpp[i]=&vp[i]; for(k=0;k<3;k++){ for(i=0;i<2;i++) 40 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.16 S bV LLMVV bMMVV V MLV WV LVVg dimensiune accesnd elementele matricelor prin intermediul unui pointer spre un vector de pointeri spre pointeri spre matrice.
Rezolvare:
#include<stdio.h> #include<conio.h> int (*(*(*pvpp)[3]))[2][3]; int (*(*vpp[3]))[2][3]; int (*vp[3])[2][3]; unsigned char i,j,k; int s[3]={0,0,0}; int a[2][3]={{1,1,1},{2,2,2}}; int b[2][3]={{3,3,3},{4,4,4}}; int c[2][3]={{5,5,5},{6,6,6}}; void main(){ clrscr(); unsigned char var=a; vp[0]=&a; vp[1]=&b; vp[2]=&c; for(i=0;i<3;i++) vpp[i]=&vp[i]; pvpp=&vpp; for(k=0;k<3;k++){ for(i=0;i<2;i++) for(j=0;j<3;j++) s[k]+=(*(*(*pvpp)[k]))[i][j]; printf("\nSuma matricei %c este: %d ",var,s[k]); var++; } 41 Structuri de date - Culegere de probleme n C++ Pointeri
getch(); }
Problema 3.17 S bV MbMMVV bV@ VVMVMVV V VL bMW bMM MbML MMV M LV MW @V MW VVV VVMVMVV acestora.
Rezolvare:
#include<stdio.h> #include<conio.h> int x[10]={1,2,3,4,5,6,7,8,9,10}; int y[10]={10,9,8,7,6,5,4,3,2,1}; int z[10]={1,1,1,1,1,1,1,1,1,1}; int s[3]={0,0,0}; int (*pv[3])[10];//vector de pointeri la vectori void main(){ clrscr(); pv[0]=&x; for(int k=0;k<3;k++){ for(int i=0;i<10;i++) s[k]+=(*pv[k])[i]; printf("\n %d",s[k]); pv[k+1]=++pv[k]; } getch(); }
Problema 3.18 Scrie @MM LV VVLMV MbMMV VVMVMV MMM VL WV M LM@MVMV LM MbML MMV`
- s1+=x i ; - s2 +=(*px1) i ; s3+=*px2 i ;
Rezolvare:
#include<stdio.h> #include<conio.h> 42 Structuri de date - Culegere de probleme n C++ Pointeri
#include<conio.h> #include<stdio.h> int a1[3][3]={{1,2,3},{4,5,6},{7,6,1}}; int a2[3][3]={{1,1,2},{3,3,3},{4,4,1}}; int a3[3][3]={{1,1,2},{4,4,7},{3,3,4}}; int a4[3][3]={{4,4,4},{2,1,1},{4,3,2}}; int a5[3][3]={{1,0,0},{0,0,7},{2,3,4}}; int a6[3][3]={{3,3,3},{3,3,1},{2,7,6}}; int a7[3][3]={{1,0,0},{6,7,8},{3,6,9}}; int (*pa[7])[3][3]; int (*pb[7])[3][3]; int (*ppb)[3][3]; int b1[3][3],b2[3][3],b3[3][3],b4[3][3],b5[3][3],b6[3][3],b7[3][3];
int (*calcul(int (*px)[3][3],int (*pxx)[3][3]))[3][3]{ int ii,j; for(ii=0;ii<3;ii++){ printf("\n"); 43 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.21. bV LLMVV V bMM VVMVMV MMV MLVq V bMM VVMVMV MMM VL LV WMML. 1M MML V WV MMM @ MMV bV L g bV M V MVMV @VMM MbM unidimensional sM @VMM MbM WMVMbMq g V ML ` a i,j =i*j; b i V gV bMM VVMVMV MbMM @VML g bV VVV MVM VV . 45 Structuri de date - Culegere de probleme n C++ Pointeri
Rezolvare:
#include<stdio.h> #include<conio.h> typedef int mat[3][3]; typedef int vect[3]; typedef mat *pmat; typedef vect *pvect; pmat pa; pvect pb; int k; int s=0;
#include<conio.h> #include<stdio.h> #include<dos.h> int *ppc; int c[2][5]={{1,2,3,4,5},{6,7,8,9,10}}; int b[10]={11,12,13,14,15,16,17,18,19,20}; void main(){ clrscr(); printf("\n"); for(int i=0;i<4;i++){ ppc=(int*)MK_FP(FP_SEG(c[i]),FP_OFF(c[i])); printf(" %d ",*ppc); } 47 Structuri de date - Culegere de probleme n C++ Pointeri
getch(); }
Rezolvare:
T1\@1111 1 111 L7LL1 1L1 N 11g .
1 6 11 16
Problema 3.24 V LMbWV MbVV MMWMVMbMV q L MW M LM@MVMV. 1VM@L VbVM LMLVM bMW 11_1$q 11_111q g MMMV @MM VVMVMV LV L MVbL respectivele masive pentru efectuarea referirii elementelor lor.
Rezolvare:
#include<conio.h> #include<stdio.h> #include<dos.h> int *ppc; int c[10]={1,2,3,4,5,6,7,8,9,10} ; int b[10]={11,12,13,14,15,16,17,18,19,20}; void main(){ clrscr(); printf("\n"); for(int i=0;i<20;i++){ ppc=(int*)MK_FP(FP_SEG(c+i),FP_OFF(c+i)); printf("%d ",*ppc); }
getch(); }
Problema 3.25 1MWL LV gV @MM`
#include<stdio.h> #include<dos.h> #include<conio.h> int x[10]={1,2,3,4,5,6,7,8,9,10}; 48 Structuri de date - Culegere de probleme n C++ Pointeri
int *px; unsigned int sg,ofsetul; void main(){ clrscr(); int s=0; sg=FP_SEG(x); ofsetul=FP_OFF(x); for(int i=0;i<10;i++){ px=(int *)MK_FP(sg+2*i,ofsetul+2*i); s+=*px; } printf(" %d",s); getch(); }
Rezolvare:
T1\@1111 1 111 L7LL1 1L1 N 11g .
Problema 3.26 1MWL LV gV @MM`
#include<stdio.h> #include<dos.h> #include<conio.h> int x[10]={1,2,3,4,5,6,7,8,9,10}; int *c; unsigned int a,b; void main(){ clrscr(); int s=0; a=FP_SEG(x); b=FP_OFF(x);
for(int i=0;i<10;i++){ c=(int *)MK_FP(a+i*sizeof(int),b+i*sizeof(int)); s+=*c; } 49 Structuri de date - Culegere de probleme n C++ Pointeri
printf(" %d",s); getch(); }
Rezolvare:
Programul n urma execu 1L1 N 11g .
suma = 55
Problema 3.27. bV bLV @MM LV @VV MML V WV LLM bMM LMVM MMM MMML. 1MML VMMV MM @MV M WV MVMV L WMML WV LVb.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct art { int marca; char nume[20]; long int nrore; long int salorar; long int retineri; long int sal; } ; typedef struct art *part; part pp; part citire() { part p; p=new art; fflush(stdin); printf("marca: "); scanf("%d",&p->marca); printf("nume: "); fflush(stdin); gets(p->nume); printf("salariu orar: "); scanf("%ld",&p->salorar); printf("numar ore: "); scanf("%ld",&p->nrore); printf("retineri: "); scanf("%ld",&p->retineri); printf("% ld ",(long)((p->nrore)*(p->salorar)) ); p->sal=(long)((p->nrore)*(p->salorar)-(p->retineri)); 50 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.28 bV bLV @MM LV WVVMM bMWVM WM-o M@ LV M M LV M MV MM MMM VMVMq bMW MM pointer la un vector de articole.
Rezolvare:
#include<conio.h> #include<stdio.h> typedef struct str{ char nume[25]; int nota; }; str b[20]; str (*pb)[20]; unsigned char i,n,max=1; void main(){ clrscr(); printf("\nDati numarul de studenti din grupa: "); scanf("%d",&n); pb=&b; for(i=0;i<n;i++){ printf("\nNumele: ");fflush(stdin); gets((*pb)[i].nume); printf("\nNota: ");fflush(stdin); scanf("%d",&(*pb)[i].nota); if((*pb)[i].nota>max) max=(*pb)[i].nota; } for(i=0;i<n;i++) if(max==(*pb)[i].nota) printf("\n nume: %s nota: %d",(*pb)[i].nume,(*pb)[i].nota); 51 Structuri de date - Culegere de probleme n C++ Pointeri
getch(); }
Problema 3.29 Exemplifica WVMV MMM VL WV bMLM q M V LM@MVMV M g gV MVM bMW operatorul de referire ntr-o expresie n care apare un vector de pointeri b@V bMLM .
Rezolvare:
#include<conio.h> #include<stdio.h> typedef struct articol{ int camp1; int camp2; }ar; ar *p_vect_articol[3];//vector de pointeri spre structura ar vect_articol[3]={{1,2},{3,4},{5,6},}; void main(){ clrscr(); for(int i=0;i<3;i++) p_vect_articol[i]=&vect_articol[i]; printf("\n%d",vect_articol[1].camp2); printf("\n%d",p_vect_articol[1]->camp2); getch(); }
Problema 3.30. Indica LV gV @MM`
#include<stdio.h> #include<conio.h> struct str { int elem; int *pelem; }; typedef int mat[1][1]; typedef struct str *pstr; typedef mat *pmat; mat a; 52 Structuri de date - Culegere de probleme n C++ Pointeri
54 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.32 Indica LV VV @MM`
#include<stdio.h> #include<conio.h> #include<string.h> struct a { char a1[20]; } ; struct b { char b1[20]; } ; typedef struct a *pa; typedef struct b *pb; a x; b y; pa px; pb py; void *pp;
Problema 3.36 1VM g VV MM Mb WMVMbM @MMMW M LVb@MWVM WVbVV VVMVMV WV @V @M MV g LM masivelor bidimensionale n care se descompune, cu elementele unui VL WV @MV. 1M V MbMM WMVMbM bV VV @M WV MWMbV WV VMM MMW LVg @MV.
Rezolvare:
#include<dos.h> #include<stdio.h> unsigned int aaa,bbb; int a[3][3][3]; int *vp[3]; int *pp; int j,k; char rasp=1;
void main(){ clrscr(); for(int i=0;i<3;i++) for(j=1;j<3;j++) for(k=0;k<3;k++) a[i][k][j]=0; for(i=0;i<3;i++) vp[i]=&a[i][0][0]; while(rasp!= ){ printf("\nintroduceti coordonatele:\n "); printf("\ncoordonata i : ");scanf("%d",&i);i--; printf("coordonata j : ");scanf("%d",&j);j--; printf("coordonata k : ");scanf("%d",&k);k--; aaa=FP_SEG(vp[i]); bbb=FP_OFF(vp[i]); aaa+=(j*3+k)*2; bbb+=(j*3+k)*2; 59 Structuri de date - Culegere de probleme n C++ Pointeri
Problema 3.37 1VM MLVq M LVb MM @ WV W g VV M WM MWM WbMLV VVMVMVV bVq M @MM LV VV VVMVMM MMM g WVML @ LVbM.
Rezolvare:
#include<conio.h> #include<stdio.h> #include<alloc.h> int poz[4]={1,1,1,1}; int min,i,j,h,k,d1,d2,d3,d4; int (*p1)[10][10]; int ((*pp1)[10][10])[10][10]; void main(){ clrscr(); printf("\n Dati dimensiunile tabloului:\n"); printf("d1= ");scanf("%d",&d1); printf("d2= ");scanf("%d",&d2); printf("d3= ");scanf("%d",&d3); printf("d4= ");scanf("%d",&d4); for(i=0;i<d1;i++) 60 Structuri de date - Culegere de probleme n C++ Pointeri
for(j=0;j<d2;j++) for(h=0;h<d3;h++) for(k=0;k<d4;k++){ fflush(stdin); scanf("%d",&(*p1)[h][k]); (*pp1)[i][j][h][k]=(*p1)[h][k]; } min=((*pp1)[0][0])[0][0]; for(i=0;i<d1;i++) for(j=0;j<d2;j++) for(h=0;h<d3;h++) for(k=0;k<d4;k++) if((*pp1)[i][j][h][k]<min){ min=((*pp1)[i][j])[h][k]; poz[0]=i+1; poz[1]=j+1; poz[2]=h+1; poz[3]=k+1; } printf("\nElementul minim %d apare prima data in aceasta structura",min); printf("\npe linia %d, col %d, din matricea de pe",poz[2],poz[3]); printf(" linia %d coloana %d",poz[0],poz[1]); getch(); }
61 Structuri de date - Culegere de probleme in C++ 1MML
4. Func LL
Problema 4.1 LV MML VLMb @VMM VMVV VMVM gMM M 1MLL g @MM @VMM gV @M M VMVM.
Problema 4.2 LV MML V LV V L @MVM MM @MV b@V MLV 1q LLMV ___q g VMMV MM @MV b@V matricea B.
Rezolvare:
#include<stdio.h> #include<stdlib.h> #include<conio.h> int a[][3]={{1,2,3},{4,5,6},{7,8,9}}; int (*pa)[3][3],(*pm)[3][3]; int (*patrat (int(*pb)[3][3]) )[3][3]{ int c[3][3]; int (*pc)[3][3]; pc=&c;
62 Structuri de date - Culegere de probleme in C++ 1MML
68 Structuri de date - Culegere de probleme n C++ Liste
5. Liste
Problema 5.1 S bV LVVV b M bM@M M M M LM V MWMq b bV M VV g b bV gVV VVMVMVV V .
Rezolvare:
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct lista { int val; lista *urm; }; lista *pa,*pb,*pc; void main() { clrscr(); pa=new(lista); pb=new(lista); pc=new(lista); pa->urm=pb; pb->urm=pc; pc->urm=NULL; pa->val=5; pb->val=50; pc->val=500; printf("\n %d",pa->val); printf("\n %d",pa->urm->val); printf("\n %d",pa->urm->urm->val); getch(); }
Problema 5.2 Defini g M b bM@M M M M . 1VLM gVVV VM@ MMM VVMVM. 1g b M q b MWL q VL VVMVMM.
69 Structuri de date - Culegere de probleme n C++ Liste
Rezolvare:
#include<stdio.h> #include<conio.h> #include<ctype.h> struct lista { int info; int vb; lista *urm; }; lista *cpl; int r1; char r2; int n,elem; lista* creare(int nr) { lista *p1=NULL; if (!nr) return NULL; else { nr--; p1=new lista; printf("Dati informatia: "); scanf("%d",&p1->info); p1->vb=1; p1->urm=creare(nr); return p1; } } int stergere(lista *p1,int k,int opt) { int vbb=0; while ( (p1)&&(!vbb) ) { if (p1->info==k) { if (!opt) { p1->vb=0; vbb=1; }
70 Structuri de date - Culegere de probleme n C++ Liste
else { p1->vb=1; vbb=1; } } else p1=p1->urm; } if (vbb) return 1; else return 0; } void tipar(lista *p1) { while (p1) { if (p1->vb) printf(" %d ",p1->info); p1=p1->urm; } } void main() { clrscr(); printf("\nDati nr. de elem. ale listei: "); scanf("%d",&n); cpl=creare(n); tipar(cpl); printf("\nElem. pe care doriti sa-l stergeti: "); scanf("%d",&elem); r1=stergere(cpl,elem,0); if (!r1) printf("\nNu exista %d in lista! ",elem); else { printf("\n%d este sters temporar",elem); printf("\nLista este: "); tipar(cpl); printf("\nDoriti sa-l rescrieti(y/n)"); r2=getche(); if (tolower(r2)==y) { r1=stergere(cpl,elem,1); printf("\nLista devine: "); tipar(cpl); } } 71 Structuri de date - Culegere de probleme n C++ Liste
getch(); }
Problema 5.3 Se consider b LV LM MV VVMVMV LV M L MM V M ` LW @WMbq LMV g @V . LV g @V MML LV LLMV V @VMM MVVV VbVMV M b .
Rezolvare:
#include<stdio.h> #include<conio.h> #include<ctype.h> struct prod { char codp[5]; int cant; double pret; prod *urm; }; prod *lista=NULL; double tot; double total(prod *list) { double s=0; while (list) { s+=(double)(list->cant)*(list->pret); list=list->urm; } return s; } prod *formeaza(prod *list) { char sf=y; prod *el; double pret; while (tolower(sf)==y) { el=new prod; printf("\nNou produs.Dati codul= "); fflush(stdin); gets(el->codp); printf("In cantitate: "); scanf("%d",&el->cant); printf("La pretul: "); 72 Structuri de date - Culegere de probleme n C++ Liste
void nerec(nod *pp) { while (pp) { printf(" %d ",pp->info); pp=pp->urm; } } void rec(nod *pp) { if (pp) { rec(pp->urm); printf(" %d ",pp->info); 73 Structuri de date - Culegere de probleme n C++ Liste
Problema 5.5 bV bLV @MM @VMM WMMV WM MLV Vq MVMV bM M WV bV MV bM@M M M MV.
Rezolvare:
#include<stdio.h> #include<conio.h>
struct mat { int lin; 74 Structuri de date - Culegere de probleme n C++ Liste
int col; int info; mat *urm; };
typedef int matrice[10][10]; typedef matrice *pmatrice; int n1,contor1,contor2; int vb1,vb2; mat *cpla,*cplb,*cplc,*pa,*pb,*pc,*prar; pmatrice pm1,pm2;
mat *creare(pmatrice pm) { int vb=0; mat *cpl=NULL,*pa,*prar; for(int i=0;i<n1;i++) for(int j=0;j<n1;j++) if (*pm[i][j]) { pa=new mat; pa->lin=i; pa->col=j; pa->info=*pm[i][j]; if (!vb) { cpl=pa; prar=pa; pa->urm=NULL; vb=1; } else { prar->urm=pa; prar=pa; } } pa->urm=NULL; return cpl; }
void tipareste(mat *pl) { while (pl) { printf("\nLinia:%1d Coloana:%1d Info:%d ",pl->lin,pl->col,pl->info); pl=pl->urm; 75 Structuri de date - Culegere de probleme n C++ Liste
} }
void copie(mat *px,mat *pc) { mat *temp; int vb; if (!pc) vb=0; else vb=1; while (px) { temp=new mat; if (!vb) { cplc=temp; vb=1; } temp->lin=px->lin; temp->col=px->col; temp->info=px->info; pc->urm=temp; pc=temp; px=px->urm; } pc->urm=NULL; } void main() { clrscr(); printf("\nDati nr. de linii si coloane pt. matricea A si B: "); scanf("%d",&n1); pm1=new matrice[10]; pm2=new matrice[10]; contor1=contor2=0; vb1=vb2=0; printf("\nDati elem. matricei A: \n"); for(int i=0;i<n1;i++)
for(int j=0;j<n1;j++) { printf(" A[%1d][%1d]=",i,j); scanf("%d",&pm1[i][j]); if (!(*pm1[i][j])) 76 Structuri de date - Culegere de probleme n C++ Liste
contor1++; } printf("\nDati elem. matricei B: \n"); for(i=0;i<n1;i++) for(int j=0;j<n1;j++) { printf(" B[%1d][%1d]=",i,j); scanf("%d",&pm2[i][j]); if (!(*pm2[i][j])) contor2++; } printf("\nMatricea A este: \n"); for(i=0;i<n1;i++) { for(int j=0;j<n1;j++) printf(" %d ",*pm1[i][j]); printf("\n"); } printf("\nDin %d elemente, %d elemente sunt zero",n1*n1,contor1); getch(); printf("\nMatricea B este: \n"); for(i=0;i<n1;i++) { for(int j=0;j<n1;j++) printf(" %d ",*pm2[i][j]); printf("\n"); } printf("\nDin %d elemente, %d elemente sunt zero",n1*n1,contor2); getch(); cpla=creare(pm1); printf("\n\nMatricea A devine:\n"); tipareste(cpla); getch(); cplb=creare(pm2); printf("\n\nMatricea B devine:\n"); tipareste(cplb); getch(); delete pm1; delete pm2; pa=cpla; pb=cplb;
vb1=0; cplc=pc=NULL; while ( (pa)&&(pb) ) { if (pa->lin<pb->lin) 77 Structuri de date - Culegere de probleme n C++ Liste
else if (pa->col<pb->col) { pc=new mat; pc->lin=pa->lin; 78 Structuri de date - Culegere de probleme n C++ Liste
pc->col=pa->col; pc->info=pa->info; pc->urm=NULL; if (!vb1) { cplc=pc; vb1=1; prar=pc; } else { prar->urm=pc; prar=pc; }; pa=pa->urm; } else if (pa->col>pb->col) { pc=new mat; pc->lin=pb->lin; pc->col=pb->col; pc->info=pb->info; pc->urm=NULL; if (!vb1) { cplc=pc; vb1=1; prar=pc; } else { prar->urm=pc; prar=pc; }; pb=pb->urm; } else { pc=new mat; pc->lin=pb->lin; pc->col=pb->col; pc->info=pb->info+pa->info; pc->urm=NULL; if (!vb1) { cplc=pc; 79 Structuri de date - Culegere de probleme n C++ Liste
vb1=1; prar=pc; } else { prar->urm=pc; prar=pc; }; pa=pa->urm; pb=pb->urm; } } if (!pa) if (pb) copie(pb,pc); if (!pb) if (pa) copie(pa,pc); printf("\n\nMatricea C este: \n"); tipareste(cplc); getch(); }
Problema 5.6 S bV LMbMbL b M bM@M M M M g @ b bV bLV MML V WV MbVV MMM VVMVM M b q bgM bV g n interiorul ei.
Rezolvare:
#include<stdio.h> #include<conio.h> struct lista { int info; lista *urm; }; lista *cpl,*p,*p1;
void scriere(lista *pp) { printf("\n"); while (pp) { printf(" %d ",pp->info); pp=pp->urm; 80 Structuri de date - Culegere de probleme n C++ Liste
} }
lista *inser1(lista *p1,int k) { lista *pp; pp=new lista; pp->info=k; pp->urm=p1; return pp; } lista *inser2(lista *p1,int k) { lista *pp,*p=p1; pp=new lista; pp->info=k; pp->urm=NULL; if (p) { while (p->urm) p=p->urm; p->urm=pp; } else p1=p; return p1; } lista *inser3(lista *p1,int n,int k) { lista *pp=p1,*q; for(int i=0;i<n-1;i++) p1=p1->urm; q=new lista; q->info=k; q->urm=p1->urm; p1->urm=q; return pp; }
void main() { clrscr(); printf("\nIntroduceti elem. listei: \n"); p=new lista; cpl=p; scanf("%d",&p->info); 81 Structuri de date - Culegere de probleme n C++ Liste
Problema 5.8 bV LMbMbL b WMM M M M LM VVMVMV citite dintr-MM VL. bV bLV MML VLMb WV @ V bV M MVV bVMbM. bV bLV MML WV bVVV MMM VVMVM WM b .
Rezolvare:
#include<stdio.h> #include<conio.h> struct listad { listad *p_anterior; listad *p_urmator; int val1,val2; 83 Structuri de date - Culegere de probleme n C++ Liste
};
const int x[5]={1,10,100,1000,10000}; const int y[5]={20000,2000,200,20,2}; listad *pprec,*pcurent,*purm,*ppp_pr,*ppp_ur,*pelem;
void main() { clrscr(); pprec=new listad; ppp_pr=pprec; pprec->p_anterior=NULL; pprec->val1=x[0]; pprec->val2=y[0]; for(int i=1;i<5;i++) { pcurent=new listad; pprec->p_urmator=pcurent; pcurent->p_anterior=pprec; 84 Structuri de date - Culegere de probleme n C++ Liste
#include<stdio.h> #include<conio.h> struct structura { int nrlin,nrcol; int val; structura *pstr; }; structura *cpl,*p1,*p2; int linii; int vb=0; 85 Structuri de date - Culegere de probleme n C++ Liste
while (p1) {printf("\nnr.linii %d nr.coloane %d valoare %d " ,p1->nrlin,p1->nrcol , p1->val); p1=p1->pstr; } getch(); }
Problema 5.10 Scrie MML MVVLMb LV VVLMV L@VV MMV bVq LM VMMV VVMVMV LV M L MM V M V M ML WVL MM MV b@VLL WV @MVM.
86 Structuri de date - Culegere de probleme n C++ Liste
Rezolvare:
#include<stdio.h> #include<conio.h>
struct lista { int info; lista *urm; }; lista *cp1,*cp2,*p1,*p2;; int nr,val;
lista *creare(int nr) { lista *p1; if (!nr) return NULL; else { nr--; p1=new lista; printf("Dati informatia: "); scanf("%d",&p1->info); p1->urm=creare(nr); return p1; } }
void tiparire(lista *pp) { printf("\n"); while (pp) { printf(" %d ",pp->info); pp=pp->urm; } } lista *copiere(lista *pp,int k) { lista *p1,*p2,*cp2; int vb=0; cp2=NULL; if (!pp) return NULL; else 87 Structuri de date - Culegere de probleme n C++ Liste
{ while (pp) { if (pp->info>k) { p2=new lista; p2->info=pp->info; if (!vb) { cp2=p2; vb=1; p1=p2; } else { p1->urm=p2; p1=p2; } }
pp=pp->urm; } if (cp2) p1->urm=NULL; return cp2; } } void main() { clrscr(); printf("\nDati nr. de elem. ale listei: "); scanf("%d",&nr); cp1=creare(nr); printf("\nLista initiala este: "); tiparire(cp1); printf("\nDati valoarea parametrului: "); scanf("%d",&val); cp2=copiere(cp1,val); printf("\nNoua lista este: "); tiparire(cp2); getch(); }
Problema 5.11 1WV g @M bVV @VbV MW WbMV MM @WMbV WVLVq bMW bMLM WV WV WV @ b M bM@M M M M .
88 Structuri de date - Culegere de probleme n C++ Liste
Rezolvare:
#include<stdio.h> #include<ctype.h> #include<conio.h> struct lista { char nume[20]; lista *urm; }; lista *cp1=NULL; int nr=0; char opt;
lista *adaug(lista *pp) { lista *p1,*p2; p1=new lista; p1->urm=NULL; nr++; printf("\nPozitia %d Nume client: ",nr); fflush(stdin); gets(p1->nume); if (!pp) return p1;
void tipar(lista *pp) { int n=1; while (pp) { printf("\n%d %s",n++,pp->nume); pp=pp->urm; } } 89 Structuri de date - Culegere de probleme n C++ Liste
lista *servire(lista *pp) { lista *p1,*p2,*p; int poz; if (!nr) { printf("Nu sunt clienti in lista! "); return pp; } else { printf("\nServim clientul de pe pozitia: "); scanf("%d",&poz); if (poz>nr) { printf("\nUltimul client se afla pe poz.%d ",nr); return pp; } else if (poz==1) { printf("\nServim normal pe clientul.%s ",pp->nume); nr--; p1=pp->urm; delete pp; return p1; } else { p1=p2=pp; p=pp; for(int i=0;i<poz-1;i++) { p2=p1; p1=p1->urm; } printf("Servim peste rand pe clientul. "); puts(p1->nume); p2->urm=p1->urm; delete p1; return p; } } } void main() { clrscr(); do { printf("\nOptiuni de lucru: "); printf("\n - A - adaugare client la lista "); 90 Structuri de date - Culegere de probleme n C++ Liste
printf("\n - S - servire client "); printf("\n - P - tiparire lista "); printf("\n - Q - terminare lucru \n"); printf("Optiunea:"); fflush(stdin); scanf("%c",&opt); switch (opt=toupper(opt)) { caseA: cp1=adaug(cp1); break; caseS: cp1=servire(cp1); break; caseP: tipar(cp1); break; } } while (opt==A||opt==S||opt==P) ; }
Problema 5.12 iMM V @Vq MVV g MMV MM V utile dintr- b . 1b MML V LV WM@ @VVq VMMV MM @MV MM VL LM V LM@MVMV LV bLMV VMVV parcurgeri listei.
Rezolvare:
#include<stdio.h> #include<conio.h>
struct lista { int b; lista *urm; }; typedef int vec[3]; typedef vec *pv; lista *cpl; int n; vec vect; pv pvec; lista *creare(int n) { lista *prad; if (!n) return NULL; else { n--; prad=new lista; printf("Dati elem.: "); scanf("%d",&prad->b); 91 Structuri de date - Culegere de probleme n C++ Liste
void main() { clrscr(); printf("\nDati nr. de elem. ale listei: "); scanf("%d",&n); cpl=creare(n); pvec=numara(cpl); printf("\nNr de elem. pozitive ale listei este: %d ",*pvec[0]); printf("\nNr de elem. negative ale listei este: %d ",n-(*pvec[0]+*pvec[2])); printf("\nNr de elem. nule ale listei este: %d ",*pvec[2]); getch(); }
Problema 5.13 LV @MM @VMM MbVV MMM VVMVM M-o b bM@M M M M V L M VVMVMV bMM WV_ bV LVbL q @V @ LV @ bV b WM .
92 Structuri de date - Culegere de probleme n C++ Liste
Rezolvare:
#include<stdio.h> #include<conio.h> struct lista { int info; lista *urm; }; lista *cpl,*pnou; lista *pp,*pp1,*pp2; char rasp; int k,vb;
lista *creare(char r) { lista *p1; int k1; if (r==n) return NULL; else { printf("\nDati un elem.: "); scanf("%d",&k1); p1=new lista; p1->info=k1; printf("\nIntroduceti elem.?[d/n] "); r=getche(); p1->urm=creare(r); return p1; } } void afisare(lista *p) { printf("\n"); if (!p) printf("\nLista vida"); else while (p) { printf(" %d ",p->info); p=p->urm; } }
93 Structuri de date - Culegere de probleme n C++ Liste
void main() { clrscr(); printf("Construiti lista?[d/n] "); scanf("%c",&rasp); cpl=creare(rasp); afisare(cpl); printf("\nDati elem. de inserat: "); scanf("%d",&k); if (!cpl) printf("\nLista vida"); else if (k<cpl->info) { pp=new lista; pp->urm=cpl; cpl=pp; pp->info=k; printf("\nElem. a fost inserat\n "); } else { vb=0; pp1=cpl; pp2=pp1->urm; while ( (pp2)&&(!vb) ) { if ( (pp1->info<k)&&(pp2->info>k)) { pnou=new lista; pnou->urm=pp2; pp1->urm=pnou; pnou->info=k; vb=1; } else if ( (pp1->info==k)||(pp2->info==k)) vb=2; pp1=pp2; pp2=pp2->urm; } } if (vb==1) printf("\nElem. a fost inserat\n "); else if (vb==2) printf("\nElem. exista deja "); else if (!pp2->urm) { pnou=new lista; pp1->urm=pnou; 94 Structuri de date - Culegere de probleme n C++ Liste
pnou->info=k; pnou->urm=NULL; printf("\nElem. a fost inserat\n "); } afisare(cpl); getch(); }
Problema 5.14. Scrie g @V MML LV LMLVMV b LM copie a sa.
struct lista { int a; lista *urm; }; int nr; lista *cap,*cap1,*cap2; lista *creare(int nr) { lista *p; if (!nr) return NULL; else { nr--; p=new lista; printf("informatia: "); scanf("%d",&p->a); p->urm=creare(nr); return p; } }
void tiparire(lista *p) { printf("\n"); while (p) 95 Structuri de date - Culegere de probleme n C++ Liste
{ printf(" %d ",p->a); p=p->urm; } } lista *concatenare(lista *p1,lista *p2) { lista *p=p1; if (!p1) return p2;
else if (!p2) return p1; else { while (p->urm) p=p->urm; p->urm=p2; return p1; } }
lista *copiere(lista *pp1) { lista *p,*p1,*p2; if (!pp1) return NULL; else { p1=new lista; p1->a=pp1->a; p=p1; pp1=pp1->urm; while (pp1) { p2=new lista; p2->a=pp1->a; p1->urm=p2; p1=p2; pp1=pp1->urm; } p1->urm=NULL; return p; } }
void main() { clrscr(); printf("\nNumar elem.: "); scanf("%d",&nr); 96 Structuri de date - Culegere de probleme n C++ Liste
Problema 5.15 LV g @V MML LV VL WL MLV @ L V L V VVMVMV bMM bLV M- b q LM MV MMM elemente ale diagonalei principale.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<ctype.h> struct lista { int lin; int col; int val; lista *urm; }; int nr; lista *cpl; lista *creare(int nr) { lista *pnou; if (!nr) return NULL; else { nr--; pnou=new lista; printf("Linia: "); scanf("%d",&pnou->lin); printf("Coloana: "); scanf("%d",&pnou->col); printf("Valoarea: "); scanf("%d",&pnou->val); 97 Structuri de date - Culegere de probleme n C++ Liste
pnou->urm=creare(nr); return pnou; } } verifica(lista *pl) { while (pl) if ( (pl->lin)!=(pl->col) )
void main() { clrscr(); printf("\nDati nr. de elem. nenule: "); scanf("%d",&nr); printf("\nDati elem. nenule ale matricei: \n"); cpl=creare(nr); if (!verifica(cpl)) printf("\nMatricea contine numai elem. diagonalei principale "); else printf("\nMatricea nu contine numai elem. diagonalei principale "); getch(); }
Problema 5.16 Scrie g @V MML LV VMMV MMMV M MMV bV MV bM@M M M MV.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<ctype.h> struct lista 98 Structuri de date - Culegere de probleme n C++ Liste
{ int info; lista *urm; }; int n,lg; lista *cpl; lista *creare(int n) { lista *ps1,*p1,*p2; p1=new lista; printf("Dati primul elem. al listei: "); scanf("%d",&p1->info); ps1=p1; for(int i=2;i<=n;i++) { p2=new lista; printf("Dati elem.: "); scanf("%d",&p2->info); p1->urm=p2; p1=p2; } p1->urm=NULL; return ps1; } int lungime(lista *pl) { int nr=0; while (pl) { nr++; pl=pl->urm; } return ((nr*sizeof(lista))+sizeof(cpl)); } void tiparire(lista *pp) { printf("\n"); while (pp) { printf(" %d ",pp->info); pp=pp->urm; } } void main() { clrscr(); printf("\nDati nr. de elem. al listei: "); scanf("%d",&n); cpl=creare(n); printf("\nElementele listei sunt: "); 99 Structuri de date - Culegere de probleme n C++ Liste
tiparire(cpl); lg=lungime(cpl); printf("\nLungimea in BYTE a listei este: %d ",lg); getch(); }
Problema 5.17 Scrie @MM LV VV bV MMV bV bM@M M M MV LM LMbVV VLM V M.
Rezolvare:
#include<stdio.h> #include<conio.h> struct lista { int info; lista *urm; };
lista *cp1,*p1; int nr,k=1,temp; lista *creare(int nr) { lista *p1; if (!nr) return NULL; else { nr--; p1=new lista; printf("Dati informatia: "); scanf("%d",&p1->info); p1->urm=creare(nr); return p1; } }
void main() { clrscr(); printf("\nDati nr. de elem. ale listei: "); scanf("%d",&nr); cp1=creare(nr); p1=cp1; while (k) { k=0; 100 Structuri de date - Culegere de probleme n C++ Liste
void main() { clrscr(); printf("\nDati n: "); scanf("%d",&n); corp(); getch();} 103 Structuri de date - Culegere de probleme n C++ Stive
6. Stive
Problema 6.1 Se realizeaz VWVM MVV VbVMV M-o magazie. La intrarea n stoc a materialelor pe baza de factura, se WM VVMVMV MMV bV. 1 VVV b@V LMbMM @WMLq bV gVV M bV. 1LVbM LMMM WM@ LMM VM M bM consumuri de mVV. V bLV g MML @VMM MMM V facturilor.
STIVA *adaug(STIVA *p1, int cant){ STIVA *p2; p2=(STIVA *)malloc(sizeof(STIVA)); p2->cant=cant; p2->paep=p1; return p2; } STIVA *sterge(STIVA *p1){ STIVA *p2; if(!p1) return 0; else{ printf("\n%d",p1->cant); p2=p1->paep; free(p1); return p2; } } 104 Structuri de date - Culegere de probleme n C++ Stive
int lungime(STIVA *p1){ int n=0; while(p1){ n++; p1=p1->paep; } return n; }
void tiparire(STIVA * p1){ printf("\n"); while(p1){ printf(" %d ",p1->cant); p1=p1->paep; } } void main(){ clrscr(); printf("\nOptiune[a/s/n/p/t]: "); scanf("%c",&optiune); do{ switch((tolower(optiune))){ case a: printf("\nCantitate: "); scanf("%d",&cantt); vs=adaug(vs,cantt); break; case s: vs=sterge(vs); break; case n: lung=lungime(vs); printf("\nLungime: %d",lung); break; case p:tiparire(vs); break; case t:return; default: printf("\nParametru necunoscut!"); } printf("\nOptiune [a/s/n/p/t]: ");fflush(stdin); scanf("%c", &optiune); }while(optiune!=t); } 105 Structuri de date - Culegere de probleme n C++ Stive
Problema 6.2 V LMbWV b MVW WV MMMV - . LV MM @VMM WVLV MVMV LM@V WV LVb b .
Rezolvare:
#include<conio.h> #include<stdio.h> #include<alloc.h> typedef struct stiva{ int cod; int cant; int pret; stiva *paep; }STIVA; STIVA *vs=0; int lung,cantt,codd,prett,n;
void main(){ do{ vs=cr_stiva(vs); printf("\nApasati <ENTER> pt.introducerea urm.element "); fflush(stdin); scanf("%c",c); 111 Structuri de date - Culegere de probleme n C++ Stive
void main(){ clrscr(); STIVA *va; printf("\nDati nod stiva(pt sfarsit tastati 0): "); scanf("%d",&n); while(n){ pvm=(STIVA *)malloc(sizeof(STIVA)); pvm->util=n; pvm->adr=vsm; vsm=pvm; printf("\nDati nod stiva(pt sfarsit tastati 0): "); scanf("%d",&n); } int nr=numara(vsm); printf("\n"); printf("\nNr de elemente din lista initiala: %d",nr); printf("\n");
115 Structuri de date - Culegere de probleme n C++ Stive
if(!(nr%2)){ nr=nr/2; vsm1=divizare(vsm,nr); printf("\nSTIVA 1: "); tiparire(pvm); printf("\nSTIVA 2: "); tiparire(vsm1); } else printf("\nStiva nu contine nr par de elemente! "); printf("\n"); getch(); }
116 Structuri de date - Culegere de probleme n C++ Arbori
7. Arbori
Problema 7.1 bV bLV @MM LV VV `
-LVV V M WV L MV -parcurgere arbore; -nserare nod n arbore; -gVVV MW WM V -WVVMMV M M VM -determinarea gVM VM -VLV WL V V M LM@V bM MM.
Rezolvare:
#include<stdio.h> #include<conio.h> struct arb { int info; arb*st,*dr; }; arb *root=NULL,*p; int nr,nr1,nr2,i; char c; void inserare(arb *&r,int k) { arb *pp; if (!r) { pp=new arb; pp->info=k; pp->st=NULL; pp->dr=NULL; r=pp; } else if (r->info>k) inserare(r->st,k);
117 Structuri de date - Culegere de probleme n C++ Arbori
else if (r->info<k) inserare(r->dr,k); else printf("Elementul deja exista! \n"); } void sterg( arb *&r) { if (r->dr) sterg(r->dr); else { arb *a=r; r=r->st; delete a; } } void stergere( int x, arb *&p) { arb *q; if (!p) printf("\nElement negasit! \n"); else if (x<p->info) stergere(x,p->st); else if (x>p->info) stergere(x,p->dr); else { q=p; if (!q->dr) { p=q->st; delete q; } else if (!q->st) { p=q->dr; delete q; } else sterg(q->st); } } 118 Structuri de date - Culegere de probleme n C++ Arbori
int max(int x,int y) { if (x<y ) return y; else return x; } int nivel(arb *r) { if (!r) return 0; else return (1+max( nivel(r->st),nivel(r->dr) )); } void preordine(arb *r) { if (r) { printf(" %d ",r->info); preordine(r->st); preordine(r->dr); } } void inordine(arb *pp) { if (pp) { inordine(pp->st); printf(" %d ",pp->info); inordine(pp->dr); } } void postordine(arb *pp) { if (pp) { postordine(pp->st); postordine(pp->dr); printf(" %d ",pp->info); } } int frunza(arb *pp) { return ( (!pp->st)&&(!pp->dr) ); } int nrfrunze(arb *r) { if (!r) return 0;
119 Structuri de date - Culegere de probleme n C++ Arbori
else if (frunza(r)) return 1; else return (nrfrunze(r->st)+nrfrunze(r->dr) ); } int putere(int nr,int k) { if (!k) return 1; else return (nr*putere(nr,k-1)); } int numara(arb *r) { if (!r) return 0; else return ( 1 + numara(r->st) + numara(r->dr) ); } void main() { clrscr(); printf("Nr.:"); scanf("%d",&nr); while (nr!=-1) { inserare(root,nr); printf("Nr.:"); scanf("%d",&nr); } inordine(root); printf("\n"); postordine(root); printf("\n"); preordine(root); printf("\n"); printf("\nCe nod doriti sa stergeti? "); scanf("%d",&nr); stergere(nr,root); preordine(root); printf("\n"); printf("\nVERIFICARE ARBORE\n "); nr=nrfrunze(root); nr1=numara(root); printf("\nArborele are %d noduri ",nr1); nr1=numara(root); printf("\nGreutatea arborelui (noduri terminale): %d ",nr); i=nivel(root); printf("\nInaltimea arborelui: %d ",i); 120 Structuri de date - Culegere de probleme n C++ Arbori
nr2=putere(2,i); if (nr1==(nr2-1)) printf("\nArbore complet - contine nr. max. de noduri pt. inaltimea sa "); else printf("\nArborele nu este complet "); getch(); }
Problema 7.2 S bV bLV MML LV VV L@VV MMM V g @ b bV gVV MWMV LVbM.
Problema 7.3 bV LMbMbL MM V M LM VVMVMV citite dintr-MM VLq b bV bLV MML V LV VMMV WVb MMM MMM nod al arborelui, nod de care se va lega un alt arbore construit cu elemente citite tot dintr-un vector.
Rezolvare:
#include<stdio.h> #include<conio.h> struct arb { int val; arb *st; arb *dr; }; typedef int mat[7]; mat y={111,112,11,121,122,12,1} ; mat z={200,300,400,500,600,700,800} ; arb *rad,*stg,*drt,*pstg,*pdrt,*p=NULL; 122 Structuri de date - Culegere de probleme n C++ Arbori
Problema 7.5 bV LMbMbL MM V M M LV MM M VbV L M WM- V MV g MM @MV b bM@M M M M . bV bLV MML WV LMbMV b q LMbMV Vq @ V b q @ V V g WV L MV MMM MW WM V LM valoare W .
125 Structuri de date - Culegere de probleme n C++ Arbori
Rezolvare:
#include<stdio.h> #include<conio.h> #include<string.h> #include<alloc.h> struct lista { char nume[30]; lista *urm; }; struct arb { int val; lista *capl; arb *st,*dr; }; arb *pp,*pcaut=NULL; int cod; void listare(lista *cap) { while(cap) { printf(" %s ",cap->nume); cap=cap->urm; } printf("\n"); } lista *inssr(lista *cap,char st[20]) { if (cap) { cap->urm=inssr(cap->urm,st); return cap; } else { lista *p=(lista*)malloc(sizeof(lista)); strcpy(p->nume,st); p->urm=NULL; return p; }; } lista *constr_lista() { lista *cp=NULL; char str[20]; 126 Structuri de date - Culegere de probleme n C++ Arbori
while (printf("Dati nume:"),fflush(stdin),gets(str),strcmp(str,"*")) cp=inssr(cp,str); return cp; } arb *constr_nod(arb *st,int val1,lista *cap,arb *dr) { arb *p; p=new arb; p->val=val1; p->capl=cap; p->st=st; p->dr=dr; return p; } lista *concatenare(lista *cap1,lista *cap2) { lista *pp; if (!cap1) return cap2; else { pp=cap1; while (cap1->urm) cap1=cap1->urm; cap1->urm=cap2; return pp; } } arb *constr_arb() { arb *b1,*b2,*c1,*c2,*b,*c,*nod; lista *capb1,*capb2,*capc1,*capc2,*capa,*capb,*capc; printf("\n\nConstruiti lista b1: \n"); capb1=constr_lista(); listare(capb1); printf("\n\nConstruiti lista b2:\n "); capb2=constr_lista(); listare(capb2); printf("\n\nConstruiti lista c1:\n "); capc1=constr_lista(); listare(capc1); printf("\n\nConstruiti lista c2:\n "); capc2=constr_lista(); listare(capc2); 127 Structuri de date - Culegere de probleme n C++ Arbori
b1=constr_nod(NULL,21,capb1,NULL); b2=constr_nod(NULL,22,capb2,NULL); c1=constr_nod(NULL,31,capc1,NULL); c2=constr_nod(NULL,32,capc2,NULL); capb=concatenare(capb1,capb2); printf("\n\nlista capb:\n"); listare(capb); capc=concatenare(capc1,capc2); printf("\n\nlista capc:\n"); listare(capc); b=constr_nod(b1,2,capb,b2); c=constr_nod(c1,3,capc,c2); capa=concatenare(capb,capc); printf("\n\nlista capa:\n"); listare(capa); nod=constr_nod(b,1,capa,c); return nod; } void caut_nod(arb *rad,int elem) { if (rad) if (rad->val==elem) pcaut=rad; else { caut_nod(rad->st,elem); caut_nod(rad->dr,elem); } } void tiparire(arb *r) { if (r) { printf(" %d ",r->val); listare(r->capl); tiparire(r->st); tiparire(r->dr); } } void main() { clrscr(); pp=constr_arb(); printf("\n\nArborele este:\n "); tiparire(pp); printf("\ndati cod: "); scanf("%d" ,&cod); 128 Structuri de date - Culegere de probleme n C++ Arbori
caut_nod(pp,cod); if (pcaut) listare(pcaut->capl); else printf("\nElement negasit in arbore! "); getch(); }
Problema 7.6 Scrie g @V MML WV L@VV MMM V M.
Rezolvare:
#include<stdio.h> #include<conio.h> struct nod { int val; nod *st,*dr; }; nod *rad1=NULL,*rad2,*rr; int nr; void creare(nod *&r,int k) { nod *pp; if (!r) { pp=new nod; pp->val=k; pp->st=NULL; pp->dr=NULL; r=pp; } else if (r->val>k) creare(r->st,k); else if (r->val<k) creare(r->dr,k); else printf("Elementul deja exista! \n"); }
129 Structuri de date - Culegere de probleme n C++ Arbori
130 Structuri de date - Culegere de probleme n C++ 1gVV
8. FiLHUH
Problema 8.1 bV bLV @MM LV VV gV @V VLM LM MMMM MMM gV WV @ V. iMMVV gVMM bV W M M WV LMMW . 1VMM VVLM @MMM M b MM gV WV VbV 11111.. 1MbV M VVLM V a programului se face cu comanda
C:\>Prob81 FISIER.TST
Rezolvare:
#include<stdio.h> #include<stdlib.h> #include<CONIO.H> void main(int argc,char *paramstr[]){ FILE*pf; int c; clrscr(); if(argc==2){ if(!(pf=fopen(paramstr[1],"r"))) {printf("\nNu se poate deschide fisierul!"); exit(1);} puts("\n"); } else {printf("\nNr necorespunzator de argumente in linia de comanda!"); exit(1);} while((c=getc(pf))!=EOF) putc(c,stdout); fclose(pf); }
131 Structuri de date - Culegere de probleme n C++ 1gVV
Problema 8.2 S bV bLV @MM LV LWL g WVLWL LM MMM MMM gV. 1MM L MMMVV gVMM WM M WV LMMW g M MML V WV @ MMV` - LWL LM MMM gVMM b@VLL g MM gV VM V MM 1WWVMq WL gV bLMMb [MM @MV vizualizat cu comanda DIR ); - WVLWL MM gV L@ g bVV MM WV gV normal
Rezolvare:
#include<stdio.h> #include<conio.h> #include<dos.h> #include<io.h>// pt prototipul functiei unlink #include<string.h> #include<process.H> void main(int argc,char *paramstr[]){ clrscr(); unsigned char opt; int c; FILE *fp,*fp1; if(argc==2){ if(!(fp=fopen(paramstr[1],"r"))){ printf("\nNu se poate deschide fisierul!"); exit(1);} puts("\n"); if(!(fp1=fopen("TEMP.TXT","w"))) {printf("\nNu se poate deschide fisierul!"); exit(1);} } else{ printf("nr necorespunzator de parametri in linia de comanda!"); exit(1);}
printf("\nCriptare/Decriptare (c/d) ?"); fflush(stdin); scanf("%d",&opt); while((c=getc(fp))!=EOF){ if(c>125)putc((char(c-125)),fp1); 132 Structuri de date - Culegere de probleme n C++ 1gVV
typedef struct tnod{ unsigned char info; int nr; struct tnod*paep; }TNOD; typedef enum{false,true} boolean; boolean vb; int contor1=0; int contor2=0; TNOD * vs; FILE * fp; unsigned char c;
TNOD * inserare( TNOD* ps, unsigned char cc){ TNOD * p; p=(TNOD*)malloc(sizeof(TNOD)); p->info=cc; p->nr=1; p->paep=ps; return(p); } boolean verificare(TNOD *ps, unsigned char cc){ if(ps==NULL)return false; else{ while((ps->info!=cc)&&(ps->paep!=0))ps=ps->paep; if(ps->info==cc){ (ps->nr)++; return true; } else return false; } } 134 Structuri de date - Culegere de probleme n C++ 1gVV
void scriere(TNOD* ps){ if(!ps) printf("\nStiva vida"); else while(ps){ switch(ps->info){ case 13:printf("\n CR %d",ps->nr); case 10:printf("\n LF %d",ps->nr); default:printf("\n%c %d",ps->info,ps->nr); } ps=ps->paep; } } void numarare(TNOD * ps){ while(ps){ contor1+=ps->nr; contor2++; ps=ps->paep; } } TNOD * ordonare(TNOD *ps){ unsigned char vb; TNOD *p1,*p2,*temp; vb=0; p1=ps; while(p1){ p2=p1->paep; while(p2!=0){ if(p1->nr<p2->nr){ temp->info=p1->info; temp->nr=p1->nr; p1->info=p2->info; p1->nr=p2->nr; p2->info=temp->info; p2->nr=temp->nr; } p2=p2->paep; } if(!vb){ ps=p1; vb=1; 135 Structuri de date - Culegere de probleme n C++ 1gVV
} p1=p1->paep; } return ps; }
void main(int argc,char *paramstr[]){ clrscr(); FILE*fp; int c; vs=0; if(argc==2){ if((fp=fopen(paramstr[1],"r"))==0) {printf("\nNu se poate deschide fisierul!"); exit(1);} puts("\n"); } else {printf("\nNr necorespunzator de argumente in linia de comanda!"); exit(1);} while((c=getc(fp))!=EOF){ vb=verificare(vs,c); if(!vb) vs=inserare(vs,c); } fclose(fp); scriere(vs); vs=ordonare(vs); printf("\nStiva ordonata!"); scriere(vs); numarare(vs); printf("\nNr de caractere din fisier: %d",contor1); printf("\nNr de caractere distincte: %d",contor2); }
136 Structuri de date - Culegere de probleme n C++ Obiecte
9. Obiecte
Problema 9.1 S bV WVMVbL MM VL LV LM MV W WV @ L g @LVWM . 1LM LM MV MWM M MM VL WV M VVMVMV WV @ MV. bV gVV VVMVMVV VLMM bMW MM @MV L g MM @MV VL. bV @VVV @LVWM WVM M LWM obiectului.
Rezolvare:
#include<stdio.h> #include<conio.h> typedef int a[10]; struct b { a c; }; class d { public: struct b e; void beta(); }; typedef b *pb; typedef d *pd; void d:: beta() { printf("\n aaa "); } ; a x; pb pp; pd ppp; void main() { clrscr(); for (int i=0;i<10; i++) x[i]=(i+1)*(i+1)*(i+1); pp=(b*)x; ppp=(d*)x; for(i=0;i<10;i++) printf("\n%5d %5d %5d",x[i],(*pp).c[i],(*ppp).e.c[i]); (*ppp).beta(); getch(); }
137 Structuri de date - Culegere de probleme n C++ Obiecte
Problema 9.2 Care este diferen MV LVV WM @MV WV M _b V VMV gV VLV WMV VV
Programul 1:
#include<stdio.h> #include<conio.h> class a { public: int x; int y; int suma (int u,int v); }; class b { public: a w; int z; int difer(a s,int t); }; int a::suma(int u,int v) { return u+v; }; int b::difer(a s,int t) { return (w.suma(s.x,s.y)-t*t); }; a gg; b jj; int aa,bb; void main() { clrscr(); gg.x=100; gg.y=222; jj.w.x=11; jj.w.y=33; jj.z=20; aa=gg.suma(gg.x,gg.y); bb=jj.difer(gg,jj.z); printf("\n aa=%d",aa); printf("\n bb=%d",bb); getch(); }
138 Structuri de date - Culegere de probleme n C++ Obiecte
Programul 2:
#include<stdio.h> #include<conio.h> class a { public: int x; int y; int suma (int u,int v); }; class b { public: a w; int z; int difer(a s,int t); }; int a::suma(int u,int v) { return u+v; }; int b::difer(a s,int t) { return (w.suma(s.x,s.y)-t*t); }; a gg; b jj; int aa,bb; void main() { clrscr(); gg.x=100; gg.y=222; jj.w.x=11; jj.w.y=33; jj.z=20; aa=gg.suma(gg.x,gg.y); bb=jj.difer(jj.w,jj.z); printf("\n aa=%d",aa); printf("\n bb=%d",bb); getch(); }
Problema 9.3 Indica LV gV @MM`
#include<stdio.h> #include<conio.h> typedef int a[10]; 139 Structuri de date - Culegere de probleme n C++ Obiecte
struct b { a c;}; class f { public: a p; void alfa(); }; void f:: alfa() { printf(" progr. orientata pe obiecte "); }; a x,*y; b m,n; f v,w; void main() { clrscr(); for (int i=0;i<10; i++) { x[i]=(i+1)*(i+1); m.c[i]=(i+1)*(i+1); v.p[i]=(i+1)*(i+1); }; y=&x; for(i=0;i<10;i++) printf("y[%d]=%4d\n",i,(*y)[i]); printf("\n"); n=m; w=v; for(i=0;i<10;i++) printf("n.c[%d]=%4d w.p[%d]=%4d\n",i,n.c[i],i,w.p[i]); w.alfa(); getch(); }
Rezolvare:
n urma execu LHLVHYDDILD
Y[0] = 1 Y[1] = 4 Y[2] = 9 Y[3] = 16 Y[4] = 25 140 Structuri de date - Culegere de probleme n C++ Obiecte
Problema 9.4 bV WVMVbL MM VL WV VLV @VMM LLMV MVW bMWVM WM-o gru@ .
Rezolvare:
#include<stdio.h> #include<conio.h> typedef int vec[5];
struct student { char nume[20]; int varsta; vec note; float med; }; class studobj { public: struct student datt; float medie(vec xx); 141 Structuri de date - Culegere de probleme n C++ Obiecte
142 Structuri de date - Culegere de probleme n C++ Obiecte
Problema 9.5 Defini MM VL MMM MMMLq MW bMLM [LV WVbLV MMMVVq bM q MMM M WV V g V MVVq MW WV @ MV @VMM WVbLVV bMM g WM @LVWM g MML V [@VMM LV WVq bLVV VM g LLM bM. LV programul care ntr- bMLM V@V @VV LVV WM @LVWM g MML @VMM LLMM bMM @VMM MM MMM WV MMML.
Rezolvare:
#include<stdio.h> #include<conio.h> struct pers { char nume[20]; int nrore; int salorar; int retineri; }; class muncitor { public: struct pers persoana; int salariu; void citeste(pers &ppersoana); int calcul(pers wpersoana); void scrie(pers tpersoana,int tsalariu); }; typedef muncitor *pmuncitor; void muncitor::citeste(pers &ppersoana) { printf("\nnume:"); fflush(stdin); gets(ppersoana.nume); printf("nr. ore lucrate:"); scanf("%d",&ppersoana.nrore); printf("salariu orar:"); scanf("%d",&ppersoana.salorar); printf("retineri:"); scanf("%d",&ppersoana.retineri); }; int muncitor::calcul(pers wpersoana) { return (wpersoana.nrore*wpersoana.salorar-wpersoana.retineri); }; void muncitor::scrie(pers tpersoana,int tsalariu) { printf("\n%s salariu: %d\n",tpersoana.nume,tsalariu); }; int nr,sal; pmuncitor pmuncit;
143 Structuri de date - Culegere de probleme n C++ Obiecte
Problema 9.6 1VM VLM bL MV g LLM V MVV MgLVq LM MMV WV MM VL WV VLVq M V WV VMM .
Rezolvare:
#include<stdio.h> #include<conio.h> struct mat { int cod; int cant; int pret; int intr; int ies; }; class stocmat { public: struct mat aaa; int valstoc(int c1,int p1); }; int stocmat:: valstoc(int c1,int p1) { return (c1*p1); };
stocmat vec[100]; int a,n; int valtot=0; void main() 144 Structuri de date - Culegere de probleme n C++ Obiecte
{ clrscr(); printf("\ndati numarul de materiale: "); scanf("%d",&n); for (int i=0;i<n; i++) { printf("\ndati cod material: "); scanf("%d",&vec[i].aaa.cod); printf("dati cantitatea:"); scanf("%d",&vec[i].aaa.cant); printf("dati pretul: "); scanf("%d",&vec[i].aaa.pret); printf("dati intrarile:"); scanf("%d",&vec[i].aaa.intr); printf("dati iesirile: "); scanf("%d",&vec[i].aaa.ies); if ((vec[i].aaa.intr==0)&&(vec[i].aaa.ies==0)) { a=vec[i].valstoc(vec[i].aaa.cant,vec[i].aaa.pret); valtot+=a; } }; printf("valoare totala a materialelor fara miscare este: %d",valtot); getch(); }
Problema 9.7 Defini g M MLV. 1VV LVb bMLM printr-un pointer membru al unui obiect, ntr- MML V MVMM LVMg VL.
Rezolvare:
#include<stdio.h> #include<conio.h> #include<alloc.h> typedef int mat[100][100]; typedef mat *pmat; class nod { public: pmat c; void init(int n,int m); void list(int n,int m); int suma(int n,int m);
}; void nod::init(int n,int m) { printf("\ndati elementele matricei: \n"); c=new mat[1]; 145 Structuri de date - Culegere de probleme n C++ Obiecte
void main() { int migr[41][41][2][2]; clrscr(); for(int i=0;i<41;i++) { for(int j=0;j<41;j++) { if(i!=j) { printf("De la judetul au migrat %d la judetul %d:\n",i+1,j+1); printf("In mediul urban %d persoane, din care %d M si %d F\n",migr[i][j][0][0]+migr[i][j][0][1],migr[i][j][0][0],migr[i][j][0][1]); printf("In mediul rural %d persoane, din care %d M si %d F\n",migr[i][j][1][0]+migr[i][j][1][1],migr[i][j][1][0],migr[i][j][1][1]); } 152 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
} } getch(); }
Problema 11.2. V LMbWV m LVL V L VVMVMV bM descrise cu n LLVbL. 1VLV LVLV V MM MMM VLV WV LM@MVMV. bV WVMVbL bMLM WV WV LV @VMV V@VVMV LVL M LM @LM M LV n=2 (nivVM g VLVM . bV LLMVV MVW g Wb@Vb @VMM VLV bVV.
float dispersie(Serie ser,float (*pf)(Serie)) { float disp=0; float med=(*pf)(ser); 153 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
Problema 11.3. V LMbWV MM MV M WM bVM MMV LM q VLV MVMM LVL MW WVbLbV MVV WbL@MVV WM M qZq H g WV bMW. bV Lalculeze mediile anuale g MVW VMV VL M bVM. 1VV bV M V definire.
clrscr(); printf("Matricola|Nume prenume|Medii ani| Media generala\n\n"); for(int i=0;i<n;i++) { memset(medii,0,sizeof(float)*ANI); med_gen=0.0; for(int j=0;j<ANI;j++) { 156 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
Problema 11.4. V LMbWV MM MV LM VMbV M- MM L V MMM MM WV@ gVgV V iq LMMbLM . 1M VLV LM bV nscriu cel mult M produse sau servic LV bV LM MV q indicndu-bV WVMMMVq MMV WV M bM q LMVq @V MMq 1. bV bLV @MM LV LLMV V @WMbV g bVL LM MV M- MM bMW VL WV LVq VL V LM Lespunzndu-i o componenta a unui vector g VL M @WMb bM bVLM LVb@MMMWM-i, de asemenea, o componenta n alt vector de articole.
Rezolvare:
#include <stdio.h> #include <conio.h>
#define N 30 #define M 20
struct ArticolFactura { char den[20]; int pu; char um[4]; int cant; int ctva; 157 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
};
struct Factura { int nra; int numar; char data[11]; ArticolFactura af[M]; };
void main() { int n,m; Factura fact[N];
double valf=0.0; double tvaf=0.0;
double total_fara_tva=0.0; double total_tva=0.0; double total=0.0; clrscr(); printf("Numar de facturi:"); scanf("%d",&n); for(int i=0;i<n;i++) { valf=0.0; tvaf=0.0; printf("\nNumar factura: "); scanf("%d",&fact[i].numar); fflush(stdin); printf("Data:"); gets(fact[i].data); printf("Articole in factura %d: ",i+1); scanf("%d",&fact[i].nra); fflush(stdin); for(int j=0;j<fact[i].nra;j++) { printf("\n\nDenumire:"); gets(fact[i].af[j].den); 158 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
fa31=new Factura; fa31->val=8000000.0;fa31->urm_fact=NULL; fa11->urm_fact=fa12; fa21->urm_fact=fa22; fa22->urm_fact=fa23; 160 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
161 Structuri de date - Culegere de probleme n C++ Agregarea structurilor
Problema 11.6. Se consider bMLM WV WV VVV @WMbV MW L MM ` WVMMMV @WMbq LMVMVV Vq LMVMV Vq @VLMM g MVW WV LLM @V MM @WMbMM. bV LVVV MM bMLM WV WV LV V M @Mb MM Wb @V M WV baz MM g MM MML V WV LLM @V MM @WMbMM.
istream & operator>>(istream &in,Adresa &a) { cout<<"Strada:"; in>>a.str; cout<<"Nr: "; in>>a.nr; cout<<"Judetul:"; in>>a.judet; return in; } 164 Structuri de date - Culegere de probleme n C++ Agregarea structurilor