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

...

1.
C. , ..
2.
, , ...
3.
..
4.
....
5.
......
6.
, ....................
7.
main().........
8.
..
9.
.
1.
...............................................................
......

4
5
16
23
34
43
53
66
70
76
84
85

Programarea
calculatoarelor

Cibernetic, Statistic i Informatic Economic 1 ,


Cibernetic i Informatic Economic Statistica 1 2
, Informatic Economic Informatic Economic i
Limbi Moderne Aplicate.
.

.



.
:

1
2
3
4
5
6
7
8
9


C. ,
, ,


,
main()

4-12
4-12
4-12
4-12
4-12
4-12
2-8
2-4
2-8

,
,
. ,
, ,
, .

.
,
. , ,
.
.

N 1
: . ,
: ,
, ,
.
:

, .
.
.
.
.
, , 3, -2, 2003, 1.2, 3.141592653, -123.456,
a, S, %, Informatica, CiBeR .
,
: a, 4, &, ; .. -
. backslash: \.
backslash : \\, \.
,
: Arborele
Lumii.
: 5, -12, 65432, 3.14,
123.456, 0.314E+1 ..
, ,
. ,
.
. ()
. ,
_. .
.

.
.
, ,
. ,
. , ,
.
.

. : char,
int, float, double. char ,
. ASCII,
5

. , char
-128 127. char
.
.
,
: char car; char rac, arc; ..

=: car=; rac=i; arc=b; ..
, char car;
char
. car=;
, car,
, .. 67 ASCII .
int .
int , , . ,
, ,
int
, -
-32768 32767. , int
.
: int an=2003,
luna; , an,
2003, luna
- .
float ,
. () ,
.. 7 . 4
. - 1.710-38 1.710+38.

double. 8 .
, .. 15 .
- 1.710-308 1.710+308.
,
, ( ). ,
- 9460800000000.
: 9.460812,
94.60811, 0.9460813 .. , ,
10. ,
3.140, 0.3141, 31.4-1, 314.0-2 3.14 .
, ,
( ) ,
.
.
. .
: short ( ), long ( ),
signed
( ),
unsigned
( ). ,
int short int, long int, signed int,
unsigned int, long signed int, long unsigned int.
signed unsigned .
, . ,

6

,
. unsigned
. ,
.
, char signed char . ,
unsigned char 0 255. ,
char 1 .
, int signed int.
unsigned int 0 65535. ,
int ( ) 2 .
long int, unsigned int
double. long int unsigned long int int :

long

unsigned long
.
( long unsigned unsigned long ) .
int 2 , long 4 . int
4 , long 4 .
long double
. 10
18 . - 3.4-4932 1.1+4932 .
, . ,
314 int, 45678
long, ..
int. long , 314.
, , l
L: 314l 314L. unsigned.
u U unsigned.
l L u U ( ).
. .
, ,
. .

/*,

*/, . /
* .
clrscr() . .
void clrscr( void ); <conio.h>.
getch()
, Enter. ASCII
.
ASCII :
0, .
.
,
getch()



- .
int getch( void ); <conio.h>.

getche( )
, Enter. ASCII
. ASCII
: 0,
. .
int getche( void ); <conio.h>.
putch( e ) .
.
. [32;126]
.
,
.
, ,
. ,
putch( 10 ),

putch( \n ), .
, .
int putch( int c ); <conio.h>.
getchar()
ASCII
ENTER.
ENTER,
.
. ,
.

ENTER.
int getchar( void ); <stdio.h>.
putchar( e ) ASCII
. , 1 .
putchar( \n ) putchar( 10 )
.
int putchar( int e ); <stdio.h>.
gets( s )

ASCII,
<Enter>.
.
char.
ENTER NUL ( \0 ),
, ..
\n

\0.
,
n n+1 .
, , ..
. EOF,
0.
<stdio.h>.

puts( s )

ASCII NUL ( \0 ).
,
. ,
char. NUL,
, , ..
\0 \n.
, ..
NUL,
1
.
<stdio.h>.
printf( c, p1, p2, p3, )
.
. , c
,
. , c
. escape, '\n, \t, \0 .
, .
p1, p2, p3,

, .
c
.
%
( . 1 ).
. p1, p2, p3,
. c.
, ,

.
,
. ,
,
. ,

.
. , ,
.
0 0
.
( ) 1
.
<stdio.h>.
scanf( c, p1, p2, p3, )
. .
,

,
.
, . 1, 2, 3, , .
&

.
%
( . 1 ).
.
,
. -
.
.
<stdio.h>.

fflush( stdin )

( ).
<stdio.h>.
exit( c ) .
. ,
0
,
. .

<stdlib.h>

<process.h>.
sizeof
. , sizeof( int ) ,
int, sizeof( d )
d.
:
1.1. , getch(), ASCII
, putch().
#include <conio.h>
#include <stdio.h>
void main(void)
{ char a;
clrscr();
a=getch();
putch(a);
getch();
}
1.2.
,
getch(),

ASCII
,
printf().
#include <conio.h>
#include <stdio.h>
void main(void)
10

{ char a;
clrscr();
getch();
printf("%d\n",getch());
getch();
a=getch();
printf("%d\n",a);
getch();
printf("%d\n",a=getch());
getch();
}

//
//
//
//

Primul apel
Al doilea apel
Primul apel
Al doilea apel

// Primul apel
// Al doilea apel

1.3. ,
, .
#include <conio.h>
#include <stdio.h>
void main(void)
{ char c;
clrscr();
c=getchar();
putchar(c-1);
getch();
}
1.4.
.
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main(void)
{ char s[256];
clrscr();
gets(s);
printf(irul are %d caractere,strlen(s));
getch();
}
1.5.
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ int a,b,c;
clrscr();
scanf(%d %d %d,&a,&b,&c);
printf(a=%d\tb=%d\tc=%d\nmedia=%.2f,
a,b,c,(a+b+c)/3.0); //media cu 2 zecimale
11

getch();
}
1.6. *
123.456f ( #define ),
float.
#include <conio.h>
#include <stdio.h>
#define a 123.456f
void main(void)
{ clrscr();
printf("*%f*\n",a);
printf("*%2f*\n",a);
printf("*%20f*\n",a);
printf("*%-20f*\n",a);
printf("*%020f*\n",a);
printf("*%.2f*\n",a);
printf("*%.10f*\n",a);
printf("*%2.2f*\n",a);
printf("*%2.10f*\n",a);
printf("*%20.10f*\n",a);
printf("*%-20.10f*\n",a);
printf("*%020.10f*\n",a);
getch();
}
:
.
.
1.0. 1.1-1.6.
1.1. ( getche() )
(
putch()
),
( putch('\n') ),
( getch() ) - .
,
ASCII?
1.2. SCII ( getchar() )
( putchar() ),
( putchar(\n) ),
( getch() )
.
,

ASCII?
putch( `\n` ) putchar( `\n` ) ?
12

1.3. ( gets() ),
( putchar() )
( ), newline (
putchar(`\n`) ),
( getch() ) - .
1.4. ( getchar() ) , (
printf() ) *,
newline,
-
.
1.5. ( getchar() ) , (
printf() ) %,
newline,

- .
1.6. *
Limbajul C++ : %s, %15s, %-15s, %10s, %15.10s,
%-15.10s.
1.7. ASCII ( getchar() ) (
printf() ) .
1.8. , ASCII (
getch() ) ( printf() ) .
1.9. 12345, #define,
10-, 8- 16- .
1.10.
123456789,
#define, 10-, 8- 16- .

1.11. * 123
( #define ) : %d, %2d,
%10d, %-10d, %010d, %ld, %u, %lu.
.
1.12. * 123.456
( #define )
double.
f, e, E, g, G, lf, le, lG, lE, lg.
1.6.
.
1.13. * 123.456l
( #define ) long
double.
Lf, Le, LE, Lg, LG.
.
13

1.6 1.12.
.
1.14. ( scanf() ) (
printf() ) ASCII .
1.15. ( scanf() ) ,
, ( puts() )
, .
1.16. ( gets() ) ,
( printf() ) ,
. ,
, Enter?
1.17. (
scanf()
) ,
4 ( printf() ) 10-,
8- 16- .
1.18. (
scanf()
) ,
9 ( printf() ) 10-,
8- 16- .
1.19. ( scanf() ) ddmmyy
( 6 ) 20yy/mm/dd.
dd, mm, yy , ,
.
1.20. ( scanf() ) (
printf() ) .
1.21. sizeof
, ( char, signed char,
unsigned char, short, signed short, unsigned short, int, signed int,
unsigned int, long int, long signed int, long unsigned int, float, double,
long double ).
:
1.1. Borland C++ ?
1.2. Borland C++ ?
1.3. Borland C++ ?
1.4. ?
1.5. help ,
clrscr() ?
1.6. ( )?
1.7. ( )?
1.8. ?
1.9. ?
14

1.10. Enter?
1.11. Enter?
1.12. getche() getchar() ?
1.13. putch() putchar() ?
1.14. ?
1.15. ?
1.16. ?
1.17. ?
1.18. Enter, ^z, ^c,
F1, BS, Esc etc.?
1.19. \n, \t, \a, ^z, ^c,
EOF, NUL etc.?
1.20. ,
ASCII?
1.21. main() ?
1.22. void main(void) ?
1.23. printf()
% ?
1.24. , ?
1.25. clrscr() ?
1.26. exit() ?
1.27. ?
1.28. ?
1.29. ?
1.30. ?
1.31. : char, int, float,
double, long double ?
1.32. Borland C++
( Home, End, ^y Edit-Copy,
Edit-Cut, Edit-Paste ).
1.33. _01
.

15

N 2
: , ,
:
,

.
:
, , pow( x, y ), poly( x,
n, c[] ), sin( x ) . <math.h>.
pow( x, y ) xy double pow( double
x, double y );
poly( x, n, c[] ) p=cnxn+cn-1xn-1+
+c2x2+c1x+c0 double poly( double x, int n, double c[] );
sin( x ) ,
double sin( double x );
.
, /
180, 3,14159265358979.
a1, a2, , an (a1a2an)1/n.
-
. - ,
. , ,
..
, , .

, .
: char, short int
enum int; ,
,
.
,
.
.
: long double, double, float, unsigned long, long, unsigned
int, int.
() , cast. ,
,
(tip)operand . , (int)x x
int.
16

: < - ,
, >= - .

<=

- ,

>

: == - , != - .
: ! - , && -
, || - . : !
0 ( false ), , 1 (
true ), ; a&&b 1
, b ; ||b 0
, b .
,
0 ( false ) 1 ( true ). int.
: ~ - 1, << -
, >> - , & - , ^ -
2, | - .
int.
:
2.1.
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ float a,b;
clrscr();
scanf(%f %f,&a,&b);
printf(Numerele citite: %f\t%f\n,a,b);
printf(Numerele ordonate: %f\t%f,
a>b?b:a,a>b?a:b);
getch();
}
2.2. , ,
3.
#include <conio.h>
#include <stdio.h>
void main(void)
{ unsigned a,b,c;
scanf(%u %u %u,&a,&b,&c);
(a+b+c)%3==0 ? printf(Da) : printf(Nu);
getch();
}
17

2.3. !a&&b, !c||d, !


e&&f||!f&&e.
#include <conio.h>
#include <stdio.h>
void main(void)
{ int a,b; float c,d; double e,f;
clrscr();
scanf(%d %d,&a,&b);
printf(\na=%-d\tb=%-d\t!a&&b=%-d\n\n,
a,b, !a&&b);
scanf(%f %f,&c,&d);
printf(\nc=%-f\td=%-f\t!c||d=%-d\n\n,
c,d, !c||d);
scanf(%lf %lf,&e,&f);
printf(\ne=%-lf\tf=%-lf\t!e&&f||!f&&e=%-d,
e,f, !e&&f||!f&&e);
getch();
}
2.4. n=max(a,b,c), d=max(a,b),
n=max(d,c) a, b, c.
, . ?!
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ unsigned a=5,b=2,c=3,d,n;
clrscr();
printf(a=%-u\tb=%-u\tc=%-u\t
max(a,b)=%-u\tmax(a,b,c)= %u,
a,b,c,d=a>b?a:b,n=d>c?d:c);
getch();
}
2.5.
<< u >>. x=1, u=4; x=2, u=8; x=3, u=5; x=216,
u=214; x= -1, u= -1; x= -2, u= -2; x= -4, u= -4; x= -5, u= -5.
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ long x,y; int u,w;
clrscr();
printf(Introdu x i u );
scanf(%ld %d,&x,&u);
w=u<<2; printf(u=%-d\t%-d<<2=%-d\n,u,u,w);
y=x>>2; printf(x=%-ld\t%-ld>>2=%-ld\n,x,x,y);
18

getch();
}
2.6.
, s.

#include <conio.h>
#include <stdio.h>
void main(void)
{ int s;
clrscr();
scanf(%d,&s);
printf(s=%-d\ts|1=%-d,s,s|1);
getch();
}
:
C
.

2.0. 2.1-2.6.
2.1. float, y=3.5x3-3.5x2+3.5x3.5 .
2.2. int, =32+7+17
.
=1, 10, 100, 1000. ,
. ,
.
2.3.

double,

19
15
9
5
a=x +x +x +x +1 x .
pow( x, y ) .
=1, 10, 100, 1000. , .
, ?
2.4. c0, c1, c2, c3, c4
double q=c4x4+c3x3+c2x2+c1x+c0 ,
q x q.

poly( x, n, c[] )

.
2.5. ()
().
( )?
2.6. , jugre,
, prjini ptrate stnjeni ptrai ( 1
19

jugr=576 prjini ptrate=5764.6415 .., 1 =10000 .. )


.
2.7.
.
2.8. , ,
g, m, s
.
sin( x ).
2.9. n una int
0, r>n, 1 .

r una

double

2.10. w 2000, 1000<=w<=2000,


1000 .
2.11. [1600;4900],
, 1, , 0,
. , 4 100
400.
2.12. [1600;4900],
, .

2.13.
(5x2+4.5x 1.5)/(5x2+4.5x +1.5)
.

x, 5x2+4.5x , 5x2+4.5x+3.5
x

2.14. , 15, 25, 35, 45


.
2.15. , , [] - , {} 82-2+4, 8[]2-2[]+4, 8{x}2-2{}+4.
2.16.
.
sqrt( ).
2.17. (n-1)/(n+1)
13- .
2.18. .
.
2.19. .
.

20

2.20. z=42+9-2000, <0,


z=2000, =0 z=42-2000, >0.
.
:
2.1. , , ; ?
2.2. ?
?
2.3. .
2.4. .
2.5. sizeof ?
2.6. & ?
2.7. ? : ?
2.8.
char ?
2.9.
?
2.10.
( float )?
2.11.
( double )?
2.12.
( long double )?
2.13. : c, 3, 3.14, 3.14l, 3.14 ?
2.14. ,
?
2.15. pow( x, y ), poly( x, n,
c[] ), cos( x ), sqrt( x ) ?

2.16. :

int, long,

unsigned, long unsigned ?


2.17. : float, double,
long double ?
2.18. char ?
2.19. ( Pi<2 )+3.4
, Pi float?
2.20. char c; c=R+b-B;
printf(%d,c); ?
2.21. int x=2,y; y=x++; x=++y; x ?
2.22. int x=2,y; y=--x; x=y--; x ?
2.23. const int x=3.14, y=2.72; x=y; x
y ?
2.24. const x=2.72, y=3.14; x=y; x y
?
2.25. int x=3.14; y=2.72; x=y; x
?
2.26. int x=2.72; y=3.14; x=y; x
?
21

2.27. int x=1; x=5, x=4, x=3, x=2; ?


2.28. int n=1; m=2; t=3; z=4; t=n/m; z=n%m;
t u z ?
2.29. bs(), ceil(), floor().
help.
2.30. ( (), [], ., ->, +
(), - (), ++, --, (type), sizeof(), ~, * (), /, %,
+ (), - (), <<, >>, <, <=, >=, >, ==, !=, & (),
^, |, &&, ||, ?, :, =, <<=, >>=, +=, -=, *=, /=, %=, &=, ^=, |=, , ).
2.31. _02
.

22

N 3
:
:
:


:
{ ; ; }.

.
.
if . :
. : if( expr ) instruction1;
:

true

expr

false

instruction1

,
. true, .. 0 ,
instruction1 ,

if.

.
if : if( expr ) instruction1; else
instruction2; . ,
, instruction1,
instruction2.
,
if.
instruction1 instruction2 ,
. if
:

23

true

false

expr

instruction1

if .
for

:
for( e1;e2;e3 ) ;

1, 2, 3

: 1 - ; e2 - ,
; e3 -
. .

.
,
. .
: for( e1; e2; e3 );
for :
1, 2.
2 0 , .. ,
- . , .. 2 0
( ), for
. ,
- , 3,
2.
for :

e1

false

e2
true

e3

24

while
: while( e1 ) ; - while( e1 ),
1, . , .
, .
:

false

e1
true

. 1
, 1 true.
e1 false,
.
.
: while( e1 );
do-while : do
while( e1 );
, .
:

e1
true
false

do-while
:
, .. , 1 . 1 ,
. , .. 1=0,
, .
. :
do while( e1 );
25

continue
continue;
.
break
switch. break ,
,
. break switch
, switch.
switch ,
.
switch :
switch( expresie )
{ case c1: ir1; break;
case c2: ir2; break;

case cn: irn; break;


default: ir; }

1, 2, , n
,
expr switch,
s1, s2,, sn s . switch
: expr
, 1, 2,
, n.
case, .. .
,
3,
case c3, .. s3.
break, s3. break
switch, .. ,
switch.
1, 2, , n, default,
.. s default.
default . ,
1, 2, , n, switch
,
switch. default
switch,

break

s.
break . ,
, . . break,
switch.
,
. ,
s127 1, 2
7 expr , case c1: case c2: case
c7: s127; break;
26

case default
( switch ).

switch

goto .
goto ; .
( ),
':' . , lab1: i++; lab1 - .
goto lab1;
lab1, .. i ++;
return
.
return; return expr; ,
.
, - , ,
expr. expr

. return
, .
return.
:
3.1.
.
unsigned dc(unsigned a, unsigned b)
{ unsigned c,d=1;
if (a==0||b==0) return a>b?a:b;
else for(c=2; c<=(a<b?a:b); c++)
if (a%c==0 && b%c==0) d=c;
return d;
}
3.2.
.
, f:\bc\mydir\ex3_1.c.
#include <conio.h>
#include <stdio.h>
#include f:\bc\mydir\ex3_1.c
void main(void)
{ unsigned a,b,c;
clrscr();
printf(Introdu 2 numere ntregi fr semn: );
scanf(%u %u,&a,&b);
c=dc(a,b);
printf(cmmmc(a,b)=%u,a*b/c);
getch();
27

}
3.3. ,
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ int n;
clrscr();
printf("Introducei o cifr: ");
while(3.14)
{ if (scanf("%d",&n)==1) break;
clrscr();
printf("\nN-ai introdus o cifr!\n");
printf("Introducei o cifr: ");
fflush(stdin); }
printf("\n\nAi introdus numrul ");
switch(n)
{ case 0: case 2: case 4: case 6:
case 8: printf("par %u",n); break;
default: printf(" %d",n); break;
case 1: case 3: case 5: case 7:
case 9: printf("impar %u",n); }
getch();
}
3.4. ,
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ int n[100],d=1,i=0,j;
clrscr();
printf("Introducei un ir numeric: ");
while( scanf("%d",&n[i])==1 ) i++;
for (j=--i; j>1; j--,i--)
{ if ( (n[i]-n[i-1])!=(n[i-1]-n[i-2]) ) d=0;}
printf("\n\nirul introdus ");
if(!d) printf("nu ");
printf("formeaz o progresie aritmetic.");
getch();
}
28

}
3.5.

.

#include <conio.h>
#include <stdio.h>
void main(void)
{ double t,x[100];int i=0,j=0,k;
clrscr();
while(scanf(%lf,&x[i++])==1);
i-=2;
printf(\nAi introdus irul:\n);
for( ; j<=i; j++) printf(%lf\t,x[j]);
for(j=0; j<i; j++)
{ for(k=j+1; k<=i; k++)
{ if( x[k]<x[j] ) { t=x[k],x[k]=x[j],x[j]=t;
}
}}
printf(\n\nirul ordonat este:\n);
for(j=0; j<=i; j++) printf(%lf\t,x[j]);
getch();
}
3.6.

. , abc cba
abbc cbba.
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main(void)
{ char cuv[256],l,i,t;
clrscr();
gets(cuv);
printf(Ai introdus cuvntul: %s\n\n,cuv);
l=strlen(cuv); //lungimea cuvntului cuv
for(i=0; i<l/2; i++)
{ t=cuv[i]; cuv[i]=cuv[l-1-i]; cuv[l-1-i]=t; }
printf(Cuvntul inversat este: );puts(cuv);
getch();
}
:
.
.
3.0. 3.1-3.6.
29

3.1. float y=5x2+3x-1,


x0 y=3x-1, <0.
if.
3.2. x double y=4x5+5x4-2x+7,
x<0, y=2000, x=0 y=5x4-2x+7, x>0.
if.
3.3.
, b

long double,

ax+b=0 .
a0; a=0 b0; =0 b=0.
if scanf()
.
3.4. a, b, c, d, e, f int,
: x=dx/det, y=dy/det, det=ac-bd, dx=cebf, dy=af-cd.
.
3.5. , , long,
++=0 .
,
if.
2

3.6. =3.32+7.710 x=1, 2, , 10.


while.
3.7. sin( x )
[ 00;3600 ], =0 1.
while.
3.8.
sin( x )

0
0
[ 0 ;360 ], =0 1.
23 ,
- .
while.
3.9. ,
. ,
.
while.
3.10. n , n! ( n
) 0<=n<=170. , n!=1, n=0 n=1
n!=123...n, n>1.
if while.

30

3.11.
,

.
while ( ).
3.12. ( printf() )
( putch() ) ASCII.
while.
3.13. ,
:
1)
m=(x1+x2+xn)/n,
2)
1/n
(x1*x2*xn) , 3) (((x1-m)2+(x2m)2++(xn-m)2 )/n)1/2.
while if .
3.14. , ..
.
70 . ,
abac a, ab, aba, bac, ac, c.
3.15. [ 0;1 ],
=10-10.
: xn+1=0.5(xn+a/xn), n=0, 1, 2,
; x0=1. |xn+1-xn|<.
3.16. s,
.
( ) ( )
s.
do-while, double cast.
3.17. long unsigned ,
.
for. ,
1 .
3.18.
, , ,

a, b, c, .

.
3.19.
.

unsigned

3.20. [ 1;7 ]
, ( 1 - , 2 - ,
, 7 ).
switch.

31

3.21. op1 op op2, 1 2 , - , (


+, -, * / ) .
switch.
3.22. n,
n, pow( x, n ).
n - int, x - long double.
3.23. xs s
int [ m; n ] 1 long double.

#include

.
3.24. ,
,
.
3.25.

,

.
:
3.1. ?
3.2. ?
3.3. ? .
3.4.
if? ,
if.
3.5. ?
?
3.6. ?
?
3.7. for,
? for?
3.8.
while,

? while.
3.9. do-while, ?
do-while.
3.10. for, while, do-while ?
3.11. switch ?
?
3.12. continue break ?
3.13. case default ?
3.14. ?
3.15. : for(;;); for(;;) continue; for(;;)
break; ? .
3.16. : while(3.14); while(0); while(1) continue; while(0) continue; while(1) break; ? .
32

3.17. :
do { ; } while( 3.14 ); do
{ continue; } while( -1 ); do { break; } while( 0 ); do { continue; } while( 0 );
do { break; } while( 1 ); ? .
3.18. char h; h=getch(); puts( );
switch( h )
{ default: puts();
case 1: printf(%d\n,1);
case 2: printf(%d\n,2);
case 3: printf(%d\n,3);
case 4: printf(%d\n,4);
}
? .
3.19. int x; do { clrscr(); puts( ! ) }
while( !scanf(%d,&x) ); ?
.
3.20. float x,y,z; do { clrscr(); puts( ! ) }
while( !(scanf(%f%f%f,&x,&y,&z)==3) );
? .
3.21. int a,b; printf( a b: ); for( ;
scanf(%d%d,&a,&b)!=2;
)
{
clrscr();
printf(
.
!\n); printf( a b: ); }.
? .
3.22. long double w; printf(\nw=); while( scanf(%Lf,&w)!
=1 ) { clrscr(); printf(! .\n); printf(\nw=); }.
? .
3.23. return ? .
3.24. _03
.

33

N 4
: ( )
:
.

( )

:
( ) ,
. -
.
( )
, .
tip *name, name -
, , tip , .. ,
- . :
int x; int *p; : p; x -
int, - int, ..
p
int. , int * .
int. ( ) int.
char *, float *, double *, long *, int **, long double *** ..
char, float, double, long, int *, long double **.
: char *s[234]; unsigned *r[25][25];
..
.
,
, , .. 2 . , near, far,
huge, ,
. far huge 2 , .. 4
, near, ,
, .. 2 .

&: int x=10; int *p; p=&x; -
, 10, -
int , , .
:
float w; float *y=&w; char *ps=Cibernetica; ps -
char * , ,
Cibernetica, ..
.
void * ( ).
,
34

, , void *p.
, -
.
void *,
.
*
( , ). - ,
* - , . , int x=10; *p;
p=&x; int,
, * , .. 10.
*=20; 20 ,
, .. =20; ,

float r, *q; q=&r; *q=3.141593,

r
3.141593.
void * ,
.. ,
. float x=2.82; void *r; r=&x;
*r ,
. *(float *)r.
*r+7.18. *(float*)r+7.18;

. , int
x=1,
*px=&x;
double
z=3.14,
*pz=&z;

print(%p%d%p
%lf,px,*px,pz,*pz);

z,
. .
.

: , ,
, , ,
.
( ++ ) ( -- ) .
++ ( -- ), , (
) , ,
. , int *p; +
+, ++, 2,
int 2 . ,
double *w; w++, ++w, w 8,
double 8 .
( )
( ) . - . +n ( -n )
( ) n*r , r , , .
, char *c; float *f; long doble *w ; unsigned n=5;
c+n, f+n, w+n; , f, w 5*1=5,
5*4=20 5*10=50 , , char
35

1 , float - 4 long double - 10


, .

, , ,
. -
tab[i] tab ( .. &tab[i] ), q tab[j] tab ( .. q
&tab[j] ). q
. ,
<q
,
i<j.
,
0 . ( )
<stdio.h> NULL.
, ,
. q
tab[i] tab[i+n], , n - ,
q-p n.
p-q -n.
. tab[n] -
n . tab ,
, .. &tab[0]. T tab+1 -
1, .. &tab[1], tab+2 - 2,
.. &tab[2] .. , *(tab+0), *tab,
tab[0], *(tab+1) - tab[1], *( tab+2) - tab[2]
..
, p , n
, p[n]=*(p+n) &p[n]=p+n.
tab[m][n] - m n
. tab[0] - ,
0 tab[m][n], .. &tab[0][0], tab[1] -
1 tab[m][n], .. &tab[1][0] ..
,
tab[0], tab[1], tab[2], ..., tab[m]

. , tab tab[m][n]
tab[0], tab[1], ..., ..
tab tab[0]. ,
&tab[i][j], tab[i]+j, *tab+i*n+j .
tab[i][j], *(tab[i]+j), *(*tab+i*n+j), , **tab - ,
tab[0][0].
:
4.1.

#include <conio.h>
#include <stdio.h>
void main()
{ int x[50]={1,2,3,4,5},*px,i,r,t;
36

clrscr();
px=x;
r=*px;
for(i=0;i<5;i++)
{ t=*(px+i);
if( r<t ) r=x[i]; }
printf("Cel mai mare numr din irul x este
%d",r);
getch();
}
4.2.
, ASEM MESA.
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main()
{ char s[256]="abracadabra",i,j,t;
clrscr();
puts("irul iniial:\n");
puts(s);
for(i=0,j=strlen(s)-1; i<j; i++,j--)
{ t=*(s+i); *(s+i)=*(s+j); *(s+j)=t; }
puts("\nirul inversat:\n");
puts(s);
getch();
}
4.3.
.
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main()
{ unsigned x[30]={20,22,10,25,20},s=0,i=0;
clrscr();
puts("irul iniial este:");
while( s<5 ) printf("%u\t",*(x+s++));
s=0;
while( i<5 )
{ s+=*(x+i); i+=2; }
printf("\nSuma elementelor de indice
par este %u",s);
getch();
}
4.4.
.
37

#include <conio.h>
#include <stdio.h>
#include <string.h>
void main()
{ char sir[256]="Asem CSIE",c,i=-1;
clrscr();
c=*sir;
for( ; ++i<strlen(sir) ; )
{ if (c<*(sir+i)) c=*(sir+i); }
puts("Caracterul de cod maxim n irul");
printf("%s\neste %c",sir,c);
getch();
}
4.5.
( . 4.6, 4.7 ).
#include <conio.h>
#include <stdio.h>
void main()
{ short i,j,t,n=12;
double x[234]={2,5,6,4,2,1,7,8,2,5,4,55};
clrscr();
puts("irul iniial:\n");
for(i=0;i<n;i++) printf("%lf\t",x[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if( x[i]<x[j] ) { t=x[i];x[i]=x[j];x[j]=t; }
puts("\n\nirul ordonat descresctor:\n");
for(i=0;i<n;i++) printf("%lf\t",x[i]);
getch();
}

4.6.

( . 4.5, 4.7 ).
#include <conio.h>
#include <stdio.h>
void main()
{ short i,j,t,n=12;
long double x[234]={2,5,6,4,2,1,7,8,2,5,4,55};
clrscr();
puts("irul iniial:\n");
for(i=0;i<n;i++) printf("%.1Lf\t",i[x]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if( x[i]<j[x] ) { t=*(x+i);x[i]=*&x[j];x[j]=t;
}
puts("\n\nirul ordonat descresctor:\n");
38

for(i=0;i<n;i++) printf("%.1Lf\t",*&i[x]);
getch();
}
4.7.
( . 4.5, 4.6 ).
#include <conio.h>
#include <stdio.h>
void main()
{ char x[256]="ACADEMIA DE STUDII ECONOMICE";
int n=27,i,j,t;
clrscr();
puts("irul iniial:\n");
puts(x);
for(i=0;i<n;i++)
for(j=i+1;j<=n;j++)
if( x[i]>x[j] ) { t=x[i];x[i]=x[j];x[j]=t; }
puts("\n\nirul ordonat cresctor:\n");
printf("*%s*",x);
getch();
}
4.8.
. ,

, .
#include <conio.h>
#include <stdio.h>
void main()
{ char c[15]="Cibernetica",s[15]="Statistica";
int i;
clrscr();
for(i=-2;i<13;i++)
printf("%p\t%c\t%p\t%c\n",c+i,c[i],s+i,s[i]);
getch();
}
4.9.

.
#include <conio.h>
#include <stdio.h>
void main()
{ char a[5][5]={ 'a','b','c','d','e',
'f','g','h','i','j',
'k','l','m','n','o',
'p','q','r','s','t',
39

'u','v','w','x','y' };
int i,j;
clrscr();
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{ printf("%p\t%p\t%p\t", &a[i][j],
&*(a[i]+j), &*(*a+5*i+j));
printf("%c\t%c\t%c\t", a[i][j],
*(a[i]+j), *(*a+5*i+j));
printf("%p\t%p\t%p\n", &a[i][j],
(a[i]+j), (*a+5*i+j));
getch(); }
getch();
}
:
.

! .
4.0. 4.1-4.9.
4.1. : a int, b float c
double. :
, , , ,
,
,
.
4.2.
.
4.3.
(
0 1 , 2
3 .. ).
4.4.
( 1, 3, ) .

4.5.
.
4.6.

40

4.7. p x, y
( p=x1y1+x2y2++xnyn ), .
4.8. y=( y1,y2,,yn ),
A mxn aij, i=1, 2, ,
m, j=1, 2, , n x=( x1,x2,,xm ) ( yi=ai1x1+ai2x2++aimxm, i=1, 2,
3, , n ).
4.9. C mxn
cij, i=1, 2, , m, j=1, 2, , n,
A mxp aik, i=1, 2, , m, k=1, 2, , p
B pxn bkj, k=1, 2, , p, j=1, 2, , n (
cij=ai1b1j+ai2b2j++aipbpj, i=1, 2, , m, j=1, 2, , n ).
4.10. ( ! ),
TRACTOR ( RAC, TOR,
ROTOR . . ). ,
.
:
4.1. ?
4.2. ?
4.3. ?
4.4. & ?
4.5.
* ?
4.6. char tab[256]; int vec[33]; float mat[4][3]; double cub[2]
[3][4]; tb, vec, mat, cub, mat[2], cub[1],
cub[2][3] ?
4.7.
hear, far, huge ?
, ?
4.8. , NULL
?
4.9. : int near *px; float far *py;
double huge *pz; char *pa; long huge *pd; long double *pw; ?
4.10. char a=a,*pa; int x =10, *px; float y=2.8, *py; double
z =12345.54321, *pz; long double w =123456789.987654321, *pw; pa =&a;
px=&x; py=&y; pz=&z; pw=&w; printf(%c\t%d\t%f\t%lt\t%Lf\
n, *pa,*px,*py,*pz,*pw); ?
4.11. char a, *pa=&a; int x, *px=&x; float q, *pq=&q; double
r, *pr=&r; long doable w , *pw=&w; scanf(%c%d%f%lf%Lf,pa,px,pq,pr,pw);

scanf()

printf(%c\t%d\t%f\t%lf\t%Lf\n,*pa,*px,*pq,*pr,*pw); ?
4.12. int x=2003, *p=&x, **q=&p, ***w=&q;
printf(%d%d%d%d,x,*p,**q,***w); ?
4.13.
char s[22]= 0,1,2,3,4,5 , *ps=s; int
x[22]={0,1,2,3,4,5},
*px=x;
float
y[22]={0,1,2,3,4,5},
*py=y;
double
41

z[22]={0,1,2,3,4,5}, *pz=z; long double w[22]={0,1,2,3,4,5}, *pw=w;

printf(%c%d%f%lf%Lf,*(ps+1),*(px+2),*(py+3),*(pz+4),*(pw+5)); ?
4.14.
short i[22]={0,1,2,3,4,5}, *pi=i;

printf(%d%d%d%d%d%d,*pi,*pi++,*pi++,*pi++,*pi++,*pi++); ?
4.15. int i[22]={0,1,2,3,4,5}, *pi=i; printf(%d
%d%d%d%d%d,*pi,*++pi,*++pi,*pi++,*pi++,*pi++); ?
4.16.
float i[22]={0,1,2,3,4,5},*pi=i+5;

printf(%f%f%f%f%f%f,*pi,*pi--,*pi--,*pi--,*pi--,*pi--); ?
4.17.
double i[22]={0,1,2,3,4,5), *pi=i+5;

printf(%d%d%d%d%d%d,*pi,*--pi,*--pi,*--pi,*--pi,*--pi); ?
4.18. _04
.

42

N 5
:
:
.
:
, text
, 100 .
30 , .
, .
, 4
100, , 400.


. ,
<string.h> str.
strcmp() int strcmp( const char *s1, const
char *s2 ); 1 2,

s1

s2,
.
, c1>c2, 0, 1=2,
, c1<c2.
stricmp() int stricmp( const char *s1, const
char *s2 ); 1 2,
s1 s2, ,
.
, c1>c2, 0, 1=2, ,
c1<c2.
strncmp() int strncmp( const char *s1, const
char *s2, unsigned n ); n
1 2, s1 s2,
. , c1>c2,
0, 1=2, , c1<c2.
strnicmp() int strnicmp( const char *s1, const
char *s2, unsigned n ); n
1 2, s1 s2,
, .
, c1>c2, 0, 1=2,
, c1<c2.
strlen() unsigned strlen( const char *s );

,

43


s.

, .

NUL,

strcpy() char *strcpy( char *d, const char *s );


,
s, d.
\0. d.
strncpy() char *strnpy( char *d, const char *s,
unsigned n ); n ,
,

s,

d.
d.
strcat() char *strcat( char *d, const char *s );
, d s:
,
s, , ,
d. \0.
d.
strncat() char *strncat( char *d, const char *s,
unsigned n ); ,
d, n ,

s:

n
,
, s,
, ,
d. d.
strchr() char *strchr( const char *s, int c );
c ,
s.

c

NULL.
strstr() char *strstr( const char *s1, const char
*s2 ); c2,
s2, c1,
s1.

s2 s1 NULL.
malloc()

void *malloc( unsigned n );

<alloc.h>.

heap

n

void,
.
, . (
calloc(), farmalloc(), farcalloc() ).

44

free() void free( void *p );


<alloc.h>. heap,
p. ( void farfree( void far *p ); ).
:
5.1.
.
char *bw(char *c1, char *c2, char *c3)
{ char *p;
p=( strcmp(c1,c2)>0 )?c1:c2;
return ( strcmp(p,c3)>0 )?p:c3;
}
5.2.
. ,
f:\bc\mydir\ex5_1.c.
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include f:\bc\mydir\ex5_1.c
void main(void)
{ char c1[30], c2[30], c3[30], c4[30], *q;
clrscr();
printf(Introdu un text din patru cuvinte: );
scanf(%s %s %s %s,c1,c2,c3,c4);
printf(\nAi introdus textul:\n\n);
printf(%s %s %s %s\n\n,c1,c2,c3,c4);
q=bw(c1,c2,c3);
q=( strcmp(q,c4)>0 )?q:c4;
printf(Cel mai mare cuvnt este: );
puts(q);
getch();
}
5.3.
.
char *sw(char *w1, char *w2, char *w3)
{ char *p;
p=( strlen(w1)<strlen(w2) )?w1:w2;
return ( strlen(p)<strlen(w3) )?p:w3;
}
5.4.
. ,
f:\bc\mydir\ex5_3.c.
45

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include f:\bc\mydir\ex5_3.c
void main(void)
{ char z1[30], z2[30], z3[30], z4[30], z5[30], *w;
clrscr();
printf(Introdu un text din cinci cuvinte: );
scanf(%s %s %s %s %s,z1,z2,z3,z4,z5);
printf(\nAi introdus textul:\n\n);
printf(%s %s %s %s %s\n\n,z1,z2,z3,z4,z5);
w=sw(z1,z2,z3);
w=sw(w,z4,z5);
printf(Cel mai scurt cuvnt este: );
puts(w);
getch();
}
5.5. heap.
, ,
.
char *wh(char *w)
{ char *p;
p=(char *)malloc( strlen(w)+1 );
if( p ) strcpy(p,w);
return p;
}
5.6. .
void sd(char *wp[], unsigned n)
{ char *t, i=0,j;
while( i<n-1 )
{ j=i+1; t=wp[i];
while( j<n )
{ if( strcmp(t,wp[j])<0 ) { t=wp[j];
wp[j]=wp[i]; wp[i]=t; }
j++; }
i++; }
}
5.7.
.
void ld(char *wp[], unsigned n)
{ char *t, i=0,j,d,r;
while( i<n-1 )
{ j=i+1; t=wp[i]; d=strlen(wp[i]);
46

while( j<n )
{ r=strlen(wp[j]);
if( r<d ) { d=r; t=wp[j];
wp[j]=wp[i]; wp[i]=t; }
j++; }
i++; }
}
5.8.
. ,
.
char *dl(unsigned nl)
{ static char *mn[13]=
{,Ianuarie, Februarie, Martie,
Aprilie, Mai,
Iunie,
Iulie,
August, Septembrie,
Octombrie, Noiembrie, Decembrie};
return mn[nl]; }
5.9.
. , .
unsigned nl(char *dl)
{ static char *mn[13]=
{,Ianuarie, Februarie, Martie,
Aprilie, Mai,
Iunie,
Iulie,
August, Septembrie,
Octombrie, Noiembrie, Decembrie};
unsigned n;
for(n=1; n<13; n++)
if( !strcmp(dl,mn[n]) ) return n;
}
5.10.
z luna a, z - , luna - , a -
( ) .
, .
unsigned nz(unsigned z, char luna[], unsigned a)
{ unsigned i=1,nl=0,d=0;
static unsigned zl[]=
{0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
static char *dl[]=
{, Ianuarie, Februarie, Martie,
Aprilie, Mai,
Iunie,
Iulie,
August, Septembrie,
Octombrie, Noiembrie, Decembrie};
while( strcmp(luna,dl[++nl]) ) ;
47

for( ; i<nl; i++)


d+=zl[i]; d+=z;
if( nl>2 ) d+=( (a%4==0)&&(a%100)||(a%400==0) );
return d;
}
5.11. (
) .
void daz(unsigned an, unsigned zi, int *d, char l[])
{ static char *dl[]=
{, Ianuarie, Februarie, Martie,
Aprilie, Mai,
Iunie,
Iulie,
August, Septembrie,
Octombrie, Noiembrie, Decembrie};
static unsigned nz[]=
{0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
unsigned nl=0,s=0;
nz[2]+=( (an%4==0)&&(an%100)||(an%400==0) );
while( s<zi ) s+=nz[++nl];
s-=nz[nl]; *d=zi-s; strcpy(l,dl[nl]);
}
5.12.
dd luna yyyy. , , luna
- , yyyy .

nz()

5.10,

f:\bc\mydir\ex5_10.c.
char *ziua(unsigned dd, char luna[], unsigned yyyy)
{ unsigned b=0,d1,d2,a1,a2; char m1[30],m2[30];
unsigned nd1,nd2,nd1nd2,i,r,bi;
/* Fie cunoscut denumirea zilei pentru o dat
reper, de exemplu 16 Noiembrie 1999 Mari
*/
d1=dd, strcpy(m1,luna), a1=yyyy,
d2=16, strcpy(m2,"Noiembrie") ,a2=1999;
nd1=nz(d1,m1,a1);
nd2=nz(d2,m2,a2);
if( yyyy<1999 )
{ if( a2-a1==1 )
{ b=(a1%4==0)&&(a1%100)||(a1%400==0);
nd1nd2=nd2+365+b-nd1; }
else { for(b=0,i=a1;i<a2;i++)
b+=(i%4==0)&&(i%100)||(i%400==0);
bi=(a1%4==0)&&(a1%100)||(a1%400==0);
nd1nd2=365*(a2-a1-1)+b+nd2+
48

365+bi-nd1; }
r=nd1nd2%7; return eliz[r]; }
if( yyyy>1999 )
{ d2=dd, strcpy(m2,luna), a2=yyyy,
d1=16, strcpy(m1,"Noiembrie") ,a1=1999;
nd1=nz(d1,m1,a1);
nd2=nz(d2,m2,a2);
if( a2-a1==1 )
{ b=(a1%4==0)&&(a1%100)||(a1%400==0);
nd1nd2=nd2+365+b-nd1; }
else { for(b=0,i=a1;i<a2;i++)
b+=(i%4==0)&&(i%100)||(i%400==0);
bi=(a1%4==0)&&(a1%100)||(a1%400==0);
nd1nd2=365*(a2-a1-1)+b+nd2+
365+bi-nd1; }
r=nd1nd2%7; return zile[r]; }
if( (yyyy==1999)&&(nd1>nd2) )
{ d2=dd; strcpy(m2,luna); a2=yyyy;
d1=16, strcpy(m1,"Noiembrie") ,a1=1999;
nd1=nz(d1,m1,a1);
nd2=nz(d2,m2,a2);
printf("nd1=%u nd2=%u\n",nd1,nd2); getch();
nd1nd2=nd2-nd1; r=nd1nd2%7; return zile[r]; }
else { nd1nd2=nd2-nd1; r=nd1nd2%7;
return eliz[r]; }
}
5.13. dd luna yyyy
. dd ,
luna


yyyy

. ,
. 5.10 5.12,

f:\bc\mydir\ex5_10.c

f:\bc\mydir\ex5_12.c, .
#include <conio.h>
#include <stdio.h>
#include <string.h>
unsigned zl[]=
{0,31,28,31,30,31,30,31,31,30,31,30,31};
char *dl[]=
{ "", "Ianuarie", "Februarie", "Martie",
"Aprilie", "Mai",
"Iunie",
"Iulie",
"August", "Septembrie",
"Octombrie", "Noiembrie", "Decembrie" };
char *zile[]=
{ "Mari", "Miercuri", "Joi", "Vineri",
"Smbt", "Duminic", "Luni" };
49

char *eliz[]=
{ "Mari", "Luni", "Duminic", "Smbt",
"Vineri", "Joi", "Miercuri" };
#include "f:\bc\mydir\ex5_10.c"
#include "f:\bc\mydir\ex5_12.c"
void main(void)
{ unsigned d,y; char luna[30], *p;
clrscr();
printf("Introdu o dat calendaristic
dd luna yyyy: ");
scanf("%2d %s %4d",&d,luna,&y);
printf("\nAti introdus data: %2d %s %4d\n\n",
d,luna,y);
p=ziua(d,luna,y);
printf("%s",p);
getch();
}
:
.
.
5.0. 5.1-5.13.
5.1. ( )
.
strcmp().
5.2. (
) .
strlen().
5.3. ( ) .
strcmp(), strcpy().
5.4. ( )
.
strlen(), strcpy().
5.5.
( ) _ .
stricmp(), strcpy().
5.6.
( ) .
strlen(), strcpy().
50

5.7. ,
EOF,
( ) , heap,
.
5.5.
5.8. ,
EOF,
( ) , heap,
.
5.5.
5.9. ,
EOF


heap,

( ) .
5.6.
5.10. ,
EOF

heap, (
) .
5.7.
5.11. ddmmyy, dd
, mm - yy -
; , , dd
luna yyyy, luna - yyyy ( 4 ) -
.
5.8.
5.12. dd luna yyyy, dd
- , luna - yyyy ( 4 ) -
; , ,
dd/mm/yy, mm , yy -
.
5.9.
5.13. ,
, ,
, .
5.11.
5.14. ,
,
.
5.15. ,
.
5.16. ,
,
.
51

:
5.1. :
strcmp( asem, ASEM ), stricmp(
ASEM, asem ), strncmp( Limbajul C, Limbajul C++, 10 ), strncmp(
Limbajul C++, Limbajul C, 12 ), strncmp( Limbajul C++, Limbajul C,
14 ), strnicmp( ABRACADABRA, arbadacarba, 1 ) ? .
5.2. : strlen( ASEM ), strlen( asem0 ), strlen(
asem\0 ), strlen( ASEM\n ), strlen( ASEM\N ), strlen( ), strlen( \0
), strlen( 0 ), strlen( Cibernetica\t ) ? .
5.3. :
strcat( Limbajul C, ++ ), strncat(
Cibernetic, , Informatic Economic, Statistic, Previziune Economic,
21 ) ? .
5.4. char s[]=abracadabra, *p; p=strchr( s, a );
printf( %c\t%d\t%p\n, *p, p+1, p+6 ); ? .
5.5. char b[]=Cibernetica\n, *r, a=\n; r=strchr( b, a );
printf( %c\n, *r ); ? .
5.6.
puts( strstr( Contabilitate financiara, abil ) );
?
5.7.

char
*s=Informatica,
*p=Cibernetica,
*q=Statistica, *r=Economica; strcpy( s, p ); strcpy( p, q ); strcy( q, r );
strcpy( r, s ); puts( s ); ? .
5.8. char *p,*q,*r; puts( strncpy( p, Limbajul
C++, 10 ) ); puts( strncpy( q, Limbajul C++, 12 ) ); puts( strncpy( r,
Limbajul C++, 14 ) ); ? .
5.9. int *x; float *y; double *z; long double *w; x=(int
*)malloc( 5*sizeof( int ) ); y=(float *)malloc( 5*sizeof( float ) ); z=(double
*)malloc( 5*sizeof( double ) ); w=(long double *)malloc( 5*sizeof( double ) );
w=(long double *)malloc( 5*sizeof( long double ) );
malloc() x, y,
z, w ? .
5.10. malloc() calloc() ?
(int *)malloc( 5*sizeof( int ) ); (int *)calloc( 5*sizeof( int ) ); ?
.
5.11. char *s=Costel, Vicu Andrei ,
*q=Vicu; puts( strstr( s, q ) ); ? .
5.12. _05
.

52

N 6
: ,
:

,
( , , , );
typedef .
:

. , ,
, .
.
.
.
.
,
, , .
:
struct name { } name1, name2, namen;
name , name1, nam2, , namen
, .. name .
name. name1, name2, ,
namen
.
name.
, .. name, , .. name1, name2, ,
namen, , .
, ,
.
name ( , )
:
struct name name_of_structure;
:
1) struct punct { int x; int y; } a, b, c, *w;
punct a, b, c
punct, .. int,
w punct.
53

2) punct.
, struct punct d, e={ 10, 20 }, *q, f[11]; d, e
punct, ( , e
); q punct,
f punct.
1) struct dosar
{
char nume[25];
char prenume[25];
struct data_calenraristica
{ int zi;
char luna[11];
int an;
} data_nasterii, data_angajarii;
} angajat, secie[100];

m.n p->n, m - , n - ,
, p - . ,
1), a.x x a punct, w->y
y punct,
w.


,
,
. , 1),
2+2=4 , .. 2 , 2
.
.

.
,
. , struct
union.
, :
union name { } name1, name2, , namen;
:
union aria {

int x;
float r;
double w;
char t[5];
} f1, f2, f3[6], *z;

//
//
//
//

54

2 octei
4 octei
8 octei
5*1=5 octei

f1, f2 aria, f3,


6 , aria,
z, aria.
, ,
, ,
. ,
8 ,
w double 8 .
,
.
, ,

.
.
.
:
enum name { name0, name1, , namen } v1, v2, , vm;
name - , nam1, name2, ,
namen - ,
, : name0 0, name2
1, , namen n. v1, v2, , vm -
name, .. .
int. ,
, name0, name1, , namen
0, 1, , n.

. ,
namei=ci namei ,
namei ci. ci
.
, ,
1. , .. name0,
0.
:

1) enum luna { ileg, ian, feb, mar, apr, mai, iun, iul, aug, sep, oct, nov,
dec } m1, m2, m3[12], *w;
2) enum mon { ian=1, feb, mar, apr, mai, iun, iul, aug, sep, oct, nov,
dec };
3)

enum boolean { false, true };


55

enum boolean bisect;


4)

enum { luni=1, marti, miercuri, joi, vineri, simbata, duminica } zi;

(
) .
, .
:
typedef old_tip_name

new_tip_name;

:
1) typedef int INTEGER;
INTEGER x,y;
2) typedef struct { double real; double imag; } COMPLEX;
COMPLEX w, tz[11];
3) typedef struct { int x; int y; } POINT;
POINT a,b;
1) typedef union { double raza; double tp; double ld[2]; double lt[3]; }
figura;
figura cerc, patrat, triunghi;
.

.
.
.
.
, .
:
struct name { pole1; pole2; ; polen; } n1, n2, , nm;
name - , n1, n2,
, nm - , ..
, pole1, pole2, , polen -
.
:
tip imea_polea dlina_polea;

56

tip - , imea_polea - , dlina_polea -


, .
, ,
. 0,
. ,
.
,
. unsigned, int, signed, char, usigned
char.
.
:
struct { unsigned
int
unsigned
unsigned
unsigned
int
unsigned

a:2;
b:2;
:3;
c:2;
:0;
d:5;
e:5; } x,y;

2 x, y .
, .. . x 2
: 0 1
a, 2 , ..
2 3,
b, 3 , .. 4, 5, 6,
,
. , 7, 8 c.
0, , d, ,
. , d
0, 1, 2, 3, 4 . , ,
e 5, 6, 7, 8, 9
.
, x.a=1; a
1, .. 0 1, 1 0.
, x.b=-1; b 1, .. 2 3 1, .. -1
- 11.
.
&.

.
:

57

6.1. .
, COMPLEX. COMPLEX
: typedef struct { double x; double y }
COMPLEX; double ,
.
z=x+iy argz :
0,

/2,

3/2,

arctg( y/x ),
arctg( y/x )+,
2+arctg( y/x ),

x0 y=0;
x=0 y0;
x<0 y=0;
x=0 y<0;
x>0 y>0;
x<0 y0;
x>0 y<0.

double darg(COMPLEX *w)


{ double a;
if( (w->x >= 0) && (w->y == 0) ) return 0.0;
if( (w->x == 0) && (w->y >=0) ) return PI/2;
if( (w->x < 0) && (w->y == 0) ) return PI;
if( (w->x == 0) && (w->y < 0) ) return 3*PI/2;
a=atan( w->y / w->x );
if( (w->x < 0) && (w->y != 0) ) return a+PI;
if( (w->x > 0) && (w->y < 0) ) return a+2*PI;
return a;
}
6.2.
. darg()
6.1, f:\bc\mydir\ex6_1.c.
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979
typedef struct { double x; double y; } COMPLEX;
#include f:\bc\mydir\ex6_1.c
void main(void)
{ COMPLEX z1,z2;
double argz1, argz2;
clrscr();
printf(Introducei 2 numere complexe: );
scanf(%lf %lf %lf %lf,
&z1.x,&z1.y,&z2.x,&z2.y);
argz1=darg(&z1); argz2=darg(&z2);
printf(\n\nAi introdus numerele
complexe:\n\n);
printf(z1=(%-.2lf)+i(%-.2lf)\n
z2=(%-.2lf+i(%-.2lf)\n\n,
z1.x,z1.y,z2.x,z2.y);
58

printf(argz1=%-lf\nargz2=%-lf\n,argz1,argz2);
printf(\nNumerele ordonate n ordinea creterii
argumentelor:\n\n);
(argz1<argz2)?printf((%-.2lf)+i(%-.2lf)\n
(%-.2lf)+i(%-.2lf)\n, z1.x,z1.y,z2.x,z2.y):
printf((%-.2lf)+i(%-.2lf)\n(%-.2lf)+i(%-.2lf)\n,
z2.x,z2.y,z1.x,z1.y);
getch();
}
6.3.
n z,
. n z .
darg() 6.1 z.
darg() f:\bc\mydir\ex6_1.c.

n
,
z=x+iy, z
z=r(cos+isin), r -
z, - z. ,
n z. n :
rm=r1/n(cos( (+2m)/n )+isin( (+2m)/n ), m=0, 1, , n-1.
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979
typedef struct { double x; double y; } COMPLEX;
#include f:\bc\mydir\ex6_1.c
void main(void)
{ COMPLEX z;
double modz, argz, mod, arg;
unsigned n, m;
clrscr();
printf(Introducei ordinul n pentru rdcin
i numrul complex z:\n\n);
scanf(%u %lf %lf, &n, &z.x, &z.y );
printf(\n\nAi introdus n=%-u i
z=(%.2lf)+i(%.2lf)\n\n, n, z.x, z.y );
//Calculm modulul numrului z
modz=sqrt( z.x*z.x+z.y*z.y );
//Calculm argumentul numrului z
argz=darg(&z);
//Calculm modulul pentru rdcini
mod=pow( modz, 1.0/n );
//Calculm rdcinile de ordinul n din z
printf(Rdcinile de ordinul %d din numrul
z=(%.2lf)+i(%.2lf) sunt:\n\n,
n, z.x, z.y );
for(m=0; m<n; m++)
{ arg=(argz+2*PI*m)/n;
59

printf(r(%2d)=(%-.2lf)+i(%-.2lf)\n,
m, mod*cos(arg), mod*sin(arg) ); }
getch();
}
6.4.
.
, RATIONAL,
. RATIONAL :
typedef struct { long unsigned m; long unsigned n; } RATIONAL;
RATIONAL *,
.
.
RATIONAL *simplifica( RATIONAL *f)
{ static RATIONAL s;
long unsigned k,h, cmmdc=1;
/* h este minimul dintre numitorul i
numrtorul fraciei f */
h=(f->m)<(f->n)?(f->m):(f->n);
/* cmmdc este cel mai mare divizor comun
pentru numrtor i numitor */
for(k=1; k<=h; k++)
if( (f->m) % k == 0 && (f->n) % k == 0 )
cmmdc=k;
s.m=(f->m)/cmmdc; s.n=(f->n)/cmmdc;
return &s;
}
6.5. , ,
, .
RATIONAL simplifica() 6.4,
f:\bc\mydir\ex6_4.c.
#include <conio.h>
#include <stdio.h>
typedef struct { long unsigned m; long unsigned n; }
RATIONAL;
#include "f:\bc\mydir\ex6_4.c"
void main(void)
{ RATIONAL *p, frac;
clrscr();
printf("Introducei o fracie raional:\n\n");
scanf("%lu %lu", &frac.m, &frac.n);
printf("\n\nAi introdus fracia:\n\n");
printf("%lu/%lu\n\n", frac.m, frac.n);
p=simplifica( &frac );
printf("Fracia simplificat este:\n\n");
printf("%lu/%lu",p->m,p->n);
getch();
}
60

6.6. : , ,
, ,
. , ,
. :
- a3, a - ;
- 4/3r3, r - ;
- 1/3r2h, h - , r -
;
- r2h, h - , r -
;
- abc, a, b, c - ;
- 1/3abh, h - , a, b -
, .
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265358979
void main(void)
{ union { double r;
double a[2];
double b[3]; } c;
int v; double volum; clrscr();
printf(Alegei o alternativ:\n\n);
printf(1 cub\n
2 sfer\n
3 con\n);
printf(4 cilinrdu\n
5 paralelepiped\n
6 piramid\n\n);
scanf(%d,&v); printf(\n\n);
switch(v) {
default:printf(Ai ales alternativa de
ieire); getch(); exit(1);
case 1: printf (Introducei lungimea
laturii cubului: );
scanf(%lf, &c.r);
volum=c.r*c.r*c.r;
printf(\n\nVolumul cubului cu
latura a=%-.2lf este ); break;
case 2: printf(Introducei lungimea razei
sferei: );
scanf(%lf, &c.r);
volum=4.0/3.0*PI*c.r*c.r*c.r;
printf(\n\nVolumul sferei cu raza
r=%-.2lf este ); break;
case 3: printf(Introducei lungimea razei
circumferinei din baza conului);
61

printf( i nlimea conului: );


scanf(%lf%lf,&c.a[0],&c.a[1]);
volum=1.0/3.0*PI*c.a[0]*c.a[0]*c.a[1];
printf(\n\nVolumul conului cu raza
bazei);
printf( r=%-.2lf i nlimea h=%.2lf este ); break;
case 4: printf(Introducei lungimea razei
circumferinei din baza cilindrului);
printf( i nlimea cilindrului: );
scanf(%lf%lf,&c.a[0],&c.a[1]);
volum=PI*c.a[0]*c.a[0]*c.a[1];
printf(\n\nVolumul cilindrului cu
raza );
printf(bazei r=%-.2lf i nlimea
h=%-.2lf este ); break;
case 5: printf(Introducei lungimile
laturilor paralelipipedului: );
scanf(%lf%lf%lf,
&c.b[0],&c.b[1],&c.b[2]);
volum=c.b[0]*c.b[1]*c.b[2];
printf(\n\nVolumul papalelipipedului
cu laturile );
printf(a=%-.2lf, b=%-.2lf i
c=%-.2lf este ); break;
case 6: printf(Introducei lungimile
laturilor dreptunghiului );
printf(din baza piramidei );
printf(si inaltimea piramidei: );
scanf(%lf%lf%lf,
&c.b[0],&c.b[1],&c.b[2]);
volum=1.0/3.0*c.b[0]*c.b[1]*c.b[2];
printf(\n\nVolumul piramidei cu
laturile dreptunghiului );
printf(din baz a=%-.2lf, b=%-.2lf
i nlimea h=%-.2lf este );
}
printf(%-.2lf\n\n,volum);
getch();
}
6.7.
, .
.
: =1, =2, =3, =4.
#include <conio.h>
#include <stdio.h>
void main(void)
{ unsigned nl;
62

enum { Ian=1, Feb, Mar, Apr, Mai, Iun,


Iul, Aug, Sep, Oct, Nov, Dec } dl;
enum { Primvara=1, Vara, Toamna, Iarna } da;
while(1)
{ clrscr();
printf(Introducei numrul unei luni
calendaristice: );
if( scanf(%u, &dl)==1 )
if( dl>0 && dl<13 ) break; }
if( dl==Dec||dl==Ian||dl==Feb )
{ da=Iarna; printf(%u Iarna\n, da); }
if( dl==Mar||dl==Apr||dl==Mai )
{ da=Primvara; printf(%u Primvara\n,
da); }
if( dl==Iun||dl==Iul||dl==Aug )
{ da=Vara; printf(%u Vara\n, da); }
if( dl==Sep||dl==Oct||dl==Nov )
{ da=Toamna; printf(%u Toamna\n, da); }
getch();
}
6.8.

, ,
.
.
#include <conio.h>
#include <stdio.h>
void main(void)
{ union { struct {
unsigned b0_3 : 4;
unsigned
: 4;
unsigned
: 4;
unsigned b12_15: 4;
} nb;
unsigned n; } nu;
clrscr();
printf("Inroducei un numr ntreg fr semn: ");
scanf("%u",&nu.n);
printf("%u\n",nu.nb.b12_15);
printf("%u",nu.nb.b0_3);
getch();
}
:

.
63

6.0. 6.1-6.8.
6.1. ( ,
, , , ,
, , , , ,
).
6.2. zn .
6.3.
.
z2+bz+c=0,

6.4.
.

a, b, c, z

6.5.
( , , , , , , ,
).
6.6. .
6.7. , :
, , ,
( ( x, y ) ! ).
6.8. , 7.
6.9.

, :
, , , .
union !
6.10.
( ,
,
,
).


,
,
,

6.11.

An A.

6.12.

det(A) - A.

6.13.

6.14.

Ax=b A.

6.15. ,
.
enum !
:
64

6.1. ? ?
6.2.
?

?
6.3. ?
6.4. ?
6.5. ?
6.6. ?
6.7. ?
6.8.
?

?
6.9. ?
6.10. ?
6.11. ?
6.12. ?
6.13. ?
6.14. ?
6.15. ?
6.16. ?
6.17. ?
6.18. ?
6.19. ?
6.20. ?
6.21. ?
6.22.
?
6.23. ?
6.24. ?
6.25. ?
6.26. ?
6.27.
?
6.28.
?
6.29.


?
6.30.


?
6.31. ?
6.32. _06
.

65

N 7
: main()
:
main()

:
( ++ ) , ..
.
main() tip main( int argc, char
*argv[] ).

argc
,
1, argv[] ,

. argv[0]
,
( .exe
).
argv[1]

, ,
argv[2]
,
.. , main()
.
MS DOS,
, ,
, ,
Enter.
Borland C++,
Run-Arguments.
Program Arguments,
, ,
Enter OK.
.
:
7.1. main().
#include <conio.h>
#include <stdio.h>
void main(int argc, char *argv[])
{ int i=0;
clrscr();
for( ; i++<argc-1; )
printf(%s\n,argv[i]);
getch();
}
66

7.2.
.
main().
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(int a, char *b[])
{ char *dl[]=
{"","Ianuarie", "Februarie", "Martie",
"Aprilie", "Mai",
"Iunie",
"Iulie",
"August", "Septembrie",
"Octombrie", "Noiembrie", "Decembrie"};
int i,bi,an,nz[]=
{0,31,28,31,30,31,30,31,31,30,31,30,31};
clrscr(); an=atoi(b[2]);
bi=(an%4==0)&&(an%100)||(an%400==0);
if( bi ) nz[2]+=1;
for(i=1; i<13; i++)
if( !strcmp(b[1],dl[i]) )
{ printf("\n%s - %d zile\n",b[1],nz[i]);
break; }
getch();
}
7.3. , main(), ,
main().
. (), 2 .
2 ,
, . ,
main() .
() ,
main().
#include <conio.h>
#include <stdio.h>
void main(int p, char **w)
{ double c(int, char **),k;
k=c(p,w);
if( k>0 ) printf("\n\nargv[1]: %s\n",w[1]);
else
printf("\n\nargv[2]: %s\n",w[2]);
getch(); }
double c(int a, char **b)
{ double k; int m,n; clrscr();
printf("Introduceti 2 numere: ");
scanf("%d %d",&m,&n);
if( m==0||n==0 ) main(a,b);
67

k=1.0/(double)m + 1.0/(double)n;
return k; }
7.4. main() .
main()
. ,
- .
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main(int a, char *b[])
{ char *s; clrscr();
printf("Limbajul de programare ? ");
gets(s);
if( strcmp(s,b[1]) )
{ printf("Greit ! Apsai o tast
!\n");
getch(); main(a,b); }
printf("Corect ! Ai ghicit !
Apsai o tast !\n");
getch(); exit(1);
}
:

.

7.0. 7.1-7.4.
7.1. .
main().
7.2. main() :
( %s )
, ( % ).
7.3. main(), argc.
7.4. mm/dd/20yy ,
MS DOS dd month yy. dd
, month , mm - , yy
, .
:
7.0. main()?
68

7.1. ( ) main()
MS DOS ?
7.2. main() Borland C++ ?
7.3. main()
?
7.4. main() ?
7.5.
main(), ?
7.6.
main()

, ?
7.7. main(),
argv[0] ?
7.8.

main() ?
7.9. Monday, Tuesday, Thursday

main( int a, char *v[] )


{ char *b=Monday, *c=Tuesday, *d=Thursday;
strcpy( v[1], d ); strcpy( v[a], b );
printf(%d\t%s\t%s\t%s\n,a,v[1],v[2],v[3]);
}
?
7.10. _07
.

69

N 8
:
:
.
:

.

,
. , (
), .
, fact( n ) .
n0 : fact( n )=1,
n=0, fact( n )=nfact( n-1 ), n>0.
,
. fact( n ), fact( n )=1,
n=0, , ,
.
fact( n )
:
double fact( int n )
{ if (n==0) return 1.0;
else return n*fact(n-1); }
sscanf( p, c, p1, p2, ) scanf().
, p.
c, p1, p2, scanf(). , char.
, ASCII. sscanf(),
scanf(),
. sscanf() ,
scanf(), , .
, , gets().
, .
:
8.1. n [ 0;170 ]
n! ( n ),
fact(n).
#include <stdio.h>
70

#include <conio.h>
#define MAX 170
void main(void)
{ double fact(int n);
int n; char t[255];
clrscr();
for ( ; ; )
{ printf(n=? );
if( gets(t)>0 && sscanf(t,%d,&n)==1 &&
n>=0 && n<=MAX ) break;
printf(Se cere un numr ntreg din
segmentul [0,%d]\n,MAX); }
printf(n=%d\tn!=%g\n,n,fact(n));
getch();
}
double fact(int n)
{ if( n==0 ) return 1.0;
else return n*fact(n-1); }
8.2. A(n,k) n
k, 1kn170. A(n,k)
: A(n,1)=n k=1 A(n,k)=(n-k+1)A(n,k-1) k=2,
3 , , n. , .
#include <stdio.h>
#include <conio.h>
unsigned A(unsigned n, unsigned k)
{ if( k==1 ) return n;
else return (n-k+1)*A(n,k-1);
}
void main(void)
{ unsigned n=17, k=7, aranjamente;
clrscr();
aranjamente=A(n,k);
printf(A(%-u,%-u)=%-u,n,k,aranjamente);
getch();
}
8.3. [
m; n ] ( m<n ). .
#include <stdio.h>
#include <conio.h>
unsigned Suma(unsigned m, unsigned n)
{ if( m==n ) return m*m;
else return m*m+Suma(m+1,n); }
71

void main(void)
{ unsigned m=1,n=10;
clrscr();
printf(%-u\t%-u\tSuma=%-u,m,n,Suma(m,n));
getch();
}
8.4.

. .
#include <stdio.h>
#include <conio.h>
int x[4]={ 23,-34,43,-3 };
void main(void)
{ int Mic(int );
int n=3;
clrscr();
printf(Cel mai mic numr este %d, Mic(n));
getch();
}
int Mic(int n)
{ if( n==0 ) return x[n];
else return x[n]<Mic(n-1)?x[n]:Mic(n-1); }
8.5.
,

^z, .e. EOF.


#include <stdio.h>
#include <conio.h>
void citex(void)
{ int *x, *s=0;
if( scanf(%d,x)!=1 ) { printf(%d,*s);
return; }
else { *s+=*x; citex(); }
}
void main(void)
{ clrscr();
citex();
getch();
}
8.6.
,
,
. .
#include <stdio.h>
#include <conio.h>
int A[3][4]={ 1, 2, 3, 4,
72

10, 11, 12, 5,


9, 8, 7, 6 };
void Tipar(int i, int j, int n, int m)
{ if( i>m||j>n) return;
else { int k;
for(k=j; k<m; k++)
printf("%d\t",A[i][k]);
for(k=i; k<n; k++)
printf("%d\t",A[k][m]);
for(k=m; k>j; k--)
printf("%d\t",A[n][k]);
for(k=n; k>i; k--)
printf("%d\t",A[k][j]);
i++; j++; n--; m--;
Tipar(i,j,n,m); }
}
void main(void)
{ clrscr();
Tipar(0,0,2,3);
getch();
}
:

.

8.0. 8.1-8.6.
8.1. A(n,k) n k,
1kn170. A(n,k) :
A(n,k)=n!/(n-k)!
.
8.2. C(n,m) n m,
1mn170. C(n,m) :
C(n,m)=n m=1 C(n,m)=C(n,m-1)/(m(n-m)) m=2, 3, , n.
, .
8.3.
m n.

cmmdc(m,n)

8.4. ,

cmmdc(m,n),
:
cmmdc(m,n)=m, n=0 m0, cmmdc(m,n)=n, m=0 n0,
cmmdc(m,n)=cmmdc(n,m%n), m>n>0. m=n=0 cmmdc(m,n)
.
73

8.5.
m n.

cmmmc(m,n)

8.6.
cmmmc(m,n)=mn/cmmdc(m,n) ( 8.3 ).
8.7.
f(n),

: f(n)=0, n=0, f(n)=1,
f(n)=f(n-1)+f(n-2), n=2, 3, .

n0
n=1,

8.8. a(m,n),
m0, n0 : a(m,n)=n+1, m=0,
a(m,n)=a(m-1,a(m,n-1)), m=1, 2, 3, , n=1, 2, 3, .
8.9. ( abcddcba
).
8.10.

f(abcd)=strcat( f( bcd ), a ).

8.11.

[ min(m,n]; max(m,n) ].
8.12.

m, n

8.13.

.
8.14.

8.15. .
!
.
, , ,
,
n

.
.

.
,
.

.
:
8.0. ?
74

8.1.
?
8.2.
, ?
8.3. ,
?
.
8.4. _08
.

75

N 9
:
:
:
(, , ,
..).
.
, . ,
, .
:
,
;
, ,
, . ,
, ,
.

(bubblesort).
.
, .
, , ..
.
. ,
.
9.1, 9.2.
Shell (shellsort; ).
, 1,
.
, .


. ,
.
Shell ,
.
.

.
, .
Shell 9.3.

76

I (quicksort; ).

, , ,
, , ,
. .
.
.
,
.
.
,
.
,
.
I (quicksort).
9.4.
II (quicksortinf;
). ,
. ,

. ,
. ,
,
.
.
,
. .

II (quicksortinf). 9.5.
:
9.0. , .
, , . EOF
. .
f_9_00.c.
// Exemplul 9_0. Functia citeste un text
int ct(char w[], char *p[]) {
// w[] - Textul, cuvintele sunt separate cu \0
// p[] - Pointerii spre cuvinte
int i=0; // Numrul de cuvinte
int j=0; // Indice pentru cuvinte
char c;
c=getchar();
while( c!=EOF ) {
while( c<'A' || c>'Z' && c<'a' || c>'z' ) {
c=getchar(); if( c==EOF ) break; }
77

if( c==EOF ) break;


p[i++]=w+j;
while( c>='A' && c<='Z' || c>='a' && c<='z' ) {
w[j++]=c; c=getchar(); }
w[j++]='\0'; } return i; }
9.1.
, .
// Exemplul 9_1. Metoda bulelor
#include <stdio.h>
#include <conio.h>
void main(void) {
int x[123], i=0,q=1,p=0,n,t; clrscr();
printf("Introdu un ir de ntregi i ^z la sfrit\n\n");
while( (scanf("%d",x+i)==1) ) ++i;
n=i; clrscr();
printf("Sortare prin metoda bulelor\n\n");
printf("\nirul introdus:\n\n");
for(i=0; i<n; i++)
printf(" %d ", *(x+i));
while(q) { q=0;
for(i=0; i<n-1; i++) {
if( *(x+i) > *(x+i+1) ) {
t=*(x+i); *(x+i)=*(x+i+1);
*(x+i+1)=t; q=1; ++p; } } }
printf("\n\nirul sortat:\n\n");
for(i=0; i<n; i++)
printf(" %d ", *(x+i));
printf("\n\n%d permutri\n",p);
getch(); }
9.2.
( )
, .
, ,
.
f_9_00.c 9.0.
// Exemplul 9_2. Metoda bulelor
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "d:\borlandc\bin\euue\lab09\exemple\f_9_00.c" // Citeste textul
#define lc 20// Lungimea maxim a unui cuvnt
#define nc 50// Numrul maxim de cuvinte
void main(void) {
char w[(lc+1)*nc]; // Textul, cuvintele sunt separate cu \0
char *p[nc], *t;// Pointerii spre cuvinte
int c,i,j,k,q,z;
z=0; // Numrul de permutri
78

i=0; // Numrul de cuvinte


j=0; // Indice pentru cuvinte
clrscr();
i=ct(w,p); // Citete textul
clrscr();
printf("Sortare prin metoda bulelor\n\n");
printf("Textul introdus:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
if( i ) { q=1;
while( q ) { q=0;
for(k=0; k<i-1; k++) {
if( stricmp( p[k],p[k+1] ) > 0 ) {
t=p[k]; p[k]=p[k+1];
p[k+1]=t; q=1; ++z; } } }
printf("\nTextul sortat:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]); }
printf("\n\n%d permutri",z);
getch(); }
9.3.
(
f_9_00.c

9.0) ,
Shell.
.
// Exemplul 9_3. Metoda Shell
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "d:\borlandc\bin\euue\lab09\exemple\f_9_00.c" // Citeste textul
#define lc 20
// Lungimea maxim a unui cuvnt
#define nc 50
// Numrul maxim de cuvinte
int shellsort(char * p[],int n) {
int i,j,z=0,inc; char *t;
for(inc=n/2; inc>0; inc=inc/2) {
for(i=inc; i<n; i++) {
for(j=i-inc; j>=0&&stricmp( p[j],p[j+inc] ) > 0; j=j-inc) {
t=p[j]; p[j]=p[j+inc];
p[j+inc]=t; ++z; } } }
return z; }
void main(void) {
char w[(lc+1)*nc];
// Textul, cuvintele sunt separate cu \0
char *p[nc];
// Pointerii spre cuvinte
char *t;
int c,i,j,k,q,z;
clrscr();
i=ct(w,p);
// Citete textul
clrscr();
printf("Sortare prin metoda Shell\n\n");
79

printf("Textul introdus:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
z=shellsort(p,i);
// Sorteaz pointerii
printf("\nTextul sortat:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
printf("\n%d permutri",z);
getch(); }
9.4.
(
f_9_00.c

9.0) ,
quicksort.
.
// Exemplul 9_4. Sortare rapid I (quicksort)
// Elementul separator este elementul din mijlocul tabloului
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "d:\borlandc\bin\euue\lab09\exemple\f_9_00.c" // Citeste textul
#define lc 20
// Lungimea maxim a unui cuvnt
#define nc 50
int z=0;
// Numrul maxim de cuvinte
int quicksort(char * p[],int inf, int sup) {
int i,j; char *x, *t;
i=inf; j=sup; x=p[(i+j)/2];
do {
while( i<sup&&stricmp(p[i],x)<0 ) i++;
while( j>inf&&stricmp(p[j],x)>0 ) j--;
if( i<=j )
{ if( i<j )
{ t=p[i]; p[i]=p[j]; p[j]=t; ++z; }
i++; j--; }
}while( i<=j );
if( inf<j ) quicksort(p,inf,j);
if( i<sup ) quicksort(p,i,sup);
return z; }
void main(void) {
char w[(lc+1)*nc];
// Textul, cuvintele sunt separate cu \0
char *p[nc];
// Pointerii spre cuvinte
int c,i,j;
clrscr();
i=ct(w,p);
// Citete textul
clrscr();
printf("Sortare prin metoda rapid I\n\n");
printf("Textul introdus:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
z=quicksort(p,0,i-1);
// Sorteaza pointerii
80

printf("\nTextul sortat:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
printf("\n%d permutri",z);
getch(); }
9.5.
(
f_9_00.c

9.0) ,
quicksortinf.
.
// Exemplul 9_5. Sortare rapid II (quicksortinf)
// Elementul separator este primul element al tabloului
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "d:\borlandc\bin\euue\lab09\exemple\f_9_00.c" // Citeste textul
#define lc 20
// Lungimea maxim a unui cuvnt
#define nc 50
int z=0;
// Numrul maxim de cuvinte
int quicksortinf(char * p[],int inf, int sup) {
int i,j; char *x, *t;
i=inf; j=sup; x=p[inf];
while( i<j ) {
while( i<sup&&stricmp(p[i],x)<=0 ) i++;
while( stricmp(p[j],x)>0 ) j--;
if( i<j )
{ t=p[i]; p[i]=p[j]; p[j]=t; ++z; } }
if( j>inf )
{ p[inf]=p[j]; p[j]=x; ++z; }
if( inf<j-1 ) quicksortinf(p,inf,j-1);
if( j+1<sup ) quicksortinf(p,j+1,sup);
return z; }
void main(void) {
char w[(lc+1)*nc];
// Textul, cuvintele sunt separate cu \0
char *p[nc];
// Pointerii spre cuvinte
int c,i,j;
clrscr();
i=ct(w,p);
// Citete textul
clrscr();
printf("Sortare prin metoda rapid II\n\n");
printf("Textul introdus:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
z=quicksortinf(p,0,i-1);
// Sorteaz pointerii
printf("\nTextul sortat:\n\n");
for(j=0; j<i; j++)
printf("%s\n",p[j]);
printf("\n%d permutri",z);
getch(); }
81

:
.
.
9.0. 9.1-9.5.
9.1. , , ,
,
. ,

. ,
, 1
.

.

.
9.2.
9.1.

.
9.3.

Shell.
.
9.4.

I.


.
9.5.

II.


.
9.6.


: , Shell,
I, II.
.
.
9.7. quicksort 9.4
, ,
. ( a, b, c, a<b<c,
b).
82

9.8. quicksortinf
9.5 , .
:
9.1.
?
9.2. .
9.3. Shell .
9.4. I .
9.5. II .
9.6. ?
9.7. 9.1-9.5?
9.8. 9.1-9.5?
9.9. , Shell
2?
9.10. , ,
,

?
9.11. I
II, ?
9.4 9.5 , .
9.12. _09
.

83


( printf() ),

, ( scanf() ),

.
: %[flags][width][.prec][dim]type
.

flag

width
prec

dim

F
N
h ( d,i,o,u,x,X )
l ( d,i,o,u,x,X)
l ( f,e,E,g,G )
L ( f,e,E,g,G )
d
i
o
u
x
X
f

type
e
g
E
G
c
s
p


.

0, .
( + - ) .
( + )
.
0 0x
0X .
g, G, f, e, E
.
.
-
; ;
- .
far.
near.
short int.
long int.
double.
long double.
signed int.
d.
.
unsigned unt.

.
x .
[-]m.n, m
, n .
[-]m.ne[+/-]d.
e .
f e ( )
e, E .
f E.
( char ).
.
.

84

B I B L I O G R A F I E
1. Dr. Kris Jamsa, Lars Klander. Totul despre C i C++. Manual fundamental
de programare n C i C++. Bucureti: Teora, 2000.
2. Herbert Shildt. C manual complet. Editura Teora, Bucureti, 1998.
3. Liviu Negrescu. Limbajele C i C++ pentru nceptori. V. 1. Editura
Microinformatica, Cluj-Napoca, 1995.
4. Vasile Petrovici, Florin Goicea. Programarea n limbajul C. Editura Tehnic,
Bucureti, 1993.
5. tefan BERZAN, Rodica BERZAN. Programarea calculatoarelor. Teste,
probleme, coduri C pentru lucrrile practice i de laborator. Editura ASEM,
Chiinu, 2003.
6. . . :
, 2002.
7. tefan BERZAN, Rodica BERZAN. . T, ,
. Editura ASEM, Chiinu,
2003.
8. .. /++ . .: -, 2001.
9. . ++.
10. Arnush Craig. Borland C++. Moscova, 1997.
11. http://www.brainbench.com
12. http://www.cplus.about.com
13. http://www.programare.ro
14. http://www.specialist.ru
15. http://www.ipg.h1.ru
16. http://www.citforum.ru

85