Вы находитесь на странице: 1из 24

1.2.1---------------------#include <wx/wx.

h>
// application class
class wxMiniApp : public wxApp
{
public:
// function called at the application initialization
virtual bool OnInit();
// event handler for button click
void OnClick(wxCommandEvent& event) {
GetTopWindow()->Close();
}
};
IMPLEMENT_APP(wxMiniApp);
bool wxMiniApp::OnInit()
{
// create a new frame and set it as the top most application window
SetTopWindow( new wxFrame( NULL, -1, wxT(""), wxDefaultPosition, wxSize( 100, 50)
) );
// create new button and assign it to the main frame
new wxButton( GetTopWindow(), wxID_EXIT, wxT("Click!") );
// connect button click event with event handler
Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(wxMiniApp::OnClick) );
// show main frame
GetTopWindow()->Show();
// enter the application's main loop
return true;
}
l.1-------------------#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char **argv)
{ short a=0x0F0F;
long b=0x0F0F0F0F;
long long c=0x0F0F0F0F0F0F;
int d;
cout<<hex<<"a:"<<a<<" b:"<<b<<" c:"<<c<<endl;
d=a; cout<<hex<<d;
d=b; cout<<hex<<d;
return 0;
}

1|Page

P2.1--------------------------------------------//LUCRUL CU MASTI
#include <iostream>
using namespace std;

//intoarce primii 3 biti dintr-un nr. unsigned short


unsigned short f1(unsigned short a)
{
unsigned short masca=0x7;
return a & masca;
}
//intoarce bitii 16,15,14 (numerotati de la dr. la st. incepand cu 1)
unsigned long f2(unsigned long a)
{
unsigned long masca=0xE000;
unsigned long s;
s= a & masca;
s>>=13;
return s;
}
//intoarce bitii 25,24,23
unsigned short f3(unsigned long a)
{
unsigned long masca=0x1C00000;
unsigned long s;
s = a & masca;
s>>=22;
return (unsigned short)s;
}
//intoarce bitul 4
unsigned short f4(unsigned long a)
{
unsigned long masca=0x8;
unsigned long s;
s = a & masca;
s>>=3;
return (unsigned short)s;
}

int main(int argc, char **argv)


{ unsigned short x;
// x=f1(8);
//x=(unsigned short)f2(0x15A5FFF);
//x=f3(0xC00000);
x=f4(0x9);
cout<<"X:"<<hex<<x;
return 0;
2|Page

P2.2----------------------#include <iostream>
using namespace std;

// utilizare SAU EXCLUSIV (XOR)

int main(int argc, char **argv)


{ short a=5,b=10;
cout<<"Inainte --- a:"<<a<<" b:"<<b<<endl;
a=a^b;
b=a^b;
a=a^b;
cout<<"Dupa ---- a:"<<a<<" b:"<<b<<endl;
}
P3.1
//citirea unui tablou cu 2 dimensiuni (date tip numeric) din fisier
#include <iostream>
#include <malloc.h>
#include <string.h>
#include <fstream>
#include <iomanip>
using namespace std;
double **alocaMatrice(int m,int n)
{ double **a;
if((a=(double**)malloc(m*sizeof(double*)))==NULL) return NULL;
for(int i=0;i<m;i++)
{ if((a[i]=(double*)malloc(n*sizeof(double)))==NULL)
{ for(int k=0;k<i;k++) free(a[k]);
free(a);
return NULL;
}
memset(a[i],0,n*sizeof(double));
}
return a;
}
double** citeste_fis(char* nume_fis,int& m, int &n)
{
ifstream fis2(nume_fis);
if(!fis2) {cout<<"Fisierul "<<nume_fis<<" nu a putut fi deschis!"<<endl;return NULL;}
//eof() functie membra a clasei fstream si intoarce false daca nu s-a ajuns la sfarsitul
fisierului
// fiecare citire din fisier are ca rezultat si parcurgerea "pointer-ului/cursorului" din
cadrul fisierului cu dimensiunea datei citite + spatiile sau /r /n
//iesirea din bucla while are loc atunci cand s-a ajuns (cu citirea) la sfarsitul fisierului
3|Page

//citim dimensiunea tabloului in vederea alocarii


//while(!fis2.eof()) sau
fis2>>m; fis2>>n;
//aloca matrice
double **a;
if((a=alocaMatrice(m,n))==NULL) return NULL;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
fis2>>a[i][j];
return a;
}
void scrie_fis(char* nume_fis, double **a, int m,int n)
{
ofstream fis1(nume_fis);
if(!fis1) {cout<<"Fisierul "<<nume_fis<<" nu a putut fi deschis!"<<endl;return;}
fis1<<m<<" "<<n<<" "<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
fis1<<a[i][j]<<" ";
fis1<<endl;
}
}
int main()
{
double **q;
int m=1,n=1;
q=citeste_fis("fx1.txt",m,n);
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
cout<<setw(5)<<q[i][j]<<" ";
cout<<endl;
}
scrie_fis("fx2.txt",q,m,n);
return 0;
}
P4.1--------------------------------#include <iostream>
#include <malloc.h>
#include <string.h>
#include <fstream>
#include <iomanip>
using namespace std;
//incarcare din fisier si parcurgere arbore
/*structura fisier
* Nr noduri
* id_nod sir_intrebare
* -------------* id_nod sir_intrebare
* ID_nodRadacina id_nod_rasp.DA id_nod_rasp.NU
4|Page

* ----------------------* ID_nodRadacina id_nod_rasp.DA id_nod_rasp.NU


*/
typedef struct NOD{
int id;
char intreb[30];
NOD *rDA,*rNU;
}NOD;
NOD* citfis(char* numefis)
{
ifstream fis2(numefis);
if(!fis2) {cout<<"Fisierul "<<numefis<<" nu a putut fi deschis!"<<endl;return NULL;}
int dim;
fis2>>dim;
NOD *vect;
vect = new NOD[dim];
for(int i=0;i<dim;i++)
{ fis2>>vect[i].id;
fis2>>vect[i].intreb;
vect[i].rDA=NULL; vect[i].rNU=NULL;
}
int r1_id,d1_id,n1_id,r1_k,d1_k,n1_k;
while(!fis2.eof())
{
fis2>>r1_id; fis2>>d1_id; fis2>>n1_id;
for(int k=0;k<dim;k++)
{if(vect[k].id==r1_id) {r1_k=k;}
if(vect[k].id==d1_id) {d1_k=k;}
if(vect[k].id==n1_id) {n1_k=k;}
}
vect[r1_k].rDA=&vect[d1_k];
vect[r1_k].rNU=&vect[n1_k];
}
return vect;
}
void parcurgeAb(NOD *cap)
{ cout<<cap->id<<":"<<cap->intreb<<endl;
if(cap->rDA) parcurgeAb(cap->rDA);
if(cap->rNU) parcurgeAb(cap->rNU);
}

//intoarce adresa nodului dat de id


//initial p---cap
NOD* identificaNod(NOD* p,int id)
{
if(p->id==id) return p;
if(p->rDA) identificaNod(p->rDA,id);
if(p->rNU) identificaNod(p->rNU,id);
}

int main(int argc, char **argv)


5|Page

{
NOD *cap, *x;
cap=citfis("fa1.txt");
parcurgeAb(cap);
x=identificaNod(cap,2);
cout<<x->id<<endl;
return 0;
}
2012!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12.2----------------------------------------//APLICATII FUNCTII RECURSIVE:
//ARBORI BINARI
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef struct NOD{
int id;
NOD *st,*dr; //adresele nodurilor - stanga, dreapta
}NOD;
//ex: creare arbore binar
NOD* creare_arbore(void)
{NOD *p[7];
//alocare memorie
for(int i=0;i<7;i++)
{ p[i]=(NOD*)malloc(sizeof(NOD));
memset(p[i],0,sizeof(NOD));
p[i]->id=i+1;
}
p[0]->st=p[1]; p[0]->dr=p[2];
p[2]->st=p[3]; p[2]->dr=p[4];
p[3]->st=p[5]; p[3]->dr=p[6];
return p[0];//intoarce radacina arborelui
}
//parcurge arbore
void parcurge(NOD *p)//preia radacina arborelui curent
{ printf("-%d-",p->id);
// if(p->st) parcurge(p->st);
if(p->dr) parcurge(p->dr);
if(p->st) parcurge(p->st);
return;
}
//intoarce adresa nodului cu id-ul transmis
NOD* adresa_nod(int id, NOD *p)
{ NOD *x=NULL;
printf("#%d#",p->id);
if(p->id==id) return p;
if(p->st) x=adresa_nod(id,p->st);
if(x) return x;
if(p->dr) x=adresa_nod(id,p->dr);
6|Page

if(x) return x;
return NULL;
}
void sterge_arb(NOD *p)
{
if(p->st) sterge_arb(p->st);
if(p->dr) sterge_arb(p->dr);
free(p);
}
void sterge_legatura(NOD *p,NOD *pdel) //sterge legatura intre nodul parinte si nodul pdel
{ if(p->dr==pdel) p->dr=NULL; else sterge_legatura(p->dr,pdel);
if(p->st==pdel) p->st=NULL; else sterge_legatura(p->st,pdel);
}
void sterge_subarb(int id, NOD *p)
{ NOD* x;
//identificare adresa nod
x=adresa_nod(id,p);
sterge_arb(x);
//sterge legatura de la parinte la nodul x
sterge_legatura(p,x);
// "de reparat data viitoare"
}

int main()
{ NOD *rad,*x;
rad = creare_arbore();
parcurge(rad);
x=adresa_nod(4,rad);
printf("\n id ptr. id:4 este: %d\n",x->id);
sterge_subarb(4,rad);
parcurge(rad);
return 0;
}
C1-----------------------------------------------------------#include <stdio.h>
//comentariu la nivel de linie
/*
comentariu bloc
*/
int main()
{ //TIPUL INTREG
int a; //intreg
short b; // intreg cu semn pe 2 octeti
unsigned short b1;// intreg fara semn pe 2 octeti
long c; //intreg cu semn pe 4 octeti
unsigned long c1; //intreg fara semn pe 4 octeti
//int64 MS
7|Page

long long d; //intreg pe 8 octeti


// tipul REAL
float e; // pe 4 octeti
double f; // pe 8 octeti
long double g; //pe 10 octeti
// tipul boolean
bool h; // 1 octet
// tipul caracter
char i;
//instructiunea de atribuire
f=-0.0000000000001;
//instructiunea conditionala
if(f)
printf("ADEVARAT");
else
printf("FALS");
// 0 - FALSE
// != 0 => TRUE
return 0;
}
C2------------------------------------------------------------------------------------#include <stdio.h>
//operatori
int main()
{ int a,b,c;//declarare variabile
a=5; b=9;
c=a+b;printf("a+b=%d ;\n",c);//valoarea lui c o pune in locul lui %d
printf("%d+%d=%d ;\n",a,b,c);
c+=a;// echivalent cu c=c+a
printf("c+=a; c=%d\n",c);
/*c=a-b; c-=a;
c=a*b; c*=a;
c=a/b; c/=a;
*/
c=b%a;
printf("c=b%a --- %d=%d%%%d \n",c,b,a);
a=-a; a=-a;
//postincrementare si preincrementare
int i=1,j=1;
a=i++; //postincrementare <=> a=i; i++; <=> a=i; ++i;
b=++j;//preincrementare <=> ++j; b=j; <=> j++; b=j;
printf("10. a=%d b=%d i=%d j=%d \n", a,b,i,j);

return 0;
}
C10.1---------------------------------------------------------//liste
8|Page

#include <stdio.h>
#include <string.h>
#include <malloc.h>
//structura unui nod / celule / element
typedef struct NOD{ //NOD numele ales ptr. structura
int id;
//.... alte inform. asociate nodului
NOD *urm,*ant; // adresele nodurilor urmator si anterior
}NOD;
//functie ptr. crearea unei liste
//intoarce adresa capului nodului (primul nod din lista)
NOD* f_creare_lista(int n) // n nr. de celule alocate
{ NOD *caplista,*p,*p1;
for(int i=0;i<n;i++)
{ p=(NOD*)malloc(sizeof(NOD));
memset(p,0,sizeof(NOD));p->id=i+1;
if(!i) {caplista=p; p1=p;continue;}
p1->urm=p; p->ant=p1; p1=p;
}
return caplista;
}
// functie parcurgere -- prelucreaza toate nodurile
void f_parcurgere_prel(NOD *caplista)
{ NOD *p;
printf("\n");
for(p=caplista; p; p=p->urm)
printf(" -%d-",p->id);
}
// functie de parcurgere cu pozitionare pe ultimul nod
//intoarce adresa ultimului nod din lista
NOD* f_poz_ultim_nod(NOD *caplista)
{ NOD *p;
for(p=caplista;p->urm;p=p->urm);
return p;
}
// inserarea in lista dupa un nod dat (s)
// functia preia nodul din lista dupa care se face inserarea (s)
// si nodul care se insereaza (t)
void f_ins1(NOD *s, NOD *t)
{ if(!s->urm) //adaugare
{ s->urm=t; t->ant=s; return;}
t->urm = s->urm; //(1)
s->urm->ant = t;//(2)
s->urm = t;//(3)
t->ant=s;//(4)
}
// functie de pozitionare pe un anumit nod, preia id, si intoarce adresa acestuia
NOD* f_poz(NOD *caplista, int id)
9|Page

{ NOD *p;
for(p = caplista; p->id != id; p = p->urm);
if(p->id == id) return p;
else return NULL;
}
// inserarea in lista inainte de un nod dat (s)
// functia preia nodul care se insereaza (t)
// si nodul inaintea caruia se face inserarea (s)
void f_ins2(NOD *t, NOD *s)
{ t->ant=s->ant;
t->urm=s;
s->ant->urm=t;
s->ant=t;
}
// stergere nod, se transmite adresa nodului care va fi sters
void f_sterge(NOD *del)
{ del->ant->urm=del->urm;
if(del->urm)
del->urm->ant=del->ant;
free(del);
}

int main()
{ NOD *x,*y,*z;
x=f_creare_lista(10);
f_parcurgere_prel(x);
printf("\n Ultimul nod:");
y=f_poz_ultim_nod(x); printf("y.id: %d",y->id);
//inserare nod (dupa)
/* z=(NOD*)malloc(sizeof(NOD)); memset(z,0,sizeof(NOD)); z->id=123;
y=f_poz(x,7);//determina adresa nodului cu id-ul 7
f_ins1(y,z);
f_parcurgere_prel(x);
*/
//alta inserare (inante)
z=(NOD*)malloc(sizeof(NOD)); memset(z,0,sizeof(NOD)); z->id=124;
y=f_poz(x,5);//determina adresa nodului cu id-ul 5
f_ins2(z,y);
f_parcurgere_prel(x);
//sterge nod 2
// y=f_poz(x,2);f_sterge(y);
//sau
f_sterge(f_poz(x,2));
f_parcurgere_prel(x);
return 0;
}
C11.1

10 | P a g e

//TRANSFERUL PARAMETRILOR PRIN FUNCTII


#include <stdio.h>
//transfer prin valoare -- referinta ramane nemodificata(x[0]),
// o copie a valorii acesteia se transmite ca si parametru
int f1(int a)
{ a++; return a;}
//transfer prin adresa -- se modifica sursa (x[1])
int f2(int *a)
{ (*a)++; return *a;}
// transfer prin referinta -- se modifica sursa (x[2])
int f3(int& a)
{ a++; return a; }
int main()
{ int x[3]={100,100,100}, y[3];
printf("Inainte de apel x[0]=%d x[1]=%d x[2]=%d\n",x[0],x[1],x[2]);
y[0]=f1(x[0]); y[1]=f2(&x[1]); y[2]=f3(x[2]);
printf("Dupa apel
x[0]=%d x[1]=%d x[2]=%d\n",x[0],x[1],x[2]);
printf("
y[0]=%d y[1]=%d y[2]=%d\n",y[0],y[1],y[2]);
return 0;
}
C11.2
//TRANSFERUL PARAMETRILOR PRIN FUNCTII (continuare)
#include <stdio.h>
#include <malloc.h>
short f1(short a, short *b, short &c, short d[][2], short **e)
{
d[0][0]++; d[1][1]++; e[0][0]++; e[1][1]++;
//a++; (*b)++; c++;
return a+(*b)+c;
}
int main()
{ short x[3][2]={{10,20},{30,40},{50,60}}, r, **y;
y=(short**) malloc(3*sizeof(short*));
for(int i=0;i<3;i++) y[i]=(short*)malloc(2*sizeof(short));
for(int i=0;i<3;i++)
for(int j=0;j<2;j++)
y[i][j]=x[i][j]; // cele 2 tablouri (x,y) au aceleasi valori
r=f1(x[0][0],&x[0][0],x[0][0],x,y);
printf("r=%d x[0][0]=%d x[1][1]=%d x[2][1]=%d\n",
r,x[0][0],x[1][1],x[2][1]);
printf(" y[0][0]=%d y[1][1]=%d y[2][1]=%d\n",
y[0][0],y[1][1],y[2][1]);
return 0;
}
// cele 2 tablouri se comporta ca si cum ar fi transmise
// prin adresa
C12.1
//FUNCTII RECURSIVE
#include <stdio.h>
short f2(short a)
{ a++;
11 | P a g e

if(a>5) return 0;
a+=f2(a);
return a;
}
int main()
{ short x=0;
x=f2(x);
printf("x=%d\n",x);
return 0;
}
C13.1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
//functii uzuale
void functii_biblmath()
{
double a=25,b=0.5,c;
c=pow(a,b); // a la puterea b
printf("pow(%.2f,%.2f)=%.2f\n",a,b,c);
c=asin(b);
printf("arcsin(%.2f)=%.2f\n",b,c);
c=sqrt(a);
printf("sqrt(%.2f)=%.2f\n",a,c);
}
void f_siruri()
{ char a[50]="Un sir finit!";
printf("sirul a: %s\n",a);
char b[50];
strcpy(b,a); //copiaza sirul a in sirul b;
// strcpy(char* destinatie, char* sursa);
printf("1.sirul b: %s\n",b);
strcpy(b,&b[7]);b[6]=9;
//printf("2.sirul b: %s\n",b);
strcpy(&a[7],"in");
strncat(a,b,1000);//concatenare strcat(char* destinatie,char*sursa, nr_caractere
//copiate din sursa in destinatie! nu pune la sfarstit terminatorul de sir!
//strncpy(dest,srs,nr.caractere) strcat(dest,sursa)
printf("2.sirul a: %s\n",a);
}
void f_conversii()
{ char str[50];
double x;
int s;
strcpy(str,"55");
s=atoi(str); //ascii to integer
printf("s=%d\n",s);
strcpy(str,"899.457");
12 | P a g e

x=atof(str);//ascii to float
printf("x=%f\n",x);
//invers
sprintf(str,"Un sir double:%f int:%d\n",x,s);
printf("Sirul:%s\n",str);

}
void fisiere_text()
{
FILE *fis;
int x=0;
char s[100];
if((fis=fopen("un_test.txt","wt"))==NULL) {printf("Fisierul nu a putut fi deschis!");return;}
fprintf(fis," Un text \n");
fprintf(fis,"Fisierul este de tip text\n Un numar intreg: 35");
fseek(fis,0L,SEEK_SET);
int m=ftell(fis); printf("\n pozitia1:%d",m);
fseek(fis,30L,SEEK_SET);
m=ftell(fis); printf("\n pozitia2:%d",m);
//fscanf(fis,"%d",&x);
fscanf(fis,"%s",s);
//printf("x tiparit:%d",x);
printf("s tiparit:%s",s);
fclose(fis);

int main()
{
//functii_biblmath();
//f_siruri();
//f_conversii();
fisiere_text();
return 0;
}
C14.1
//test#2 -- partea a 2-a
#include <stdio.h>
void f2(int a, int* b, int& c)
{ int m=22;
static int t;
a++;(*b)++;c++;t++;m++;
printf("a=%d b=%d c=%d t=%d\n",a,*b,c,t);
return;
}
13 | P a g e

int main()
{
int x=1,y=2,z=3,w[3];
f2(x,&y,z);
printf("x=%d y=%d z=%d\n",x,y,z);
w[0]=x; w[1]=y; w[2]=z;
f2(w[0],&w[1],w[2]);
printf("w[0]=%d w[1]=%d w[2]=%d\n",w[0],w[1],w[2]);
return 0;
}
//
//1: a=2 b=3 c=4 t=1
//2: x=1 y=3 z=4
//3: a=2 b=4 c=5 t=2
//4: w[0]=1 w[1]=4 w[2]=5
C14.2
#include <stdio.h>
typedef struct NODA{
int ID;
NODA *pSt, *pDr;
} NODA;
typedef struct NODL
{int ID;
NODL *urm, *ant;
} NODL;

NODL* f_prel(NODA *rad)


{

int main(int argc, char **argv)


{
printf("hello world\n");
return 0;
}
C21
#include <stdio.h>
int main(int argc, char **argv)
{
// operatori la nivel de bit
// a>>3 a deplasat la dreapta cu 3 biti
//int a=7;
14 | P a g e

//deplasare.shiftare la dreapta
//a=a>>2;//a>>=2;
// 111 ->2 =>1
//deplasare la stanga
//a<<=2;//a=a<<2; 11100
//printf("a=%d\n",a);
//operatori logici la nivel de bit
// int a,b,c;
//sau la nivel de bit |
//a=5;b=4;
//c=a|b;// 101 | 100 => 111
// si la nivel de bit
//c=a&b; // 101 & 100 ->100 (4)
//unsigned short d=1;
//c=~d; //(d--->0000000000000001) => c-->1111111111111110
// XOR -sau exclusiv
//c=a^b; //101 ^ 100 001
//printf("c=%d",c);
// miniaplicatie XOR
/*
{ int a,b;
a=6; b=9; //0110 1001
printf("a=%d b=%d\n",a,b);
a=a^b; // 1111 1001
b=a^b; // 1111 0110
a=a^b; // 1001 0110
printf("a=%d b=%d",a,b);
}
*/
//operatori logici
// && - si
// || -sau
// ! -negat
//operatori relationari
// < <= > >= ==
// ; instructiune vida
// a=5;
//INSTRUCTIUNI CONDITIONALE
/*{ int a;
if(1) a=4;
if(a<5) a=9;
if(a>=9) a=11; else a=-1;
// if(conditie) {adevarat}
// if(conditie) { adevarat} else {false}
}*/
// 0 => false ., restul => true
/*int a;
if(-0.000000000000000000000001) a=5; else a=4;
printf("a=%d",a);
*/
// ZERO - false
/*
15 | P a g e

//INSTRUCTIUNEA SWITCH
int ziua=4;
switch(ziua)
{ case 1: printf("luni\n"); break;
case 2: printf("Ma\n"); break;
case 3: printf("Mi\n"); break;
case 4: printf("Joi\n");
case 5: printf("Vineri\n"); break;
case 6: printf("Sambata\n"); break;
case 7: printf("Duminica\n"); break;
default: printf("NUMAR ZI ERONAT!");
}
*/
// BUCLA WHILE
int a=5;
//while(expresia) {}
/*while(a>=0)
{ printf("a=%d\n",a);
a--;
}
*/
//BUCLA DO WHILE
/*
do{
printf("a=%d\n",a);
a--;
}while(a>=0);
*/
// BUCLA FOR
//for(expresie1;expresie2;expresie3) {}

return 0;
}
C22
#include <stdio.h>
double f1(int x)
{ return x*x-5*x+2;}
double integr(double a, double b, int n)
16 | P a g e

{ double q,sum,pas=(b-a)/(n*1.0);
for(sum=0,q=a+pas;q<=b;q+=pas)
sum+=(f1(q)+f1(q-pas))*pas/2.0;
return sum;
}
int main()
{ double h=integr(2,8,10000);
printf("Integrala: %f\n",h);
return 0;
}
C31
#include <stdio.h>
//TABLOURI CU O DIMENSIUNE (VECTORI)
//variabile globale
int a1, a2[5];
double b1, b2[10];
char c1,c2[15];
short d1,d2[10];
//d1 ocupa 2 octeti(bytes) iar d2 ocupa 20 octeti
//toate tablourile incep cu indexul ZERO

int main()
{ //variabile locale
for(int i=0;i<10;i++) d2[i]=0;
d1=5;
d2[7]=d1;
d2[0]=4;//primul element din vector
d2[9]=8; // ultimul element din vector
for(int i=0;i<10;i++)
printf("d2[%d]= %d ; ",i,d2[i]);
return 0;
}
C32
//TABLOURI CU 2 DIMENSIUNI
#include <stdio.h>
int main()
{ double a[10][14]; //matrice cu 10 linii si 14 coloane cu elemente de tip double
short b[3][9],c;
//initializarea
for(int i=0;i<3;i++)
for(int j=0;j<9;j++)
b[i][j]=0;
b[0][0]=7; //primul element din matrice
b[2][8]=5; //ultimul element din matrice
for(int i=0;i<3;i++)
{ for(int j=0;j<9;j++)
printf("b[%d][%d]= %d ",i,j,b[i][j]);
printf("\n");
}
17 | P a g e

printf("\n\n\n");//sare 3 randuri
for(int i=0;i<3;i++)
{ for(int j=0;j<9;j++)
printf(" %d ",b[i][j]);
printf("\n");
}
return 0;
}
C33
//POINTERI SI TABLOURI N-DIMENSIONALE (1)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//pointer - variabila ce contine o adresa de memorie
int main()
{
short a,*b,c[5],*d;
// prin declarea "short *b" s-a specificat ca b este o variabila ce contine
// o adresa catre un vector de date de tip short
// este adresa de inceput a vectorului ce contine date de tip short
b=(short*)malloc(7*sizeof(short));
//malloc aloca memorie (void*) malloc(int dimensiune)
//in cazul in care alocarea nu reuseste intoarce NULL ( 0 ca pointer)
//altfel intoarce adresa de memorie alocata (de la care a fost alocat spatiul cerut)
// spatiul cerut prin functia maloc este exprimat in octeti
//ptr. initializare se poate utiliza si functia memset()
memset(b,0,7*sizeof(short));
//in lipsa intializarii va afisare valorile ce au fost in memorie,
// memorie utilizata si de alte aplicatii
for(int i=0;i<7;i++) b[i]=100+i;
for(int i=0; i<7; i++)
printf(" b[%d]=%d ", i,b[i]);
d=&b[2];//d preia adresa celui de al treilea elent din cadrul lui b =>
//prin d se pot accesa 5 elemente
printf("\n");
for(int i=0; i<5; i++)
printf(" d[%d]=%d ",i, d[i]);
// b === &b[0]
//
//

a=b[3];//a preia valoarea celui de al patrulea element din vector


a=*(&b[3]); // *(&b[3]) === b[3]
// * la declarare ==> pointer , ca instructiune => preia valoarea de la adresa...

printf("\n Ex1: *(&b[0]) :%d",*(&b[0]));


printf("\n Ex2: *b :%d",*b);
printf("\n Ex3: *(b+1) :%d",*(b+1));
printf("\n Ex4: *(b+3) :%d",*(b+3));
printf("\n Ex5: *(&b[5]-2) :%d",*(&b[5]-2));
return 0;
18 | P a g e

}
C34
//POINTERI SI TABLOURI N-DIMENSIONALE (2)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{ short a[3][4],**b,*c,*d[5],e,f[6];
//*d[5] vector ce contine 5 adrese la vectori de tip short
//** adresa unui tablou cu 2 dimensiuni, altfel spus
// b reprezinta adresa unui vector ce contine adrese
// ale unor vectori de tip short

return 0;
}
C41
//POINTERI SI TABLOURI N-DIMENSIONALE (2)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{ short a[3][4],**b,*c,*d[5],e,f[6];
//*d[5] vector ce contine 5 adrese la vectori de tip short
//** adresa unui tablou cu 2 dimensiuni, altfel spus
// b reprezinta adresa unui vector ce contine adrese
// ale unor vectori de tip short

return 0;
}
C42
//POINTERI SI TABLOURI N-DIMENSIONALE (3)
// ALOCAREA DINAMICA A UNEI MATRICI DE TIP DOUBLE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <bfd.h>
//alocare dinamica ptr. un tablou 2D cu m linii si n coloane
// functia intoarce NULL( 0 ca pointer) in cazul in care alocarea
//dinamica a memoriei esueaza
double **alocareT2(int m,int n)
{ double **a;
int i,j;
//alocare vector pointeri
19 | P a g e

//a = (double**)malloc(m*sizeof(double*));
//if(a==NULL) return NULL;
//echivalent cu
//if((a=(double**)malloc(m*sizeof(double*)))==NULL) return NULL;
//sau
if(!(a=(double**)malloc(m*sizeof(double*)))) return NULL;
for(i=0;i<m;i++)
{ //a[i]=(double*)malloc(n*sizeof(double));
if((a[i]=(double*)malloc(n*sizeof(double)))==NULL)
{for(j=0;j<i;j++) free(a[i]);
return NULL;
}
}//endfor i
//evaluarea expresiilor este de la dreapta la stanga
//paranteza,NULL,argumentul functiei, apel malloc, asignarea valorii in a[i]
return a;
}
//eliberare memorie alocata
void dealocareT2(double **a, int m)
{for(int i=0;i<m;i++)
free(a[i]);
free(a);
//dealocarea s-a realizat in ordine inversa alocarii
}

int main()
{
double **x;
int m1=3,n1=4;
//intializare generare numere aleatoare (baza ceasul calc)
srand(time(NULL));
//apel functie alocare dinamica
x=alocareT2(m1,n1);
//initializarea variabilei alocate dinamic cu valori aleatoare
for(int i=0;i<m1;i++)
for(int j=0;j<n1;j++)
x[i][j]=rand()%100; //rand() genereaza nr aleatoare intre 1 - 2^16
//tiparire
for(int i=0;i<m1;i++)
{ for(int j=0;j<n1;j++)
printf(" %f ",x[i][j]);
printf("\n");
}
dealocareT2(x,m1);
return 0;
}
C51
#include <stdio.h>
void f1() //explicitarea functiei f1()
{
20 | P a g e

short a1,*a2,a3[4],**a4,a5[2][3],*a6[4];
//declarare + initializare
short b1[6]={10,20,30,40,50,60};
short b2[3][4]={{11,22,33,44},{16,27,38,49},{9,21,31,41}};
/*1*/ a1=b1[3]; printf("1: %d\n",a1);// R:40
/*2*/ a1=*(b1); printf("2: %d\n",a1);// R:10
/*3*/ a1=*(b1+3); printf("3: %d\n",a1);// R:40
/*4*/ a1=*(&b1[2]+2); printf("4: %d\n",a1);// R:50
/*5*/ a1=*(&b1[2]-1); printf("5: %d\n",a1);// R:20
/*6*/ a1=*(&b1[2])-1; printf("6: %d\n",a1);// R:29 (b[2]-1)
/*7*/ a1=**b2; printf("7: %d\n",a1);// R:11
// **b2 === *(*b2) === *(*&b2[0]) === *(b2[0]) ===
// === *(&b2[0][0]) === b2[0][0]
/*8*/ a1=**(b2+1); printf("8: %d\n",a1);// R:16
//**(b2+1) === **(&b2[1]) === *b2[1] === *(&b2[1][0]) === b2[1][0]

int main(int argc, char **argv)


{
f1();//apelul functiei functiei f1
return 0;
}
C61
//TESTUL #1
#include <stdio.h>
#include <string.h>

double f1(double **a, double *b)


{ int i,j,k;
double sum_i,sum_j,sum_k;
for(i=0,sum_i=0;i<9;i++)
{
for(j=0,sum_j=0;j<6;j++)
{
for(k=0,sum_k=0;k<8;k++)
sum_k+=b[k]/(a[i][j]-a[i][1]);
sum_j+=a[i][j]-sum_k;
}
sum_i+=(1-a[i][1])*sum_j;
}
return sum_i;
}

double f2(double **a, double *b)


{ int i,k;
double sum_i,sum_k;
for(i=0,sum_i=0;i<6;i++)
{
21 | P a g e

for(k=0,sum_k=0;k<8;k++)
sum_k+=a[i][k]-a[k][k];
sum_i+=
b[i]-22/sum_k;
}
return 11+sum_i;
}

int main(int argc, char **argv)


{
{//1
int a=19; (a %= +3)++; printf("1...a=%d\n",a); //R1:2
//===> (a=a%(+3))++ ===> (a=a%3)++ ==>
//(a=19%3)===> (a=1)++ ===> (1)++ ====> 1++ ===>2
}
{//2
int a=11; a >>= 3; printf("2...a=%d\n",a); //R2: 1
// a>>=3 ===> a=a>>3 ===>a=(1011)baza2 >> 3 ===> a=1
}
{//3
int a=15,b; b = (a-6)&(a^4); printf("3...b=%d\n",b); //R3:9
//(a^4) ===> (1111)b2 ^ (0100) => (1011)
//(a-6) ===>9 ===(1001)
//(1001) ^ (1011) => (1001) =>9
}
{//4
int a,b[7] = {17,16,15,14,13,12,11}; a = *(&b[3]-1);
printf("4...a=%d\n",a); //R4:15
}
{//5
int a,c[4][2] = {{8,5},{1,2},{3,9},{6,4}};
a = **c+*(*(c+**(&c[1])));
printf("5...a=%d\n",a);
}
{
char str[7]; strcpy(str,"ABCDEF"); //R:
str[1] = str[1]+1-'C';
//str[1]='B'+1-'C'; ===>str[1]=0 ---> 0 terminator de sir
//==>afiseaza numai str[0]
str[3]=str[4]-(str[3]+1);
printf("str: %s\n",str);
}
return 0;
}

C91
#include <stdio.h>
#include <string.h>
void fx()
{
//1a
22 | P a g e

{ int a = 3; a %= -3; printf("1a: %d\n",a);


//R: 0
}
{ int a,b = 15; a = (++b)--; printf("1b: %d\n",a);
// (++15) => 16 (16) ==> a
// a=16--; R:16
}
{ int a=15; a >>= 2;printf("2: %d\n",a);
//a=(1111) >== (0011) => 3
//R:3
}
{ int a=15,b,c,d; b= a^10; c=a|2; d=(a^10)&(a|2);
printf("3a: %d\n",b); //R: 5
printf("3b: %d\n",c);//R:15
printf("3c: %d\n",d);//R: 5
//(1111)^(1010) =>(0101) => 5
//(1111)|(0010) =>(1111) =>15
//(0101) & (1111) =>(0101) =>5
}
{int a[2],b[7]={10,15,20,25,30,35,40}; a[0]=*(&b[3] + 3); a[1]=1 + *b;
printf("4a: %d \n",a[0]); //R:40
printf("4b: %d\n",a[1]); //R:11
}
{ int a[3],c[4][3]={{10,20,30},{11,21,31},{12,22,32},{13,23,33}};
a[0]=*(&c[2][1] - 1); a[1]=**(&c[1] + 1); a[2]=1 + **c;
printf("5a %d \n",a[0]); // R:12
printf("5b %d \n",a[1]); // R:12
printf("5c %d \n",a[2]); // R:11
}
{
char *str="ABCDEFG"; str[5]=str[3]-str[0]+'B'; str[6]=!(str[5]-str[3]);
printf("/n 6: %s",str);
}
}
/*
void f2(int a, int* b, int& c)
{ int m=4; static int t;
a--;b--;c--;t--;m--; printf("a=%d b=%d c=%d t=%d\n",a,*b,c,t);return;}
void fy()
{ int x=30,y=10,z=20,w[3];
f2(x,&y,z); printf("x=%d y=%d z=%d\n",x,y,z);
w[0]=x; w[1]=y; w[2]=z;
f2(w[0],&w[1],w[2]); printf("w[0]=%d w[1]=%d w[2]=%d\n",w[0],w[1],w[2]);
}
*/
int main(int argc, char **argv)
23 | P a g e

{ fx();
// fy();
return 0;
}
C92
#include <stdio.h>
#include <string.h>
//structuri
struct PERS{
char nume[10];
int varsta;
float inaltime;
};
typedef struct{
char nume[10];
int varsta;
float inaltime;
}PERS1;
typedef struct PERS2{
char nume[10];
int varsta;
PERS2 *n;
float inaltime;
}PERS2;

int main(int argc, char **argv)


{ struct PERS a;
PERS1 b;
PERS2 c;
strcpy(a.nume,"Ion");a.varsta=20;a.inaltime=1.75;
strcpy(b.nume,"Vasile");b.varsta=25;b.inaltime=1.85;
strcpy(c.nume,"Ilie");c.varsta=15;c.inaltime=1.55;
printf("a: nume:%s ,varsta:%d, inaltime:%.2f\n",a.nume,a.varsta,a.inaltime);
printf("b: nume:%s ,varsta:%d, inaltime:%.2f\n",b.nume,b.varsta,b.inaltime);
printf("c: nume:%s ,varsta:%d, inaltime:%.2f\n",c.nume,c.varsta,c.inaltime);
return 0;
}

24 | P a g e