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???? ???
?????
?????
?????
???? ???
????? ???
4
6???? ?????
????? ?? ?????? ????? ?? ???? C Loops, structs,
arrays, basic data types,
????? ???? ?? C
???? ??? ?????? ????? ????? ???? ??? ...
5
7Simple 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
8Example
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
9Variable declaration
??? ??????? ?? ?? ????? ???? ???? ????? ?????
- 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
10Data Types
- Minimal set of basic data types
- - Primitive data types
- int(4)
- float
- double
- char(1)
- void
- ???? ? ??? ????? ???? ?? ???? ??? ?? ?????????? ?
????? ????? ????????? ?????? ????.
9
11Data Types(char)
- ??? ??????? char ???? ????? ???? ????????? ????
?????? ?? ?? ????????????? ??? ???? ???????? - ???? ???? ? ???? A-Z ? a-z
- ????? ????? 0-9
- ??? ???? ?? blank
- ?????????? ????? ( !
- ?????????? ?????? \n, \t
- ??? ??? ????????? ?? ??? ??????? 8 ??? ?? ????
????? ?? ???? ????? ???? ????????. - ???????? ???? ????? ???? ?????? ??? char ?? 127-
?? 127 ????? ???. - ??? ??????? char ??? ????? ?? ??? ?????????
signed ? unsigned ???????.
???? ????? ?? ???????? ?????? ?? ?????? ?????
??????? ??????. \n, A, 5
???? ????? ?? ???????? ?????? ?? ?????? ?????
??????? ??????. \n, A, 5
10
12Data Types(char)
- ?????????? ??? ?? ???? (?????) ??? ??????? ?
?????????? ???? ?? ?? ????? ???? (??) ?? ?? ?? ??
???? ?? ??? ????? ??????? ? ?? ?? ??? ?? ??????
????? ?? ????. - ?? ????? ASCII ?? ????? ?????? ????? ? ??????????
????? ?? ??? ?????? ???? ?? ???.
11
13Data Types(float, double)
- ??? ??????? float ???? ????? ???? ????? ??????
????? 12/5? 12345/43 ? 45/6 ???? ??????. - ??? ??? ????????? ?? ??? ??????? 32 ??? ?? ????
????? ?? ???? ????? ???? ???????? ? ????? 7 ???
????? ??? ???????. - ???? ??????? ????? ?????? ?????? ?? float ?? ???
double ?? 64 ??? ???? ???? ???? ????? 15 ???
????? ??????? ??????.
12
14Data 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
15Data 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
17constant
- ???? ???? ????? ?? ????? ?? ???? ??? ??? ????? ??
?? ???? ????? ??? ????. - 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
18constant
- 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
19Operator
- 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
20Arithmetic Operators
Operation Operator Expression Explanation
Positive A3
Negative - B-4
Increment i ii1
Decrement -- j-- jj-1
19
21Arithmetic Operators
Operation Operator Expression
Addition ab2
Substraction - ab-4
Multiplication ab3
Division / ab/5
Modulus(integer) abc
20
22Division
- 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
23Assignment 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
24Assignment operators
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
25Equality 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
26Logical 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
27Bitwise 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
28Bitwise Operators
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
29Conditional Operator
- if (condition)
- Expresion1
- else
- Expresion2
- Condition ? Expresion1 Expresion2
28
30sizeof
- returns the number of bytes
- returns an unsigned integer result
- sizeof variable-identifier
- sizeof (variable-identifier)
- sizeof (Data_Type)
29
31Operator precedence
Category Operator Associativity
Postfix () . -gt Left to right
Unary - ! exp --exp (typecast) sizeof Right to left
Multiplicative / Left to right
Additive - Left to right
Shift ltlt gtgt Left to right
Relational lt lt gt gt Left to right
Equality ! Left to right
Bitwise AND Left to right
Bitwise XOR Left to right
Bitwise OR Left to right
Logical AND Left to right
Logical OR Left to right
Conditional ? Right to left
Assignment - / gtgt ltlt Right to left
Post increment exp exp--
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
-
- ???? ? ????? ?? ?? ????? ????? ?? ???? for
- ???? 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
91int 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????? ??
37
94???? ??
37