You are on page 1of 5

Tipuri de date

1: #include <stdio.h>
2: #include <iostream>
3: #include <stdlib.h>
4:
5: using namespace std;//pt scrierea prescurtata, fara std::cout, etc
6:
7: int main()
8: {
9: //date de baza
10: char c1, c2 = 'A';//tipul char
11: int i1, i2 = 10;//tipul int
12: float f1, f2 = 1.2;//tipul float
13: double d1, d2 = 1.e-4;//tipul double
14: printf("%c\t%d\t%3.1f\t%g\n", c2, i2, f2, d2);//in stdio
15: cout << c2 << "\t" << i2 << "\t" << f2 << "\t" << d2 << endl;//in iostream
16: cout << "char: " << sizeof(c2) << "\t" << "int: " << sizeof(i2) << "\t" <<
"float: " << sizeof(f2) << "\t" << "double: " << sizeof(d2) << "\tOcteti" <<
endl;
17:
18: //ponteri
19: //void
20: void *pv;
21: pv = &i2;
22: printf("%d\n",*(int*)pv);
23: cout << *(int*)pv << endl;//convertirea tipului void - modificator de tip
24:
25: //pt un tip definit, si anume int
26: int *p;
27: int a = 10;
28: p = &a;//adresa lui a
29: *p = 20;// continutul memoriei de la adresa punctata de p
30: cout << *p << "\t" << a << endl;
31:
32: int *p1 = new int;//alocare zona memorie, al carei continut il reprezinta
un intreg, adresa ei fiind reprezentata de p1
33: *p1 = 30;
34: cout << *p1 << endl;
35: delete p1;//obligatoriu, datorita alocarii memoriei pt pointerul p1
36:
37: /*
38: int *p1;
39: p1 = (int *)malloc(1 * sizeof(int));
40: *p1 = 55;
41: cout << *p1 << endl;
42: free(p1);
43: */
44:
45:
46: //tablouri
47: int tablou1[4] = {1, 2, 3}, tablou2[][2][3] = {1}, tablou3[2][2] = {1, 2,
3};
48:
49: int i, j;
50:
51: cout << tablou1[2] << "\t" << *(tablou1+2) << endl << endl;
52:
53: for(i = 0; i < 2; i++)
54: {
55: for(j = 0; j < 2; j++) cout << tablou3[i][j] << "\t";
56: cout << endl;
57: }
58:
59:
60: system("PAUSE");//in stdlib
61: return 0;
62: }
63:
64:
65:
66:
67:
68:
FunctieRecursivaIterativa

1: #include <iostream>
2: #include <stdlib.h>
3:
4: using namespace std;//pt scrierea prescurtata, fara std::cout, etc
5:
6: int fact1(int n)//varianta recursiva
7: {
8: int raspuns;
9:
10: if(n == 1) return(1);
11: raspuns = fact1(n-1)*n;
12: return(raspuns);
13: }
14:
15:
16: int fact2(int n)
17: {
18: int raspuns = 1;
19: for(int i = 1; i <= n; i++) raspuns = raspuns *i;
20: return(raspuns);
21: }
22:
23:
24: int main()
25: {
26:
27: cout << fact1(5) << "\t" << fact2(5) << endl;
28:
29:
30: system("PAUSE");//in stdlib
31: return 0;
32: }
33:
34:
35:
36:
37:
38:
Structuri

1: #include <iostream>
2: #include <stdlib.h>
3:
4: using namespace std;//pt scrierea prescurtata, fara std::cout, etc
5:
6: int main()
7: {
8: typedef struct {
9: float re;
10: float im;
11: } complex;
12:
13: struct punct {
14: int x;
15: int y;
16: };
17:
18: complex z1 = {1, 3}, *pz1 = &z1;
19: punct P1 = {1, 2}, *pP1 = &P1, P2 = P1;
20:
21: cout << z1.re << "\t" << z1.im << endl;
22: cout << pz1 -> re << "\t" << pz1 -> im << endl;
23:
24: cout << P1.x << "\t" << P1.y << endl;
25: cout << pP1 -> x << "\t" << pP1 -> y << endl;
26:
27: /* struct nod {
28: int informatie;
29: nod *urmatorul;
30: };
31:
32: typedef nod *pnod;
33:
34: nod n1;
35: pnod pn2;
36: n1.informatie = 10;
37: n1.urmatorul = NULL;
38:
39: pn2 = new nod;
40: pn2->informatie = n1.informatie;
41: pn2->urmatorul = NULL;
42:
43: cout << n1.informatie << "\t" << pn2->informatie << endl;
44: pn2->informatie = 20;
45: cout << n1.informatie << "\t" << pn2->informatie << "\t" << pn2->urmatorul
<< endl;
46: */
47:
48:
49: typedef struct nod {
50: int informatie;
51: nod *urmatorul;
52: } NOD, *PNOD;
53:
54:
55: NOD n1;
56: PNOD pn2;
57: n1.informatie = 10;
58: n1.urmatorul = NULL;
59:
60: pn2 = new NOD;
61: pn2->informatie = n1.informatie;
62: pn2->urmatorul = NULL;
63:
64: cout << n1.informatie << "\t" << pn2->informatie << endl;
65: pn2->informatie = 20;
66: cout << n1.informatie << "\t" << pn2->informatie << "\t" << pn2->urmatorul
<< endl;
67:
68: struct a {
69: int informatieA;
70: struct b *pb;
71: };
72:
73: struct b {
74: int informatieB;
75: struct a *pa;
76: };
77:
78: a varA1;
79: b varB1;
80:
81: varA1.informatieA = 10;
82: varA1.pb = &varB1;
83: varB1.informatieB = 20;
84: varB1.pa = &varA1;
85: cout << varA1.informatieA << "\t" << varA1.pb->informatieB << endl;
86:
87: /* union uniune {
88: int valoareIntreaga;
89: float valoareReala;
90: char *variabilaText;
91: };
92: */
93:
94: typedef union {
95: int valoareIntreaga;
96: float valoareReala;
97: char *variabilaText;
98: } uniune;
99:
100: uniune u1;
101: uniune *pu1 = &u1;
102:
103: u1.valoareIntreaga = 13;
104: cout << u1.valoareIntreaga << "\t" << u1.valoareReala << endl;
105: u1.variabilaText = "Test";
106: cout << u1.valoareIntreaga << "\t" << u1.valoareReala << "\t" <<
u1.variabilaText << endl;
107: cout << pu1->valoareIntreaga << "\t" << pu1->valoareReala << "\t" << pu1-
>variabilaText << endl;
108:
109: system("PAUSE");//in stdlib
110: return 0;
111: }
112:
113:
114:
115:
116:

You might also like