PowerPoint Tutorial - PowerPoint PPT Presentation

1 / 252
About This Presentation
Title:

PowerPoint Tutorial

Description:

PowerPoint Tutorial – PowerPoint PPT presentation

Number of Views:131
Avg rating:3.0/5.0
Slides: 253
Provided by: Amber157
Category:

less

Transcript and Presenter's Notes

Title: PowerPoint Tutorial


1
(No Transcript)
2
??? ?????? ????? ???????C Programming
??????? ?????? ??? ????
  • ???? ???? ?????????

1393-1394
3
??????? C ? C
BCPL, 1967, Martin Richards Writing Operating
Systems software and compiler
B, 1969, Ken Thomson -based on BCPL
C, 1972, Dennis Ritchie -based on BCPL and B
C, early 1980s, Bjarne Stroustrup - Object
Oriented Programming
2
4
?????
P. Deitel, H. Deitel, C how to program
J. Soulie, C Language tutorial, available at
http//www.cplusplus.com/doc/tutorial Zeynab_b
agherabadi_at_yahoo.com
3
5
???? ???
?????
2 ????
?????
4 ????
????? ? ?????? ?????
3 ????
???? ???
5 ???? (???? ??? ????????)
????? ???
8 ????
4
6
???? ?????
????? ?? ?????? ????? ?? ???? C Loops, structs,
arrays, basic data types,
????? ???? ?? C
???? ??? ?????? ????? ????? ???? ??? ...
5
7
Simple C Program
Examples
//Simplest Program int main () return 0
//int is return data type //main is entrance
function int main () statement1
statement2 //... return 0
/ Objective print on Screen / include
ltstdio.hgt//preprocessor statements int main ()
printf(welcome to c!!) return 0//indicate
that program ended successfully
6
8
Example
includeltstdio.hgt define pi 3.14 //pl
constant(preprocessor) using namespace std int
main() /variable definiton/ float
Radius float area0 /get radious of circle
from user/ printf("Enter Radius
\n") scanf("f", Radius ) /calculate area of
circle/ areapiRadiusRadius printf("Areaf",a
rea) printf("\n Pause") return 0
7
9
????? ?????
??? ??????? ?? ?? ????? ???? ???? ????? ?????
????? ????? ?? ????? ?? ?? ???? ??? ????? ???
  • Data_Type identifier
  • int width
  • float area
  • char separator
  • Data_Type identifier Initial_Value
  • int width10
  • float area255
  • char separator,
  • Data_Type identifier1, identifier2,, identifier
  • int width, length
  • float radios, area0

8
10
????? ????
  • Minimal set of basic data types
  • - Primitive data types
  • int(4)
  • float
  • double
  • char(1)
  • void
  • ???? ? ??? ????? ???? ?? ???? ??? ?? ?????????? ?
    ????? ????? ???????? ?? ?????? ????.

9
11
????? ????(??????)
  • ??? ??????? char ???? ????? ???? ????????? ????
    ?????? ?? ?? ????????????? ??? ???? ????????
  • ???? ???? ? ???? A-Z ? a-z
  • ????? ????? 0-9
  • ??? ???? ?? blank
  • ?????????? ????? ( !
  • ?????????? ?????? \n, \t
  • ??? ??? ????????? ?? ??? ??????? 8 ??? ?? ????
    ????? ?? ???? ????? ???? ????????.
  • ???????? ???? ????? ???? ?????? ??? char ?? 127-
    ?? 127 ????? ???.
  • ??? ??????? char ??? ????? ?? ??? ?????????
    signed ? unsigned ???????.

???? ????? ?? ???????? ?????? ?? ?????? ?????
??????? ??????. \n, A, 5
???? ????? ?? ???????? ?????? ?? ?????? ?????
??????? ??????. \n, A, 5
10
12
????? ????(??????)
  • ?????????? ??? ?? ???? (?????) ??? ??????? ?
    ?????????? ???? ?? ?? ????? ???? (??) ?? ?? ?? ??
    ???? ?? ??? ????? ??????? ? ?? ?? ??? ?? ??????
    ????? ?? ????.
  • ?? ????? ASCII ?? ????? ?????? ????? ? ??????????
    ????? ?? ??? ?????? ???? ?? ???.

11
13
???? ??? ??????
  • ??? ??????? float ???? ????? ???? ????? ??????
    ????? 12/5? 12345/43 ? 45/6 ???? ??????.
  • ??? ??? ????????? ?? ??? ??????? 32 ??? ?? ????
    ????? ?? ???? ????? ???? ???????? ? ????? 7 ???
    ????? ??? ???????.
  • ???? ??????? ????? ?????? ?????? ?? float ?? ???
    double ?? 64 ??? ???? ???? ???? ????? 15 ???
    ????? ??????? ??????.

12
14
Data Types qualifiers
  • Modify the behavior of data type to wich they are
    applied
  • - Size qualifier
  • short multiple by 0.5
  • long multiple by 2
  • short can be applied to int
  • long can be applied to int and double
  • - Sign qualifier
  • signed and
  • unsigned
  • they can be apply to int and char

13
15
Data Types qualifiers
14
16
???? ??? ??? bool
  • ??? bool ?? ??? ???? ??? ?? ???????? ??? ??? ???
    ????????? ????? true ?? false ????? ?????.
  • ???? ???? ?????? ??????? ?? ?????? true ?? false
    ???? ???????? ?? ??? ??? ????? ??????? ????? ???
    ??? ?????? ?? ??? ?? ???? 0 ? 1 ???? ????????
    ????? ???????.
  • bool flagfalse flag
  • ?????? ??????? ?? ??? ?????? true ? false ??
    ??????? ?? ????? ????? ??? bool ?? ????????
    ?????? ???? ???? ???? ?? ????? ??? 0 ???? ???
    bool ????? ????? true ????? ???.
  • bool a0
  • bool b6
  • bool c-34

0
0
1
15
17
???? ??
  • ???? ???? ????? ?? ????? ?? ???? ??? ??? ????? ??
    ?? ???? ????? ??? ????.
  • define without memory consume
  • const with memory consume
  • define identifier constant_value
  • define pi 3.14
  • define EROR Disk error
  • define ONE 1
  • define TWO ONEONE

16
18
???? ??
  • const Data_type identifier constant_value
  • const p 3 //const int p3
  • const p
  • p 3.14 //compiler error
  • const p 3.14//p3 because default is int
  • const float p 3.14

17
19
???????
  • Arithmetic Operators
  • - unary Operators
  • Operators that require only one Operand
  • - binary Operators
  • Operators that require two Operands
  • Assignment Operators
  • Equality and relational Operators
  • Logical Operators
  • Bitwise Operators
  • Conditional Operator
  • sizeof

18
20
???????? ?????
  • Unary Operators

Operation Operator Expression Explanation
Positive A3
Negative - B-4
Increment i ii1
Decrement -- j-- jj-1
19
21
???????? ?????
  • Binary Operators

Operation Operator Expression
Addition ab2
Substraction - ab-4
Multiplication ab3
Division / ab/5
Modulus(integer) abc
20
22
?????
  • the division of variables of type integer will
    always produce a variable of type integer as the
    result
  • Example
  • int a7, b
  • float z
  • b a/2
  • z a/2.0
  • printf(bd , zf \n, b, z)

21
23
Assignment operators
  • lvaluervalue
  • Example
  • int i
  • float f
  • i 2 //i2
  • 2 i //error invalid value assignment
  • f 5.6
  • i f //i5
  • i -5.9 //i-5

22
24
Assignment operators
  • c9

Operator Expression Equivalent statement Results
c7 cc7 c16
- c-8 cc-8 c1
c10 cc10 c90
/ c/5 cc/5 c1
c5 cc5 c4
c2 cc2 c0
c2 cc2 c11
c2 cc2 c11
ltlt cltlt2 ccltlt2 c36
gtgt cgtgt2 ccgtgt2 c2
23
25
Equality and Relational Operators???????? ??????
??
  • Equality Operators
  • Relational Operators

Operator Example Meaning
xy x is equal to y
! x!y x is not equal to y
Operator Example Meaning
gt xgty x is greater than y
lt xlty x is less than y
gt xgty x is greater than or equal to y
lt xlty x is less than or equal to y
24
26
Logical Operators
  • -if ( a2 b3 )
  • -if ( c3 d4 )
  • -if ( ! (rlt90))

Operator Description Example
Called Logical AND operator. (A B) is false.
Called Logical OR Operator. (A B) is true.
! Called Logical NOT Operator. !(A B) is true.
25
27
Bitwise Operators
p q p q p q p q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
  • ???? A60 ? B13
  • A 0011 1100
  • B 0000 1101
  • -----------------
  • AB 0000 1100
  • AB 0011 1101
  • AB 0011 0001
  • A  1100 0011

26
28
Bitwise Operators
  • ???? A60 ? B13

Operator Description Example
Binary AND (A B),0000 1100
Binary OR (A B),0011 1101
Binary XOR (A B),0011 0001
Binary Ones Complement (A ),1100 0011(-61)
ltlt Binary Left Shift A ltlt 2 ,1111 0000
gtgt Binary Right Shift A gtgt 2,0000 1111
27
29
???????? ????
  • if (condition)
  • Expresion1
  • else
  • Expresion2
  • Condition ? Expresion1 Expresion2

28
30
sizeof
  • returns the number of bytes
  • returns an unsigned integer result
  • sizeof variable-identifier
  • sizeof (variable-identifier)
  • sizeof (Data_Type)

29
31
Operator precedence
  • ???? ????? ?????? ??????? ?? ?? ???? ??
  • ????? ???? ?? ???

1 ()
2 !      --  sizeof
3   /  
4   -
5 ltlt  gtgt
6 lt  lt  gt  gt
7   !
8
9
10
11
12
13 ?
14     -    /  
15 ,
30
32
????????? ??????
  • Sequence
  • Decision Selection Statement
  • if
  • if else
  • switch
  • Repetition
  • - while
  • do while
  • for

31
33
????????? ?????
  • if(condition)
  • // statement(s) will execute if the Boolean
    expression is true

32
34
????????? ?????
  • ???? ?? ???? ?????????? ???? ?? ????? if
  • ??? ?????? ?? ??? ???? ?? ????? ? ?? ?????
    ????????? ????? ?? ??? ??????
  • int main()
  • int x, y
  • cout ltlt "Enter two integers "
  • cin gtgt x gtgt y
  • if (x gt y) int temp x
  • x y
  • y temp
  • //swap x and y
  • cout ltlt x ltlt " lt " ltlt y ltlt endl

33
35
????????? ?????
  • if(condition)
  • // statement(s) will execute if the Boolean
    expression is true
  • else
  • // statement(s) will execute if the boolean
    expression is false

34
36
????????? ?????
  • ???? ?? ????? ?????? ?????
  • int main()
  • int n, d
  • cout ltlt " Enter two positive integers "
  • cin gtgt n gtgt d
  • if (nd)
  • cout ltlt n ltlt " is not divisible by " ltlt d ltlt
    endl
  • else
  • cout ltlt n ltlt " is divisible by " ltlt d ltlt
    endl

35
37
????????? ?????

36
38
????????? ?????
  • switch(expression)
  • case constant-expression1
  • statement(s)1
  • break //optional
  • case constant-expression2
  • statement(s)2
  • break //optional
  • // you can have any number of case statements.
  • default //Optional
  • statement(s)

37
39
???? ??? ?????
  • ???? while
  • ????? ???? ?? ?? ????
  • ???? do while
  • ???? for
  • ????? break
  • ????? continue
  • ????? goto

38
40
???? ? while

39
41
???? ? while
  • ???? ?????? ???? ??? ????? ???? ?????? ?? ????
    while
  • ??? ?????? ????? 1 2 3 n ?? ???? ???
    ????? n ?????? ??????
  • int main()
  • int n, i1
  • cout ltlt "Enter a positive integer "
  • cin gtgt n
  • long sum0
  • while (i lt n)
  • sum i
  • cout ltlt "The sum of the first " ltlt n ltlt "
    integers is "
  • ltlt sum

40
42
????? ???? ?? ?? ????
  • ?? ????? break ???? ????? ???? ?? ??????? ???
    ??????? ??????? ???.
  • int main()
  • int n, i1
  • cout ltlt "Enter a positive integer "
  • cin gtgt n
  • long sum0
  • while (true)
  • if (i gt n) break
  • sum i
  • cout ltlt "The sum of the first " ltlt n ltlt "
    integers is " ltlt sum

41
43
????? ???? ?? ?? ????
  • ???? ?????? ???? ??? ????? ???? ?????? ?? ????
    while
  • ??? ?????? ????? 1 2 3 n ?? ???? ???
    ????? n ?????? ??????
  • int main()
  • int n, i1
  • cout ltlt "Enter a positive integer "
  • cin gtgt n
  • long sum0
  • while (i lt n)
  • sum i
  • cout ltlt "The sum of the first " ltlt n ltlt "
    integers is "
  • ltlt sum

42
44
????? ???? ?? ?? ????
  • ???? ?????? ???? ??? ????? ???????? ?? ?? ??
    ?????? ???? ?? ?? ????? ?????? ??????? ?????? ?
    ??? ??????
  • int main()
  • long bound
  • cout ltlt "Enter a positive integer "
  • cin gtgt bound
  • cout ltlt "Fibonacci numbers lt " ltlt bound ltlt "\n
    f0, f1"
  • long f00, f11
  • while (true)
  • long f2 f0 f1
  • if (f2 gt bound) break
  • cout ltlt ", " ltlt f2
  • f0 f1
  • f1 f2

43
45
????? ???? ?? ?? ????
  • ???? exit(0) ??? ????? ???? ????? ???? ?? ??
    ????? ???. ????? ?? ??? ???? ???????? ????? ??
    ?????? ?? ????? ??????
  • int main()
  • long bound
  • cout ltlt "Enter a positive integer "
  • cin gtgt bound
  • cout ltlt "Fibonacci numbers lt " ltlt bound ltlt
    "\n0, 1"
  • long f00, f11
  • while (true)
  • long f2 f0 f1
  • if (f2 gt bound) exit(0)
  • cout ltlt ", " ltlt f2
  • f0 f1
  • f1 f2

44
46
???? ? dowhile
  • ?????? do..while ??? ????? ???? ????? ???? ???.
    ??? ?? ?? ???? ??? ???
  • do statement while (condition)
  • ?? ??? condition ?? ??? ???? ??????? ? ?? ???
    statement? ????? ?? ????? ???? ??????? ?? ????
    ??? ????? ???.

45
47
???? ? dowhile
  • ?????? ???? ??? ????? ???? ??????? ?? ????
    do..while?
  • int main()
  • int n, i0
  • cout ltlt "Enter a positive integer "
  • cin gtgt n
  • long sum0
  • do
  • sum i
  • while (i lt n)
  • cout ltlt "The sum of the first " ltlt n ltlt "
    integers is " ltlt sum

46
48
???? ? dowhile
  • ?????? ??? ???? ????? ????????? ?? ?? ?? ??? ????
    ??? ?????????? ??? ??????
  • int main()
  • long bound
  • cout ltlt "Enter a positive integer "
  • cin gtgt bound
  • cout ltlt "Factorial numbers lt " ltlt bound ltlt
    "\n1"
  • long f1, i1
  • do
  • cout ltlt ", " ltlt f
  • f i
  • while (f lt bound)

47
49
???? ? for
  • ??? ?????????? for ?? ???? ??? ???
  • for (initialization condition update)
    statement
  • ?? ???? ???? ??????? ???? ?? ????? ???????.
  • ???? ??????? ?? ???? for ???? ?????? ????? ?????
    ???? ??????

48
50
???? ? for
  • ???? ??????? ?? ???? for ???? ?????? ????? ?????
    ???? ??????
  • int main()
  • int n
  • cout ltlt "Enter a positive integer "
  • cin gtgt n
  • long sum0
  • for (int i1 i lt n i)
  • sum i
  • cout ltlt "The sum of the first " ltlt n ltlt "
    integers is " ltlt sum

49
51
???? ? for
  • ???? ?? ???? for ?????? ?????? ???? ?? ??? ????
    ???? ?? ?? ????? ????? ??? ??????
  • int main()
  • for (int i10 i gt 0 i--)
  • cout ltlt " " ltlt i
  • ???? ? ????? ?? ?? ????? ????? ?? ???? forT ?
    ???? for ?? ?????? ??? ?? ????? ????? ????
  • int main()
  • for (int m95, n11 mn gt 0 m - 3, n)
  • cout ltlt m ltlt "" ltlt n ltlt " " ltlt mn ltlt
    endl

50
52
????? continue
  • ??? ?????? ????? ???? ???? ?? ??? ???? ? ?????
    ??? ???? ???? ?? ???? ??????.
  • ????? ??????? ?? ???????? break ? continue
  • ??? ?????? ?????? ???????? break ? continue ??
    ??? ??????
  • int main()
  • int n 1
  • char c
  • for( n )
  • cout ltlt "\nLoop no " ltlt n ltlt endl
  • cout ltlt "Continue? ltyngt "
  • cin gtgt c
  • if (c 'y') continue
  • break
  • cout ltlt "\nTotal of loops " ltlt n

51
53
????? goto
  • ?????goto ??? ????? ?? ???????? ??? ???. ???? ???
    ??? ???? ?? ????? ???? ??????.
  • ????? ???????? ??? ?? ???? ?? ????? ????( )
    ?????? ? ???? ?? ????? ???? ???? ???????.
  • ?? ???? ????? goto ??? ??? ?? ?? ??????? ?? ??
    ??????? ?? ??? ???????? ?????? ???? ?? ? ?? ????
    ??????? ?? ?????? ??? ????.

52
54
????? goto
  • ????? ??????? ?? ????? goto ???? ???? ??? ??
    ???????? ??????
  • int main()
  • const int N5
  • for (int i0 iltN i)
  • for (int j0 jltN j)
  • for (int k0 kltN k)
  • if (ijkgtN) goto esc
  • else cout ltlt ijk ltlt " "
  • cout ltlt " "
  • esc cout ltlt "." ltlt endl

53
55
?????
54
56
?????
  • ?? ??????? ?? ????? ?? ???? ?????? ??? ???? ?????
    ????? ????? ??? ?????? ?? ???? ??? ??? ?????? ??
    ????? ????? ? ?????? ??? ????? ?? ???.
  • ????? ???? ??
  • ????? ?????? ??? ?????
  • ????? ????? ???? ??
  • ???? ???? ?? ????

55
57
?????
  • ?? ???? ????? ?? ???? ???
  • ???? ?????
  • ???? ????????
  • ???? ?????

56
58
????? ???????? ?? C ?????????
  • ???????? C ????????? ????????? ??? ?? ????
    ?????? ?? ??? ????? ??? ? ???? ????? ?????? ????.
    ??? ????? ? ????? ?? ???? ????????? ????
    ???????????.
  • ???? ???? ?? ????? ?? ??????? ????????? ????
    ()sqrt ?? ?? ltcmathgt ????? ??? ??? ?... .
  • ???? ?????? ???? ???? ???? ?? ??? ????? ??? ???
    ?? ?? ??? ???????
  • include ltcmathgt // defines the sqrt()
    function
  • include ltiostreamgt // defines the cout object
  • using namespace std
  • int main()
  • //tests the sqrt() function
  • for (int x0 x lt 6 x)
  • cout ltlt "\t" ltlt x ltlt "\t" ltlt sqrt(x) ltlt
    endl

57
59
????? ???????? ?? C ?????????
  • ??? ??? ???????? ???? ?? ????? ???? ????
    ??????. ???????? ???? ?? sqrt(x) ???? ???? ????
    sqrt() ???????? ???????. ????? x ???? ??????
    ??????? ?? ??????? ????? ???????? ??????
    ??????. ?? ???? ????? ???????? ?? x ???? ?????
    ?? ???? ??????? ??????. ??? ???? x3 ???? ??
    ????? ?? sqrt(x) ???? sqrt() ???????? ??? ? ?????
    3 ?? ?? ??????? ??????. ???? ????? ??? ????
    1.73205 ?? ?? ????? ???? ???????????
  • ??????? ??? ????? ?? ??? ??? ???
  • functionName (argument)
  • ??????? ????? ?? ????? ??? ?? ????? ??? ????
  • ????
  • sqrt( 4 )
  • ?????
  • sqrt( x )
  • ?????
  • sqrt( sqrt( x ) )
  • sqrt( 3 - 6x )

58
60
????? ???????? ?? C ?????????
  • ????? ????? ????? ????? ?? ?? ????????????? ??
    ???? ???? ?? ?????? ltcmathgt ????? ??? ???. ????
    ?? ??? ????? ?? ???? ??? ???? ???? ???

59
61
????? ???? ?????
  • ???? ???? cube() ? ?? ???? ???? ?? ????? ????
    ?????
  • int cube(int x)
  • // returns cube of x
  • return xxx

60
62
????? ???? ?????
?? ???? ???? ????? ?? ???? ????
  • 1-????? 2- ????.
  • ????? ?? ???? ?? ???? ??? ???
  • (?????? ?????????) ???? ???? ????????
  • ????
  • int cube(int x)
  • ???? ????

61
63
?????? ? ?????
  • ???? ?? ???? ???? ???? ?? ????? ?????? ???? ????
    ?? ???? ?? ?? ?? ?????? ???? ?????? ????. ????
    ????????? ?????? ????? ?????? ??????.
  • ???? ??? ??? ??????? ?????? ???? ???? ? ????? ???
    ??? ?? ???.
  • ?????? ????? ?? ?????? ????? ??? ?? ???? ???? ?
    ???? ?????
  • ????
  • ???? ???? ?? ?? ???? ????????? ????????????
    ??? ????????? ?????? ???????? ? ????????? ?????
    ?? ???? ????.

62
64
?????? ? ?????
  • ????? ?? ?????? ????? ???? ???? cube()? ?? ???
    ???? ???? cube() ? ?????? ????? ?? ???
  • int cube(int x)
  • // returns cube of x
  • return xxx
  • int main()
  • // tests the cube() function
  • int n1
  • while (n ! 0)
  • cin gtgt n
  • cout ltlt "\tcube(" ltlt n ltlt ") " ltlt cube(n)
    ltlt endl

63
65
?????? ? ?????
  • ?? ??? ????? ?? ?????? ??????? ?? ??????? ?? ??
    cube(n) ?? ???? cube() ??????? ??????. ?????
    ??????? ?? ????? ??????? ????? cube(n) ???? ? ??
    ??????? ?? cout ?? ????? ??? ?? ???.

cube()
main()
5
x
5
5
int
125
64
66
?????? ? ?????
????? ?? ?????? ????? ???? ???? max()? ???? ???
?? ??????? ????. ??? ???? ?? ?? ????? ??????? ???
?? ??? ????? ??????? ?? ??????????? int max(int
x, int y) // returns larger of the two given
integers int z z (x gt y) ? x y
return z int main() int m, n do
cin gtgt m gtgt n cout ltlt "\tmax(" ltlt m ltlt ","
ltlt n ltlt ") " ltlt max(m,n) ltlt endl while
(m ! 0)
65
67
????? ? ?????? ?????
  • ?? ?? ??? ?????? ????? ?? ????? ????
  • 1-????? ??? ?? ???? main() ?? ??? ???? ?? ????
    ?????? ????? ????.
  • 2-??? ????? ?? ????? ???? ???? ??? ???? ??? ??
    ????? ???? ????? ???? ??? ??? ?????? ????main()
    ?????? ?? ?? ?? ????? ???? ???? ???? ?????.
  • ????? ???? ?? ????? ???? ????? ????.
  • ????? ????? ??? ????? ???? ??? ?? ?? ??????? ??
    ?????? ?? ???? ????.
  • ????? ????? ??? ???? ???? ??? ?? ?? ???? ?????
    ??? ? ?? ???? ????

66
68
????? ? ?????? ?????
  • ????? ????max() ?? ?????? ??? ?? ????? ??
  • int max(int,int)
  • int main()
  • int m, n
  • do
  • cin gtgt m gtgt n
  • cout ltlt "\tmax(" ltlt m ltlt "," ltlt n ltlt ") "
  • ltlt max(m,n) ltlt endl
  • while (m ! 0)
  • int max(int x, int y)
  • if (x lt y) return y
  • else return x

67
69
??????? ? ???????
  • ????????? ????????? ????? ?? ?? ????? ??????? ??
    ???? ??? ???? ???????.(????? ????? ????)
  • ????????? ???????? ???? ???? ???? ????? ????????
    ???? ??? ?? ??? ????? ???? ???? ?????.
  • ?????????? ????????? ????? ?? ?? ?????? ???? ??
    ???? ??????? ???????. (????? ???????? ????)

68
70
???????? ????? ????? ????
  • ????? ????? ?????? ??? ?? ?? ???? ?? ???? ?????
    ????. ??? ???? ??????? ??? ?? ???? ???? ????? ??
    ????? ??????? ???? ??????? ?????.
  • ??? ???? ????? ???? ?? ???? ??? ?? ???????? ?????
    ??? ?? ?? ???? ???????? ???? ???? ?? ???? ?????.
  • ??? ??????? ??? ?? ???? ?? ???? ?? ??? ??? ???
    ???? ?????.
  • ?????????? ???? ??? ???????? ???? ????? ???????.

69
71
?????
  • ????? ???? ????????? ???????? ??? ???? n ?????
    ??? ??
  • n! n(n-1)(n-2)..(3)(2)(1)
  • ???? ???? ???????? ??? n ?? ?????? ???????
  • long fact(int n)
  • //returns n! n(n-1)(n-2)...(2)(1)
  • if (n lt 0) return 0
  • int f 1
  • while (n gt 1)
  • f n--
  • return f

70
72
????? ????
71
73
???? void
  • ???? ???? ??? ????? ???? ?????? ?? ????????. ??
    C ???? ???? ???? ???? ?????? ?? ???? ????? void
    ?? ????? ??? ??????? ???? ??????? ???????
  • ?? ???? void ????? ??? ?? ??? ????? ???????
    ?????.
  • ?? ????? ?? ?? ???? void ?????? ?? ?????????????
    ????? ?? ????? return ???? ??? ??? ???? ???? ???
    ????? ?? ?? ???? void ???? ????? ???? ?? ?? ??
    ??? ???? ??????? ???? ???? ??? ?? ??? ?? ????
    return ??? ??? ????? ?????
  • return
  • ?? ??? ???? ????? return ??? ???? ?? ?????
    ??????.

72
74
????? ????
  • ?? ?????? ?? ????? ???? ??? ?? ??????? ???? ?????
    ???.
  • ??? ????? ??? ??? ?? ??????? ????? ???? ?????
    ????? ???? ??? ?? ?? ???? ??? ????? ?? ????? ???.
    ??? ??? ?????? ?????? ?? ?? ??????? ???????
    ?????? ????? ???? ???.
  • ????? ???? ??? ?? ????? ?? ???????????? true ??
    false .
  • ??? ????? ???? ?? ?????? ?? ??? ????? ??????
    ??????? ???? ????? ???? ????? ?? ?? ???? ?????
    ???? ??? ?? ??? ???????.

73
75
  • ????? ?????? ??? ??? ???? ????? ?? ????? ???????
    ?? ??? ?? ???? ???? ??? ?? ????? ?????? ??? ???
    ???? ????? ??? ?? ??? ??? ??? ?? ???
  • bool isPrime(int n)
  • // returns true if n is prime, false otherwise
  • float sqrtn sqrt(n)
  • if (n lt 2) return false // 0 and 1 are
    not primes
  • if (n lt 4) return true // 2 and 3 are
    the first primes
  • if (n2 0) return false // 2 is the only
    even prime
  • for (int d3 d lt sqrtn d 2)
  • if (nd 0) return false // n has a
    nontrivial divisor
  • return true // n has no
    nontrivial divisors

74
76
????? ????? ? ????? (Input/Output)
  • ???????? ?? ?????? ?? ?? ?????? ??? ? ?? ???
    ????????? ? ???? ?? ??? ???? ?????? ????? ???? ??
    ??????? ?? ????? ????. ?? ???? ?????? ???????
    ????? ???????? ??????.
  • ??? ???? ?????????? ???? ????? ?????? ???????
    ????? ???????? ?? ????? ?????? ????????? ?? ???
    ???????. ?? ??? ????????? ???? ??? ?? ?? ??????
    ?? ????? ??? ?? ??????? ?????? ?????? ???? ???.
    ??? ????? ?????? ?? ?? ?? ???? ?? ???? ????????
    ????????? ??????? ?? ???? ????? ????? ??????
    ????? ?? ?? ??? ????? ???? ? ... ?? ?? ??? ????
    ?????????? ???? ???? ??? ?? ?? ???? ?????? ????
    ????? ????.

75
77
  • ????? ????? ???? ?????? ?? ?????? ???? ???? ????
    ?? ????? ?? ??????? ?????? ? ????? ?????? ??? ??
    ?? ?????? ???? ????????. ??? ???? ?????? ??????
    ??? ? ?? ??? ???? ????? ??? ????? ?? ?? ?????? ?
    ?? ??? ???? ??????? ????? ????? ?????? ?? ??? ??
    ?? ??? ???? ?? ?????? 7 ?? 120 ?????? ????
  • int age()
  • // prompts the user to input his/her age and
    returns that value
  • int n
  • while (true)
  • cout ltlt "How old are you "
  • cin gtgt n
  • if (n lt 0) cout ltlt "\a\tYour age could not
  • be negative."
  • else if (n gt 120) cout ltlt "\a\tYou could
    not
  • be over 120."
  • else return n
  • cout ltlt "\n\tTry again.\n"

76
78
  • ?? ?????? ????? ? ????? ???? ?? ?? ?? ????? ????
    ???
  • int age()
  • int main()
  • // tests the age() function
  • int a age()
  • cout ltlt "\nYou are " ltlt a ltlt " years old.\n"
  • How old are you? 125
  • You could not be over 120
  • Try again.
  • How old are you? -3
  • Your age could not be negative
  • Try again.
  • How old are you? 99
  • You are 99 years old

77
79
  • ?? ???? ????? ????? ??????????? ??? ?? ??????
    ?????? ??? ????? ????? ?????? ???????. ?????
    ????? ????? ?????? ?? ?? ???????? ???? ??? ???
    ?????? ?????? ? ??? ??? ????? ?? ?????????? ????
    ???? ??????? ??????.
  • ???? ?? ???????? cube(x) ????? ????? x ?????? ???
    ? ??? ??? ????? ?? ????? ???? n ?? ???? ???????
    ?????? ? ?? ?? ?? ???? ??? ???? ?? ???? ??????.
    ?? ?? ????? ???? ???? ??? ????? n ????? ??? ???
    ??? n ???? ??? ??? ?????? ??? ????? x ?????????.
  • ?? ??? x ?? ???? ??????? ???? ????? ?? ???? ????
    ??? ??????.
  • ????? ???? ??? ????? ??? ??? ???? ???? ???
    ?????? ?? x ???? ?????. ?? ??? ????? ????
    ???????? ????? x ?? ?????? ??? ????????? ????? x
    ?? ????? ???.
  • ?? ???? ???? ?? x ?? ??????? ??? ???????
    ????????.
  • ???? ????? ?? ????? ????? ????? ????? ????????
    ???? ??????? ?? ?????? ??????? ???.
  • ???? ???? cube() ?? ??????? ?? ????cube(2x-3)
    ???????? ??? ?? ?? ??? cube(2sqrt(x)-cube(3))
    ???????? ????. ?? ?? ?? ?? ??? ?????? ????? ????
    ?????? ?? ??? ?? ????? ??? ?????? ??? ? ???? ??
    ????? ?? ???? ??????? ??????.

78
80
????? ?? ???? ?????(????)
  • ????? ?? ???? ????? ???? ?????? ?? ????????
    ?????? ???? ?? ??????? ???????? ?? ????? ????
    ??????.
  • ??? ???? ????? ???? ????????? ??? ????? ?? ???.
    ???? ????????? ?? ???? ?????? ??????? ?????
    ??????? ??? ?? ?? ?? ???????? ???. ?? ??? ???? ??
    ????? ?? ???? ????? ???????? ???????.
  • ???? ??? ?? ???? ???? ?? ??????? ?? ???? ?????
    ????? ??????? ????? ?? ?? ??? ??????? ?? ?????
    ?????????? ???? ????? ???????. ??? ???? ?????? ??
    ???? ?? ??? ??? ?? ?? ??? ???? ?? ?? ???????
    ????? ???? ??? ??????? ???? ?? ?? ??? ?????.
  • ?? ??? ????? ???? ?? ???????? ????? ???????
    ??????? ??? ?? ?????? ? ?? ???????? ????? ?? ??
    ????? ???. ?? ??? ???? ?? ??????? ?? ???????
    ???????-?????? ????? ???.

79
81
????? ?? ???? ?????(????)
  • ?? ?????? ?? ??? ??????? ???????-?????? ?? ????
    ???? ???? ?? ??? ?????? ??? ????? ?????? ????
    ????? ??????. ?? ???? ??? ???? ????.
  • ???? swap ?? ?? ???? ???? ???? ?? ?????? ?????
    ????.
  • void swap(float x, float y)
  • // exchanges the values of x and y
  • float temp x
  • x y
  • y temp

???? ??? ???? ????? ???? ?? ????? ??? ?? ?? ??
??????? ???????. ???? ??? ????? ?????????? x ? y
?? ???? ?????????? ????? ????? ??????? float x,
float y
80
82
  • ????? ?????? ????? ?????? ??? ?? ??? x ? y
    ??????????? ?????? ???? ??????. ?????? ????? ?
    ????? ??????? ?? ?? ??? ???? ???
  • void swap(float, float)
  • // exchanges the values of x and y
  • int main()
  • // tests the swap() function
  • float a 55.5, b 88.8
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt endl
  • swap(a,b)
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt endl

a 55.5, b 88.8 a 88.8, b 55.5
81
83
  • ???? ????? ?? ???? ????? ? ????? ?? ???? ?????
  • void f(int,int)
  • int main()
  • int a 22, b 44
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt endl
  • f(a,b)
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt endl
  • f(2a-3,b)
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt endl
  • void f(int x , int y)
  • x 88
  • y 99

a 22, b 44 a 22, b 99 a 22, b 99
82
84
????? ?? ???? ????? ????? ?? ???? ?????
int x int x
??????? x ?? ????? ??? ??????? x ?? ????? ???? ???
x ?????? ?? ??????? ??? x ?? ??? ?? ??????? ???
???????? ??????? ??????? ?? ????? ??? ????? ??????? ??????? ???? ????
??????? ????? ??? ?? ???? ????? ??? ???? ?? ????? ???? ??????? ????? ??? ?? ???? ????? ???????? ?? ????? ?? ????? ?? ?? ????? ????
??????? ???????-?????? ??? ??????? ??? ??????? ???
83
85
  • ???? ?????? ??? ?? ?? ?????? ????? ??? ?? ????
    ?? ??????? ?????? ?? ????? ?? ???????????? area
    ? circumference (???? ? ??????) ???? ???????? ??
    ???? ?? ??? ????? r ???
  • void ComputeCircle(double area, double
    circumference, double r)
  • // returns the area and circumference of a
    circle with radius r
  • const double PI 3.141592653589793
  • area PIrr
  • circumference 2PIr
  • ?????? ????? ???? ??? ? ?? ????? ??????? ?? ??
    ?????? ??? ???? ???? ??? ???.

84
86
  • void ComputerCircle(double, double, double)
  • // returns the area and circumference of a
    circle with radius r
  • int main()
  • // tests the ComputeCircle() function
  • double r, a, c
  • cout ltlt "Enter radius "
  • cin gtgt r
  • ComputeCircle(a, c, r)
  • cout ltlt "area " ltlt a ltlt ", circumference "
  • ltlt c ltlt endl

85
87
????? ?? ???? ????? ????
  • ????? ????????? ?? ???? ????? ?? ????? ??? ????
  • ??? ??? ?? ???? ???????? ??? ??????? ?????
    ???????? ????
  • ??? ??? ?? ?? ????? ??????? ????? ???????
    ??????.
  • ??? ????? ??? ???? ????? ??????? ???? ????
  • ????? ?? ???? ????? ????. ??? ??? ????? ????? ??
    ???? ????? ??? ?? ??? ??? ?? ???? ?????????
    ??????? ??????? ????? ?? ???????? ????? ? ???
    ????? ?????? ?? ?? ????.
  • ???? ??? ?? ???????? ?? ?? ??? ????? ???? ?????
    ???? ???? ????? const ?? ?? ?????? ????? ?? ?????
    ??????.

86
88
  • ???? ????? ?? ???? ????? ????? ?? ???? ??? ??
    ????? ????? ??????? ?? ??? ???? ???
  • void f(int x, int y, const int z)
  • x z
  • y z
  • cout ltlt "x " ltlt x ltlt ", y " ltlt y ltlt ", z
    "
  • ltlt z ltlt endl

87
89
  • ???? ?????? ? ????? ? ?? ????? ??????? ?? ????
    ???
  • void f(int, int, const int)
  • int main()
  • // tests the f() function
  • int a 22, b 33, c 44
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt ", c
    "
  • ltlt c ltlt endl
  • f(a,b,c)
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt ", c
    "
  • ltlt c ltlt endl
  • f(2a-3,b,c)
  • cout ltlt "a " ltlt a ltlt ", b " ltlt b ltlt ", c
    "
  • ltlt c ltlt endl

a 22, b 33, c 44 x 66, y 77, z 44 a
22, b 77, c 44 x 85, y 121, z 44 a
22, b 121, c 44
88
90
??????? ?????
  • ?? C ????????? ??? ???? ????? ????? ?? ???? ??
    ??? ?????. ?? ??? ???? ???????? ?? ???? ??????
    ??????? ????. ??? ??? ??? ?? ??? ?? ?????
    ?????????? ??? ????? ?? ?????? ????? ????? ????.
    ???? ????? ????????? ?????? ???? ?? ??? ?? ??? ??
    ?????????? ?????? ?? ??? ??????.
  • ????? ??????? ???? max()?? ?? ???? ??? ???? ????
    max ?? ????? ???? ???? ???? ????? ????? ?? ????
    ??? ??? ???? ?????? ????? ??????? ? ??? ?? ?? ??
    ?????? ?? ??? ????????
  • int max(int, int)
  • int max(int, int, int)
  • int max(double, double)
  • int main()
  • cout ltlt max(99,77) ltlt " " ltlt max(55,66,33) ltlt
    " " ltlt max(44.4,88.8)

89
91
int max(int x, int y) // returns the maximum
of the two given integers return (x gt y ? x
y) int max(int x, int y, int z) // returns
the maximum of the three given integers int m
(x gt y ? x y) // m max(x , y) return (
z gt m ? z m) int max(double x, double y)
// return the maximum of the two given doubles
return (xgty ? x y)
90
92
???? main
  • ??????????? ?? ?? ???? ?????? ??? ????? ????? ??
    ??? main() ?????.
  • ???? C ??? ??? ??? ?? ?? ?????? ???? ?????
    ????? ?? ??? main() ????.
  • ?? ????? ?? ?????? ????? ?? ?? ???? main() ??
    ????? ????? ???? ????? ??? ??? ?? ?? ?? ?? ???
    ????? ?? ??? ?????? ?? ??? ?????? ?? ???? ????
    main() ???????? ???????.
  • ??? ?????? ?? ???????? ???? main() ???? ??????.
  • ??? ??? ???? ?? ??? ??????? int ????? ????? ???
    ?? ???? ???? main() ???? ????? return 0 ????
    ????? ?? ?? ???? ?? ??????????? C ??? ?? ??????
    ???? ? ??????? ?? ?? ??? ????.
  • ????? ????? ?? ?? ????? return ?? ????? ????
    ????????? ???? ????? ????? ?? ????? ???. ?????
    ??????? ?? 0 ??? ?? ??? ???? ?? ?????? ???? ???
    ????? ????? ???.

91
93
????? ??
92
94
????? ??
  • ?? ?????? ?? ?????? ?? ????????? ??? ?? ??? ?? ??
    ??? ?????.
  • ?? ??? ??????? ????? ????? ????????.
  • ?? ??? ????? ?? ?? ????? ???? ?????? ?? ?? ???
    ????? ?????? ?? ??????? ????????
  • ????? ?? ????? ?? ???????? ??? ?? ?? ?? ?????
    ????? ???????. ?? ??? ????? ????? ?? ??????? ????
    ?? ????? ???? ??? ?? ??? ??? ??? ?? ????????
    ????? ????? ??? ? ?? ???? ?? ?? ???? ???? ????.

93
95
????? ??
??? ????? ????? a ?? ??? ???? ???? ?? ????
??????. ???? a0 ???? ????? 17.5 ? ???? a1
???? 19.0 ? ???? a4 ???? ????? 18.0 ???.
0 17.50
1 19.00
2 16.75
3 15.00
4 18.00
94
96
????? ??
????? ?? ???? ?? ??? int
int x5
x

0 1 2 3 4
????? ???? x0
?????? ???? x4
95
97
????? ??
???? ??????? ?????? ?? ????? ?????? ?????? ????
??? ?? ????? ?? ????? ?? ????? ?????? ? ???
??????? ?? ?? ?? ???? ???? ? ??????? ??? ??????
?? ??? ??????
int main() int a3 a2 55 a0
11 a1 33 cout ltlt "a0 " ltlt a0 ltlt
endl cout ltlt "a1 " ltlt a1 ltlt andl
cout ltlt "a2 " ltlt a2 ltlt endl
a0 11 a1 33 a2 55
96
98
????? ??
  • ??? ??? ???? ????? ????? ?? ??? ??? ???
  • type array_namearray_size
  • ????? type ??? ????? ????? ?? ???? ??????.
    array_name ??? ????? ??? .
  • array_size ????? ????? ????? ?? ???? ??????. ???
    ????? ???? ?? ??? ???? ???? ???? ? ???? ???? ????
    ????? ???? ?????.
  • ????? ????? ? ???????? ??? ????? ????? ???
    ?????

97
99
????? ??? ????? ??
  • ?? C ????????? ?? ????? ?? ?? ??????? ?? ?????
    ????????? ????? ? ?????????? ????
  • float a 22.2,44.4,66.6
  • ?? ??? ????? ?????? ???? ????? ?? ???? ?????? ??
    ???? ??????? ???? ????? ????? ???? ????????.
    ?????? ????? ??? ????? ?? ????? ????? ????? ??
    ????? ????? ???.
  • ?? ???? ?? ?????? ???????? ?? ??? float ? ?? ???
    a ? ?? ????? ?? ???? ????? ???? ? ?? ?? ???? ??
    ?? ???????? ???? ?????? ???????? ??????.
  • float a5 22.2,44.4,66.6

??? ????? ?????? ????? ???? ?? ????? ?????? ?????
???? ?????? ????????? ???? ????????? ????? ?????
??? ????????.
98
100
????? ??? ????? ??
  • float a 22.2,44.4,66.6
  • float a5 22.2,44.4,66.6
  • float a5
  • a022.2
  • a144.4
  • a266.6

22.2 44.4 66.6
22.2 44.4 66.6 0.0 0.0
22.2 44.4 66.6 - -
99
101
????? ??? ????? ??
???????? ?? ??????? ?? ??????? ?? ????? ????????
???????? ??? ??? ???????? ????? ????? ?? ??
?????? ????? ??? float a7 22.2, 44.4, 66.6
float b7 33.3, 55.5, 77.7 b a
// ERROR arrays cannot be assigned! ???????
??????????? ??? ????? ?? ?? ??? ?????? ?????
???????? ?? ????? ???? ??????? ????? float a7
22.2, 44.4, 66.6 float b7 a //
ERROR arrays cannot be used as initializers!
100
102
?????? ????? ?? ???? ????? ??
?? ???? ?? ???????? ?????????????? ?????? ?????
????????? ?? ?????? ????? ??? ???? ?? ????? ????.
???? ???? ?? ?????? ??? ????? a ?? ????? ??? ????
????? ??? ???? ? ????? a7 ??????? ???? ??????
?? ??? ???????. ??? ????? ?????? ?? C ????
?????. ???? ???? ???? ?????? ?? ?????? ?? ?????
????? ??????? ???????? ????? ?? ????? ????? ???
???? ?? ???? ? ??? ?? ???? ??? ?? ????? ?????
???? ?????? ????? ????.
101
103
?????? ????? ?? ???? ????? ??
????? ????? ?????? ????? ?? ?????? ????? ???
???? ?? ?????? ??? ?? ???? ???? ???? ????? ??
???? ?? ????? ??????? ?????? ?? ?? ?????? ?????
????? ???
in main() const int SIZE4 float aSIZE
33.3, 44.4, 55.5, 66.6 for (int i0 ilt7
i) //ERROR index is out of bounds! cout
ltlt "\ta" ltlt i ltlt " " ltlt ai ltlt endl
a0 33.3 a1 44.4 a2 55.5 a3
66.6 a4 5.60519e-45 a5 6.01888e-39 a6
6.01889e-39
102
104
????? ??? ???????? ?????? ??? ?? ?????? ???? ??
?????? ??????? ?????? ? ??? ???? ?????? ?? ?????
?? ????? ?? ??? ???????? ????? ???
int main() const int SIZE4 float a
22.2, 44.4, 66.6 float x11.1 cout ltlt
"x " ltlt x ltlt endl a3 88.8 // index
is out of bounds! cout ltlt "x " ltlt x ltlt
endl
x 88.8
103
105
????? x ??? ?? ????? a ?????? ???? ?? ?? ????
????????? ???????? ??? ?? ?????? ???? ????? ?? ??
????? ???????. ???????? ???? ?????? ???? ??????
????? 88.8 ?? ?? a3 ???? ??? (?? ??? ?????
????) ??? ????? ?? ??? ???????? ?? x ????
???????. ??? ????? ???? ?????? ???? ??? ????? ??
????? ?? ??????.
a 0 22.2 1 44.4 2 66.6 x 88.8



















22.2
  • ??? ??? ??? ?? ????????????? ?????? ???? ??????
    ???? ???? ??? ???? ??????? ???? ??? ?? ??? ????.
    ??? ???? ??? ?? ??? ??? ???????? ?????????? ?????
    ?? ?? ??? ????? ?? ???? ???? ? ??? ???? ?????
    ?????? ?? ?? ????? ???. ?? ??? ??? ??? ???????
    ????????. ??? ????? ??????????? ??? ?? ????? ???
    ?????? ????? ??????? ?? ?????? ?? ???? ????.

44.4
66.6
88.8
104
106
????? ????? ?? ?? ?????
?? float a ?? ????? a ?? ????? ?????? ?? ???
?? ?? ???????? ??????? 1- ??? ?? ??? ????? a ???
2- ????? ????? ?? ??? float ?????. ???? a ?????
????? ????? ?? ????? ??????. ???? ???? ?????
????? ????? ?? ???????? ???? ??? ???? ?? ???
????? ????? ?? a ??????? ????? ?? ??????? ????.
?? ???? ???? ??????? ?? ????? ?? ?? ???? ?????
???. ???? ??? ??? ????? ? ????? ????? ?? ?? ?????
??????? ?? ???? ??????? ??????.
105
107
????? ????? ?? ?? ?????
?????? ????? ????? ?? ????? ?? ????? ????? ?????
?? ??????????? int sum(int,int) int main()
int a 11, 33, 55, 77 int size
sizeof(a)/sizeof(int) cout ltlt "sum(a,size)
" ltlt sum(a,size) ltlt endl int sum(int a, int
n) int sum0 for (int i0 iltn i)
sum ai return sum
????? ????? ?? ???? ???? ????? ????? ?? ????
????? ???.
106
108
????? ????? ????? ? ????? ???? ?? ?????? ?? ???
?????? ?? ???? read() ??????? ?????? ?? ???????
?? ???? ????? ???? ???. ??? ?? ??????? ?? ????
print() ?????? ???? ????? ??? ???????
void read(int,int) void print(int,int) int
main() const int MAXSIZE100 int
aMAXSIZE0, size read(a,size) cout ltlt
"The array has " ltlt size ltlt " elements "
print(a,size)
Enter integers. Terminate with 0 a0 11 a1
22 a2 33 a3 44 a4 0 The array has 4
elements 11 22 33 44
107
109
void read(int a, int n) cout ltlt "Enter
integers. Terminate with 0\n" n 0 do
cout ltlt "a" ltlt n ltlt " " cin gtgt
an while (an !0 n lt MAXSIZE)
--n // don't count the 0
void print(int a, int n) for (int i0 iltn
i) cout ltlt ai ltlt " "
108
110
?? C ????? ???? ?????? ????? ????? ????? ??????
?? ????? ????. ???????? ?? ????? ????? ????????
?? ???? ?? ?? ????? ??????? ??????
1 ???? ????? ???? ????? 2 ????? ????? ?????
3 ??? ????? ?????
???? ?? ??????? ?? ??? ?? ???? ???????? ?? ?? ??
????? ????? ??????? ???.
109
111
???? ???? ????? ??-???? ???? ?????
  • ????????? ????? ??? ?? ????????? ????????????
    ????????? ???.
  • ?? ??? ???? ????? ????? ????? ???? ?????? ? ??
    ?? ????? ????????? ???? ????? ?? ??? ???? ?????
    ?????? ? ??? ?????? ????????? ???? ????? ???? ???
    ????? ??????.
  • ?? ????? ??? ???????? ????? ???? ??? ???.

110
112
???? ???? ????? ??-???? ???? ?????
  • ????? ????? ????????? ???? ? ?????? ?? ?? ?????
    ????? ???? ?? ??? ??? ???
  • ????? ???? ????? ?? ???? ??? ?????? ??????.
  • ??? ???? ??? ??????? ???? ??? ??? ?? ?? ?? ???
    ??????.
  • ??? ???? ??? ?? ???? ??? ?????? ??????.
  • ??? ???? ??? ??????? ???? ??? ??? ?? ?? ?? ???
    ??????
  • ? ?? ???? ????? ?????? ? ??????? ??????? ??????
    ????? ??????? ?? ???? ?? ?????? ????? ???????
    ????????? ??? ????? ?? ???? ??????? ???? ?????
    ????.
  • ?? ??? ???? ?????? ????? ??? ????? ??????
  • ? ?????? ??????? ????? ??? ??? ?????? ??????? ??
    ??? ??????? ???? ?? ???? ????? ?????? ????? ???.
    ??? ???? ????? ??????? ?? ??? ?? ???? ??????
    ????? ?? ???? ??? ????? ??? ????? ???? ??? ???.

111
113
????? ?????????? ?????? ??? ????? ?? ??????
?????? ?? ??? ???? ?? ??????? ?? ????????? ?????
?? ????? ?? ???? ????????
void print(float,int) void sort(float,int) i
nt main() float a55.5,22.2,99.9,66.6,44.
4,88.8,33.3, 77.7 print(a,8) sort(a,8)
print(a,8)
55.5, 22.2, 99.9, 66.6, 44.4, 88.8, 33.3,
77.7 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8,
99.9
112
114
void sort(float a, int n) // bubble sort
for (int i1 iltn i) for (int j0 jltn-i
j) if (aj gt aj1) swap
(aj,aj1)
???? sort() ?? ?? ???? ?????? ??????? ??????. 1-
???? for ????? ??????? ?????? ?? ?? ?? ??????
?????? ? ??? ????? ???? ?? ????? ?????? ??? ?? ??
?? ?? ?? ??? ??????. ???? for ????? ?? ?????
????? ????????? ???? ????? ?? ?????? ???? ??
?????? ?? ????? ??? ???. 2-??? ???? for ??????
?????? ????? ?? ??? ?? ?????? ? ?????? for ?????
?? ??? ????????? ?? ????????? ???? ???? ?? ???
????? ????? ????? ???.
113
115
????? ?? ????? ??-?????? ???
???????? ????? ???? ?????? ?? ?????? ?? ???????
?? ??? ???????. ???? ???? ??? ?? ????? ??? ???
?? ????? ??? ???? ?? ????? ????? ??? ?? ???.
????????? ??? ??? ??? ?? ?? ????? ???? ????? ????
???? ? ??? ??? ??? ????? ????? ?? ????? ?????? ??
?????? ?? ????? ???? ??? ?? ???? ???? ???? ?????.
?? ??? ??? ?????? ??? ????????.
114
116
????? ?? ????? ??-?????? ???
????? ?????? ???? ?????? ??? ????? ?? ??????
?????? ?? ?? ??? ???? ?? ??? ?????? ??? ????
????? ?? ????? ??? ??????? ???
int index(int,int,int) int main() int a
22, 44, 66, 88, 44, 66, 55 cout ltlt
"index(44,a,7) " ltlt index(44,a,7) ltlt endl
cout ltlt "index(50,a,7) " ltlt index(50,a,7) ltlt
endl int index(int x, int a, int n) for
(int i0 iltn i) if (ai x) return
i return n // x not found
index(44,a,7) 1 index(40,a,7) 7
115
117
????? ?? ????? ??-?????? ??????
  • ?? ??? ?????? ?????? ?? ?? ????? ???? ???? ???.
  • ????? ????? ????? ?? ??? ?? ?? ??? ?????? ?
    ?????? ????? ??????.
  • ????? ???? ????? ?? ????? ???? ??? ?????? ??????
    ??????.
  • ??? ??? ???? ??????? ?? ????? ????? ???? ????
    ????? ?? ??? ?????? ???? ????? ? ???? ?? ???
    ?????? ?? ????? ?? ???.

?????? ??? ?????? ?? ?? ??? ????? ??????? ?
??????? ???? ????? ??????. ??????? ?????? ?????
?? ?? ???? ????? ?????? ?? ?? ?? ???? ?? ????
????? ????? ??? ? ???? ????? ???? ??? ? ?? ??? ??
?? ???? ?? ???? ????? ????? ???? ? ??? ???? ?????
?? ????? ???? ?????. ??? ??? ????????? ?? ???
?????? ??? ??? ??? ?? ??? ????? ??????? ?? ????
???????.
116
118
????? ?????? ??????? ?????? ????? ??? ?? ??????
????? ???? ?????? ??? ??? ??? ??? ????? ?? ?? ???
???? ?? ??? ?????? ?????? ???? ????? ????? ????
????? ??????? ??????
int index(int, int,int) int main() int a
22, 33, 44, 55, 66, 77, 88 cout ltlt
"index(44,a,7) " ltlt index(44,a,7) ltlt endl
cout ltlt "index(60,a,7) " ltlt index(60,a,7) ltlt
endl
117
119
int index(int x, int a, int n) // binary
search int lo0, hin-1, i while (lo lt
hi) i (lo hi)/2 // the
average of lo and hi if (ai x) return
i if (ai lt x) lo i1 // continue
search in ai1..hi else hi i-1
// continue search in a0..i-1 return
n // x was not found in
a0..n-1
index(44,a,7) 2 index(60,a,7) 7
118
120
????? ?? ????? ??-?????? ??????
?? ???? ??? ?? ??? ?? ???? ????? ??????? ??????
????? 50 ??????? ??????. ?? ????? n ?????? ???
?????? ?????? ?????? ?? log2n1 ?????? ???? ????
?? ?? ???? ????. ??? ?? ?? ?? ??? ?????? ??? ??
n ?????? ???? ???.
119
121
????? ?? ????? ??
  • ???????? ?????? ?????? ? ???
  • ?????? ?????? ??????? ?? ?????? ??? ???.
  • ????? ????? ?? ??? ??? ?? ??? ??? ???? ?????
    ?????? ?????? ?????? ????? ?????? ??? ?????
    ????????? ?????? ?? ??????????? ??? ?? ????
    ?????? ?????? ???????? ??? ?? ???? ??????
    ?????????? ??????.
  • ????? ??? ?? ??? ??? ?? ?????? ?????? ??? ???
    ????????? ???? ?????? ???? ? ??? ???????? ????
    ?????? ?????? ?????? ???? ??? ?????? ??? ??????
    ??? ????? ???? ???? ????? ???.

120
122
????? ?? ? ??? ????
??? ?????????? ?? ?????? ????? ?????? ?? ????
?????? ??? ?????? ??????? ?????. ?????????
???????? ????? ???? ?? ?? ??? ????? ????? ???? ??
???? ?? ?? ?????? ??? ?? ????? ????. ?? ??? ????
????????? ????????? ??????? ????????. ?? ?????
?? ???? ???????? ??? ?? ?? ???? ?? ??? ??? ??
????? ?? ???? ????. ???? ???? ?? ???????? ????
????? ?????? ?? ?? ????? ?? ?? ???? ??????? ??
???. ?? ????? ?? ???? ???????? ??? ?? ?? ???? ??
?? ?? ????? ?? ???? ????.
121
123
????? ?? ? ??? ????
  • ????? int a5 ???????? ?? ??? ???? ?? ??? int
    ????? ??????. ??? ?? ????? ?? ???? ???.
  • ????? int a35 ???????? ?? ?? ???? ?????
    ?????? ?? ?? ????? ??? ?? ????? ??? ????? ?? ???
    int ???. ??? ?? ????? ?? ???? ??? ?? ?? ?????
    ?????? ??? ????.
  • ????? int a235 ???????? ?? ?? ???? ?????
    ?????? ?? ?? ????? ?? ????? ??? ?? ?? ????? ???
    ??? ?? ??? int ????. ??? ?? ????? ?? ???? ??? ??
    ?? ????? ?? ??? ????.

????????? ??? ???? ??? ????????? ?? ???? ?? ?????
??????? ??????? ?? ??? ????? ?? ????? ????? ?
????? ???? ??????? ???? ????? ????? ???? ??? ??
???? ??? ???? ??? ???.
122
124
????? ?? ? ??? ????
  • ???????? ?????? ????????? ?????? ?? ?????????????
    ???? ??????.

int a34
???? 0 ???? 1 ???? 2 ???? 3
??? 0 a00 a01 a02 a03
??? 1 a10 a11 a12 a13
??? 2 a20 a21 a22 a23
a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23
123
125
????? ?????? ????? ?? ????? ?????
????? ?? ? ??? ????
  • int a34 1,2,3,4, 5,6,7,8, 9,10,11,12
  • int a34 1,2,3,4, 5,6,7,8, 9,10,11,12
  • int a4 1,2,3,4, 5,6,7,8, 9,10,11,12

0 1 2 3
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
124
126
????? ?? ? ??? ????
???? 1
  • int a34 1, 2,3 , 4,5,6

0 1 2 3
0 1 0 0 0
1 2 3 0 0
2 4 5 6 0
125
127
????? ?? ? ??? ????
???? 2
  • int a34 1, 2, 3, 4,5

0 1 2 3
0 1 2 3 4
1 5 0 0 0
2 0 0 0 0
126
128
????? ?? ? ??? ????
???? 3
  • ?? ?? ????? ????????? ?? ???? ?? ????? ????????
    ?? ?????? ???. ?? ????? ????????? ???????? ???
    ????? ?????? ?????? ???.

int a 41,2,3,4,5
0 1 2 3
0 1 2 3 4
1 5 0 0 0
127
129
???? ??
  • ?? c ???? ??? ????? ??? ???? ???? ?? ???? ?????
    ?? ?? ???????? ????? ?? ???
  • ?? c ???? ????? ?????? ???? ?? ?????? ???? ??
    ??? NULL ??????? ?? ??? ?? ?? \0 ???? ?? ???.
  • ???????? ????? ?????? ???? \0 ?? ????.
  • Char S10
  • ?? ???? ???? ?? S ????? ??? ?????????? ?? 9 ????
    ?? ???? ??????? ???? ????? ???? ? ??? ?? ?????
    ????? NULL ?? \0 ????.
  • ??? ???? ?? ?? ???? ?? ???? ????? ?? ???? ????
    ????? ????? ????.

128
130
????? ????? ???? ?? ???? ??
  • char S computer
  • char St12algorithm
  • char Strp, q, t, \0

129
131
????? ????? ???? ??
  • ?? ???? ???????? ?? ???? ????? ????? ???? ?? ????
    ??????? ???? ?? ???? ?? ???? ???? ????? ?????
    ???? ?? ??? ??????? ???
  • char f20
  • cin gtgt f
  • cout ltltf is ltltf

130
132
????? ????? ???? ??
  • cin.get(??? ???? , ??? ????)
  • cin.get(??? ???? , ??? ???? , ????????)
  • ???? get ??? ???cin ?? ???.
  • ????? cin ? get
  • ?? get ??? ???? Enter ?????? ???? ?? ???? ??
    ???? ??? ????? ?????? ???? ?? ??????? ?? ?????
    ??? ?????? ????? ?? ???? ??? ????? ???? ????
    ????. ?? ??? ???? ???? ?? ????? ???? ????? ? ??
    ??? ????.
  • ?? ????? cin ????? ? ?? ??? ?? ????? ?????????
    ??? ????? ????? ?? ????.
  • ???? ????? ???? ?? ?? ????? ????? ????? ????? ??
    ?? ????? ?? ????.

131
133
????? ??? ?? ???? ??
???? ???
strcpy(s1, s2) Copies string s2 into string s1.
strcat(s1, s2) Concatenates string s2 onto the end of string s1.
strlen(s1) Returns the length of string s1.
strcmp(s1, s2) Returns 0 if s1 and s2 are the same less than 0 if s1lts2 greater than 0 if s1gts2.
strchr(s1, ch) Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2) Returns a pointer to the first occurrence of string s2 in string s1.
132
134
????? ????
133
135
????? ????
  • ????? ?????? ?? ??????? ?? ???? ?? ????? ???? ??
    ??? ????. ???? ???? ????????? ?? 256 ??????? RAM
    ?? ????? ???? ???????? ?? ?????? 268?435?456 ????
    ??? ?? ?????? ?? ???? ?? ???? ???.
  • ??? ??????? ????? ?????? ??? ?? 268?435?455
    ?????. ?? ?????? ?? ????? ???? ????? ?? ????????.
  • ???????? ????? ?? ?? ????? ?????????? ????
    ???????. ?? ?????? ????? ????? ?????? ????
    0x00000000 ?? 0x0fffffff ???????.
  • ?? ??? ?? ?????? ?? ????? ???????? ?? ????? ?????
    ?? ?? ????? ???? ???? ?????? ??? ????? ? ???
    ????? ? ???? ????? ??.

134
136
????? ????
  • ????? int n? ??? int ? ??? n ? ???? ??? ???? ??
    ????? ?? ????? n ?? ?? ???? ??????? ?? ?? ??????
    ????? ???????. ??? ???? ???? ??? ????? 0x0050cdc0
    ???. ???????? ????????? n ?? ????? ??? ????? ????
    ????

135
137
????? ????
??? ????? ??? ?? ??? int n32 ???????? ?????
???? ????? ???? ????? ?? ??? ??? ????? ???. ?????
32 ?? ???? ????? ?? ???? ?? ????? ????? ??? ?????
??????.
0x0050cdb8 0x0050cdb9 0x0050cdc0 0x0050cdc1 0x0050
cdc2 0x0050cdc3 0x0050cdc4 0x0050cdc5
n
32
32
0x0050cdc0
int
136
138
????? ?????
?? C ???? ???? ????? ???? ?? ????? ??????? ??
????? ????? ??????? ????. ?? ??? ????? ?????
???? ??? ????????. ????? n ???? ????? n ?? ??
??? ??????.
int main() int n44 cout ltlt " n " ltlt n
ltlt endl cout ltlt "n " ltlt n ltlt endl
n 44 n 0x00c9fdc3
137
139
????? ????
???????? ?? ????? ???? ?? ???? ?? ?????? ?? ???
int ??????? ???? ? ????? ?????? ?? ?? ?????????
?? ??? float. ?? ???? ????? ????????? ?? ???? ??
????????? ?? ??? char ???????? ? ???????? ?????
?? ?? ????????? ?? ??? bool.
??? ???? ????? ?? ?? ?? ??? ?????? ???? ???? ?????
?????? ?? ?? ???? ?? ?? ????? ?????? ????????
?????? ??????.
138
140
????? ????
  • ??? ??? ????? ????? ????
  • type pointername
  • ????? ?? ??????? ??????????? ?????? ??? ?? ?????
    ?? ??? int ?? ??? n ? ?? ???????? ?? ??? int ??
    ??? pn ?? ????? ??????
  • int main()
  • int n44
  • cout ltlt "n " ltlt n ltlt ", n " ltlt n ltlt
    endl
  • int pnn // pn holds the address of n
  • cout ltlt " pn " ltlt pn ltlt endl
  • cout ltlt "pn " ltlt pn ltlt endl

n 44, n 0x0064fddc pn 0x0064fddc pn
0x0064fde0
139
141
????? n ?? ????? 44 ????????? ??? ? ???? ??
0x0064fddc ???????. ???????? pn ?? ????? n ????
???? n ???????? ???. ?? ????? ???? pn ????? ??
0x0064fddc ???? (?? ??? ????? ??? ????? ?? ?????
??????) .
???? ???????? pn ?? n ????? ?????? ???? ???? pn
???? n ???? ????.
??? pn ?? ????? ????? ??? ? ???? ?????? ????. pn
???? pn ?? ?? ??? ??????. ?? ??? ????? ????
?????? ?? ????? pn ????? ?? ????? n ???. ?????
??? ?? ??? ???? ??? ????? ??? ??????. ?? ???
????? ????????? ??? n ? pn ???? ???? ???. pn ??
???????? ?? n ??? ? n ????? 44 ????.
n
44
int
140
142
????? ????
??? ???? n ????? ????? 22 ???? ? pn ????????? ??
n ????. ?? ??? ???? ???? ????? ?? ???? pn ??
????? 22 ????. ?? ??????? ?? ??????? ?????? ??
???????? ?? ?? ????? ???? ?? ?? ??? ????. ?? ???
??? ????????? ???????? ????????.
141
143
????? ????
  • ?????? ????????? ?? ????????? ???? ??????? ?????
    ??????? ???? ??? ???. ??? ?? ?? ?? ????? ????
  • int main()
  • int n44
  • cout ltlt "n " ltlt n ltlt ", n " ltlt n ltlt
    endl
  • int pnn // pn holds the address of n
  • cout ltlt " pn " ltlt pn ltlt endl
  • cout ltlt "pn " ltlt pn ltlt endl
  • cout ltlt "pn " ltlt pn ltlt endl

n 44, n 0x0064fdcc pn
0x0064fdcc pn 0x0064fdd0 pn 44
142
144
????? ??? ????? ????
  • ??? ?????? ????? ???? ?? ??????
  • ????? ???????? ??? ? ?????
  • ??? ?????? ????? ????
  • ??????
  • int p1, p2, x, y
  • X50, y100
  • P1x
  • p2
Write a Comment
User Comments (0)
About PowerShow.com