You are on page 1of 116

/ ,

&


( C )

. . ,

, 2005

H.

-1-

/ ,

H.

-2-

/ ,


..................................................................................................3
1. ................................................................................................................5
1.1 .............................................................................................6
1.2 ...........................................................................................................8
2. - ..........................................................................9
2.1 ..........................................................................9
2.2 .........................................................................................................10
2.3 ..............................................................................13
2.4 ...........................................................................15
3. C..................................................................................................17
3.1 C .......................................................................................17
3.2 ......................................................................17
3.3 .......................................................................................18
3.4 ............................................................................................................20
4. , , & ...........................22
4.1 ...................................................................................................22
4.2 ...............................................................................................22
4.3 T .........................................................................................23
4.4 ........................................................................................24
4.5 ...............................................................................24
4.6 C .................................................................................24
4.7 ................................................................................................................25
4.8 .............................................................................................................25
4.9 .............................................................26
4.10 ...............................................................................26
4.11 ..........................................................................27
4.12 printf( ) ......................................................................................27
4.13 scanf( ).......................................................................................29
4.14 ...........................................................................................................31
4.15 ..........................................................................................................39
5. & .....................................................42
5.1 ............................................................................................42
5.2 ................................................................................................43
5.3 ........................................................44
5.4 ....................................................................................................46
5.5 .......................................................................................46
5.5 ...................................................................................49
5.6 ................................................................................58
5.7 (switch break) ..............................................................60
5.8 ............................................................................................................64
6. .................................................................................69
6.1 while.............................................................................69
6.2 do while.....................................................................71
6.3 for.................................................................................78
6.4 O ............................................................................................84
6.5 break...................................................................................................85
6.6 continue..............................................................................................86
H.

-3-

/ ,

6.7 char ()...........................................................87
4.8 ..................................................................................................89
6.9 B A .................................................91
6.10 ..........................................................................................................94
7. ................................................................................................................97
7.1 ....................................................................................97
7.2 ......................................................................................103
7.3 ...................................................................................106
7.4 .............................................................107
7.5 ..........................................................................................................109
.............................................................................................................115

H.

-4-

/ ,

1.
, 17
9m x 17m , .
ENIAC (Electronic Numerator Integrator Analyser and Computer)
1946. ,
17.000


. , ENIAC
.

, 1971 Intel
(computer chip) 12 12 ENIAC,
:
.
, 60
. .
, ,
.
. (Internet)
.
.
.
,
, / . ,

.



.
,

.

.

.
:
, ,
DNA, DNA,
,
CD/DVD,
,
,

H.

-5-

/ ,

(),
,
,
, .


,


.

1.1

. () ,
, .

2000 .. : .

.
.

( ),
Napier 1600 .. 1642 Pascal
(1623-1662)
. ,

Leibnitz
1673,
.
19 , Charles Babbage (1792-1871)
1822
. ,
.
,
.
100
,
Hollerich ( IBM ).
, .
, ,
.
,
,
. Howard Aiken
1944 Mark I. Mark I 0,3sec , 4sec
10sec . , Eckert
H.

-6-

/ ,

Mauchly ENIAC (Electronic Numerical Integrator And
Calculator).
, ..., 100
Mark I .

.
.
ENIAC .
( 1955) (transistors)
100sec .
( 1965) .
1sec.
.

.
1970
. .

. ,
,
.
,
. 1950,
.
, ,
, .
, (Internet)
.
;
;
.
.

.
.
,
.
. ' ,
.

.

H.

-7-

/ ,

1.2


laptop
, ,
.


, ( )
.
( nanometer
40.000 ).

, , ,
..

H.

-8-

/ ,

2. -
2.1
/

. , ()
.
/
:
; ,
(
) .

. ,
,
.
. /
:
1

.
. ,
(. )
.

.
.

. .


.\

.

.

.
,
/ .

/.
( )
/. ,
-:

H.

-9-

/ ,

5.1

5.2
5.3

5.4

( flowchart block diagram).


/.

/. (

top-down bottom-up ).
.
, (
/) .
.
/.
, ,
/. (source
program).
. /
.

.

2.2
/
(
) ( /).
/ ,
. /
.
, .. ,
, .
.
,
:
1

, .
.
2 , .
.
3 , .
.
4 , .
.
5 .
,
, /.

H.

-10-

/ ,


.
/
,
:
: (
), ,
XA-B
:
,
>5
:
,

:
(.. ).
:
(..
).
:
, ,

,
.
.

/.
,

.
.

H.

-11-

/ ,

1. .
., ;
.
:
1. , , ,
2. X A /
3. Y / B
4. X > Y .
5. X < Y
.
6. X = Y .
2. / 3 A, B, C .
;
:
3. A, B, C
4. A > B
a. MAX A
b. MAX B
5. C > MAX
a. MAX C
6. MAX
7.
MAX .. MAX A.
, MAX A .
MAX A.
, .. MAX = A, .
A MAX.
3. 5 .
4 5
6.
.
:
4. , A, B, C, D, E
5. P 0 ( )
6. MO 0 ( )
7. A > 5 P P +1
8. B > 5 P P +1
9. C > 5 P P +1
10. D > 5 P P +1
11. E > 5 P P +1
12. MO (A+B+C+D+E) / 5
13. (P > 3 MO > 5)

H.

-12-

/ ,

a.
b.
14.

2.3
/
.
, ..
, ..
. ,

(). / - / -
.
.
.
.
/ .
:

.
/ ,
(machine language). , /
( ).
/
( PRINT ASCII
, 10100001010010100100110011101010100).

(.. )
.

- - assembly.
/
. /
assembler.

.
. ,
/ (
).
.
- -
.
.
,
H.

-13-

/ ,

* - + / ( ) .. /
(source program). ,
/
(compiler interpreter) .
(object program).
(Loader)
(executable program) /.
,
- -
FORTRAN, COBOL, ALGOL, BASIC, PASCAL, C, ADA, C++
.

FORTRAN ( FORmula TRANslation = )


1955 IBM
.
. 1955 FORTRAN
FORTRAN IV, V, 66
FORTRAN 77, FORTRAN 88 , Parallel FORTRAN ..
COBOL ( Cmmon Business Oriented Language =
)
50 .

( ).
.
ALGOL ( ALGOrithmic Language = ),
1958
ALGOL-68.
.
FORTRAN .
ALGOL,
.
ALGOL
Pascal C .
BASIC 1964 Dartmouth College
J. Kemeny T. Kurtz.
, Beginner's All purpose Symbolic
Instruction Code.

.
. ,
FORTRAN.
PASCAL Blaise Pascal (1623 1662). N. Wirth 1971
ALGOL.
.

H.

-14-

/ ,

C, , ( D.
Ritcie ( 1972), ALGOL like ( ALGOL ) , Pascal .
.
, 80% software C.
UNIX.
ADA ( Augusta Ada Byron (1815 - 1851) C.
Babbage ) .
1979 . ,
. ,
,
.

.
C++ C
(object oriented) .
Java, ,
.
.

. ,
.. Prolog
.
2.4
:
compiler,
o interpreter.

. compilers

.
. , interpreters
,
/
,
.

. interpreters

.
H.

-15-

/ ,

H.

-16-

/ ,

3. C
3.1 C
C .
( ),
. C
.
:
C UNIX
(
SOLARIS, LINUX ).
C . C
( )
.
C .
.


.
, C .

.
.
(program reusability)
C.
3.2
/
/.
.
.
/ /
.
. / !
/
:
1
2
3
4
5

,
,
( C),
,
.

H.

-17-

/ ,

6


, :
6.1
. .


Microsoft Word .
text pad Microsoft Windows text
editor .
( source code)
6.2

.
.
(executable code). .
.


.

1.

1.

3.3
( C)
/ , C, !.

( ,
).
.
1. #include <stdio.h>
2. int main() {

H.

-18-

/ ,

3.
4.
5. }

printf(" !\n");
return 0;



() .c , Hello.c

cc Hello.c
gcc Hello.c
(compiler). (
UNIX.)
,
a.out. ()

a.out
/ :
!


( ).
C :
#include < .h>
stdio.h (STandard Input Output)
/ .
(.. )
(..
). .h
(header file).
main( )
.
main
( ). int
main( ) (

).

H.

-19-

/ ,

{ } .

.
printf( ) ( )
. stdio.h.

( \n).
n printf( )
.
return .
(
main int ).
:
3
.
#include <stdio.h>
int main()
{
printf("\n \n \n!\n");
return 0;
}
(
:
#include <stdio.h>
int main()
{
printf("\n ");
printf("\n "):
printf(\n!\n");
return 0 ;
}

3.4
1.
C.
2.
:

H.

-20-

/ ,

,
3. :
********************
*
*
*
*
********************
4.
:
1.
2.
3.
4.
.
5. :



6. :

-----------1
2
1
2
1
2

H.

-------------25
19
10
35
23
42

-21-

/ ,

4. , , &

.
, ,
C.

4.1
C (identifiers)
, ,
. C
.

, .
C . ,
grade GRADE .
C,
C.
.

counter
temp12
long_term

3counter
exp!new
longterm

4.2
C .
C

. C, : ,
, .

char, int, float double.
char ASCII
, , . int
.
float double

. float double
H.

-22-

/ ,

( )
.
C.

char
int
float
double

bit
8
16
32
64

0 255
-32768 32767
3.4 -38 3.4 + 38
1.7 308 1.7 + 308

4.3 T

(modifiers).
.
:
signed
unsigned
long
short

signed, unsigned, long short


char int.
long double. ,
signed ,
.

.

char
unsigned char
signed char
int
unsigned int
signed int
short int
unsigned short int
signed short int
long int
signed long int
unsigned long int
float
double
long double

H.

bit
8
8
8
16
16
16
16
16
16
32
32
32
32
64
64

-128 127
0 255
-128 127
-32768 32767
0 65535
-32768 32767
-32768 32767
0 65535
-32768 32767
-2147483648 2147483649
-2147483648 2147483649
0 4294967296
3.4 -38 3.4 +38
1.7 308 1.7 + 308
3.4 4932 1.1 + 4932

-23-

/ ,

4.4
:
_;
C,
_
.
:
#include <stdio.h>
int main()
{
int a, b, c;
float balance;
return 0;
}

4.5
C,
. :
_ = ;

.
:
#include <stdio.h>
int main()
{
int i = 0;
return 0;
}

4.6 C
, C
.
(keyword) , ,

.

H.

-24-

/ ,

- ANSI .
auto
break
case
char
const
continue
default
do

double
else
enum
extern
float
for
goto
if

int
long
register
return
short
signed
sizeof
static

struct
switch
typedef
union
unsigned
void
volatile
while

. C
, while , WHILE
.
.

4.7
C .
.
/* */.
/*

*/

//

4.8
C,
.
. .
(a, , ).
(5, 10, 50).

(3.14, 9.81).
.

char
int
long int
H.

a \t
7
2 234
2456
32000
-43

-453

-25-

/ ,

short int
unsigned int
float
double

7 -10
9000
132.34
132.34

80
879
30000
3.44e -3
132456
-0.96758

:
#include <stdio.h>
#define DEFAULT_I 29
int main()
{
char c_val;
float f_val;
int i_val1, i_val2;
i_val1 = DEFAULT_I;
i_val2 = 3333;
c_val = Q;
f_val = 23.5 * 2.5;
printf( %c %d %d %f\n, c_val, i_val1, i_val2, f_val);
return 0;
}

4.9

8 16 10.
8
0 7. , 10 8 .
16 .
0 9 A F,
10 15, . 10
16 .
, C
, .
0x.
.
int hex = 0xFF;
into ct = 011;

// 255
// 9

4.10
, C
: .
. ,
.
. ,

H.

-26-

/ ,

.
.

4.11

, , ,
. C
,

\b
\f
\n
\r
\t
\
\
\?
\\
\v
\000
\xhhh

:
#include <stdio.h>
int main()
{
printf( \n);
printf(%d \t %d,2,3);
return 0;
}

4.12 printf( )

printf( ). printf( ) :
printf( , )
To ,
printf( ) ,
.

H.

-27-

/ ,

%c
%d
%i
%e
%f
%g
%o
%s
%u
%x
%%
%p




, e
,
%e %f


(a f)
%

printf( ) stdio.h.
, ,
% .
,
. , printf( )
printf(%d %c %s,5,a,);
5 .

, ,
. %
.

.
, printf( ),
.
.
, 0
. , %05d

.

,

. %10.4f
10 .
,
.
%5.7s
7 .
,
.

H.

-28-

/ ,

:
,
.
%.
-%10.2f
.
printf( )
long short.
d, i, o, u, x. O l
printf( ) long. , %ld
long int. l
e, f g
double. O h printf( ) short int.
%hu short unsigned int.
.
printf( )
(%-5.2f,123.234)
(%5.2.f,3.234)
(%10s,hello)
(%-10s,hello)
(%5.7s,123456789)

123.23
3.23
hello
hello
1234567

4.13 scanf( )
H scanf( ).
,
.
scanf( )
scanf( , );
scanf( ) stdio.h.
:
y
y
y -
%,
scanf( )
buffer .
, , . ,
scanf( )
, , (
) , .

H.

-29-

/ ,

-
scanf( ) .
, %d,%d scanf( )
, ,
.
, scanf( ) .
scanf( )
.
.
, count,
scanf( )
scanf(%d,&count);
H scanf( ) ,
.
address
scanf(%s,address);
, address
&.

, , .
scanf(%d%d,&a,&b);
10 20, 10,20.
printf( ), scanf( )
.
(*) % ,
,
.
scanf(%d%*c%d,&x,&y);
10/20, x 10,
, y 20.
.
%

. ,
20 str,
scanf(%20s,str);

H.

-30-

/ ,

buffer 20 ,
. ,

, scanf( ) str
20 , Y,
. , .
scanf( )
scanf(%s,str);
str.

. scanf( )
.
, ,
, ,
. , x y,
scanf(%c%c%c,&a,&b,&c);
x a, b,
y c.

,
, ,
,
buffer .
, 10t20
scanf(%st%s,&x,&y);
10 x 20 y. O
t t .
scanf(%s ,name);
,
. ,
%s scanf( ) ,
.

4.14
O (operator)
. C
: (arithmetic),
H.

-31-

/ ,

(relational) (logical) bit (bitwise).
,
sizeof.

:

+
*
/
%
-++


C.

. 10/3 3
.
% .
float double.
:
#include <stdio.h>
int main()
{
int x, y;
x = 10;
y = 3;
printf(%d\n, x/y);
// 3
printf(%d\n, x % y);
// 1
x = 1;
y = 2;
printf(%d %d\n, x /y, x % y); // 0 1
return 0;
}
H printf( ) 0 1 0
1.
-1.
, .
E T
H.

-32-

/ ,

C =. O
.
:
#include <stdio.h>
int main()
{
int a, b, sum;
printf( : );
scanf(%d %d,&a,&b);
sum = a + b;
printf( %d \n,sum);
return 0;
}
sizeof
H C sizeof
. sizeof
. sizeof ,
.
sizeof(int)
:
#include <stdio.h>
int main()
{
printf( int %d bytes\n,sizeof(int));
return 0;
}

C . ++
--. 1
, 1 .
:
x++;
x--;

x = x + 1;
x = x 1;

H.

-33-

/ ,


. ,

x = x + 1;

x++;

++x;
, .
,
, C
. C
1.
:
x = 10;
y = ++x;
H C y 11 x
y.
x = 10;
y = x++;
C y 10 1
x. x 11,
.
:

++ -* / %
+ -

,
. ,
. C
,
.


i=i+4

,
i += 4

H.

-34-

/ ,

O += (assignment operator).
: -=, *=, /=, %=.
_1 _2 ,
_1 = _2

_1 = (_1) (_2)

x *= y + 2

x = x * (y + 2)

x=x*y+2
:
#include <stdio.h>
int main()
{
int a,b;
int temp;
printf( : );
scanf(%d,&a);
temp = a;
printf( : );
scanf(%d,&b);
a += b;
printf(%d\n,a);
a = temp;
a -= b;
printf(%d\n,a);
a = temp;
a *= b;
printf(%d\n,a);
a = temp;
a /= b;
printf(%d\n,a);
a = temp;
a %= b;
printf(%d\n,a);
return 0;
}

H.

-35-

/ ,

, , .
C
.
, .

, C
. C

, :
1. char short int int. float
double.
2. , .
long double,
long double. double,
double. long
long.
unsigned unsigned.
,
,
. ,

, ch int float f double.


ch/i double f * d double.
double double.

H.

-36-

/ ,


,
(cast). H
:
()
C. , x

x/2 float ,

(float) x / 2
(float) x, 2 float
float ( x / 2.0
float).
(float) (x / 2)
,
float.
:
#include <stdio.h>
int main()
{
float result;
int x;
printf( : );
scanf(%d,&x);
result = (float) x / 2;
printf(%.2f\n,result);
result = (float) (x / 2);
printf(%.2f\n,result);
return 0;
}

,
, .
:
x = 5/y*(26/x);
x = 5 / y * ( 26 / x )
H
.
,
.
x = y / 2 23 * grade 43;
x = (y / 2) _ ( (23 * grade) 43);
.

H.

-37-

/ ,

2.1
:
) ,
) .
#include <stdio.h>
int main()
{
int a,b;
printf( :);
scanf(%d %d,&a,&b);
printf( %d\n,a * b);
printf( %.2f\n,(float) a / b);
return 0;
}
2.2
:
) h
,
) u
.
u = 2 gh (g = 9.81 m/sec2).
#include <stdio.h>
#include <math.h>
int main()
{
int h;
float u;
printf( h : );
scanf(%d,&h);
u = sqrt(2 * 9.81 * h);
printf( : %.2f\n,u);
return 0;
}
2.3
,
.
1 = 39,3700787 .
#include <stdio.h>
int main()
{
int meters;
float in;
printf( : );
scanf(%d,&meters);
in = meters * 39.3700787;
printf( %.2f\n,in);
return 0;
}
2.4
:
H.

-38-

/ ,

) L ,
) .
L
T = 2
g=9,81 m/sec2
g
#include <stdio.h>
#include <math.h>
int main()
{
int L;
float T;
printf( : );
scanf(%d,&L);
T = 2 * 3.14 * sqrt(L / 9.81);
printf( %.2f\n,T);
return 0;
}
2.5
.
.
1 = 0,9834 $
#include <stdio.h>
int main()
{
int euro;
float dollar;
printf( : );
scanf(%d,&euro);
dollar = euro * 0.9834;
printf( : %.2f\n,dollar);
return 0;
}

4.15
1.

. : a b
= 2 y (a + b) = a y b.
2.

. : R = 2 y y R
= y R2 ( = 3,14).
3. 1 Kbyte = 1024 bytes 1 byte = 8 bits.
:
. Kbytes,
. bytes bits,
. Kbytes, bytes bits .

H.

-39-

/ ,

4. a = 5 m/sec2.
:
. ,
.
1
. = y t s = at 2 .
2
5. :
. r h ,
. V A
. : V = r2h A = 2rh + 2r2 ( = 3,14).
6. Video Club DVD :
25 DVD 1,5 .
, DVD 2 .
:
. DVD
.
.
7. . 1
27 % , 2 33 %
3 40 %.
:
. ,
. .
8. :
. a,b c ,
. .
E = t (t a )(t b)(t c) t
t = (a + b + c) / 2.
9.
.
38 83.
10.
.
385 16.
11.
.
389
9 43,22.
12. :
) .
) 5.
) .
) 4.
) 2.
) .
3.

.

H.

-40-

/ ,

13.
,
.
9
F = C + 32 F
5
C .
14. :
)
, ,
)
.

30% 70%. (
7,5 6,
7,5 * 30 % + 6 * 70%).
15. :
) ,
) ,
.
30%
10% (
).

H.

-41-

/ ,

5. &
5.1
:
( C) ( C).
,
.
C
:

<

>

<=

>=

!=

==

.

#include <stdio.h>
int main()
{
int a = 3;
int b = 5;
if (b > a)
printf( 5 3);
return 0;
}


ASCII.

#include <stdio.h>
int main()
{
char ch1 = A;
char ch2 = B;
if (ch2 > ch1)
printf( );
return 0;
}

= = , != .

H.

-42-

/ ,

# include <stdio.h>
#define FALSE 0
#define TRUE 1
int main()
{
int a = TRUE;
int b = TRUE;
if (a == b)
printf( );
return 0;
}

# include <stdio.h>
int main()
{
enum bool {false,true};
enum bool a = true;
enum bool b = true;
if (a == b)
printf( );
return 0;
}

5.2
(
).
! (), && (), || ().

( )
( ).
. ! ( 3 > 5) 1 ()
(3 > 5) 0 ().


. ,
.
. (3 < 5) && (6 > 4) 1 ()
(3 < 5) 1 () (6 > 4).

H.

-43-

/ ,


.
, .
. (3 > 5) || (4 > 3) 1 ()
(3 > 5) 0 () (4 > 3)
1 ().

!X

X && Y

X || Y

C
0 0.

5.3
! .
(*, /, %, +, -)
.
( >, >=, <, <=) .
(==, !=).
(&&, ||)

-
.

.

!
> >= <
<=
== !=
&& ||


, 10 > 1 + 12
10 > ( 1 + 12 ).

H.

-44-

/ ,

1 && !0 || 1 :
!0 1 ()
1 && 1 1 ()
1 || 1 1 ()
, 1 && ! (0 || 1) :
( 0 || 1) 1 ()
! 1 0 ()
1 && 0 0 ()

, .
, .
o
, :
:
( )
( ).

# include <stdio.h>
#define CLOSE 0
#define OPEN 1
int main()
{
int a = OPEN;
int b = OPEN;
if (a == b)
printf( );
return 0;
}
:
( )
( ).

# include <stdio.h>
#define RED 0
#define GREEN 1
int main()
{
int a = GREEN;
int b = GREEN;
if (a == b)
printf( . !);
return 0;
}

H.

-45-

/ ,

5.4


.


.
:
.

5.5
:
if ( )
{
1
2

( { ) ( } )

.
.

,
.
,
.

.
,
.
3.4.1

.
.
#include <stdio.h>
int main()
{
int num;
printf( : );

H.

-46-

/ ,

scanf(%d,&num);
if (num < 0)
num *= -1;
printf( : %d,num);
return 0;
}
3.4.2

a, b, c. :
. a, b, c,
.
. ,
8 .
#include <stdio.h>
int main()
{
float a, b, c, mo;
printf( : );
scanf(%f %f %f,&a,&b,&c);
mo = (a + b + c)/3;
printf( %.2f\n,mo);
if (mo > 8)
printf(.\n);
return 0;
}
3.4.3

a, b, c. :
. a,b c.
. .
. ,
(0).
.
.
#include <stdio.h>
int main()
{
float a, b, c, mo, max, min;
printf( : );
scanf(%f %f %f,&a,&b,&c);
mo = (a + b + c) / 3;
printf( : %.2f\n,mo);
if (mo < 0)
printf(.\n);
max = a;
if (b > max)
max = b;
if (c > max)

H.

-47-

/ ,

max = c;
min = a;
if (b < min)
min = b;
if (c < min)
min = c;
printf( %.2f\n,max);
printf( %.2f\n,min);
return 0;
}
3.4.4
.
:
. ,
,
. %,
. 30%,
, .
#include <stdio.h>
int main()
{
int m; // m ->
float a,t,p,k;
// a -> , t -> , p -> , k ->
printf(" : ");
scanf("%d",&m);
printf(" : ");
scanf("%f %f",&a,&t);
p = 100 * (t - a) / a;
printf(" = %.2f %\n",p);
if (p > 30)
{
printf(" \n");
k = p * m * a / 100;
printf(" %.2f \n",k);
}
return 0;
}
3.4.5
.

(
.
( 303, 909).
#include <stdio.h>
int main()
{
int num, a, b, c, number;

H.

-48-

/ ,

printf( );
scanf(%d,&num);
c = num / 100;
b = (num % 100) / 10;
a = (num % 100) % 10;
number = a * 100 + b * 10 + c;
if (num == number)
printf( \n);
return 0;
}

5.5
if - else .
,
. :
if ( )
1
else
2

1.
2.

if - else
.
, if ,
, .

#include <stdio.h>
int main()
{
float b,y;
printf ( : );
scanf(%f %f,&b,&y);
if (b < 80)
if (y < 1.80)
printf( .\n);
return 0;
}
if,
.
:

H.

-49-

/ ,

#include <stdio.h>
int main()
{
float b,y;
printf ( : );
scanf(%f %f,&b,&y);
if (b < 80 && y < 1.80)
printf( .\n);
return 0;
}

if - else if - else.
:
if ( 1)
1
else if ( 2)
2

else


,
.

#include <stdio.h>
int main()
{
int x;
printf( : );
scanf(%d,&x);
if (x > 0)
printf( .\n);
else if (x < 0)
printf( .\n);
else
printf( .\n);
return 0;
}
3.5.1
.
.
#include <stdio.h>
int main()
{
int a,b;

H.

-50-

/ ,

printf( : );
scanf(%d %d,&a,&b);
if (a > b)
printf( %d\n,a);
else
printf( %d\n,b);
return 0;
}
,
.
. .

.


ax + b = 0.
:
b
0
x =
a
= 0 0,
= 0,
#include <stdio.h>
int main()
{
float a,b,x;
printf( : );
scanf(%f %f,&a,&b);
if (a != 0)
{
x = -b /a;
printf( %.2f\n,x);
}
else
if (b != 0)
printf( .\n);
else
printf( .\n);
return 0;
}


C (
).
#include <stdio.h>
int main()
{
float a,b,x;
H.

-51-

/ ,

printf( : );
scanf(%f %f,&a,&b);
if (a) // <> 0
{
x = -b /a;
printf( %.2f\n,x);
}
else
if (b) // b <> 0
printf( .\n);
else
printf( .\n);
return 0;
}
3.5.3
30000 . :
. ,
. ,
. ,
.
#include <stdio.h>
#define POSO 30000
int main()
{
float a,y; // a -> , y ->
printf( : );
scanf(%f,&a);
y = POSO a;
if ( y > 0)
printf( %.2f \n,y);
else
printf( .\n);
return 0;
}
3.5.4

. :
. ,
.
,
. .

29,35
(Km)
1-100
101-1000
1001
#include <stdio.h>
H.

( / Km)
0,13
0,16
0,18

-52-

/ ,

#define PAGIO 29.35


int main()
{
int Km;
//
float x; //
printf( ( > 0) : );
scanf(%d,&Km);
if (Km <= 100)
x = PAGIO + Km * 0.13;
else if (Km <= 1000)
x = PAGIO + Km * 0.16;
else
x = PAGIO + Km * 0.18;
printf( = %.2f \n,x);
return 0;
}
3.5.5

200.
:

30 %

70 %

100 %
:
.
,
. ,
.
,
.
.
#include <stdio.h>
int hl,sf,asf;
float sft,sfp,sfb;
/*
hl ->
sf ->
asf ->
sfb ->
sfp ->
sft ->
*/
int main()
{
printf( : );
scanf(%d,&hl);
printf( : );
scanf(%d,&sf);
asf = 200 hl;
sfb = sf + sf * 30 / 100 ;

H.

-53-

/ ,

sfp = sf + sf * 70 / 100;
sft = sf + sf * 100 / 100;
printf( : %d\n,sf);
printf( : %d\n,asf);
printf( : %.2f\n,sfb);
printf( : %.2f\n,sfp);
printf( : %.2f\n,sft);
if (asf >= sfb)
if (asf >= sfp)
if (asf >= sft)
printf( .\n);
else
printf( .\n);
else
printf( .\n);
else
printf( .\n);
return 0;
}
3.5.6
,
,
() . :
( Kgr )
=
2 (m)
:
) ,
) ,
)
:

< 18.5
18.5 < 25
25 < 30
30

#include <stdio.h>
#include <math.h> // pow
int main()
{
float b,y,dms; // b -> , y -> , dms ->
printf( : );
scanf(%f,&b);
printf( : );
scanf(%f,&y);
dms = b / pow(y,2.0); // -> double pow(double base,double exp)
printf( : %4.2f\n,dms);
if (dms < 18.5)

H.

-54-

/ ,

printf(.\n);
else if (dms < 25)
printf(.\n);
else if (dms < 30)
printf(.\n);
else
printf(.\n);
return 0;
}
3.5.7

ax2 + x + = 0 .
,
,
.
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,x1,x2,D;
printf( , : );
scanf(%f %f %f,&a,&b,&c);
if (a == 0)
printf( .\n);
else {
D = pow(b,2.0) 4 * a * c;
if (D > 0) {
x1 = (-b + sqrt(D)) / (2 * a);
x2 = (-b sqrt(D)) / (2 * a);
printf( %.2f %.2f\n,x1,x2);
}
else if (D == 0) {
x1 = -b / (2 * a);
printf( %.2f\n,x1);
}
else
printf( .\n);
}
return 0;
}

pow( )
sqrt( ).
math.h .
:
double pow(double base,double exp)
pow( ) base exp (baseexp).
double sqrt(double num)

H.

-55-

/ ,

sqrt( ) num.
sqrt( ) ,
.
3.5.8

.
:
. ,
. ,
. .
,
, .

:



0,50
espresso
0,90

0,60

0,40
#include <stdio.h>
int main()
{
int e, p; // e -> , p ->
float y,k; // y -> , k ->
printf(1. - 0,50 \n);
printf(2. espresso 0,90 \n);
printf(3. 0,60 \n);
printf(4. 0,40 \n);
printf( (1-4) : );
scanf(%d,&e);
printf( : );
scanf(%d,&p);
printf( : );
scanf(%f,&y);
if (e == 1)
k = p * 0.50;
else if (e == 2)
k = p * 0.90;
else if (e == 3)
k = p * 0.60;
else
k = p * 0.40;
printf( : %.2f\n,k);
if (k > y)
printf( .\n);
else {
y = y k;
printf( . %.2f\n,y);
}
return 0;
H.

-56-

/ ,

}
3.5.9

, .
4. ,
400. 100 (
1700, 1800, 2000 ..).
#include <stdio.h>
int main()
{
enum bool {false,true};
enum bool disekto;
int etos;
printf( : );
scanf(%d,&etos);
if (etos % 100 == 0)
if (etos % 400 == 0)
disekto = true;
else
disekto = false;
else
if (etos % 4 == 0)
disekto = true;
else
disekto = false;
if (disekto == true)
printf( %d .\n,etos);
else
printf( %d .\n,etos);
return 0;
}
3.5.10

:
1 : 40% 12
1/18 .
2 : 50% 24
1/40 .
:
.
. 1 2 ,
(
).
#include <stdio.h>
int main()
{
float a, prog1, prog2; //a ->
printf( : );

H.

-57-

/ ,

scanf(%f,&a);
prog1 = a * 40 / 100 + 12 * a / 18;
prog2 = a * 50 / 100 + 24 * a / 40;
if (prog1 < prog2)
printf(1 .\n);
else
printf(2 .\n);
return 0;
}

5.6
if - else
. :
? 1 : 2

( )
1. (),
2.

, .
if - else ,
.
.

#include <stdio.h>
int main()
{
int x, y;
printf( : );
scanf(%d,&y);
x = (y > 0) ? y : -y;
printf( %d %d\n,y,x);
return 0;
}

y.
(y > 0) ? y : -y. y
x = y, x = -y.
if
else.
if (y > 0)
x = y;
else
H.

-58-

/ ,

x = -y;

, ? ,
.
, .

#include <stdio.h>
int main()
{
int max, a, b;
printf( : );
scanf(%d %d,&a,&b);
max = (a > b) ? a : b;
printf( %d %d %d\n,a,b,max);
return 0;
}

a
b. (a > b) ? a : b.
b max = a, max = b.
if
else.
if (a > b)
max = a;
else
max = b;
3.6.1
:
. ,
. , ,
.
#include <stdio.h>
int main()
{
int a, b, result;
printf( : );
scanf(%d %d,&a,&b);
result = (a % 2 == 0 && b % 2 != 0) ? a + b : a b;
printf( %d\n,result);
return 0;
}
3.6.2
:
. a b,

H.

-59-

/ ,

. ,
.
.
#include <stdio.h>
int main()
{
int a, b, result;
printf( : );
scanf(%d %d,&a,&b);
result = (a > b) ? a b : b a;
printf( %s.\n,(result % 2 == 0) ? :);
return 0;
}

5.7 (switch break)


if - else
.
,
if - else if - else,
switch. :
switch ()
{
case 1

case 2

case

default

1;
break;
2;
break;
_ ;
break;
default;
break;

.
( 1, 2, ,
), .
,
default.
break switch
switch. break,
( 1, , )
switch.

H.

-60-

/ ,

.
( )
.

#include <stdio.h>
int main()
{
int num;
printf( : );
scanf(%d,&num);
switch (num % 2)
{
case 0 :printf(" .\n");
break;
case 1 :printf(" .\n");
break;
}
return 0;
}
.

case :

#include <stdio.h>
int main()
{
char ch;
printf( a. \n);
printf( . \n);
printf( . \n);
printf( ( ) : );
scanf(%c,&ch);
switch (ch)
{
case :
case :
printf( .\n);
break;
case :
case :
printf( .\n);
break;
case :
case :
printf( .\n);
break;
default :
printf( .\n);
break;

H.

-61-

/ ,

}
}
.

.
.
,

default. default
, .
Switch if - else ;

switch if - else ;
. switch
float.
switch
. , :
if (grade >= 0 && grade <= 100)
switch,
0 100.
switch
.
3.7.1

. :

: 5

: 5 6
: 7 8

: 9 10

.

#include <stdio.h>
int main()
{
int bathmos;
printf( : );
scanf(%d,&bathmos);
switch (bathmos)
{
case 5 :
case 6 :
printf(.\n);
break;
case 7 :
case 8 :printf( .\n);
break;
H.

-62-

/ ,

case 9 :
case 10 :
printf(.\n);
break;
default :
printf(.\n);
break;
}
return 0;
}
3.7.2
:
. 0 6,
. (0 , 1 ,
, 6 ).
#include <stdio.h>
int main()
{
int choise;
printf( (0 6) : );
scanf(%d,&choise);
switch (choise)
{
case 0 : printf(\n);
break;
case 1 : printf(\n);
break;
case 2 : printf(\n);
break;
case3
: printf(\n);
break;
case4
: printf(\n);
break;
case5
: printf(\n);
break;
case6
: printf(\n);
break;
}
return 0;
}
3.7.3

.
:
. ,
1 8,
. .


1

0,9972
2

0,6499

H.

-63-

/ ,

3
4
5
6
7
8

1,4865
1,1710
7,4846
1,5545
9,3741
0,5819

#include <stdio.h>
int main()
{
int ep;
float poso, iso;
printf( : );
scanf(%f,&poso);
printf( (1 8) : );
scanf(%d,&ep);
switch (ep)
{
case 1 : iso = poso / 0.9972;
break;
case 2 : iso = poso / 0.6499;
break;
case 3 : iso = poso / 1.4865;
break;
case 4 : iso = poso / 1.1710;
break;
case 5 : iso = poso / 7.4846;
break;
case 6 : iso = poso / 1.5545;
break;
case 7 : iso = poso / 9.3741;
break;
case 8 : iso = poso / 0.5819;
break;
}
printf( : %.2f\n,iso);
return 0;
}

5.8
1. 12
, b, c.
:
. a, b, c.
. .
. 37
.

H.

-64-

/ ,

2. .

:
. , 20 C,
. , 25 C.
3. :
. R1 R2
, :
1
2
. R.
:
: R = R1 + R2
RR
: R = 1 2
R1 + R2
4. 700 , 1
500 Kg 0,6
500 Kg. :
. Kg
. .
5. ,
(
< 90 , > 90 = 90 ).
6. :
. HDL mg/dl,
. HDL :

HDL

< 200

200 239

240 299
> 300
7.


1 100
300
101 - 200
280
201 - 300
260
301
240
:
. ,
. , ()
( + ).
18%.
8. :
5000 0 %
5001 10000 5
10001 20000 15 %
20000 25 %

H.

-65-

/ ,


.
9. :


100
0%
100 200
5%
200 400
10 %
400
20 %
:
.
,
.
.
! .
10. 5 .
:
. ,
. ,
, 40 ,
5 .
11. 800 .
10000
. 10000 20000 5 %
. 20000 8 %.
:
.
,
. , ,
,
. :
:

:
10% .
12.
:
20

0,03

0 200
0,04

201 400
0,05
( )
401
0,06
( ) : 18 %
:
. ,
. :

:

:
:
H.

-66-

/ ,

:
13. ()

.
,
1 3 . :
. ,
. .
14. on-line.
.
:
/ %
0 50
0
51 100
5
101 150
10
151
15
:
. ,
. .
1020 .
15. :
1,5 ,
1 .
:
. ,
. .
16.
:
. ,
. ( 20
2 , 3 ),
. (
0,5 ),
. .
20 15 .
, . 8 %.
17. 3 ,
2000 2,7 , 2000
. 1,5 ,
4000
.
:
.
,
. ,
.

H.

-67-

/ ,

18. 10
, .
20 . :
. ,
.
. : .
19.
:

%
8.000
0
0
15.000
6
10
12
15
24
20
15.000
0
0
20.000
6
8
12
12
24
18

0
0
20.000
6
6
12
10
24
15
:
. ,
. .
20.
5 .
:

60
50
45
, :
.
,
. .
. . 4.

H.

-68-

/ ,

6.

.
C while, do - while for.

6.1 while
while :
while () ;

, ,
. .

( 0).
( = 0),
.

#include <stdio.h>
int main()
{
int i = 1,sum = 0;
while (i != 0) {
printf(" (0 ) : ");
scanf("%d",&i);
sum += i;
}
printf(" %d.\n",sum);
return 0;
}


. (
) 0.
while ,

.
i.

#include <stdio.h>
int main()
{

H.

-69-

/ ,

int i=1,n,average,sum = 0;
printf(" : ");
scanf("%d",&n);
while (i <= n) {
sum += i;
i++;
}
average = sum / n;
printf(" %d \n",average);
return 0;
}
.

#include <stdio.h>
int main()
{
int grade,max = 0;
printf(" ( ) : ");
scanf("%d",&grade);
while (grade > 0) {
if (grade > max) max = grade;
printf(" ( ) : ");
scanf("%d",&grade);
}
printf(" %d\n",max);
return 0;
}

(
, )
.

#include <stdio.h>
int main()
{
int i=1,j,col,row;
printf(" : ");
scanf("%d %d",&row,&col);
while (i <= row) {
j = 1;
while (j <= col) {
//
if (i == 1 || i == row) printf("%c",'*');
else
//
if (j == 1 || j == col) printf("%c",'*');
else printf("%c",' ');

H.

-70-

/ ,

j++;
}
printf("\n");
i++;
}
return 0;
}
(
),
*.

1.
, while ,
, ,
. while
, , .
.

#include <stdio.h>
int main()
{
int i = 0;
while (i++ < 5) ;
printf(%d\n,i);
return 0 ;
}

,
.
while, printf( )
. i
.

6.2 do while
while , do
while .
do while .
do while :
do {

;
} while ();
,
.
H.

-71-

/ ,

do while
( ).
.
,
.
.

#include <stdio.h>
int main()
{
int choise;
do {
printf("1. \n");
printf("2. \n");
printf("3. \n");
printf("4. \n");
printf(" (1 - 4) : ");
scanf("%d",&choise);
} while (choise < 1 || choise > 4);
printf(" %d\n",choise);
return 0;
}
4.2.1

a > 0.
#include <stdio.h>
int main()
{
int a;
do {
printf( > 0 : );
scanf(%d,&a);
} while (a <= 0);
return 0;
}

do while.

while.
#include <stdio.h>
int main()
{
int a;
printf( > 0 : );
scanf(%d,&a);
while (a <= 0)
{

H.

-72-

/ ,

printf( > 0 : );
scanf(%d,&a);
}
return 0;
}
4.2.2

( 1 10).
#include <stdio.h>
int main()
{
int grade;
do {
printf( :);
scanf(%d,&grade);
} while (grade < 1 || grade > 10);
return 0;
}

do while.

while.
#include <stdio.h>
int main()
{
int grade;
printf( :);
scanf(%d,&grade);
while (grade < 1 || grade > 10)
{
printf( :);
scanf(%d,&grade);
}
return 0;
}
4.2.3

n n
S = 12 + 22 + 32 + + n2.
#include <stdio.h>
#include <math.h>
int main()
{
int n, i = 1;
long s = 0;
do {
printf( n ( 1) : );

H.

-73-

/ ,

scanf(%d,&n);
} while (n < 1);
while (i <= n)
{
s += pow(i,2);
i++;
}
printf( %ld\n,s);
return 0;
}

n
S = 2 + 4 + 6 + 8 + + n.
#include <stdio.h>
int main()
{
int i = 2, n;
long s = 0;
do {
printf( n ( 2) : );
scanf(%d,&n);
} while (n < 2);
while (i <= n) {
s += i;
i += 2;
}
printf( %ld\n,s);
return 0;
}
4.2.5

1 2 3 4
99
S = + + + + ... +
2 3 4 5
100
#include <stdio.h>
int main()
{
int i = 1;
float s = 0.0;
while (i <= 99) {
s += (float) i / (i + 1);
i++;
}
printf(" %.2f\n",s);
return 0;
}
4.2.6

H.

-74-

/ ,



1 2 3 4
N
S = + + + + ... +
4 6 8 10
K
.

i = 1, 2, 3, 4, . . . , N
2 * i + 2.
#include <stdio.h>
int main()
{
int i = 1, n;
float s = 0.0;
do {
printf( ( 1) : );
scanf(%d,&n);
} while (n < 1);
while (i <= n) {
s += (float) i / ( 2 * i + 2);
i++;
}
printf( %.2f\n,s);
return 0;
}
4.2.7
1.
k,
1+2+3+4+...+k>a
#include <stdio.h>
int main()
{
int a, i = 0, s = 0;

do {
printf( 1 : );
scanf(%d,&a);
} while (a <= 1);
while (s <= a)
s += ++i;
printf( %d\n,i);
return 0;
}
4.2.8

1 2 3 4 5 4 3 2 1
while.

H.

-75-

/ ,

9.
i 1, 2, 3, 4, 5, 6, 7, 8, 9.
6 4, 7 3, . . . ,
9 1. i > 5
: = 10 i.
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 9) {
if (i <= 5)
printf(%3d,i);
else
printf(%3d,10 i);
i++;
}
return 0;
}
4.2.9

,
. 0,
.
#include <stdio.h>
int main()
{
int a, pl = 0, sum = 0, mo;
// a -> , pl -> , sum -> , mo ->
do {
printf( : );
scanf(%d,&a);
} while (a <= 0);
while (a != 0) {
pl++;
sum += a;
do {
printf( (0 ) : );
scanf(%d,&a);
} while (a < 0);
}
mo = sum / pl;
printf( : %d\n,pl);
printf( : %d\n,sum);
printf( : %d\n,mo);
return 0;
}
4.2.10

H.

-76-

/ ,

> 1
12 + 22 + . . . + 2 .
,
.
#include <stdio.h>
#include <math.h>
int main()
{
int i = 2, sum;
double x;
// T , N > 1.
sum = 1 + pow(2,2);
x = sqrt(sum);
while (floor(x) != x) {
i++;
sum += pow(i,2);
x = sqrt(sum);
}
printf(O %d\n,i);
return 0;
}
pow( )
floor( ).
math.h .
:
double pow(double base,double exp)

pow( ) base exp (baseexp).


double floor(double num)

floor( ) ,
double num.
4.2.11

.
1.
5432,45 ( ->
103).

10 0.
#include <stdio.h>
#include <math.h>
int main()
{
float a;
int x, pl = 0;

H.

-77-

/ ,

printf( : );
scanf(%f,&a);
x = floor(a);
while (x != 0) {
// while (x)
x = x / 10;
pl++;
}
printf( %d\n,pl);
printf( %d\n,pl 1);
return 0;
}

6.3 for
for :
for ( ; ; )

for
.
- ,
,
.


. ( ),
. ,
. for
, . ,
.
.
4.3.1
#include <stdio.h>
#define COUNTER 10
int main()
{
int count;
for (count = 1;count <= COUNTER;count++)
printf(" %d\n",count);
return 0;
}
for
.
for . ,
.
( count COUNTER), .
.

H.

-78-

/ ,

4.3.2
#include <stdio.h>
int main()
{
int number;
printf("%5s %15s\n","n","n ");
for (number = 1;number <= 10;number++)
printf("%5d %15d\n",number,number * number);
return 0;
}

for
1 10 . for
num, num
num .

#include <stdio.h>
int main()
{
int i;
for (i = 1;i <= 1000;i++)
;
printf(" i = %d\n",i);
return 0;
}

10000 ,

printf( ) i 10001
( i ).
for
, .

#include <stdio.h>
int main()
{
int i;
for (i = 10; i > 0; i--)
printf(%d\n,i);
return 0;
}

O ( ) ( )
,
2 100.

H.

-79-

/ ,

#include <stdio.h>
int main()
{
int i, sum = 0;
for (i = 2; i <= 100; i += 2)
sum += i;
printf(To %d\n,sum);
return 0;
}

for .

#include <stdio.h>
int main()
{
char ch;
for (ch = A; ch <= Z; ch++)
printf(H ASCII %c %d\n,ch,ch);
return 0;
}

ASCII A
.
for
.
.

#include <stdio.h>
int main()
{
int i = 0;
float d;
for(d = 1000.0;d < 3100.0; d *= 1.05)
printf("%4d %10.2f\n",++i,d);
return 0 ;
}

( i)
1000 , 5% (d *= 1.05),
3000 .
(
).

.

H.

-80-

/ ,

#include <stdio.h>
int main()
{
int i;
for(i = 0;i < 10; )
printf("%4d %4d\n",i,2 * (++i + 1));
return 0 ;
}

2 y (i+1)
i 1 10.

#include <stdio.h>
int main()
{
for( ; ; )
printf( ;\n);
return 0;
}


.
.
printf( ).
,
.

#include <stdio.h>
int main()
{
int num = 1;
for(printf(" . 0 .\n"); num != 0; )
scanf("%d",&num);
printf(" 0.\n");
return 0;
}

,
, 0, .
4.3.1
.

.
#include <stdio.h>
int main()

H.

-81-

/ ,

{
int i,a,b,c,count = 0;
for (i = 1; i <= 10; i++) {
printf( : );
scanf(%d %d %d,&a,&b,&c);
if (a + b == c || a + c == b || c + b == a)
count++;
}
printf( %d.\n,count);
return 0;
}
4.3.2
.
1, = 0
N!=
1 * 2 * 3 * ... * ( N 1) * N , > 0
#include <stdio.h>
int main()
{
long N = 1;
int n,i;
do {
printf( : );
scanf(%d,&n);
while (n < 0);
for (i = 2; i <= n; i++)
N *= i;
printf( %d %ld.\n,n,N);
return 0;
}
4.3.3
(
) .
.
#include <stdio.h>
int main()
{
int i,n;
do {
printf( : );
scanf(%d,&n);
} while (n <= 0);
for (i = 2; i < n; i++)
if (n % i == 0) break;
printf(" %s\n",(i < n) ? " " : " );
return 0;
}

H.

-82-

/ ,

4.3.4
.
111 999 x < y < z,
x z .
.
#include <stdio.h>
int main()
{
int x,y,z,code;
for (x = 1; x <= 9; x++)
if (x % 2 == 0)
for (y = x + 1; y <= 9; y++)
for (z = y + 1; z <= 9; z++)
if (z % 2 != 0) {
code = 100 * x + 10 * y + z;
printf( = %d\n,code);
}
return 0;
}
4.3.5
2
1000.
.
, 6 , 1,
2, 3, 6 : 1 + 2 + 3 + 6 = 12 = 2 * 6.
#include <stdio.h>
int main()
{
int i,j,sum;
for (i = 2; i <= 1000; i++) {
sum = 1 + i;
for (j = 2; j < i; j++)
if (i % j == 0)
sum += j;
if (sum == 2 * i)
printf(" %d\n",i);
}
return 0;
}
4.3.6
N

1 1 1
1
S = + + + ... +
2 4 6
N
,
1 1 1
1
S = + + + ... +
1 3 5
N
.

H.

-83-

/ ,

#include <stdio.h>
int main()
{
float S = 0.0;
int i, N, start;
do {
printf( : );
scanf(%d,&N);
} while (N <= 0);
if (N % 2 == 0)
start = 2;
else
start = 1;
for (i = start; i <= N; i += 2)
S += (float) 1 / i;
printf( %.2f\n,S);
return 0;
}
4.3.7

,
.
( 371 = 33 + 73 + 13)
#include <stdio.h>
#include <math.h>
int main()
{
int i, m, d, e;
for (i = 100; i <= 999; i++) {
e = i / 100;
d = (i e * 100) / 10;
m = i e * 100 d * 10;
if (i == pow(e,3) + pow(d,3) + pow(m,3))
printf(%5d,i);
}
printf(\n);
return 0;
}

6.4 O

. ,
x = (y = 3, y + 1);
3 y 4 x.
,
H.

-84-

/ ,

. ,
.
,
. ,

y = 10;
x = (y = y 5, 25 / y);
x 5 y, 10, 5,
25 y 5.
for,

for.

#include <stdio.h>
int main()
{
int up, down, target;
printf( ; );
scanf(%d,&target);
for (up = 1, down = target; (up <= target) && (down >= 1); up++, down--)
printf(%5d %5d\n,up, down);
return 0;
}

for
, 1 target target
1.

6.5 break
, break switch. H
break for,
while do while,
,
. break
, .

#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 100; i++) {
printf(%5d,i);
if (i == 10) break;

H.

-85-

/ ,

}
return 0;
}
1 10
break .

#include <stdio.h>
#define MAX_COUNT 10
int main()
{
int val, count = 0, sum = 0;
printf( : );
scanf(%d,&val);
while (val >= 0) {
count++;
if (count > MAX_COUNT) {
printf( .\n);
count--;
break;
}
sum += val;
printf( : );
scanf(%d,&val);
}
if (count > 0)
printf(O %d %.2f\n,count, sum/count);
else
printf( .\n);
return 0;
}

, 10
, break while
while .

6.6 continue
,
switch. continue break
. , continue

.

#include <stdio.h>
int main()
{

H.

-86-

/ ,

int i;
for (i = 0; i < 100; i++)
if (i % 2) continue;
printf(%5d,i);
return 0;
}
.
, if,
2 1,
. ,
continue, , printf( )
.

#include <stdio.h>
#define TARGET 1000
int main()
{
int i;
for (i = 1; i <= TARGET; count++)
if (i % 2) continue;
else if (i % 4) continue;
else if (i % 6) continue;
else if (i % 8) continue;
else if (i % 10) continue;
else if (i % 12) continue;
else
printf(%d\n,i);
}
return 0;
}


1 1000 2, 4, 6,
8, 10, 12.

6.7 char ()
C char character
(). byte

-128 127. ,
.

#include <stdio.h>
int main()
{

H.

-87-

/ ,

char ch;
do {
printf( (q Q ) : );
scanf(%c,&ch);
printf(O %c\n,ch);
} while (ch != q && ch != Q);
return 0;
}
scanf( )
,
getchar( ) (
getc(stdin) ).
putchar( ) (
putc(ch, stdout) ). Ta getchar( ) putchar( )
stdio.h :
int getchar(void);
int putchar(int ch);

(, - ).
getchar( ) . byte
. , putchar( )
, byte .

#include <stdio.h>
int main()
{
char ch;
printf( : );
ch = getchar();
putchar(ch);
return 0;
}

getchar( )
<Enter>.

getchar( ).

#include <stdio.h>
int main()
{
char ch1,ch2;
printf( : );
ch1 = getchar();
ch2 = getchar();
printf( %c %c.\n,ch1,ch2);

H.

-88-

/ ,

return 0;
}
,
: abcdefg <Enter>,
ch1 a ch2
b.
<Enter>.
getchar( )
.

#include <stdio.h>
#define SPACE
int main()
{
char ch;
printf( \n);
while ((ch = getchar()) != \n) {
if (ch == SPACE)
putchar(ch);
else
putchar(ch + 32);
}
return 0;
}


<Enter> (
ASCII 65 a 97. 32).
while ((ch = getchar()) != \n) .
: ch
. ch = getchar()
!= .
getchar( )
ch. ch
ch != \n.

4.8
C (string)
(null). C .

.
:
char str[80];

H.

-89-

/ ,

80 ,
str. ,
(index) .
C 0. , str[0] ,
str[1] , str[79] .
, . C
0 \0.

,
,
.
char str[] = ;

\0

,
gets( ). gets( )

<Enter>. <Enter>
.

#include <stdio.h>
int main()
{
char name[20];
printf( : );
scanf(%s,name);
printf(To %s\n,name);
return 0;
}

scanf( )
printf( ) .

#include <stdio.h>
int main()
{
char name[20];
printf( : );
gets(name);
printf(To );
puts(name);
return 0;
}

gets( )
puts( ) .
gets( )
H.

-90-

/ ,

. ,
, .

6.9 B A
C .

string.h .
strlen( )

:
int strlen(char *str)

strlen( ) ,
, str.
.

#include <stdio.h>
#include <string.h>
int main()
{
char str[80];
printf( : );
gets(str);
printf( %d\n,strlen(str));
return 0;
}

#include <stdio.h>
#include <string.h>
int main()
{
int i;
char str[80];
printf( : );
gets(str);
for (i = strlen(str);i >= 0; i--)
printf(%c,str[i]);
printf(\n);
return 0;
}
strcpy( )

:
H.

-91-

/ ,

char *strcpy(char *str1,char *str2)

strcpy( )
str2 str1. O str1
str2. To str2
.

#include <stdio.h>
#include <string.h>
int main()
{
char str1[80];
strcpy(str1, .);
printf(%s\n,str1);
return 0;
}
strcat( )

:
char *strcat(char *str1,char *str2)

strcat( ) str2 str1,


str2.

.

#include <stdio.h>
#include <string.h>
int main()
{
char str2[20],str1[80] = To , ;
printf( ; );
gets(str2);
strcat(s1,str2);
printf(%\n,str1);
return 0;
}
strcmp( )

:
int strcmp(char *str1,char *str2)

H.

-92-

/ ,

strcmp( ) NULL
, :
. = 0, ,
. > 0, str1 str2,
. < 0, str1 str2.

#include <stdio.h>
#include <string.h>
int main()
{
char password[] = 1212;
char code[15];
do {
printf( : );
gets(code);
} while (strcmp(code,password));
return 0;
}

#include <stdio.h>
#include <string.h>
int main()
{
char str[80];
for ( ; ; ) {
printf( : );
gets(str);
if (! strcmp(str,)) break;
}
return 0;
}

H strcmp( ) ( = 0)
. NOT (!),
. ,
,
, .
strchr( )

:
char *strchr(char *str,int ch)

strchr( ) byte
ch str.
, .

H.

-93-

/ ,

#include <stdio.h>
#include <string.h>
int main()
{
char *p;
p = strchr( C ,(int) );
printf(p);
return 0;
}

To
.

6.10
1. 20
.
2.
2003 (365
) .
3. :
. .
1000
.
. .
4. :
. .
0,
. ,
.
5. , :
.
.
1 20, 21 50, 51 80.
0.
6.
.
2000 :
; . /
: , ,
. :
. (1 = , 2 = ,
3 = ),
. % ,
.
.
7. :
) 50 100,

H.

-94-

/ ,

) ,
) ,
.
100.
8.
1000.
.
9. ,
.
10. ,
5 40 ,
.
. 40 ,
, 60 .
:
. 5 ,
. ,
,
.
40
3 1 .
11. 1000 .
,
10 %.
,
1 .
12. ,
,
PIN.
.
.
,
,
.
.
: PIN
. .
13. 5 %.
2002 1000 2 %
.
(
500 ).
.
: 2003 2 % 1000 + 1000
* 2 % = 1020 5 %
1020 1020 * 5 % = 969 .
14. 40.000 .
6 %
.
10 .

H.

-95-

/ ,

15. . ,
. ,
.
50
.
16. super market
. :
. Kg ( ), 5
,
. .
Kg .
17. 1200 .
,
, 120 .
:
.
.
. , .
18. (1, 2, 3 4).
,
.
:
. .
0,
.
,
.
.
19. :
. ,
. :
S1 = 1 - 2 + 3 - 4 + 5 - + (2N+1)
S2 = 52 + 102 + 152 + 202 + (5N)2

H.

-96-

/ ,

7.
(array)
.
. ,
.
.
.

7.1
:
[];
O .
.
. .

#include <stdio.h>
int main()
{
int table[10], i;
for (i = 0;i < 10; i++)
table[i] = i;
return 0;
}

C,
. , 10
, table[0] table[9].
, :
bytes = sizeof() *

#include <stdio.h>
int main()
{
int table[10], size;
size = sizeof(int) * 10;
printf(T s bytes %d.\n,size);
return 0;
}

H.

-97-

/ ,

,
. ,

.

#include <stdio.h>
int main()
{
int table[30], i, sum = 0;
for (i = 0; i < 30; i++) {
printf( table[%d] : ,i + 1);
scanf(%d,&table[i]);
sum += table[i];
}
printf( %d.\n,sum);
return 0;
}

H C, ,
.
,

. ,
,
,
. ,

,
.

#include <stdio.h>
int main()
{
int table[10], i;
for (i = 0; i < 100; i++) {
printf( table[%d] : ,i + 1);
scanf(%d,&table[i]);
}
return 0;
}

, 100
table . C
, assembly
. , C
, .
5.1.1

H.

-98-

/ ,

:
) 50 ,
) ,
)
, ,
) .
#include <stdio.h>
int main()
{
int table[50], i, sum = 0, count = 0, min, pos = 0;
float average;
for (i = 0; i < 50; i++) {
printf( table[%d] : ,i + 1);
scanf(%d,&table[i]);
sum += table[i];
}
average = sum / 50.0;
printf( %.2f\n,average);
for (i = 0; i < 50; i++)
if (table[i] < average) {
count++;
printf( : %d\n, i + 1);
}
printf( : %d\n,count);
min = table[0];
for (i = 1; i < 50; i++)
if (table[i] < min) {
min = table[i];
pos = i;
}
printf( : %d\n,min);
printf( : %d\n,pos + 1);
return 0;
}
5.1.2
:
) 20
,
) 40
5,
)
.
#include <stdio.h>
int main()
{
int table[20], i, sum = 0;
for (i = 0; i < 20; i++) {
printf( table[%d] : ,i + 1);
scanf(%d,&table[i]);

H.

-99-

/ ,

}
for (i = 0; i < 20; i++)
if (table[i] > 5 && table[i] < 40) {
sum += table[i];
printf(%4d,table[i]);
}
printf(\n %d\n,sum);
return 0;
}
5.1.3
:
) 40 20 30,
,
)
.
#include <stdio.h>
int main()
{
int table[40], s[11], i, pos, max;
for (i = 0; i < 40; i++) {
printf( table[%d] ( 20 30) : ,i + 1);
do {
scanf(%d,&table[i]);
} while (table[i] < 20 || table[i] > 30);
}
for (i = 0; i < 11; i++) s[i] = 0;
for (i = 0; i < 40; i++) {
pos = table[i] 20;
s[pos]++;
}
max = s[0];
for (i = 1; i < 11; i++)
if (s[i] > max)
max = s[i];
printf( %d\n,max + 20);
return 0;
}
5.1.4
table :
5, 3, 1, 27, 15, 4, 9, 8, 7, 12, 32, 10, 5, 4, 2

.
#include <stdio.h>
int main()
{
int table[15] = {5,3,1,27,15,4,9,8,7,12,32,10,5,4,2}, i, j, temp;
// bubble sort
for (i = 0; i < 15; i++)

H.

-100-

/ ,

for (j = 14; j > i; j--)


if (table[j1] > table[j]) {
temp = table[j];
table[j] = table[j-1];
table[j-1] = temp;
}
for (i = 0; i < 15; i++)
printf(%4d,table[i]);
printf(\n);
// bubble sort
for (i = 0; i < 15; i++)
for (j = 14; j > i; j--)
if (table[j1] < table[j]) {
temp = table[j];
table[j] = table[j-1];
table[j-1] = temp;
}
for (i = 0; i < 15; i++)
printf(%4d,table[i]);
printf(\n);
return 0;
}
5.1.5
:
) 20 ,
) ,
)
.
#include <stdio.h>
int main()
{
int table[20], key, i, position = -1;
for (i = 0; i < 20; i++) {
printf( table[%d] : ,i + 1);
scanf(%d,&table[i]);
}
printf( : );
scanf(%d,&key);
i = 0;
// ,
while (i < 20 && position == -1)
if (table[i] == key)
position = i;
else
i++;
if (position != -1)
printf( %d\n,position + 1);
return 0;
}

H.

-101-

/ ,

5.1.6
source, 20 ,
:
1, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 22, 25, 28, 30, 33, 34, 65, 67, 78
:
) n,
) target, 21 ,
n ,
.
#include <stdio.h>
int main()
{
int source[20] = {1,6,7,8,9,11,12,14,15,16,17,22,25,28,30,33,34,65,67,78};
int n, i = 0, j, complete = 0, target[21];
printf( : );
scanf(%d,&n);
while (i < 20 && !complete)
if (source[i] < n) {
target[i] = source[i];
i++;
}
else {
target[i] = n;
complete = 1;
}
if (!complete)
target[i] = n;
else
for (j = i; j < 20; j++)
target[j+1] = source[j];
for (i = 0; i < 21; i++)
printf(%4d,target[i]);
printf(\n);
return 0;
}
5.1.7
A, 6 ,
1, 3, 5, 7, 9, 11 B, 7 , 2,
4, 6, 8, 10, 12, 14. ,
C, 13 , .
#include <stdio.h>
#define N 6
#define M 7
int main()
{
int A[N] = {1,3,5,7,9,11}, B[M]={2,4,6,8,10,12,14}, C[M+N],i=0, j=0, k=0,
n;
while (i < N && j < M)

H.

-102-

/ ,

if (A[i] < B[j]) {


C[k] = A[i];
k++;
i++;
}
else {
C[k] = B[j];
k++;
j++;
}
if (i >= N)
for (n = k; n < N+M; n++) {
C[n] = B[j];
j++;
}
else
for (n = k; n < N+M; n++) {
C[n] = A[i];
i++;
}
for (i = 0; i < N+M; i++)
printf(%4d,C[i]);
printf(\n);
return 0;
}

7.2
C .
. ,
.
, 5 ,10 :
int table[5][10];

,
, C
.

#include <stdio.h>
int main()
{
int i, j, table[4][5];
//
for(i = 0; i < 4; i++)
for (j = 0; j < 5; j++)
table[i][j] = i * j;
//

H.

-103-

/ ,

for(i = 0; i < 4; i++) {


for (j = 0; j < 5; j++)
printf(%4d,table[i][j]);
printf(\n);
}
return 0;
}
H C
,
.
C
.
,
bytes :
bytes = * * sizeof( )

#include <stdio.h>
int main()
{
int size, table[4][5];
size = 4 * 5 * sizeof(int);
printf( bytes %d.\n,size);
return 0;
}
5.2.1
:
)
[100,100],
)
( 1 , 100 , 100
1 ).
#include <stdio.h>
int main()
{
int i, j, A[100][100], sum = 0;
for (i = 0; i < 100; i++)
for (j = 0; j < 100; j++) {
printf( : );
scanf(%d,&A[i][j]);
}
for (i = 0; i < 100; i++)
sum += A[0][i] + A[99][i] + A[i][0] + A[i][99];
printf( %d\n,sum);
return 0;
}

H.

-104-

/ ,

5.2.2
:
) 100
[100],
) [10,10]
( 10 1
, 10 2 ...).
# include <stdio.h>
int main()
{
int i, j, A[100], B[10][10];
for (i = 0; i < 100;i++) {
printf( : );
scanf(%d,&A[i]);
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
B[i][j] = A[i * 10 + j];
printf(%4d,B[i][j]);
}
printf(\n);
}
return 0;
}
5.2.3

:

1
2
3
4
5

2
4
6
8
10

3
6
9
12
15

4
8
12
16
20

5
10
15
20
25

# include <stdio.h>
int main()
{
int i, j, A[5][5];
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
A[i][j] = (i + 1) * (j + 1);
printf(%4d,A[i][j]);
}
printf(\n);
}
return 0;
}
5.2.4
:

H.

-105-

/ ,

) [i,j] 100 x 100,


) ,
)
.
:
y (i = j).
y ,
(i > j).
y ,
(i < j).
#include <stdio.h>
int main()
{
int i, j, A[100][100], sumd = 0, sumup = 0, sumdown = 0;
for (i = 0; i < 100; i++)
for (j = 0; j < 100; j++) {
printf( [%d][%d] : ,i, j);
scanf(%d,&A[i][j]);
}
for (i = 0; i < 100; i++)
for (j = 0; j < 100; j++)
if (i == j)
sumd += A[i][j];
else if (i > j)
sumdown += A[i][j];
else
sumup += A[i][j];
printf( %d\n,sumd);
printf( %d\n,sumdown);
printf( %d\n,sumup);
return 0;
}

7.3
, C
.
:
[ 1] [ 2] [ ];
4 x 3 x 2:
int table[4][3][2];


. , C
.

H.

-106-

/ ,

7.4
C .
,
:
[ 1][ 2] [ ] = { };
,
.
,
, ...

#include <stdio.h>
int main()
{
int i, table[10] = {1,2,3,4,5,6,7,8,9,10};
for (i = 0; i < 10; i++)
printf(%4d,table[i]);
printf(\n);
return 0;
}

table[0] 1 table[9]
10.

:
char [] = ;

#include <stdio.h>
int main()
{
char str[8] = ;
puts(str);
return 0;
}

str :
char str[8] = {, , , , , , , \0};
C (null),
.
str 8 ,

H.

-107-

/ ,

7 .
( 5.1.4.2), .

.

#include <stdio.h>
int main()
{
int table[10][2] = {

1, 1,
2, 4,
3, 9,
4, 16,
5, 25,
6, 36,
7, 49,
8, 64,
9, 81,
10, 100
};
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0;j < 2; j++)
printf(%5d,table[i][j]);
printf(\n);
}
return 0;
}
( ),
,
, . C
, .
,
, C
.

#include <stdio.h>
int main()
{
char str[] = ;
printf( %s %d\n,str, sizeof str);
return 0;
}

H C ,
.

H.

-108-

/ ,

, C
. ,

.

#include <stdio.h>
int main()
{
int table[][2] = {

1, 1,
2, 4,
3, 9,
4, 16,
5, 25,
6, 36,
7, 49,
8, 64,
9, 81,
10, 100
};
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0;j < 2; j++)
printf(%5d,table[i][j]);
printf(\n);
}
return 0;
}

7.5
1. :
. 30 ,
. ,
. .
2. :
. 30 ,
. ,
. .
3. :
. 30
,
. ,
.
.
4. :
. 30 ,
. ,

H.

-109-

/ ,

. .
5.
. :
. 31 ,
( 8 ..) 2004,
.
,
. 0 C.
6.
200 . :
. ,
. ,
. ,
=

( x x ) 2
n

7. :
. 100 :
5 1, 0,
. .
8. 10
0 10.
,
. :
. 10
,
.
.
9. 30 . :
. ,
.
,
.
.
10. , , .
,
.
11. , 15
. :
. ,
.
.
. 15 .
12. :
. 10
, ,
. [i]
[1], [2], ... , [i] (
[4] 4 ),
. .

H.

-110-

/ ,

13. :
. , 100
,
.
,
.
.
14. :
. 10 100 (
, 10 N 100),
. [100],
. ,
.
, :
=[1 0 0 9 0 0 6 4 0 0]
:
=[1 9 6 4 0 0 0 0 0 0]
15. :
. <= 1000 (
, 0 < N 1000),
. ,
. :
[], [ - 1], ... , [3], [2], [1]
:
=[2 4 5 7 1 0 9]
:
=[9 0 1 7 5 4 2]
16. :
. 1 100 (
, 1 N 100),
. , ,
(
) :
0, 1, 2, 3, ..., 9
.
.
:
=[1 2 2 3 3 3 4]
:
1 1
2 2
3 3
4 1
17. = [ 1,4,2,3,1,6,8,2,3,7,9,11,13,10,12,8,9,4,3, 1 ],
. :
.
[20] ( ) C[20] ( ).
0,
. ,
. .

H.

-111-

/ ,

18. 150
1 150 .
:
. 150 (

5 100 )
,
. ,
. ()
75, ,
75,
1
75.
19. ,
.
.
20.
(10) . (6)
. :
. ,
.
,
.
,
. (
).
21. 5 . [5]
.
C[5,12]
2004, , i i .
, :
. ,
.
2004,
.
2004.
22. .

.
:
. ,
. ,
. ,
. ,
.
.
23. 500 .

0 100.
:

H.

-112-

/ ,

.
,
.
,
. ,
.
.
.
.
.
24. , :
. (8 ..)
2004 (365 ).
-20 C 45 C,
. 2004 (
),
.
.
25. 10 x 10 99.
,
99 ( ).
:
. ,
.
.
26. 2001 1200 .
, :
.
( ),
.
. :
0 25
26 45
46 65
65
. 3 ,
. 3 .
27. , :
. ,
. ,
. ,
. .
:
0 2, 1 3, 2 4, 3 5, 4 6, 5 7, 6 8, 7 9, 8 0, 9 1.
28. 50 .
:
20% 10.000 .
10% 10.000 .
, :

H.

-113-

/ ,

.
,
.
,
.
.
29. 500
.
1 50.
40.
, :
.
,
. ,
.
.
30. 15 30 .
, :
. 15 x 30
: 1,
0,
. ,
. ,
. 10 .

H.

-114-

/ ,

1 . Kernigham, D. Ritchie, C,
, 1986
2 . , C, , 1993
3 K. , C++: , , 2003
4 N. , C,
, ,
2003.
5 B. , C , , 1995,
2001
6 Aho, J. Hopcroft, J. Ullman, Data Structures And Algorithms, Addison-Wesley
Publishing Company, USA, 1985 (Reprinted with corrections)
7 L. Ammeraal, C for Programmers, 2nd Edition, John Willey & Sons, England,
1991
8 L. Atkinson, M. Atkinson, Using C, Que Corporation, Carmel, USA, 1990
9 H. M. Deitel, P. J. Deitel, C: How To Program, Prentice-Hall, Inc., New Jersey,
USA, 1998
10 W. Feibel, Using ANSI C in Unix, Osborne McGraw-Hill, USA, 1990
11 L. Hancock, M. Krieger, and S. Zamir, The C Primer, 3rd Edition, McGrawHill, New York, USA, 1991
12 W. Hunt, The C Toolbox, Addison-Wesley, 1989
13 Kelley, and I. Pohl, A Book on C, An Introduction To Programming In C, The
Benjamin/Cummings Publishing Company, Inc California, USA, 1984
14 Kelley, and I. Pohl, C by Dissection, The Benjamin/Cummings Publishing
Company, Inc California, USA, 1986
15 H. Schildt, C: The Complete Reference, Fourth Edition, McGraw Hill, 2000
16 M. G. Sobell, A Practical Guide to the UNIX* System,
Benjamin/Cummings Publishing Company, Inc California, USA, 1984

The

17 Tenenbaum, Y. Langsam, M. Augenstein, Data Structures Using C, Prentice


Hall International Editions, USA, 1990
18 T. Zhang, C 24 , , , 2000

H.

-115-

/ ,

19 C/C++ Users Journal, Advanced Solutions For C/C++ Programmers, Boulder


Co., USA
20 Computer Language, Freeman Miller Publications, USA
21 The C Users Journal, R & D Publications, USA

H.

-116-

You might also like