You are on page 1of 71

2

( ).

(machine language).
0 1 (01-patterns).
assemblers mnemonics
CPU.

Basic Fortran C (
C++ Visual ).
(keywords)
.
interpreters compilers.
interpreter
.
compiler
object (
*.obj). linker
object (
*.exe). .
:
(RAM),
.
,
,
.
C .
(
)
( platforms
CPUs) .
1

H C/C++
,
. 30
.

(micro-controllers)
1 (operating systems) Windows XP
Linux Unix.
C/C++
.
.
.

Visual Basic compiled interpreted



internet (VbScript).
Java
internet (JavaScript) . Java


( internet
C).
C C++ ?
C++ C. C++
ANSI C (American National Standards Institute)
(object oriented) ( ++ ).
Bjarne Stourstrup
1


(hardware) (software)

.

, C++
,
C.

C++, .
4 object oriented
:
1.
(encapsulation units).

.
2.
( data hidding)

.
3.

(inheritance and reuse).


.
4. (function and class polymorphism)

.
C
(functions) 3
. C++
(classes).
C C++ block
,

.

.


C/C++:

run time errors ( )


3
1. , 0.
2. (overflow)

.
3. .
(
) .

C.

.
01
1 #include <stdio.h>
2 int main( )
3{
4 printf( "Hello world\n" );
5 return 0;
6}
() (

simple.cpp) :


simple.cpp
1 : #include <stdio.h>
#include <stdio.h>,
(header file) stdio.h
(standard I/O library)
, folder INCLUDE compiler
C++. ,
printf( ),
. #include <stdio.h>
.
# -
(pre-processor) C, o (
C/C++)
6

. stdio.h
( )
(standard in) (standard out).
#include <stdio.h>,
-

. < .> stdio.h folder include.
C\ C++
stdio.h, math.h, time.h, string.h, iostream.h (
stdio) .
2 : int main()
int main( )
.
main int.

.
:
main ,

C.
C (arguments)
main( ) = main(void).
C

void ().
main
(: int integer = )
3 : { ( )
3 {,
}, 6 .

.
4 : printf("Hello world\n");
(print function) printf( ) .
.
7

( ),
, . \n
(the newline character)
.
5 : return 0
return 0 0
main.
:
C

.

main( ). main( )
.
( ) -
- (sub-programs modules)
.
main
( Windows).
CPU
main( ).
01b
void 01 :
#include <stdio.h>
void main()
{
printf("Hello world!\n");
return ;
}
: main .
return. main
void.

int C++ 01 :
01c
#include <iostream.h>
int main()
{
cout << Hello world!" << endl;
return ( 1 );
}
:
printf( ) cout( ) header
file: iostream.h
<< (insertion character)
endl newline character
\n.
01
void C++
01.
int main
01?
C++?

01
.
(
).
,

.
(variables).


double a1;

8 (8 bytes) a1
(
) .
( 4 bytes)
:
float a1;
.
RAM
(
) .
:
.
(space) .
.
( C/C++
case sensitive )

.

area embadon.
1xy ab cd .
area Area .

02:
1 #include <stdio.h>
2 int main()
10

3 {
4
5
6
7
8 }

double a1;
a1=5.78;
printf(O arithmos einai %lf, a1);
return 0;


01.
,

. .
4 : double a1;
.
(

)

. a1
double.
5 : a1=5.78;
a1 5.78.

. C 8
bytes a1 ,
a1
.
6 : printf(O arithmos einai %lf, a1);
printf

1.
: O arithmos einai , ,
%lf.
%lf
, ,
, a1.
% (percent)
lf (long float).

11

, ..
, 10 (),
. double
, %lf.
:
1. = :
a1=5.78
a1 5.78 ( = ) (
assignment operator). :
double a1;
a1=5.78;
a1=a1-1.78; ( a1 -= 1.78)
printf(O arithmos einai %lf, a1);
a1 (
) ( ) 1.78.
O arithmos einai 4.0
2.
C
. 4 5
02 ,
,
double a1=5.78;

. ,
,
C/C++,
(format fields) ( printf
) ( scanf
).

12

int, short int

%d

long int

%ld

float

%f, %e (
)

double

%lf, %le (
)

char

%c



;
.



.
(
, ,
)

(
, )


:
03:
1 #include <stdio.h>
2 int main()
3 {
4
double a1,a2;
5
long int Z;
6
a1 = 5.78;
7
a2 = 6.97;
8
Z = 1000;
9
printf(Oi arithmoi einai: a1= %lf, a2= %lf, %ld \n, a1, a2, a3);
10
return 0;
11 }
13

4 5
4 5 .
2 double

( , ).
.
6 8
a1, a2 Z.
9
9 03
printf
.
printf( )
. 4
:

: (Oi arithmoi einai:)


.
,
.

14



printf( ) \n.
9 :
printf("Oi arithmoi einai:\n a1 = %lf\n a2 = %lf\n Z = %lf\n", a1, a2, Z);

,
.

.
.

\f

\n

\r

\t

(tab)

02:
03 void
.

printf( )
. ,
printf.

15

04:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <stdio.h>
void main()
{
double D=1456.7867697685;
printf("O arithmos einai: %10.5lf \n", D);
printf("O arithmos einai: %10.4lf \n", D);
printf("O arithmos einai: %10.3lf \n", D);
printf("O arithmos einai: %10.2lf \n", D);
printf("O arithmos einai: %10.1lf \n", D);
printf("O arithmos einai: %10.0lf \n", D);
printf("O arithmos einai: %20.10lf \n", D);
printf(" \n");
printf("O arithmos einai: %10.5le \n", D);
printf("O arithmos einai: %10.4le \n", D);
printf("O arithmos einai: %10.3le \n", D);
printf("O arithmos einai: %10.2le \n", D);
printf("O arithmos einai: %10.1le \n", D);
printf("O arithmos einai: %10.0le \n", D);
printf("O arithmos einai: %20.10le \n", D);
printf(" \n");
return;
}

5 10
%lf .
%10.lf =5,4,3,2,1,0.
10 (
, )
=5,4,3,2,1,0 .
( ),
11
%lf %10.20lf
10
20
12 20
printf ( )
\n.
.

16

13 19
5 10
le (long exponential)
()

RAM sizeof( )
sizeof()

short int, int, long int
05:
01 #include <iostream.h>
02 void main()
03 {
04
short I; int J; long K;
05
cout <<"size of short = " << sizeof(I)<< endl;
06
cout <<"size of int = " << sizeof(J) << endl;
07
cout <<"size of long = " << sizeof(K) << endl;
17

08
09 }

return;

, short int, int long int


RAM 2, 4 4 bytes .
long int
int C
( system
dependent).
03:

char, float double


/.
/ (
) .

a - b

a + b

18

a * b

a / b

a b = a % b

Pow ( . , . )

double a double b = Pow(a , b )


( #include math.h)


1. sum x=14.2, y=12.3,
z=3.6.
2. root
3. intpart root.
4. remainder
intpart J=3

05:
01 #include <stdio.h>
02 #include <math.h>
03 void main()
04 {
05
double x = 14.2, y = 12.3, z = 3.6;
06
double sum, root;
07
int intpart, J=3, remainder;
08
09
10

sum = x + y + z;
printf("Oi arithmoi einai: x= %lf y = %lf z = %lf \n", x, y, z);
printf("To athroisma einai: sum = %lf\n", sum);

11
12

root=pow(sum, 0.5);
printf("H riza einai: root = %10.8lf\n", root);

13
14
15
16

intpart= (int) root;


remainder = intpart % J;
printf("To akeraio meros tis rizas einai: intpart = %d\n", intpart);
printf("To ypoloipo tis diairesis toy intpart me to J = %d einai : %d\n", intpart,
remainder);

17
18 }

return ;

5 7

19

x, y, z J. sum, root remainder


.
8 10
x, y z sum
.
11 2
root = pow(sum, 0.5) sum
pow( ). header file:
math.h.
2.
13 16
(int) ( . ) 2
. (int) (3.14159) = 3. .
remainder intpart J.
.

1) 05 cout( ),
09 :
cout << "Oi arithmoi einai: x=" << x << "y =" << y << "z =" << z << endl;

2) ?
#include <iostream.h>

20

void main(void)
{
int I=10, J=4;
cout <<"I=" << I << " J=" << J<< endl;
cout <<"I/J=" << I/J << " (float) I/J=" << (float) I/J << endl;
return ;
}

5
. ,
.
06:
01 #include <stdio.h>
02 int main()
03 {
04
double a1=4.2, a2=2.3, a3=3.6, a4=2.0, a5=7.2, sum, average;
05
sum = a1 + a2 +a3 + a4 + a5;
06
average = sum/5;
07
printf(Oi arithmoi einai: %lf %lf %lf %lf %lf\n, a1,a2,a3,a4,a5);
08
printf(To auroisma einai: %lf\n, sum);
09
printf(O mesos oros einai: %lf\n, average);
10
return (0);
11 }

4.
05
sum
06 5
average.
07 a1, a2,
a3, a4, a5.
.
06,
sum
06b
01 #include <stdio.h>
02 int main()
03 {
04
double a1=4.2, a2=2.3, a3=3.6, a4=2.0, a5=7.2, average;
05
average = a1 + a2 +a3 + a4 + a5;

21

06
07
08
09
10 }

average = average /5;


printf(Oi arithmoi einai: %lf %lf %lf %lf
a1,a2,a3,a4,a5);
printf(O mesos oros einai: %lf\n, average);
return (0);

%lf\n,

05 06
average = a1 + a2 +a3 + a4 + a5;
average = average /5; ( average /= 5)
average (
) .

a1,,a5.


, ,

. .. *(+)
.. *+.
+
,

. /
:
1. .
2. , , .
3. .
(,)
(,) .
.. int X1=0, X2=1;
(;) .
average =( a1=4.2, a2=2.3, a3=3.6, (a1 + a2 + a3)/3 );

22

average
, (a1 + a2 + a3)/3.
group :
a1=4.2
a2=2.3;
a3=3.6;
average =(a1 + a2 + a3)/3;


.
a1=( a2=2.3, a3=3.6);

a1 a3.
a1=a3=3.6; a1=(a3=3.6);

(, ,
, ).
, .
, <stdio.h>
,
, ,
, . <math.h>
<stdio.h> 8.
4
<math.h>.
C/C++

abs(x)

sqrt(x)

pow(x,y)

, xy

cos(x)

sin(x)

tan(x)

exp(x)

ex

log(x)

ln(x)

log10(x)

log10(x)
23

asin(x)

acos(x)

atan(x)

07:
01 /* ABS.CPP: This program computes and displays
02 * the absolute values of several numbers.
03 */
04 #include
05 #include
06 #include
07 void main(
08 {
09
int
10
long
11
double

<stdio.h>
<math.h>
<stdlib.h>
void )
ix = -4, iy;
lx = -41567L, ly;
dx = -3.141593, dy;

12
13

iy = abs( ix );
printf( "The absolute value of %d is %d\n", ix, iy);

14
15

ly = labs( lx );
printf( "The absolute value of %ld is %ld\n", lx, ly);

16
17
18 }

dy = fabs( dx );
printf( "The absolute value of %lf is %lf\n", dx, dy );

Output
The absolute value of -4 is 4
The absolute value of -41567 is 41567
The absolute value of -3.141593 is 3.141593

01 03

. /*
*/ compiler.
12 17

abs, labs fabs int, long int float (


double) .

.
24


:



() ,
.
.
:
area = base*height/2

, . . :
height = sqrt(base*base - (base/2)*(base/2))
:
base height

. , sqrt( )
C/C++ <math.h>.
08:
01 /* equilateral.CPP:
02 *
03 * .
04 */
05
06
07
08
09

#include <stdio.h>
#include <math.h>
int main()
{
double base, height, area = 1.5;

10
height = sqrt(base *base - (base/2)*(base/2));
area = base*height/2.0;
11
12

printf(to mhkos ths pleyras einai: %lf \n, base);


printf(To embado einai: %lf\n, area);

25

13
}

return 0;

8
printf
. :
, sqrt,
<stdio.h> , ,
<math.h>.
2 #include
<math.h>.
( 5).
( base) .
.
8
. , ..
7,
height = sqrt(base *base - (base/2)*(base/2));

,
height = sqrt(pow(base,2) - pow(base/2,2));

pow(x,y) 4
x y. pow
height :
height = pow (pow(base,2) - pow(base/2,2), 0.5);



<math.h>. ,

,
<math.h>.

26


C 2
a b.

09:
#include <stdio.h>
int main()
{
int a=10, b=100, temp;
printf( a=%d b=%d\n, a, b);
temp=b;
b=a;
a=temp;
printf( a=%d b=%d\n, a, b);
return 0;
}


(temporary) temp. H temp=b;
b. H b=a;
a b. a=temp; a
b.

:
a=(temp=b, b=a, temp);

scanf( )

:
()
. .
,
, . ,
27

,
EXE.

, , EXE
.
EXE
.
,
.
:
scanf.
9, , 8,

,
.
9:
01 #include <stdio.h>
02 #include <math.h>
03 int main()
04 {
05
double base, height,surface;
06
printf(\n Dwste thn pleura tou isopleurou trigwnou \n);
07
scanf(%lf,&base);
08
height = sqrt(base *base - (base/2)*(base/2));
09
surface = base*height/2.0;
10
printf(to mhkos ths pleyras einai: %lf \n, base);
11
printf(To embado einai: %lf\n, surface);
12
return 0;
13 }

9 8 6
7. scanf.
6 printf.
,
: Dwste thn pleura tou isopleurou trigwnou /


. ,
scanf 7,
base.

28


.


.

.
scanf( ) base
RAM,
base. scanf
(& =address operator) scanf(%lf,&base);


, ,
.

scanf ,
10, .
10:
#include <stdio.h>
#include <math.h>
int main()
{
double pleyra1,pleyra2,ypoteinoysa;
printf(\n Dwste to mhkos ths 1hs kai 2hs katheths pleuras tou orthogwnioy
trigwnou\n);
scanf(%lf %lf,&pleyra1,&pleyra2);
ypoteinoysa = sqrt(pleyra1*pleyra1 + pleyra2*pleyra2);
printf(To mhkos kathe mias apo tis kathetes pleyres einai: %lf kai %lf\n,
pleyra1,pleyra2);
printf(H ypoteinoysa einai: %lf\n, ypoteinoysa);
return 0;
}

10 , ,

.
scanf
29

.
7 %lf
. /
2 .

, ,
%lf
.

printf( ) scanf( )
C

. sqrt( )
.
printf( ) .
.
printf( ) .
printf( )
:
int rvp;
( rvp= return value printf) :
rvp = printf("x=%lf y=%lf z=%lf\n", x, y, z);
printf( )
, e printf(
) ( +1
\n) rvp.
scanf( ) .
.
( scanf( )
0.

scanf( )

int rvs;
30

:
rvs=scanf("%lf %lf %lf", &x, &y, &z);

scanf( )
3 x, y, z ( ),
e scanf ( ) rvp (
rvs 3).
11
#include <stdio.h>
void main()
{
double x, y, z;
int rvp, rvs;
printf("Input 3 numbers\n");
rvs=scanf("%lf %lf %lf", &x, &y, &z);
printf("The returned value is: %d\n", rvs);
rvp=printf("x=%lf y=%lf z=%lf\n", x, y, z);
printf("The returned value is: %d\n", rvp);
return;
}

31




CPU
.

.
.
(platforms)
,
.
: .
( )
2 0 1 ( bits).
.



. :

103
5

102
7

101
3

100
2



23
1

22
0

21
1

20
1


1 23 + 0 22 + 1 21 + 1 20 =11= (1011)2

0.4728
101
102
4
7

103
2

104
8

0.9375

21
22
1
1

23
0

5 103 + 7 102 + 3 101 + 2 100 =5732

4 101 + 7 102 + 2 103 + 8 104 =0.4728


1 21 + 1 22 + 0 23 + 1 24 = 0.9375

24
1

= ( 0.1101)2

32


(most significant digit). 2
bit
.
-
-
:
0,1,K , 1}
( n1 L1 0 ) = n1 n1 + L + 1 1 + 0 0 , . i {14
4244
3

-
:
0,1,K , 1}
( 0.1 L n1 n ) = 1 1 + L + n n , . i {14
4244
3

2, 8, 10, 16 , ,
.
( =16 ), 5
=11,...,F=15 i {0,1,K ,9, A, B, C , D, E , F }

(i) 107 :
107 = 2 53 + 1
= 2 ( 2 26 + 1) + 1 = 22 26 + 1 21 + 1 20

= 22 ( 2 13 + 0 ) + 1 21 + 1 20 = 23 13 + 0 22 + 1 21 + 1 20
= 23 ( 2 6 + 1) + 0 22 + 1 21 + 1 20 = 2 4 6 + 1 23 + 0 22 + 1 21 + 1 20
= 24 ( 2 3 + 0 ) + 1 23 + 0 22 + 1 21 + 1 20 = 25 3 + 0 24 + 1 23 + 0 22 + 1 21 + 1 20

= 25 (2 1 + 1) + 0 24 + 1 23 + 0 22 + 1 21 + 1 20 = 1 26 + 1 25 + 0 24 + 1 23 + 0 22 + 1 21 + 1 20
= (1101011 )2

(ii) 107, 1007

33

107 = 6 16 + 11 = 6 161 + B 160 = ( 6 B )16


1007 = 16 62 + F
= 16 (16 3 + E ) + F = 3 162 + E 161 + F 160 = ( 3EF )16

(bit patterns)
(
)
.
(.. A,K , Z 0,1,K ,9 )
(.. \n).
set
.
C/C++
ASCII (American Standard Code for
Information Interchange) set .
1 byte (= 8 bits) 28 =256
0
255.
ASCII
0,...,9 48,...,57. A,
,Z 65,...,90 a, ,z 97,...,122

34

getchar() putchar()
C/C++
getchar( ) putchar( )
.
getchar()
ASCII
.
putchar( )
.

( ASCII )

35

12 Version 01
01 #include <stdio.h>
02 int main()
03 {
04
char ch;
05
ch=getchar();
06
ch=ch+1;
07
putchar(ch);
08
return 0;
09 }

05
05
.
enter. ch
ASCII .
W ch
87.
06
ch 1.
07
ch
88. X.
12 Version 02
01 #include <stdio.h>
02 int main( )
03 {
04
char ch=getchar( );
05
ch++;
06
putchar(ch);
07
return 0;
08 }

04
ch getchar( ).
05
ch 1.

36

12 Version 03
01 #include <stdio.h>
02 int main( )
03 {
04
char ch=getchar( )
05
putchar(++ch);
06
return 0;
07 }

05
05 2 :
c=c+1;
putchar(ch);

++ch - ch.
ch putchar.
putchar(c++) (
putchar - ch),
putchar c.
12 Version 03
01 #include <stdio.h>
02 int main( )
03 {
04
char ch=getchar( ), NewLineChar = '\n';
05
putchar(++ch);
06
putchar(NewLineChar);
07
printf("ASCII value of \\n is %d\n", NewLineChar);
08
return 0;
09 }

4
ch
NewLineChar \n.
6
putchar(NewLineChar); \n
.
7
\\n \n.

37

:
W

X
ASCII value of \n is 10

:
ch=getchar( ); scanf( % c, &ch); cin >> ch;

:
putchar(ch) printf(% c, ch); cout << ch;
ASCII
,

.

(sign and magnitude code)



. bit .
0 1

6-bit 13 13
:
Bit
0
1

24
0
0

23
1
1

22
1
1

21
0
0

20
1
1


=13
=-13

13/32 -13/32
Bit
0
1

21
0
0

22
1
1

23
1
1

24
0
0

25
1
1

38


=13/32
=-13/32

2 (2s complement)
2 (2s complement)
(
c/c++).

,

.
n-bit

[ an1 an2 L a1 a0 ]2 = an1 ( 2n1 ) + {an2 2n2 + L a1 21 + a0 20 }


6-bit :

25

24

23

22

21

20

0
0
0
1
1

1
0
0
1
0

1
0
0
1
0

1
0
0
1
0

1
0
0
1
0

1
1
0
1
0

=31 o
=1
=0
= -32+31 = -1
= -32

6-bit x
2s complement 25 x 25 1 .
n - bit :
n2

n 1
L 0 = 2 x 01{
L1 = 2r = 2n 1 1
10{
n 1 2 r =0
n 1 2

39

19 = [ 010011]2
19 = [101101]2 0 1 1 0
1:

25

24

23

22

21

20

0 1
+

0
1
0
1

1
0
0
0

0
1
0
1

0
1
0
1

1
0
0
0

1
0
1
1

=19
=-20
=1
=-19

23 = [101001]2
23 = [ 010111]2 :

25

24

23

22

21

20

0 1
+

1
0
0
0

0
1
0
1

1
0
0
0

0
1
0
1

0
1
0
1

1
0
1
1

=-23
=22
=1
=23

y n-bit 2s
complement y
:
y + y = an 1 ( 2n 1 ) + an 2 2n 2 + L + a0 + an 1 ( 2n 1 ) + an 2 2n 2 + L + a0

= ( 2n 1 ) + 2n 2 + L + 20
= ( 2n 1 ) + ( 2n 1 1)
= 1

( ai + ai = 1 )

y = y + 1

: x y = x + y + 1 .

40

29 = [ 011101]2
7 = [ 000111]2 :

0 1
+

25

24

23

22

21

20

0
1
0
1
0
0

0
1
0
1
1
1

0
1
0
1
1
0

1
0
0
0
1
1

1
0
0
0
0
1

1
0
1
1
1
0

=7
=-8
=1
=-7
=29
=22


-7 29 carry out,
.

1 (1s complement)

. 1
2s complement

[ an1 an2 L a1 a0 ]1 = an1 ( 2n1 + 1) + {an2 2n2 + L a1 21 + a0 20 }


6-bit :

25 +1

24

23

22

21

20

0
0
0
1
1
1

1
0
0
1
1
0

1
0
0
1
1
0

1
0
0
1
1
0

1
0
0
1
1
0

1
1
0
1
0
0

= 31 ( )
= 1
= 0
= -31+31 = 0
= -31+30= -1
= -31 ( )

6-bit x
( 25 1) x 25 1 .
0 0 = [ 000000]1 = [111111]1

41

1s complement
0 1 ,
.


1 (carry out = 1)
.
; 7 = [111000]1 29 = [ 011101]1 ,
( carry out = 1) 1
:

0 1

Carry out=1

25 +1
0
1
0
0

24
0
1
1
1

23
0
1
1
0

22
1
0
1
1

21
1
0
0
0

20
1
0
1
1
1
0

=7
=-7
=29
=22

-18 5 ( carry out = 0)


:

0 1

Carry out=0

25 +1
0
1
0
1

24
1
0
0
1

23
0
1
0
0

22
0
1
1
0

21
1
0
0
1

20
0
1
1
0

= 18
=-18
=5
=-13


(floating point number)
2 .
mantissa ( )
[1 / 2, 1] exponent ( ).

(
)
x=
2
.

42

n-bit
:

( m0 m1 L mn1 | en en1 L e0 ) = ( 1)

m0

{m 2
1

+ L + mn 1 2( n 1)

( 1)en {en1 2n1 +L+ e0 20 }

4-bit .
mantissa

exponent

21

22

23

22

21

20

1 1
2 =1
2
5 4
2 = 10
8
3 2 3
2 =
4
16
7
26 = 56
8



.
n-bits 2s complement

2n 1 x 2n 1 1 1s complement ( 2n 1 1) x 2n 1 1 .
overflow ()
( underflow
). C/C++

overflow/underflow.
overflow 2s complement
To over/underflow ,
(carry in),
(carry out).

43

6 bits
32 x 31 .

32 14 + 9 31 6 bits 2s complement :

25

24

23

22

21

20

0
0
0

0
0
1

1
1
0

1
0
1

1
0
1

0
0
1

= 14
= 9
= 23

carry out = 0, carry in=0 .


25+18>31 6 bits 2s complement

25

24

23

22

21

20

0
0
1

1
1
0

1
0
1

0
0
0

0
1
1

1
0
1

= 25
= 18
=-21

carry out = 0, carry in=1 overflow .


32 17 13 31 6 bits 2s complement :

25

24

23

22

21

20

0
1
0

1
1
0

0
0
0

0
0
1

0
1
0

1
1
0

= 17
= -13
= 4

carry out = 1, carry in=1 .


-8-31<-32 6 bits 2s complement

25
24
23
22
21
20

1
1
1
0
0
0
= -8
1
0
0
0
0
1
= -31
0
1
1
0
0
1
= 25

carry out = 1, carry in=0 underflow .

44

under/overflow
13
#include <stdio.h>
#include <math.h>
int main()
{
char k, l;
short int i, j;
int m, n;
// UNDERFLOW WITH 8-BIT INTEGERS
k = - (char) pow(2.0, 7.0);
l = k - 1;
printf("underflow: k=%d, k - 1=%d\n", k, l);
// OVERFLOW WITH 8-BIT INTEGERS
k = (char) (pow(2.0, 7.0) - 1.0);
l = k + 1;
printf("overflow: k=%d, k + 1=%d\n", k, l);
// UNDERFLOW WITH 16-BIT INTEGERS
i = - (short int) pow(2.0, 15.0) ;// the minimum negative short integer
j = i-1;
printf("underflow: i=%d, i - 1=%d\n", i, j);
// OVERFLOW WITH 16-BIT INTEGERS
i = (short int) (pow(2.0, 15.0)-1.0);//the maximum positive short integer
j = i + 1;
printf("overflow: i=%d, i + 1=%d\n", i, j);
// UNDERFLOW WITH 32-BIT INTEGERS
m = - (int) pow(2.0, 31.0) ;// the minimum negative short integer
n = m-1;
printf("underflow: m=%d, m - 1=%d\n", m, n);
// OVERFLOW WITH 32-BIT INTEGERS
m = (int) (pow(2.0, 31.0)-1.0);//the maximum positive short integer
n = m + 1;
printf("overflow: m=%d, m + 1=%d\n", m, n);
return 0;
}

2 math.h
pow(x, y) x y .
x y double double.

45

8 // .
9 (char) double
pow(2.0, 7.0) char ( 8bit ).
:

C
2s complement 1 -128
127 ( 8 bit
128 = 27 x 27 1 = 127

27

26

25

24

23

22

21

20

1
1
0

0
1
1

0
1
1

0
1
1

0
1
1

0
1
1

0
1
1

0
1
1

carry in = 0 carry out = 1

46

= -128
= -1
= 127

#define const
(volume)
(radius) :
14 Version01
#include <stdio.h>
#include <math.h>
double volume(double, double);

//Function prototype

int main( )
{
double radius, PI;
PI=4.0*atan(1.0);
printf("PI=%20.17lf\n", PI);
printf("Input the radius:");
scanf("%lf", &radius);
printf("Volume=%20.17lf\n", volume(PI, radius));
return 0;
}
double volume(double pi, double R)
{
return (4.0/3.0)*pi*pow(R, 3.0);
}
:

47

= 3.1415926535897931

?
C #define
main( ). :
#define PI 3.1415926535897931
directive #define - C.
PI 3.1415926535897931.
14 Version02
#include <stdio.h>
#define PI 3.1415926535897931
double volume(double);
int main( )
{
double radius;
printf("PI=%20.17lf\n", PI);
printf("Input the radius:");
scanf("%lf", &radius);
printf("Volume=%20.17lf\n", volume(radius));
return 0;
}
double volume(double R)
{
return (4.0/3.0)*PI*R*R*R;
}


( conflict ) directive:
#ifndef PI
48

#define PI 3.1415926535897931
#endif
- PI
.
C++ const
const
compiler
.
#define PI 3.1415926535897931
:
const double PI=3.1415926535897931;
14 version03 (the C++ version)
01 #include <iostream.h>
02 #include <iomanip.h>
03 const double PI=3.1415926535897931;
04 double volume(double);
05 int main()
06 {
07
double radius;
08
09
10
11

cout << setprecision(17);


cout << "PI=" << PI << endl;
cout << "Input the radius:";
cin >> radius;

12
cout << "Volume=" << volume(radius) << endl;
13
return 0;
14 }
15 double volume(double R)
16 {
17
return ((4.0/3.0)*PI*R*R*R);
18 }

49

:
1. 08 cout (precision)
, 17 .
cout << setprecision(17);
( 02)
iomanip.h (input output manipulation.header).
2. 08 09 :
cout << setiosflags (ios :: scientific);

,

:
cout << setiosflags (ios :: fixed);
C\C++

(= =), (!=), (>, <) (>=, <=)
C C++ (
Visual Basic Boolean).
:

.
x, y z
3, 5 8. ch
'A'.

r = (x = = y) ;
s = (y != z) ;
t = (x > x) ;
u = (z <= (x + y));
v = (x = 10);

3==5
5 != 8
3>3
8 <= (3 + 5)
50

r=0;
s = 1;
t = 0;
u = 1;
v = x = 10;

w = (ch = = a)

A = = a

w = 0;

15
#include <stdio.h>
int main()
{
int x = 3, y = 5, z = 8;
char ch = 'A';
int r, s, t, u, v, w;
r = (x = = y) ;
s = (y != z) ;
t = (x > x) ;
u = (z <= (x + y));
v = (x = 10);
w = (ch = = a);
printf("\n r=%d, \n s=%d, \n t=%d, r, s, t);
printf(\n u=%d, \n v=%d, \n w=%d \n", u, v, w);
return 0;
}
:

&&, | | !
&& (AND), | | (OR) ! (NOT)

(a != b) && (b != c) && (c != a) a b c a
(a <= x) && (x <= b) x [ a, b ]

51

!((a <= x) && (x <= b))) x [ a, b] = (, a) (b, )


((a <= x) && (x <= b)) | | ((c <= x) && (x <= d))
x [ a , b ] U [ c, d ]

((a <= x) && (x <= b)) && ((c <= x) && (x <= d))
x [ a , b ] [ c, d ]

:
P && Q = TRUE P Q TRUE
P && Q = FALSE P Q FALSE
P || Q = TRUE P Q TRUE
P || Q = FALSE P Q FALSE
! P= TRUE P FALSE
! P= FALSE P TRUE
16
#include <stdio.h>
int main()
{
int a=2, b=3, c=5;
int r, s, t, u;
r = (a = = b) || (b != c) ;
s = !((c - b) = = a);
t = ((a+b) != c) && ((a*b) >= c);
u = (a > 0) && ((b != 3) || (a <= c));
printf("\n r=%d, s=%d, t=%d, u=%d\n", r, s, t, u);
return 0;
}

52

I (Conditional processing )
1

.

test
.
if
if( TEST ) /* if( ) */
{ L GROUP L }
{ L GROUP L } TEST = true
if( TEST ) /* if( ) else */
{ L GROUP1 L }
else
{ L GROUP2 L }
{ L GROUP1 L } TEST = true
{ L GROUP2 L } TEST = false

53

if( TEST1 ) /* if( ) else (DEEPLY NESTED if( ))*/


{ L GROUP1 L }
else if( TEST2 )
{ L GROUP2 L }
else if( TEST3 )
{ L GROUP3 L }
M
M
else if( TESTK )

{ L GROUPk L }
else
{ L GROUPk +1 L }
{ L GROUP1 L } :
TEST1 = true

{ L GROUP2 L } :
TEST1 = false , TEST2 = true
M

{ L GROUPk L } :
TEST1 = false , TEST2 = false ,..., TESTk 1 = false , TESTk = true
{ L GROUPk +1 L } :
TEST1 = false , TEST2 = false ,..., TESTk 1 = false , TESTk = false

(THE CONDITIONAL
OPERATOR)
x x
:

x = (TEST ? 1 : 2 ) ;
x = 1 TEST = TRUE
x = 2 TEST = FALSE

54

#include <stdio.h>
int main()
{
double x;
printf("Input a real number\n");
scanf("%lf", &x);
\\ if -- else.
if(x > 0)
{ printf("+1\n"); }
else
{
if(x < 0)
{ printf("-1\n"); }
else
{ printf("0\n"); }
}
\\ if -- else.
if(x > 0)
{ printf("+1\n"); }
else if(x < 0)
{ printf("-1\n"); }
else
{ printf("0\n"); }
\\
(x > 0.0) ? printf("+1\n") : ((x < 0.0)
? printf("-1\n") : printf("0\n")) ;
return 0;
}
(x > 0.0) = true,
+1 (x > 0.0) = false (x < 0.0) = true,
-1 (x > 0.0) = false (x < 0.0) = false,
0
iostream.h
#include <iostream.h>
int main()
{

55

double X;
cout << "Input X:";
cin >> X;
(X>0.0)? cout << "X is positive" :((X<0.0)
? cout << "X is negative" : cout << "X is zero" <<
endl);
return 0;


:
max ( x, y )
min ( x, y )
abs ( x ) : x

sign ( x ) : x

#include <stdio.h>
int main()
{
double X=-1.2, Y=3.14;
double maxXY, minXY, absX;
int signX ;
maxXY
minXY
absX
signX

=
=
=
=

(X
(X
(X
(X

> Y)
> Y)
>= 0)
> 0)

?
?
?
?

X
Y
X
1

: Y ;
: X ;
: -X ;
: ((X < 0)

? -1 : 0) ;

printf("maxXY=%lf, minXY=%lf, absX=%lf, signX=%d\n",


maxXY, minXY, absX, signX);
}

return 0;

C if

digit, capital case letter, lower case letter

56

#include <stdio.h>
int main()
{
char ch;
printf("Input a charcter:");
ch = getchar();
if((ch >= '0') && (c <= '9'))
{
printf("The character is a digit\n");
}
else if((ch >= 'A') && (c <= 'Z'))
{
printf("The character is capital case letter\n");
}
else if((ch >= 'a') && (c <= 'z'))
{
printf("The character is lower case letter\n");
}
else
{
printf("Not digit, capital or lower case letter\n");
}
}

return 0;

ch=getchar(); printf("%c", &ch);

II
(unconditional branching)

:
100

sum = k 2
k =1

counter = 1; sum = 0;

tag: if(counter <= 100)


{

sum += counter*counter;
counter++;
goto tag;

57

counter = 1; sum = 0;
counter 100
sum counter*counter (sum = sum +
counter*counter;).
update counter
(counter = counter + 1).
(label) tag
goto (goto tag;).

?
#include <stdio.h>
int main()
{
double counter, start = 0.5, end = 3.5, step;
int
n = 6;

TAG:

step = (end - start)/n;


counter = step;
if(counter < end)
{
printf("counter = %lf\n", counter);
counter += step;
goto TAG;
}
return(0);

(Loops)
group
.
loops

for - loop
group
for. for

58

for ( 1; 2 ; 3

group
}

:
1 = .
2 = ,
. 2 false for
group
.
3 = (update)


Fahrenheit Celsious
#include <stdio.h>
#define C(f) (((f) - 32) * 5 / 9)
void main()
{
double f;
printf("\n|Fahrenheit| Celsius |\n");
printf("----------------------\n");
for (f=0; f <= 100; f += 10)
{
printf("| %5.1lf
| %5.1lf
|\n", f, C(f));
}
printf("----------------------\n\n");
}

59



,
.
#include <stdio.h>
#include <math.h>
#define maxcounter 100
int main()
{
int i, counter;
double average=0.0, stddev=0.0, A[maxcounter];
counter=0;
for(
;
;
)
{
printf("Input number %2d :", counter+1);
if(!scanf("%lf", &A[counter])) break;
average += A[counter];
++counter;
}

if(counter)
{
average /= counter;
for(i=0; i<counter; ++i) stddev += pow(A[i]-average,2.0);
stddev = pow(stddev, 0.5);
if(counter>1)
{
stddev /= (counter - 1);
printf("counter=%2d average=%10.5lf
stddev=%10.5lf\n", counter, average, stddev);
}
else
printf("There is only 1 number (no stddev)\n");
}
else
printf("There are no numbers (no average and stddev)\n");
}
return 0;

60


1. double average=0.0, stddev=0.0, A[maxcounter]; A[ ]
maxcounter double .

.
2. for( ; ; )
for.
(infinite loop) break;
.
for loop.
3. counter break
double .
4. if(!scanf("%lf", &A[counter])) break;
(
for loop) scanf false,
C .
double.
5. if(counter) if
counter 0 ( C
false
true).
6. if(counter)
.
7. :

1 counter
average =
A[ K ]
counter K
=1
stddev =

counter
2
1
A i average )
(

counter 1 i =1

61


for loops:
for( int i=1, j=5; (i < 10) && (j > 0); i++, j--)
printf("i = %d j = %d\n", i, j);
for( int i=1, j=5, k=25;
(i < 10) && (j > 0) && (k > 5);
i++, j--, k -= 5)
printf("i = %d j = %d k=%d\n", i, j, k);

printf("--------------------------------------\n");
printf("|
ASCII TABLE
|\n");
printf("--------------------------------------\n");
printf("| dec | hex | chr || dec | hex | chr |\n");
printf("--------------------------------------\n");
for(int i=32; i < 126; i += 2)
{
printf("| %3d | %3X | %c
printf("| %3d | %3X | %c
}

|", i, i, i);
|\n", i+1, i+1, i+1);

printf("--------------------------------------\n");
for(
{
}
for(
{
}

x = 2.4, y = 9.3, z = 5.2;


x <= 5.7 || y > 6.9 || z >= 3.2;
++x, y -= 0.5, --z)
printf("x = %4.1lf y = %4.1lf z = %4.1lf\n", x, y, z);
printf("\n");

x = 2.4, y = 9.3, z = 5.2;


x <= 5.7 && y > 6.9 && z >= 3.2;
++x, y -= 0.5, --z)
printf("x = %4.1lf y = %4.1lf z = %4.1lf\n", x, y, z);
printf("\n");

for (a=1, b=6; a < b; a++, printf("%d\n",a));

() for loops

C ( )
for ( for ).

62

for( I1 = 1; I1 < N1 ; I1 ++)


for( I 2 = 1; I 2 < N 2 ; I 2 ++)

for( I m = 1; I m < N m ; I m ++)


{
Group
}

group N1 N 2 L N m .

A mn
B rs ( ).

1

A= 4

10 20 30
2 3

5 6 , B = 40 50 60 .

8 9
70 80 90

main( ).
#include <stdio.h>
#define AROWS 3
#define ACOLS 3
#define BROWS 3
#define BCOLS 3
int main()
{
double A[AROWS][ACOLS]={{1.0, 2.0, 3.0},
{4.0, 5.0, 6.0},
{7.0, 8.0, 9.0} };
double B[BROWS][BCOLS]={{10.0, 20.0, 30.0},
{40.0, 50.0, 60.0},
{70.0, 80.0, 90.0} };
double C[AROWS][BCOLS];
int i, j, k;
// A[][] :
printf("A[][]=\n");
for(i = 0; i < AROWS; i++)
{

63

for(j = 0; j < ACOLS; j++) printf("%lf ", A[i][j]);


printf("\n");

}
printf("\n");

printf("B[][]=\n");
// [][] :
for(i = 0; i < BROWS; i++)
{
for(j = 0; j < BCOLS; j++) printf("%lf ", B[i][j]);
printf("\n");
}
printf("\n");
if(ACOLS == BROWS)
{
// C = :
for(i = 0; i < AROWS; i++)
for(j = 0; j < BCOLS; j++)
for(C[i][j]=0.0, k = 0; k < ACOLS; k++)
C[i][j] += A[i][k]*B[k][j];
}
else
printf("T C = \n");
// C[][] :
printf("C[][]=\n");
for(i = 0; i < AROWS; i++)
{
for(j = 0; j < BCOLS; j++) printf("%7.2lf ", C[i][j]);
printf("\n");
}
printf("\n");
}

return 0;

64

while do while
for
group .
while do while
group .
:

for ( 1 ; 2 ; 3
{

1;

while ( 2 )

1;
do

group

group
3 ;

group
3 ;

} while ( 2 )

while do while
.
while
2 = false (
for).
do while
.

:
100

sum = k 2 ,
k =1

65

: goto, for, while, do - while


#include <stdio.h>
int main()
{
int counter, sum;

tag:

counter = 1;
sum = 0;
if(counter <= 100)
{
sum += counter*counter;
counter++;
goto tag;
}
printf("goto : sum = %d\n", sum);
for(counter = 1, sum = 0; counter <= 100; counter++)
sum += counter*counter;
printf("for : sum = %d\n", sum);
counter = 1;
sum = 0;
while(counter <= 100)
{
sum += counter*counter;
counter++;
}
printf("while : sum = %d\n", sum);
counter = 1;
sum = 0;
do
{
sum += counter*counter;
counter++;
} while(counter <= 100);
printf("do
: sum = %d\n", sum);

return 0;

II
switch

switch multiple choice .


:

66

switch( X )
{
case C1 : { group1 ; brake; }
case C2 : { group2 ; brake; }
M

case CK : { groupK ; brake; }


default :
{ groupK +1 ; brake; }
}
X (char, double,
int, ... C ).

{C1 , C2 ,K , CK } .
X = CJ , 1 J K , J
groupJ . X {C1 , C2 ,K , CK } (default)
groupK +1 .

C
calculator.

x+y
x-y
x*y
x/y
x^y
x%y
x@y

x y
x y
x y
x y
x y
x y
x y

67

#include <stdio.h>
#include <math.h>
int main()
{
double x, y, result;
int flag, loop;
char praxi;
start_again: flag=1;
scanf("%lf %c %lf", &x, &praxi, &y);
switch(praxi)
{
case '+' : { result = x + y ;
break ; }
case '-' : { result = x - y ;
break ; }
case '*' : { result = x * y ;
break ; }
case '/' : { result = x / y ;
break ; }
case '^' : { result = pow(x, y);
break ; }
case '%' : { result = (int) x % (int) y; break ; }
case '@' : { result = sqrt(x*x+y*y);
break ; }
default : { flag = 0;
break ; }
}
(flag) ? printf(" = %lf\n", result) : printf("wrong entry\n");
printf("to continue 1 to exit 0:");
scanf("%d", &loop);
if (loop) goto start_again;

printf("user chooses to exit!\n") ;


return 0;

praxi
{' + ', ' ', ' * ', ' / ', ' ^ ', ' % ', ' @ '} .
scanf("%lf %c %lf", &x, &praxi, &y); x + y

praxi = ' + ' case(praxi) 1


result= x + y

praxi {' + ', ' ', ' * ', ' / ', ' ^ ', ' % ', ' @ '}
default flag 0.
(wrong entry).

.
printf("to continue 1 to exit 0:");
scanf("%d", &loop);

68

1, loop=1 if (loop) goto


start_again;
label start_again. 0, loop=0,
if (loop) goto start_again; .
printf("user chooses to exit!\n") ;
return 0; .
(case while)
. 6.3 . 91

read write

,
.
file (
) (
) FILE.
fptr FILE

FILE * fptr;
fptr
file ( fopen( )).
C
.
file
( fopen( )).
:
w (write).
r (read).
file a
(append).

file data.txt",
.
69

file
dataout.txt
#include <stdio.h>
int main()
{
FILE *fprin, *fprout;
int i;
double x, sum, average;
//fprin data.txt
fprin=fopen("data.txt", "r");
// fprout dataout.txt
fprout=fopen("dataout.txt", "w");
for(sum=0.0, i=1; i<=20; i++)
{
// x data.txt
fscanf(fprin, "%lf", &x);
// i x dataout.txt
fprintf(fprout, "x[%2d] = %10.5lf\n", i, x);
sum = sum + x;
}
average = sum / (i-1);
// i average dataout.txt
fprintf(fprout, "average = %lf \n", average);

//
fclose(fprin);
fclose(fprout);
return 0;
}
data.txt dataout.txt :

70

71

You might also like