Professional Documents
Culture Documents
gr
832 00
JJ
II
1 223
Copyright ,
All rights reserved
...
.
, .
.
.
.
.
.
JJ
II
2 223
.
. ;
.
,
( ,
, ).
.
.
, , .
Pascal C . -
.
.
...
.
JJ
II
3 223
1.1.
. ,
.
.
.
.
: (operation code) (operands)
0 1.
.
.
.
...
.
JJ
II
4 223
CONTROL SIGNALS
M
E
M
O
R
Y
ADDRESS
MAR
DATA
CONTROL UNIT
MBR
B
U
S
IR
C
O
N
T
R
O
L
L
I
N
E
S
ADD
IN T ERN AL BU S
A
L
U
PC
...
R0
SUB
R1
AND
XOR
ALU
CARRY_IN
R15
JJ
II
5 223
1.1: .
1.1
. :
1. ALU
. : () (
-). 2 bit 22 = 4
1.1,
2. IR
,
3. PC
,
...
4. R0 . . . R15
,
5. Memory Address Register (MAR ) /
,
6. Memory Buffer Register (MBR )
MAR,
MAR,
7. CONTROL UNIT IR
CONTROL SIGNALS .
JJ
II
6 223
0
1
2
3
00
01
10
11
-
-
ADD
SUB
AND
XOR
1.1:
. (
) .
.
R2 R3 R1. ,
<_><_>
<_1> <_2>.
:
00 0001 0010 0011
2 bit 00
1.1, 4 bit 0001 = (1)10
R1 .
bit 0010 0011 R2
R3 . , R1 = R2 + R3 .
. , -
...
.
JJ
II
7 223
,
, .
,
.
(Assembly
Languages). , (mnemonics) .
ADD R1 , R2 , R3
.
. , (Assembler)
.
.
...
.
JJ
II
8 223
1.2.
1.1, . ,
,
.
,
.
.
.
d = a + b c
( b, c
d).
.
:
1. ,
,
2. ,
( )
,
3. ,
,
,
...
.
JJ
II
9 223
4. ,
, .
, ,
. ,
. ,
(reusability).
. FORTRAN (FORmula TRANslation) IBM
, LISP (LISt Processing)
COBOL
(COmmon Business-Oriented Language)
.
C,
UNIX .
C.
system programming Language. Prolog
.
(Object Oriented Programming OOP)
60 Small-
...
.
JJ
II
10 223
talk. ,
. OOP ,
. , .
.
. OOP C++
Java.
C.
...
1.3.
JJ
II
11 223
:
1.3.1 A , A
A . A .
B C
B
{a, b, c, . . . , x, y, z, A, B, C, . . . , X, Y, Z, 0, 1, . . . , 8, 9,
C B .
.
,
.
:
...
.
1. .
2.
.
,
, .
.
1.3.2 :
JJ
II
12 223
1. T :
,
2. N :
,
,
3. P
.
,
{,
, , , , , . . .}.
x y x y
.
x y.
:
...
.
1. , .
2.
,
3. 1 2 .
.
JJ
II
13 223
,
+, , = .
T = {A, B, C, +, , (, ), =}
N = {asgn_stat, exp, term, factor, id }
:
exp
term
id
-.
.
:
...
.
asgn_stat
A|B|C
id
(exp)|id
factor
id = exp
asgn_stat
id = exp
(1.1)
id = exp + term
(1.2)
JJ
II
(1.3)
id = exp + term id
(1.4)
id = exp + term C
(1.5)
id = exp + factor C
(1.6)
id = exp + id C
(1.7)
id = exp + B C
(1.8)
(1.9)
14 223
id = term + B C
(1.10)
id = factor + B C
(1.11)
id = id + B C
(1.12)
id = A + B C
(1.13)
A =A+BC
(1.14)
1.1 asgn_stat
id = exp. exp exp +term
1.2.
. 1.14 A
B C.
. 2.
2. 2
(context-free grammar)
.
. ,
...
.
JJ
II
15 223
.
.
.
. (type system)
. type theory.
.
,
(runtime exceptions).
1.4.
,
. ,
, C, (compilation).
...
.
JJ
II
16 223
. C, . (compiler)
.
Machine Dependent
Libraries
Source Code
Preprocessor
Lexical
Analysis
Syntactic
Analysis
Object Code
Generation
Executeble
Linking
...
.
1.2: .
1.2 C.
(preprocessor) ,
. (Lexical Analysis)
(token)
. (Syntax Analysis) .
(object code generation)
JJ
II
17 223
. ,
(Linking)
( ),
. ,
.
...
.
JJ
II
18 223
...
C
2.1.
C .
main C
main
.
:
(standard lib) ANSI C
(user defined).
C,
( .h) . #include (preprocessor) C.
...
.
JJ
II
19 223
C
C. 2.1
ANSI C .
...
C
stdarg.h
io.h
limits.h & float.h
math.h
mem.h
stdio.h
stdlib.h
string.h & ctype.h
time.h
/
.
2.1:
2.1 C
This is my first code!!!.
main . main printf
This is my first code!!! (
3 ). printf
stdio.h ( 0 )
...
.
JJ
II
20 223
<>.
...
0: #include <stdio.h>
1:
2: int main(){
3:
printf("This is my first code!!!\n");
4:
return 0;
5: }
2.1: .
2.1 .c .. myFirstCode.c. UNIX,
cc 2.1 #cc myfirstCode.c
myfirstCode
.
...
.
2.2.
C :
char: byte
256 . ASCII 128.
int: byte
.
JJ
II
21 223
float:
double:
...
, , C byte
. byte sizeof
sizeof(<_>).
float sizeof(float).
< limits.h > < float.h >. ,
:
...
JJ
II
22 223
. long double
double.
signed () unsigned ( ) char 754 IEEE
. . char 8-bit
bit , 1
0 +. 8-bit 10000001 1 (
)
unsigned 128
, 1 27 + 1 20 = 128. unsigned char 0 . . . 255
128 . . . 127.
int.
C
typedef :
...
C
...
.
; C
.
int integer :
JJ
II
23 223
byte
0 . . . 255 :
...
C
2.3.
...
.
C case-sensitive
.
:
_
.
C
.
2.2 (reserved
words).
JJ
II
24 223
-
int, char, double, float
long, short, signed, unsigned,
register, static, extern,
auto, void
const, volatile
break, case, continue, switch,
default, do, while, for, if,
else, return, goto
typedef, enum
struct, union
...
C
...
2.2: -
.
:
< typename > < variable_name >=< value >;
(2.1)
JJ
II
25 223
. ;
C, .
:
int count = 5;
...
C
byte ( int)
count 00000000 00000000 00000000 00000101
5.
:
double percent = 28.5, rate = 5.6;
percent
rate 28.5 5.6 .
. .
count = 5;
5 count.
2.4.
C, ,
.
...
.
JJ
II
26 223
. C:
literal: = 3.14159 literal
3.14159.
, literal 3.14159 PI . 2 C
:
1. const: const float PI = 3.14159;
2. #define:
#define PI 3.14159
...
C
PI.
#undef.
PI :
#undef PI
int (signed)
long.
long L U, .. 2048L 2048U
2048UL.
0 0x 0X , .. 077 = (63)10
0x2f = (63)10 .
...
.
JJ
II
27 223
(.. 23.45)
(2e-3) ( 2 103 ) double
:
...
F f float
L l double.
..
x . , 8 bit ASCII
0 x 0
120 x ASCII .
.
2.3 ASCII . C, \
. . 0 n 0 n 0 \n 0
ASCII ASCII 10. \
.
0 \t 0 0 \ 0 ASCII . ,
0 \00 . , ,
.
(string constant) , .. Hello World
string. 0 \00
0 0
...
.
JJ
II
28 223
, \0 Hello World
Hello World\0.
...
C
...
.
JJ
II
29 223
97(a)
98(b)
99(c)
100(d)
101(e)
102(f)
103(g)
104(h)
105(i)
106(j)
107(k)
108(l)
109(m)
110(n)
111(o)
112(p)
113(q)
114(r)
115(s)
116(t)
117(u)
118(v)
119(w)
120(x)
121(y)
122(z)
65(A)
66(B)
67(C)
68(D)
69(E)
70(F)
71(G)
72(H)
73(I)
74(J)
75(K)
76(L)
77(M)
78(N)
79(O)
80(P)
81(Q)
82(R)
83(S)
84(T)
85(U)
86(V)
87(W)
88(X)
89(Y)
90(Z)
48(0)
49(1)
50(2)
51(3)
52(4)
53(5)
54(6)
55(7)
56(8)
57(9)
2.3: ASCII
.
...
C
...
.
JJ
II
30 223
2.5.
C
.
2.5.1.
...
C
C . : ++,
. x ++
x 1 x + + + + x x = x + 1.
x x x = x 1.
x + +
+ + x. x + +
x . + + x
1 .
:
int z, y, x = 5;
y = + + x;
z = x + +;
...
.
JJ
II
31 223
y 6, z
6 x 7.
: +, -, /, *, % .
/ %
modulo x %y x
y.
. *,/,%
+ -.
.
.
:
...
C
8%3 6. % * ,
,
12.
3+4*6.
27.
...
.
(3+4)*6. ,
42.
w x/y z. * / ,
, ((w x )/y) y.
wx/y+z/y. .
+
.
. , ((w x )/y) + (z/y).
JJ
II
32 223
2.5.2.
C , , bit
. x n (x n) bit x, n () 0 n
() x.
.
x = (12)10 = (00001100)2 n = 2 x n ( ) x 0
(00000011)2 = (3)10 . n = 3
x n x 0
(01100000)2 = (96)10
x n (00000001)2 = (1)10 . x ,
(12)10 = (00001100)2 .
,
2.
n , bit
2n .
n , .
7 3
2.
,
; :
.
, bit
0 ( ) 1 ( ) bit .
...
C
...
.
JJ
II
33 223
2.5.3.
, C
2.4.
: 1 0 .
...
C
==
>
<
>=
<=
!=
?
?
?
?
?
?
x==y
x>y
x<y
x>=y
...
.
x<=y
x! =y
JJ
II
2.4:
34 223
(x + 6) > y.
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
() [ ] .
! + + ( ) (cast )
&( ) sizeof +() -()
() / %
+
== ! =
& ( )
&&
...
k
?:
= + = = = / = % = &= = | = = =
,
2.5:
2.5.4.
bit
bit , C bit:
JJ
II
35 223
& :
| :
:
(240)10 = (11110000)2 (85)10 = (01010101)2
,
...
C
, C bit
bit
. n = (254)10 = (11111110)2 ,
n (1)10 = (00000001)2 .
. &
|.
,
. 2.5 C.
2.5.5.
, C
2.6. ,
1 0 .
.
...
.
JJ
II
36 223
! .
&& k.
, ,
bit ( 11 12 2.5).
a < b k b < c && c < d
(a < b) k ((b < c ) && (c < d )).
...
C
&&
k
!
2.6:
2.5.6.
. C
? : ( )
.
:
< cond >? < expr1 >:< expr2 >
...
.
JJ
II
37 223
(a > b) a z
b.
2.5 .
...
C
2.5.7.
C, . bit
op. :
< variable >=< variable > op < expr >;
< variable > op =< expr >;
...
.
op=, op , bit, .
:
x = x + (8 b + a );
x (8 b + a ).
+ = :
x + = (8 b + a );
JJ
II
38 223
, . ,
. ,
.
x = (a + + , b + +);
a=0 b=4, x 4
...
C
x = (a + + , + + b);
x 5 (?).
...
2.6.
,
.
:
, .
(cast)
.
JJ
II
39 223
...
C
int w = 4, x = 3;
float y;
y = (float ) w/x ;
(2.2)
...
.
2.2, (float )
w
. ,
x ,
.
y 1.333 . . .. 2.7
C.
JJ
II
40 223
1
2
3
4
5
6
7
8
9
long double
long double.
double
double.
float
float.
char short int
int
unsigned int.
unsigned long
unsigned long.
unsigned int long, long
unsigned int
long unsigned long.
long,
long.
unsigned int,
unsigned int.
int.
2.7:
...
C
...
.
JJ
II
41 223
C, ,
. char int; C
. -
char
(signed unsigned).
...
2.7.
C :
printf()
scanf()
2.7.1.
printf()
printf() .
stdio.h :
int printf (char format, arg1, arg2, . . .);
...
.
,
. 7 C .
( ).
:
JJ
II
42 223
.
...
C
% 2.8. printf:
printf(The result is:%d, v);
...
.
JJ
II
43 223
d,i
u
c
x
X
o
f
e, E
g, G
s
p
a, b, c, d, e, f
a, b, c, d, e, f
m.dddddd
d
6
m.dddddde + / xx m.ddddddE + / xx
d
6
4
%e/%E
%f
...
C
...
.
JJ
II
44 223
2.8: printf
% ,
.
:
...
C
1. -, .
2. +, .
3. ,
.
4. 0,
0.
.
5. # 2.8
:
...
.
o 0 .
x X 0x 0X.
e, E f .
JJ
II
g G, -
.
6. .
0 .
45 223
7. , .
8. :
, ,
,
0,
, (e, E f)
(g G).
...
C
9. h short int, l
long int L
long double.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
...
#include <stdio.h>
#include <stdlib.h>
JJ
II
46 223
return 0;
}
2.2: printf.
2.2 C
printf. 3 LOWLET .
7
a 20
. , 8
0X1E 30
9
80.75 literal 80.756. \n
(newline ) .
...
C
2.7.2.
scanf()
scanf() .
stdio.h :
int scanf (char format,
, ,
. 7
C .
( ).
scanf:
scanf(%d, &v);
%d
...
.
JJ
II
47 223
v.
& . &
, 7,
scanf .
:
...
C
1.
...
2.
3. , %
,
, h, l, L
2.9.
JJ
II
48 223
d
i
u
c
x
o
f, e, g
,
0
0x 0X
0x 0X
0
,
, E
...
C
...
.
2.9: scanf
( , , ). , .
2.3 scanf.
, scanf 7
. Arithmos -
JJ
II
49 223
5
. Arithmos543.645
scanf y 543.6 5
scanf 7.
, 45, scanf 9
%
f y.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
...
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
float y;
scanf("Arithmos%5f", &y);
printf("%f\n", y);
scanf("%*f",&y);
printf("%f\n", y);
...
.
return 0;
}
2.3: scanf.
JJ
II
50 223
2.8.
2.8.1 ;
;
int x, y = 6, z = 8;
x = y + + + + + z;
printf (%d %d %d \n, x, y, z );
x = + + y + z + +;
printf (%d %d %d \n, x, y, z );
x = + + y + + + z;
printf (%d %d %d \n, x, y, z );
x = y + z ;
printf (%d %d %d \n, x, y, z );
...
C
2.8.2 ;
d = (a = 4) (c = 8);
2.8.3 . , .
int x = 5, y = 6, z = 8, w = 7, u = 11;
x/y/z
9 + z / w u
4 x % y + z 2
23 / u +12 % z
x += y= z= 4 8
...
.
JJ
II
51 223
2.8.4 .
;
...
int u = 3;
unsigned int v = 3;
if (u 1 == v 1) printf (Eiserxontai mhdenika kata thn metatopish \n );
else printf (To bit proshmou eiserxetai kata thn metatopish \n );
2.8.5 .
int x = 3, w = 6, u = 4, v = 2;
float y = 2.0;
...
.
JJ
II
52 223
...
If
switch
...
1,
.
-.
, -
.
.
. ,
.
JJ
II
53 223
. , ,
.
,
.
C
C.
...
If
switch
3.1.
C, ; .
( ;),
.
(block)
{ } (compound-statement)
:
...
{
_;
-1;
...
-n ;
JJ
II
54 223
,
. C main.
3.2.
If
2 ? :
,
.
C
if :
if () ;
if () -
0
if .
. ,
if.
.
...
If
switch
...
if () -1 ;
else -2 ;
if () --1
else --2
, -1/--1
-2/--2.
if else
.
JJ
II
55 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
#include <stdio.h>
#include <stdlib.h>
...
int main()
{
int a, b;
If
switch
if (a == b)
printf("\n a is equal to b\n");
if (a > b)
printf("\n a is greater than b\n");
if (a < b)
printf("\n a is smaller than b\n");
return 0;
...
3.1: if
3.1 if.
8 9
a. ,
11 12 ,
b. 14.
a b
JJ
II
56 223
. , 16
a b.
18, a
b.
.
.
:
...
If
switch
...
if (-1) --1
else if (-2) --2
else --3
-;
JJ
II
57 223
,
a b 3.2.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
#include <stdio.h>
#include <stdlib.h>
...
int main()
{
int a, b;
printf("\nGive an Integer Value for a: ");
scanf("%d", &a);
If
switch
if (a == b)
printf("\n a is equal to b\n");
else if (a > b)
printf("\n a is greater than b\n");
else printf("\n a is smaller than b\n");
...
return 0;
}
3.2: if/else
3.3.
JJ
II
switch
58 223
, ,
. ,
1
2
if-else.
if-else , C switch
.
...
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
#include <stdio.h>
#include <stdlib.h>
main()
{
int key;
printf("Enter a number between 1 and 5:");
scanf("%d", &key);
switch (key)
{
case 1: printf("You entered 1.\n");
case 2: printf("You entered 2.\n");
case 3: printf("You entered 3.\n");
case 4: printf("You entered 4.\n");
case 5: printf("You entered 5.\n");
default: printf("Out of range, try again.\n");
}
return 0;
}
If
switch
...
JJ
II
59 223
3.3: switch
switch .
switch
. switch
.
switch default
. :
...
If
switch
switch() {
case --1: ;
case --2: ;
...
default: ;
}
...
. , case
. case
switch. default ,
case . ,
.
JJ
II
60 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
#include <stdio.h>
#include <stdlib.h>
...
main()
{
int key;
3.4: switch
If
switch
...
JJ
II
61 223
3.3 switch
.
2? :
You entered 2.
You entered 3.
You entered 4.
You entered 5.
Out of range, try again.
...
.
break C .
3.4 break
C case.
2 printf
break
switch,
switch. , :
swi-
If
tch
You entered 2.
...
3.4.
, ,
.
. C
for, while do-while
.
JJ
II
62 223
3.4.1.
while
while
. :
...
while () ;
while () -
If
swi-
.
. ,
while.
. .
while
while,
while
.
c = getchar ();
while(c ==0 0 k c ==0 \n 0 k c ==0 \t 0 ) c = getchar ();
c = getchar () getchar ()
C ( stdio.h)
. ,
c
getchar ().
while (c ==0
0
kc ==0 \n0 kc ==0 \t0 ) while c =
tch
...
JJ
II
63 223
getchar ().
c 0 0 , 0 \n0
0 \t0 .
, ,
c
while. while
. while
.
while
...
If
switch
...
3.4.2.
for
,
for C. :
JJ
II
64 223
for (_; ; -) ;
for (_; ; -) -
for
.
.
- for.
-
.
...
If
( )
, :
switch
...
for (_; ; -) ;
for (_; ; -) -
,
,
break .
3.5
. C . 1
s 15 .
i- s, s[i ].
JJ
II
65 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
int i, n, sign;
char s[15];
for (i = 0; isspace(s[i]); i++);
...
sign = (s[i] == -) ? -1 : 1;
If
switch
3.5: .
...
JJ
II
66 223
1 0 .
for n = 0, s[i ]
n.
i 1 for
. s -45
9,
:
...
If
switch
sign
1;
1;
0;
s[1] 0 40
n = n 10 +(s[1]0 00 ) 0 s[1]0 00
4 ASCII 0 40 52 ASCII 0 00 48. ,
n 4 i 2.
s[2] 0 50 n = n 10 + (s[2] 0 00 )
n 4
n = 4 10 + (53 48) 53 ASCII 0 50 ,
n = 45. i (i = 3) s[3]
s. (s[3] =0 \00 ) for
11
n sign = 1.
3.6 for , hello olleh. for
( < string.h >) strlen () . ,
...
JJ
II
67 223
i j .
i- j-.
, for
for
.
...
If
swi-
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
int c, i, j;
tch
//Version 1
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
// Version 2
for (i = 0, j = strlen(s)-1; i < j; i++, j--)
c = s[i], s[i] = s[j], s[j] = c;
3.6: .
3.4.3.
do-while
for while
.
C do-while,
, .
...
JJ
II
68 223
do ; while ;
do - while ;
...
3.7 . .
0 1 i, n, sign int s 15 . 3,
.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
3.7: .
5 7 do-while.
6 n
10 ASCII 0 00
ASCII .
ASCII i s i
1 .
If
switch
...
JJ
II
69 223
n/ = 10
0.
6 8
i s
. , 9 s
0 \00 .
n = 45
s :
...
If
switch
s[0]
0 0
s[1]
0 0
s[2]
s[3]
\00
...
3.4.4.
continue
,
, break.
3.8 break.
for if. i- i-
s .
if for
.
break for.
JJ
II
70 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
\\Break Command
for (n = strlen(s)-1; n >= 0; n--)
if (s[n] != && s[n] != \t && s[n] != \n) break;
...
s[n+1] = \0;
\\Continue Command
for (i = 0; i < n; i++){
if (a[i] < 0) continue;
...
}
If
switch
...
3.4.2 for
. continue ,
continue.
a. 3.8
for continue
a.
continue while do-while.
JJ
II
71 223
3.5.
...
2 3.
If
switch
3.5.1.
(x, y),
(r, ) [0, 2 ).
y, x = 0.
x, y . (x, y)
scanf 3.9
10.
y x
if-else 12 18.
y 18
if.
...
JJ
II
72 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
#include <stdio.h>
#include <math.h>
...
#define PI 3.14159
void main()
{
float x=0, y=0, r, angle;
If
switch
if (x){
r = sqrt((pow(x,2) + pow(y,2)));
angle = atan2(y, x);
angle += (x<0) ? PI : ((y>=0)? 0 : 2*PI);
printf("(%g, %g)\n", r, angle);
}
else printf("To shmeio anhkei sto axona y");
...
}
.
3.9:
r = x 2 + y2
. < math.h >
sqrt() pow(). sqrt()
x y
x y .
13.
JJ
II
73 223
arctan( xy )
arctan( y ) + 2
=
arctan( xy ) +
x
if x > 0 and y 0
if x > 0 and y < 0
if x < 0
atan2()
y
y, x x .
14 . 15
.
...
If
switch
...
JJ
II
74 223
3.5.2.
(plaintext string) Attack at Dawn f (x ) =
65 + (5x + 4) mod 26 x ASCII
(ciphered string).
.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
#include <stdio.h>
main()
{
char p[]="Attack At Dawn";
int i;
for(i=0; p[i]!=\0; i++){
char x;
switch(p[i]){
case \n:
case \t:
case : break;
default:
x=65 + (5*p[i]+4) % 26;
printf("%c", x);
break;
}
}
printf("\n");
}
...
If
switch
...
JJ
II
75 223
3.10:
3.10 f (x ) x .
2 ASCII
. 0 D 0
x 68. , x
(5 x + 4) mod 26 (5 68 + 4) mod 26 = 6
0 G 0 (
0). ASCII
65 0 A0 .
.
.
switch 10 18
ASCII
.
, for 7 19
switch 10 18.
for 8
.
3.5.3.
bit
x n, bits x
n x
bits .
bits n ,
...
If
switch
...
JJ
II
76 223
n n bits
n bits .
...
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
#include <stdio.h>
void main()
{
unsigned int x, n, y;
If
swi-
tch
...
3.11: bit
.
n x
x n. n = 2 x = 11001010
0010100.
x n
bit n .
8 sizeof (int ) n
x ( 8 sizeof (int ) bit int),
x (8 sizeof (int ) n ) |
x n x (8 sizeof (int ) n ), x n |x (8 sizeof (int ) n )
.
JJ
II
77 223
3.5.4.
n-bit 2, [0 . . . 2n 1 ]
2
.
...
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
#include <stdio.h>
If
swi-
tch
void main()
{
unsigned char x;
printf("Positive Numbers\t Negative Numbers\n");
printf("\t (%d) %0x \t\t\t \n", 0, 0);
for(x=1; x<128; x++){
unsigned char i, y;
i=1;
while (!((x >> (i-1)) & LEASTMASK)) i++;
y = x ^ (MASK8 << i);
printf("\t (%d) %0x \t\t\t (-%d) %x\n", x, x, x, y);
}
printf("\t \t \t\t\t (-%d) %x\n", x, x);
}
...
JJ
II
78 223
3.12: 2
2 bit
bit,
bits bit.
3.12 2 8-bit
.
, .. (1)FF .
for 1 . . . 128
2 .
while 16 bit
(
LEFTMASK 1
bit) bit .
while i bit.
i bit .
MASK8 1 8-bit
i :
11 . . . 1 0
. . .}
0
| {z
...
If
switch
...
-
. i bit
x {0, 1} x 0 = x x 1 = x 0.
17 2 .
JJ
II
79 223
3.6.
3.6.1
x s1, s2, s3
:
...
x
1
2
3
1
s1
s3
s1
2
s2, s3
s1
s2
3
s3
s2
s1, s2
If
switch
s3
x
...
3.6.2
while (scanf (%lf , &capital ) == 1){
...
}
JJ
II
80 223
capital 8%
1% , ,
.
3.6.3 w
ai :
a0 = 1
...
ai +1 =
1
2
(ai +
w
ai
If
Newton-Raphson i ai
switch
w.
w
ei = w ai2 .
e
ei e.
...
3.6.4 a, b, c
ax 2 + bx + c
. .
JJ
II
81 223
3.6.5 3.13
break continue.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
while (c=getchar()){
if (isdigit(c)) break;
++cnt;
if (islower(c)) printf(''%c'', toupper(c));
}
j=-10;
n=65; sum=0;
while (j<n) {
j++;
if ((j%2)==0) continue;
sum += j;
printf("j= %d kai athroisma = %d\n", j, sum);
}
...
If
switch
3.13: 3.6.5
...
JJ
II
82 223
...
,
&
C
...
,
main
.
scanf() printf()
sqrt(), pow() atan2().
( ).
.
JJ
II
83 223
.
.
(decomposition). ;
(top down design).
.
.
.
C
. 2, C (source file)
.
(Modular Programming). module
main main module.
128-bit (16 byte) module math_128.c main
module crypto.c.
...
,
&
C
...
.
JJ
II
84 223
...
,
&
4.1.
C main
C .
main
.
.
,
. C :
3.1:
...
.
JJ
II
85 223
{
<_>;
<-1>;
...
<-n >;
return(<>);
}
...
,
. ,
, . <_>
int.
return, : return(<>
); .
.
return.
ANSI C void :
1. ,
2. ,
&
C
...
.
JJ
II
86 223
3. .
. :
void<_> (< >)
{
<_>;
<-1>;
...
<-n >;
return(<>);
}
...
,
&
return,
}.
. :
C
...
.
JJ
II
87 223
k . ,
. ,
k x
.
...
,
4.1:
&
C
...
.
xi
i =0 i !
Pk
4.1 calcSeries
. k
int x double double.
.
JJ
II
88 223
.
.
k
.
,
,
for.
i for result
.
k !
x k
x
i .
8-12 4.1
fact x_pow
.
return result
.
4.2.
.
1, C .
,
. ,
...
,
&
C
...
.
JJ
II
89 223
;
main . C
main main
main
main.
main .
, A,
B
B, B
B .
;
.
. ANSI C,
:
<><_> (<_1> <__1>, . . .);
...
,
&
C
...
.
<><_> (void );
.
.
.
,
A B.
:
JJ
II
90 223
...
,
&
C
...
.
. .
. printf()
< stdio.h > #include .
JJ
II
91 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
#include <stdio.h>
#include <stdlib.h>
int isPerfect(int);
int combinations(int n, int k);
double calcSeries(int k, double x);
int main()
{
int n, k, choice, i;
double x=0;
...
,
&
C
...
.
JJ
II
92 223
4.2:
calcSeries
P
i
ki=0 xi ! . myMath.c
main 4.2 myCode.c.
5 myCode.c
calcSeries main
19.
.
...
,
4.3.
&
C.
().
. 19
4.2 scanf
x 10
printf
calcSeries.
, ,
.
.
:
1. .
calcSeries
C
...
.
JJ
II
93 223
x scanf
i i + +
for.
...
,
2. , , . i
k x
x
main .
3. .
calcSeries.
.
.
(call by value).
(call by reference).
4.
(stack frame).
byte
. calcSeries 36 byte ( )
2 8 byte (
&
C
...
.
JJ
II
94 223
)
2 4 byte.
5. return
return }.
6. ( ).
, calcSeries 19
printf printf
i . ,
,
.
.
case 1 16-20
x
P
i
Sk (x ) = ki=0 xi ! k = 1, 2, . . . 10.
4.4.
C
TRUE FALSE.
...
,
&
C
...
.
JJ
II
95 223
...
,
islower (ch )
isupper (ch )
isalnum (ch )
isalpha (ch )
isdigit (ch )
ispunct (ch )
isspace (ch )
,
,
,
,
,
,
,
0
0
0
0
0
0
0
ch
ch
ch
ch
ch
ch
ch
.
.
.
.
.
.
.
4.1: C.
&
C
...
.
C, .
C
bool
.
JJ
II
96 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
bool IsEven(int k)
{
return(k % 2 == 0);
}
bool IsLeapYear(int year)
{
return(((year%4 == 0) && (year % 100 != 0)) || (year % 400 == 0) );
}
...
,
&
C
...
4.3:
.
2 C.
C.
.
(enumeration type ).
:
JJ
II
97 223
. bool,
TRUE FALSE. :
...
,
Bool :
&
C
...
.
JJ
II
98 223
, 0 1.
bool months :
...
FALSE 0, TRUE 1
typedef enum {January=1, February, March, April, May, June, July, August, September, October, November, December} months;
month :
month = (month + 1)%12.
.
4.3. IsEven k
1 0 k 2
0 .
bool FALSE TRUE.
IsLeapYear year
1 0
&
C
...
.
JJ
II
99 223
.
bool FALSE TRUE. ,
IsVowel (ch)
tolower ()
TRUE FALSE .
...
,
0: bool isPerfect(int n)
1: {
2:
int i, sum;
3:
4:
sum=0;
5:
for(i=1; i<n; i++){
6:
if ((n%i)==0) sum+=i;
7:
if (sum>n) return FALSE;
8:
}
9:
if (sum == n) return TRUE;
10:
else return FALSE;
11: }
&
C
...
.
4.4:
. n
n.
. isPerfect (n ) n
TRUE FALSE .
sum
n.
n
JJ
II
100 223
1 n 1 n
. 7
n.
FALSE
n
TRUE FALSE.
[1 . . . 9999] 4 .
...
,
&
4.5.
.
,
.
.
.
.
.
,
...
.
JJ
II
101 223
.
3.
.
.
.
. ,
.
.
( ).
.
extern.
.
.
sp
val int 400 .
:
...
,
&
C
...
.
JJ
II
102 223
int sp;
double val [400];
:
extern int sp;
extern double val [];
...
,
val
. extern
;
byte
extern
.
, 4 byte
sp ( int) 400 8 byte val (
8 byte double) extern
.
extern
.
.
, ,
.
,
&
C
...
.
JJ
II
103 223
. , .
static
,
.
:
static int stackPointer ;
static
.
. .
static
. static,
.
1.1 1 .
. C
(register )
.
. :
G (register int x, register long y) {
register int j;
...
...
,
&
C
...
.
JJ
II
104 223
. ,
, scanf () .
...
,
4.6.
.
(decomposition). (recursion)
.
.
4.1
(1 + x )
n
X
&
C
...
.
C(n, k ) x
(4.1)
k =0
C(n, k )
4.2
.
JJ
II
105 223
C(n, k )
n!
k ! (n k )!
(4.2)
C(n, k ) .
C(n, k ) k n
.
0 n, C(n, 0) = 1.
k n k > n,
C(n, k ) = 0 k > n.
, n :
1. k 1 n 1,
C(n 1, k 1) k
n k ,
2. k n 1,
C(n 1, k ) k
n k .
C(n, k ) = C(n 1, k 1) + C(n 1, k )
C(n, k ) C(n 1, k 1) C(n 1, k ).
. ,
C(n 1, k 1) C(n 1, k )
C(n, 0) C(n, k ) k > n
.
:
...
,
&
C
...
.
JJ
II
106 223
C(n, 0)
C(n, k )
0 k > n
C(n, k )
(4.3)
: 4.2 4.3.
0: int combinations(int n, int k)
1: {
2:
if (n<k) return(0);
3:
else if (k==0) return(1);
4:
else return(combinations(n-1,k-1)+combinations(n-1,k));
5: }
...
,
4.5:
&
; ,
. 4.5
4.3. combinations n k C(n, k ).
2-3
4 2
. 4.2
3 combinations.
:
C
...
.
JJ
II
107 223
1.
.
.
2.
.
:
...
,
if ( ) return( )
else {
return(
.)
}
Fibonacci. Fibonacci
4.4.
&
C
...
.
JJ
II
108 223
fib(0)
fib(1)
fib(n )
fib(n 1) + fib(n 2)
(4.4)
. 4.6
4.4
.
...
,
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
#include <stdio.h>
#include <stdlib.h>
&
int fib(int);
int main()
{
int n;
printf("Dvste ton oro ths akolouthias Fibonacci: ");
scanf("%d", &n);
printf("Fib(%d)=%d\n", n, fib(n));
}
int fib(int n)
{
if (n<=0) return n;
else return (fib(n-1)+fib(n-2));
}
4.6: Fibonacci.
...
.
JJ
II
109 223
. 4.7 , e_gcd ()
recusiveGCD () .
. ,
.
.
...
,
&
C
...
.
JJ
II
110 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
#define TRUE 1
22:
23:
int recursiveGCD(int x, int y);
24:
int e_gcd(int x, int y);
25:
26:
int main()
27:
{
28:
int x, y;
29:
30:
printf("Dvse tous arithmous x kai y:");
31:
scanf("%d %d", &x, &y);
printf("%d %d\n", e_gcd(x,y), recursiveGCD(x, y)); 32:
33:
return 0;
34:
}
35:
36:
37:
#include <stdio.h>
#include <stdlib.h>
...
,
while (TRUE){
r = x%y;
if (r==0) break;
x=y;
y=r;
}
return y;
}
&
C
...
.
JJ
II
111 223
if ((r=x%y)==0) return y;
else return recursiveGCD(y, r);
}
. 4.2 combinations() k = 2 n. n = 10
100.
n k .
.
...
,
&
C
...
n
3
4
5
6
7
8
9
10
11
19
29
41
55
71
89
109
4.2: combinations() n
k = 2.
JJ
II
112 223
C(3,2)
...
+
,
C(2,1)
C(2,2)
+
C(1,1)
C(1,1)
C(1,2)
1
C(1,0)
C(0,1)
&
C(0,0)
1
C(0,1)
C(0,0)
Trivial Case 2: Return 1
...
.
,
. byte
. 4.8
C(3, 2).
C(3, 2), ,
C(2, 1) C(1, 0)
2 C(2, 1) 1.
-
JJ
II
113 223
. C(2, 1) C(1, 0) C(1, 1)
C(0, 0)
1. .
C(3, 2)
.
.
. n k
.
.
...
,
&
C
...
.
4.7.
C #include #define.
#include
#define .
<> #include <
stdio.h >. #include
#define
JJ
II
114 223
.
...
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, x;
&
x=0xFF;
for(i=0; i<6; i++) {
x = BitClr(x,i);
printf("%d %x\n", POWER2(i), x);
}
}
4.9: .
C
...
.
. #define,
macro :
JJ
II
115 223
. .
bit x n :
...
,
, , #define .
left_rotate (x, n ) x n .
left_rotate (x, n ) .
.
.
4.9 .
2 2 .
POWER2(x ). ,
bit . BitSet (a, pos) BitClr (a, pos)
a pos bit .
&
C
...
.
JJ
II
116 223
4.8.
4.8.1 4.11;
...
,
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
y -= x *= z--;
printf("%6d%6d%6d\n", x, y, z);
{
double y = 3.0, x;
&
x += z = 5 * y;
printf("%6.2f%6.1f%6d\n", x, y, z);
}
printf("%6d%6d%6d\n", x, y, z);
C
...
4.10: 4.8.1
JJ
II
117 223
4.8.3 4.12.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
#include <stdio.h>
#include <stdlib.h>
#define FOREVER 1
#define END 500
main(void){
int k=5;
void proc(int);
...
,
while(FOREVER) proc(k);
}
&
4.11: 4.8.3
4.8.4 m n m n
m n.
...
.
JJ
II
118 223
...
&
&
, ,
, .
(arrays) C
.
5.1.
.
bit,
0 1. -
...
.
JJ
II
119 223
, bit bit.
. bit
byte. byte
byte:
...
&
1. 1024Bytes = 1KB
&
...
.
2. 2048Bytes = 2KB
JJ
II
120 223
Address
Memory Byte
...
1KByte Memory
&
&
500
501
...
502
503
JJ
II
121 223
1022
1023
5.1: byte.
5.1 1KB,
1024 byte. byte N
byte 0
N 1. byte (address).
, 1024 byte, byte
0 . . . 1023.
byte byte,
. byte
.
.
...
&
&
(memory allocation)
byte .
char a =0 A0 ; byte a char 2
byte 65
ASCII 0 A0 .
byte a
a byte a. 5.2 a 500
byte ASCII 0 A0 . C
& , scanf ,
. &a byte
a a .
...
.
JJ
II
122 223
Address
Memory Byte
Address
Memory Byte
...
2
Byte for variable a of
type char
a = A
&x
&a
1KB Memory
500
65
1KB Memory
400
501
401
502
402
503
403
404
405
406
1022
407
1023
&
1022
1023
&
...
.
byte
int, double, byte (words).
byte
byte. double x ; 8 byte x byte
x. &x x. 5.2 x byte
400, 401, . . . , 407 400
byte x &x
400. , scanf (%f , &x )
JJ
II
123 223
&x.
.
byte .
.
...
&
&
5.2.
5.2.1.
(array) :
1. , :
, .
2. : .
(element).
C
.
. :
...
.
JJ
II
124 223
<_> _[_];
...
, coeff :
&
&
...
int coeff[10];
(index) . 0
.
. coeff
5.3 )
.
.
JJ
II
125 223
coeff
0
...
A)
coeff
B)
&
&
5.3: ) coeff )
.
...
: .
:
_[]
JJ
II
. ,
:
coeff [0] = 4;
coeff [1] = 5;
coeff [2] = 3;
126 223
, coeff
5.3 ). , ,
:
...
&
&
...
.
JJ
II
127 223
5.2.2.
byte
C float grades[5]
grades 5 4 = 20 byte
4 byte 4 byte
(sizeof (float )) 5 .
.
5.4 byte grade
. 500.
...
Address
Memory Byte
&
&grades[0]
500
&
&grades[1]
504
...
.
&grades[2]
&grades[3]
&grades[4]
508
Memory
allocated for the
array
float grades[5]
512
JJ
II
128 223
516
grades[i ] &grade [i ] :
...
&
&
...
.
&grade [i ] = 500 + 4 i
.
.
.
JJ
II
129 223
5.2.3.
...
Address
Memory Byte
2
Byte allocated for char a[] =
Attack
1KB Memory
&
&a[0]
500
65 (A)
&a[1]
501
116 (t)
&a[2]
502
116(t)
&a[3]
503
97(a)
&a[4]
504
99(c)
&a[5]
505
107(k)
&a[6]
506
0(\0)
&
...
.
1022
JJ
II
130 223
1023
C, (string)
. char a[]=Attack sizeof (a ) = 7, 6
byte 0 \00 .
5.5 byte
byte ASCII byte
.
&a [0] = 500.
,
...
&
&
a [i ] = 65 + (5 a [i ] + 6) mod 26;
for (i = 0; a [i ]! =0 \00 ; i + +) <>;
...
.
ASCII .
for,
0 \00
i .
.
JJ
II
131 223
5.2.4.
...
&X[0][0]
Byte
500
Byte
&X[0][1]
504
&X[0][2]
508
&
&X[1][0]
512
int X[3][3]
&
&X[1][1]
516
&X [1][2]
520
...
.
&X[2][0]
524
&X[2][1]
528
&X[2][2]
JJ
II
532
132 223
,
.
. C
X , 3 3 int X [3][3];
. byte
5.6. ,
3 3 = 9 . int sizeof (int ) = 4
byte X 9 4 = 36 byte.
sizeof (int ) = 4 byte :
,
.
.
&X [0][1] (
0) 5.6 504.
.
...
&
&
...
.
JJ
II
133 223
for (i = 0; i < 3; i + +; )
for (j = 0; j < 3; j + +)a [i ]+ = x [i ][j];
...
i
j. :
for (j = 0; j < 3; j + +; )
for (i = 0; i < 3; i + +)a [j]+ = x [i ][j];
5.2.5.
( ).
.
byte
(allocated size).
( )
(effective size). ,
,
.
:
1. ,
int copyString(char s[], char t []);,
2.
, int multiplyMatrices(int s[][4], int t [][3]);
&
&
...
.
JJ
II
134 223
4
(
, call by value).
, .
:
...
-
1. ,
.
(call by reference).
&
&
...
2.
.
.
return. ,
.
JJ
II
135 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
...
#define N 20
void ConvertToUpperCase(char s[], char d[]);
int main()
{
char s[N+1], d[N+1], c;
int i=0;
&
&
...
.
JJ
II
136 223
5.7: .
.
ConvertToUpperCase s
. 5.7
. 20 ,
ConvertToUpperCase.
ConvertToUpperCase source .
0 \00 .
isalpha islower ()
< ctype.h > .
( < string.h >) toupper ()
.
37 0 \00 .
...
&
&
...
.
JJ
II
137 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
#include <stdio.h>
#include <stdlib.h>
#define N 5
float evaluate(int p[], float x)
{
int i;
float temp, result;
result = x*p[N];
for(i=N-1; i>0; i--)
{
temp = p[i] + result;
result = x*temp;
}
return (p[0]+result);
}
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
...
&
&
5.8:
. p
x N a0 , a1 , . . . , aN
(N + 1)
p. p x
Horner:
p(x ) = a0 + x (a1 + x (a2 + . . . + x (aN 1 + x (aN ))))
p q
N
...
.
JJ
II
138 223
:
...
d (x )
= p(x ) + q(x )
= (a0 + b0 ) + (a0 + b0 )x + . . . + (an + bn )x
(5.1)
5.8 :
1. evaluate () p
x. p(x ) .
Horner
x. 10
x p[n ]
.
2. addPolyonyms() p, q, w
p q w
p q.
3. printPolyonym () p
.
addPolyonyms() printPolyonym return. 5.9
a0 , a1 , . . . , aN p,
&
&
...
.
JJ
II
139 223
p.
x, x
. , b0 , b1 , . . . , bN
q,
q, p q
p q .
N
.
...
&
&
...
.
JJ
II
140 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
#include <stdio.h>
#include <stdlib.h>
#define N 5
float evaluate(int p[], float x);
void addPolyonyms(int p[], int q[], int w[]);
void printPolyonym(int p[]);
...
int main()
{
int p[N+1], q[N+1], w[N+1], i;
float x;
...
&
&
JJ
II
141 223
5.9: 5.8
5.3.
5.1 x
byte x.
8 byte x double
500 5.2.
x &, &x.
C byte .
(pointer)
byte. :
...
<_> *<_>;
&
&
.
. :
double x ;
double y;
...
double y 4 byte
y ( int )
NULL ( 0) stdlib.h.
. y
&y. x 8 byte (
)
&x. 5.10
x &x = 500 y &y = 1020
4 byte
NULL. y
JJ
II
142 223
byte .
...
Address
Memory Byte
Address
Memory Byte
Byte allocated
for
double x
1KB
Memory
&
&
&x
&x
500
500
501
501
502
502
503
503
...
504
504
505
505
506
506
507
507
1020
1021
1022
1023
1020
y = &x;
1022
1021
NULL
500
JJ
II
143 223
1023
5.10:
NULL
.
...
y = &x ;
&
&
y
x y = 500. y
5.10.
x y.
x y?
x y * y. ,
, :
...
.
JJ
II
144 223
((y) 5 + 3)/10
(x 5 + 3)/10
Address
Memory Byte
256
Address
x
Memory Byte
256
5,2
...
5,2
260
260
3,2
y
3,2
264
a1
264
NULL
256
b1
268
a1
b1
268
NULL
&
260
&
float x=5.2, y=3.2;
float *a1,*b1;
Address
a1 = &x;
b1 = &y;
Memory Byte
256
Address
x
...
Memory Byte
256
9.8
x
9.8
260
3,2
3,2
a1
264
260
a1
264
256
JJ
II
256
145 223
b1
268
260
b1
268
256
b1 = a1;
5.11:
. :
float x = 5.2, y = 3.2;
float a1, b1;
...
x y (float)
5.2 3.2
a1 b1 NULL.
5.11
. , 256 260
x y a1 b1
264 268. a1 b1:
&
&
a1 = &x ;
b1 = &y;
a1 b1 x y .
.
a1 =
(b1) 2 + 3.4;
...
.
(5.2)
JJ
II
y
b1 y x
a1 byte a1
x. 5.11
5.2 x 9.8.
,
146 223
b1 = a1;
b1 x
5.11.
...
&
&
...
.
JJ
II
147 223
5.4.
5.2.5
.
,
.
.
return .
...
&
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
#include <stdio.h>
int swap(int *, int *);
main(void)
{
int x=3,y=5;
5.12: swap .
.
.
5.12 x
y. x y 3 5
&
...
.
JJ
II
148 223
.
5.13
.
...
&
&
...
.
JJ
II
149 223
Address
Memory Byte
256
Address
x
256
260
Memory Byte
260
...
264
264
256
268
268
260
Stack Frame
272
temp
&
swap(&x, &y);
int temp;
&
Address
Memory Byte
256
Address
x
256
260
Memory Byte
260
...
.
264
256
b Stack Frame
268
264
256
272
260
temp
3
268
260
272
Stack Frame
temp
JJ
II
150 223
temp = *a;
*a = *b;
*b = temp;
5.13: swap
swap
. a
b . 2
, int,
,
. swap
temp
5.13. ,
x y (&x &y ) a b
. 5.13 a b
256 260 x y.
temp
a.
b, y, a,
x. 16 17
5.13 x
y. temp
b, y,
.
5.13 y x.
5.5.
5.2
.
...
&
&
...
.
JJ
II
151 223
.
, :
...
float grades[5];
float p;
&
5 float 20 byte &grade [0] 500 &grade [1] 504, &grade [2] 508,
&grade [3] 512 &grade [4] 516. , C
&
...
.
grades &grades[0]
.
p float &p 1022
NULL. 5.14
.
JJ
II
152 223
Address
0
Memory Byte
Address
0
Memory Byte
...
&grades[0]
500
&grades[0]
500
&grades[1]
504
&grades[1]
504
&grades[2]
float grades[5]
508
&grades[2]
&
508
&
&grades[3]
512
&grades[3]
512
&grades[4]
&grades[4]
516
...
516
.
&p
&p
1022
1022
NULL
float *p;
p = grades;
500
5.14:
JJ
II
153 223
p = grades;
...
grades p
5.14. p
grades . ,
p
j- p[j].
+ - .
, p + j
grades[j]. , p,
sizeof (float )
float
. grades[j]
p (p + j).
&
&
...
.
, p = grades
p :
JJ
II
1. p[j]
154 223
2. (p + j)
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define N 20
void ConvertToUpperCase(char *s, char *d);
int main()
{
char s[N+1], d[N+1], c;
int i=0;
...
5.15:
&
&
...
.
JJ
II
155 223
5.7 ConvertToUpperCase .
. 5.15
ConvertToUpperCase
source dest. .
.
. p1
grades[2] p2 grades[0]
p1 p2 2 2 2
. p + + *
p p
(p + +).
4 void
.
...
&
&
...
.
void p;
p .
. q
int w double, p = q
p p = w p double.
JJ
II
156 223
5.6.
:
1. :
.
2. : ,
(stack)
.
C
.
,
(heap).
C
stdlib.h:
1. void *calloc(size_t, size_t)
( ) byte ( ). byte
, 0 .
, NULL.
2. void *malloc(size_t): byte
.
NULL.
...
&
&
...
.
JJ
II
157 223
malloc calloc .
calloc .
,
malloc ,
.
malloc calloc
(cast). calloc:
...
int a, n ;
a = calloc (n, sizeof (int ));
a = (int ) calloc (n, sizeof (int ));
int
cast .
.
malloc calloc
NULL.
NULL
malloc calloc NULL .
calloc:
&
&
...
.
JJ
II
158 223
if(a==NULL){
printf( : byte a);
exit (1);
}
a NULL
exit (1) .
, .
...
&
&
...
( ) .
free . free
malloc calloc. free (heap)
.
malloc calloc.
.
free
.
JJ
II
159 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
#include <stdio.h>
#include <stdlib.h>
void fillData(int *a, int n);
main(void)
{
int *a, n;
...
&
&
free(a);
...
5.16:
.
. (embedded
systems) , . .
malloc
calloc . . 5.16
. , scanf
JJ
II
160 223
n.
5.17
9 10, n 5
a NULL.
...
&
&
...
.
JJ
II
161 223
Address
Stack
Heap
n
256
1024
...
Stack Frame of
main()
260
a
NULL
264
268
int *a, n;
scanf(%d, &n);
&
&
Address
Stack
256
1047
Stack Frame of
main()
260
1024
Heap
...
1024
a1
264
b1
268
JJ
II
162 223
1047
5.17:
...
&
&
...
.
N ,
(min), (max)
(mean)
.
5.18 statistics
: N
JJ
II
163 223
#include <stdlib.h>
#include <mem.h>
void statistics(int N, float *mean, float *min, float *max)
{
int i;
float *p;
p = (float *) malloc(N*sizeof(float));
for(i=0; i<N; i++)
{
printf("Dvse ton %d o arithmo=", i+1);
scanf("%f", &p[i]);
printf("\n");
}
for(i=0; i<N; i++) *mean += p[i];
*mean /= N;
*min=p[0]; *max=p[0];
for(i=0; i<N; i++)
{
*min = (*min <= p[i]) ? *min : p[i];
*max = (*max >= p[i]) ? *max : p[i];
}
free(p);
}
5.18:
...
&
&
...
.
JJ
II
164 223
17 22 24 30
.
. ,
*. ,
. ,
*. 31
malloc. 5.18
18 21 26 30
N .
...
5.7.
&
&
main
.
main . C main
:
...
.
int argc: .
char argv[]: strings
.
. , .
JJ
II
165 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
#include <stdio.h>
#include <stdlib.h>
...
&
&
...
.
5.19: convertStringToNumber
. Dir
Dos ls Unix
d f
. convertStringToNumber
:
JJ
II
166 223
...
&
&
...
.
JJ
II
1. convertStringToNumber
2. convertStringToNumber f 534.2
167 223
3. convertStringToNumber d 534.2
5.8.
2 (string constant)
5.2.3
C .
.
...
-
&
&
...
.
5.1: C.
ANSI C . 5.1
string.h.
strlen . 5.20
strlen .
s + +
.
JJ
II
168 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
...
&
&
...
5.20: string.h
strcat strncat :
strcat s2 s1, strncat n
s2. 5.20 strcat
temp
s1 ( 22)
s2 ( 23).
s1.
strcmp s1 s2
JJ
II
169 223
0 , > 0 s1
s2 < 0 . strncmp n
s1 s2.
, strcpy strncpy
: strcpy s2 s1
strncat n s2. 5.20
strcpy temp
s1 ( 11) (
13) s2 temp s1. s1.
s1 s2.
...
&
&
5.9.
5.9.1 .
?
int k = 3;
int u = &k ;
printf (%p %d %d %d \n,u, u + 4, 4 &u + 2, 3 (u (u 1)));
...
.
JJ
II
170 223
5.9.2 5.21
calculate (). ?
...
&
&
...
5.21: 5.9.2
5.9.3 reverse .
Hello World ldroW olleH.
main, :
char input [] = abcdefghijklmnopqrstuvwxyz ;
printf (%s\n, reverse (input ));
5.9.4 mean () stdDeviation ()
double
JJ
II
171 223
.
,
,
,
.
5.9.5
N N .
:
1.
TRUE FALSE .
...
&
&
2.
.
...
3. TRUE
FALSE .
JJ
II
172 223
...
,
.
, . C (structure)
. :
struct point {
float x;
float y;
};
JJ
II
173 223
...
(5.5, 6.0) :
p1.x = 5.5;
p1.y = 6.0;
p1 p2
:
p1 = p2;
p2 p1.
.
, .
, -
, -
. , :
JJ
II
174 223
struct date {
int day, month, year;
char d_name[10], m_name[10];
};
...
, :
struct{
int day, month, year;
char d_name[10], m_name[10];
}yesterday, today, tomorrow;
tag .
tag
.
.
.
struct complex {
float re, im;
};
struct tag complex :
JJ
II
175 223
struct complex x, y;
tag
typedef .
:
typedef struct complex {
float re, im;
} complex;
...
,
complex. :
complex x, y;
tag :
typedef struct{
float re, im;
} complex;
complex x, y;
?
. ,
(5.0, 6.0) :
JJ
II
176 223
...
,
6.1.
JJ
II
.
,
.
return
.
177 223
point e;
e = updatePoint(e);
point updatePoint(point d) {
return d;
}
updatePoint e d.
return .
- - e
updatePoint.
...
,
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
#include <stdio.h>
typedef struct {
float x;
float y;
} point;
6.1: .
JJ
II
178 223
6.1 .
2 5 point.
p1 p2 (3.0, 5.2) (4.0, 5.2) .
AddPoint point
a
. target point
( )
printf 25.
...
,
6.2.
, ,
.
:
typedef struct{
char last_name[20];
char first_name[20], grade;
int student_id;
} student;
student 20 ,
.
41
JJ
II
179 223
student.
. C,
.
:
student *st;
...
,
st student.
:
typedef struct{
char last_name[20];
char first_name[20], grade;
int student_id;
} *student;
student st;
student
.
student, .
(4 bytes) .
st
malloc ( ):
st = (student) malloc(sizeof(*st));
if(st == NULL) {
printf(there is no available memory for pointer st);
exit(1);
}
JJ
II
180 223
...
,
:
*(st.grade);
.
grade :
(*st).grade;
C
( ) [ ]
, :
st grade (st ).grade
JJ
II
181 223
6.3.
.
64 bit
. .
C
.
:
.
...
,
6.3.1.
.
- - .
(LIFO (Last In First Out)).
pop push.
JJ
II
182 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
#include <stdio.h>
#include <stdlib.h>
#define MAXVAL 100
#define EMPTY 0
#define FULL MAXVAL
typedef struct{
int sp;
double val[MAXVAL];
} stack;
typedef enum {FALSE, TRUE} bool;
void push(double f, stack *st)
{
if (st -> sp < MAXVAL)st->val[st->sp++] = f;
else printf("error: stack full, cant push %g\n", f);
}
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
...
,
6.2: Module
stack:
JJ
II
183 223
typedef struct{
double val[MAXVAL];
int sp;
} stack;
val
, sp
. stack
push pop
.
reset isEmpty
TRUE isFull
TRUE .
6.2 module .
module st stack.
st .
, 12 bool .
...
,
6.3.2.
(stack),
(FIFO, First In First Out), .
.
JJ
II
184 223
data
next
struct linkedList {
int data;
struct linkedList *next;
};
...
,
struct linkedList x, y;
x.data = 5;
y.data = 6;
x.next = y.next = NULL;
NULL
NULL
x.next = &y;
JJ
II
185 223
NULL
6.3:
:
1. (head) ,
...
2.
,
3. (tail) .
6.3 tag linkedList :
linkedList. x y linkedList data 5 6
next NULL .
x y :
x.next = &y;
next x y.
:
1. .
2. .
3. .
JJ
II
4. .
5. .
6. .
7. .
186 223
4.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedList {
int data;
struct linkedList *next;
} element;
typedef element *link;
int countList(link head)
{
int count=0;
for(; head!=NULL; head=head->next) ++count;
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
...
,
free(p);
}
return count;
}
void printList(link head)
{
if (head == NULL) printf("NULL\n");
else{
printf("%d ->", head-> data);
printList(head->next);
}
}
6.4: Module
JJ
II
187 223
6.4 module
. : element
link
element.
.
...
,
countList
element
. head ,
, (
)?
JJ
II
188 223
printList .
.
Insert Operation
a1
6
...
NULL
b->next = a1->next;
a1->next = b;
a1
Delete Operation
a1
.
6
p = a1->next;
a1->next = p->next;
a1
JJ
II
189 223
6.5:
insert
a1
b
. 6.5
.
, delete a1
. 6.5
. free.
...
,
6.4.
6.4.1 .
.
structstudent {
int id ;
char firstName [20];
char familyName [20];
char grade ;
} tmp, w = &tmp;
tmp.grade = C;
tmp.id = 1040
tmp.familyName = Petrou;
JJ
II
190 223
tmp.grade
tmp.familyName
(w).id
w familyName + 4
(w familyName + 3)
...
,
6.4.3 :
1. .
2. :
,
.
:
N
,
N N ,
,
JJ
II
191 223
.
6.4.4
:
...
,
head
NULL
NULL
6.6:
6.6 . insert delete
.
6.4.5 ( queue) FIFO (First In First Out)
6.7 size
JJ
II
192 223
, front tail
.
...
,
size
Head
tail
NULL
6.7:
.
:
1. ,
2. ,
3.
4. TRUE FALSE.
5. TRUE FALSE.
JJ
II
193 223
...
/ /
.
(devices). , .
:
: .
JJ
II
: .
: .
C
(streams).
(
byte ).
194 223
(input stream).
(output stream).
. ,
C
byte
.
.
C :
1. (text), .
255 .
...
/ /
2. (binary), ( ).
.
, byte ASCII . ,
65 A
65. .
.
ANSI C standard input / output files:
stdout: (default) , ,
JJ
II
195 223
stdin: (default) , ,
stderr: (default) ,
.
,
< stdlib.h > :
scanf () stdin.
printf ()
stdout.
...
/ /
7.1.
/ /
,
stdin
,
stdout.
?
.
(text stream) (binary stream):
0 255
JJ
II
196 223
...
r
w
r+
w+
a+
.
NULL.
.
. ,
.
.
. ,
.
.
. ,
.
.
. ,
.
.
. ,
.
7.1:
/ /
JJ
II
197 223
. C (string). command line Windows XP
...
/ /
7.1
. .
b .
w+b
7.1. fopen
FILE < stdio.h >
:
JJ
II
198 223
int _bufsiz;
char *_tmpfname;
}FILE;
...
.
/
. /
.
fopen ()
FILE.
fclose
:
int fclose(FILE *fp);
fp . 0
1 . (
main () exit ()) .
. ,
byte
,
.
.
(.. , )
, .
/ /
JJ
II
199 223
fflush :
...
.
EOF stdio.h 1.
EOF
. ,
1 feof ()
:
/ /
feof 0 -
.
7.2.
/ . :
(getc (), )
JJ
II
200 223
(scanf (), )
.
:
(buffered)
,
stdin. ,
.
(unbuffered)
stdin .
,
.
scanf .
, :
(putc, .)
(printf , .)
: .
. stderr
.
.
...
/ /
JJ
II
201 223
7.2.1.
.
.
C.
/
:
...
/ /
/ :
char *fgets(char *buf, int N, FILE *fp):
fp buf
\n N 1 . N
. buf NULL
EOF .
int fputs(char *buf, FILE *fp): buf fp.
\0 . \n
\0. EOF .
JJ
II
202 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
#include <stdio.h>
main(int argc, char *argv[])
{
FILE *fp=NULL;
char c;
...
fp = fopen(argv[1], "r");
/ /
7.1: / / .
7.1 . readFile
Command ( Windows
, cmd
) :
readFile <filename>
< filename > .
, argc main 2
argv[0] =readFile argv[1] =< filename >. 7
argv[1]
fp FILE.
9
fp EOF .
.
JJ
II
203 223
7.2.2.
/
...
. .
spreadsheets
C.
/ /
fprintf () printf ()
:
fscanf () scanf ()
:
JJ
II
204 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
#include <stdlib.h>
#include <stdio.h>
...
/ /
fclose(fp1);
fclose(fp2);
7.2: .
JJ
II
205 223
7.2 .
addNumbers
Command ( Windows ,
cmd
) :
addNumbers <InputFile> <OutputFile>
...
/ /
JJ
II
cat <OutputFile>
206 223
7.2.3.
C. , .
.
C.
fwrite () :
int fwrite(void *buf, int size, int count, FILE *fp)
...
/ /
fp , buf ,
size byte count
.
. (buf )
20 :
JJ
II
fp , buf ,
size byte count
.
.
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
#include <stdio.h>
#include <stdlib.h>
...
int main()
{
int i, x, outBuffer[30], inBuffer[30];
FILE *fp;
/ /
rewind(fp);
if (fread(inBuffer, sizeof(int), 30, fp) != 30)
{
fprintf(stderr, "Error reading file.");
exit(1);
}
JJ
II
208 223
7.3: / / .
30
ai = 2 i, i N .
30
fwrite. 7.3 . 8 9
outBuffer.
...
11 15 direct
(w+b) fopen
fp.
fp. NULL
.
/ /
fwrite () 17
outBuffer fp.
30 , .
FILE
.
0.
fwrite () 30 sizeof (int ) = 30 4 = 120 byte
( int 4 byte) .
120
fwrite ().
17
0.
rewind () 23
JJ
II
209 223
rewind () .
fread () 25 30
inBuffer. 30
( ) .
, 120
.
, 31 32
outBuffer inBuffer
.
...
/ /
7.3.
7.3 FILE
. , byte
. 0
.
/ /
,
byte .
. C
JJ
II
210 223
. .
. 150
( ). ,
, 150
.
.
stdio.h :
...
/ /
JJ
II
211 223
.
MS DOS UNIX mode .
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
#include <stdio.h>
#include <stdlib.h>
main(int argc, char *argv[])
{
FILE *fp=NULL;
char c;
...
/ /
fp = fopen(argv[1], "rb");
fseek(fp, 0, SEEK_END);
fseek(fp, -1, SEEK_CUR);
while(ftell(fp) > 0){
c=getc(fp);
putchar(c);
fseek(fp, -2, SEEK_CUR);
}
return 0;
}
JJ
II
7.4: .
212 223
.
.
7.4
reverseRead.c.
, Command (
Windows , cmd )
:
reverseRead <textFile>
...
, main :
argv[0]=reverseRead argv[1]=<textFile>. ( 8)
. fseek 9,
. fseek 10
byte
.
while, ftell .
FALSE while
. while
byte
. fseek 15 2 byte
byte
. while
ftell () 0 .
/ /
JJ
II
213 223
7.4.
createRand
:
N 0 . . . RAND_MAX
filename.
seed .
7
argv main. ,
:
/ /
argv[0]=createRand
argv[1]=-o
argv[2]=filename
argv[3]=-s
argv[4]=seed
argv[5]=-n
argv[6]=N
6
6 .
6
argv[2]
. 7.5
.
JJ
II
214 223
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
#include <stdlib.h>
#include <stdio.h>
void createRandNum(int, int *, int);
int main(int argc, char *argv[])
{
FILE *fp=NULL;
int *randNum, size, seed, i;
if (--argc == 6) {
//Opens the file as binary in read/write mode
fp = fopen(argv[2], "w+b");
//Converts strings argv[6] and argv[4] into size seed respectively.
size = atoi(argv[6]);
seed = atoi(argv[4]);
//Dynamic Array of size Elements
randNum = (int *) malloc(size * sizeof(int));
//Random Number Generation
createRandNum(seed, randNum, size);
//Writes Dynamic Array into file fp
fwrite(randNum, sizeof(int), size, fp);
}
else {
printf("The syntax of the command:");
printf("createRand -o filename -s <seed> -n number_of_Rands\n");
return -1;
}
fclose(fp);
return 0;
...
/ /
JJ
II
}
215 223
argv[4] argv[6]
atoi
seed
C.
argv[4] argv[6]
-s -n .
malloc randNum N .
.
createRandNum : seed ,
.
C srand (),
stdlib.h :
int srand(unsigned int seed);
rand () stdlib.h, 0 RAND_MAX (
),
.
createRandNum () main () .
fwrite () C.
...
/ /
JJ
II
216 223
7.5.
7.5.1 fread ()
. ,
...
/ /
7.5.4
: N int
, N double.
4x 3 + 5x + 4
x .
.
7.5.5 N > 10
double : N int
, N
double. 10
f (x ) = 3x 2 + 6.
.
JJ
II
217 223
JJ
II
218 223
JJ
II
219 223
: 1 < k < n n %k == 0 n .
...
4.8.2
&
C
...
.
JJ
II
220 223
: fread fwrite.
2
7.5.4
...
/ /
JJ
II
221 223
: 10 (rand ()%N ) + 1,
srand C.
1 . . . N fseek
.
2
7.5.5
...
/ /
JJ
II
222 223