afee passe - PowerPoint PPT Presentation

About This Presentation
Title:

afee passe

Description:

afee passe – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 205
Provided by: nikolaossp
Category:
Tags: afee | double | passe

less

Transcript and Presenter's Notes

Title: afee passe


1
??af??e?e? pa???s??se??
DRAFT
30/03/03
  • ? ???ssa p????aµµat?sµ?? C
  • ?????aµµat?st???? te??????
  • ??µ?? ded?µ????

2
? ?st???a t?? C
  • 1969-1973 ATT Bell Labs, Dennis Ritchie
  • 1978 The C Programming LanguageKR Kernighan
    Ritchie
  • 1983 S?stas? ANSI Standardization Committee X3J11
  • 1989-1990 ?p?d??? ANSI/ISO Standard? ANSI C
  • 1990-1999 ??a?e???s? t?? standard ?p? e??????
    C9X? C99

3
?a?a?t???st??? t?? C (i)
  • G??ssa p??sta?t???? p????aµµat?sµ??
  • G??ssa µet???? ep?p?d??
  • ??????µ?a st?? ??f?as?(??t? ?a? pe??e?t???)
  • S?et??? ?a?a?? s?st?µa t?p??
  • F???s?f?a ? p????aµµat?st?? ??e? p???? ??e???
    ?a? e????eta? ??a ta sf??µat? t??

4
?a?a?t???st??? t?? C (ii)
  • ?d?a?te?a d?µ?f???? st?? p????
  • ??e? ???s?µ?p????e? ??a t?? p????aµµat?sµ? e?????
    f?sµat?? s?st?µ?t?? ?a? efa?µ????
  • ??e? ???s?µ?p????e? ?? ß?s? ??a p?????a ?????
    ???ss?? C, Java

5
Hello world!
  • include ltstdio.hgt
  • void main ()
  • printf("Hello world!\n")
  • ?a?at???se??
  • ? ep????????a µe t?? ??? ??sµ? ???eta? µ?s? t??
    ß?ß???????? s??a?t?se??
  • ?? s?µe?? ??a???? t?? p?????µµat?? e??a? ?
    ????a s????t?s? main.

6
??t??es? p????aµµ?t??
stdio.h
hello.c
???e?aep??efa??da?(header files)
?eta???tt?st??C (compiler)
???a?????d??a?(source)
Objectcode
hello.o
??te??s?µ?p????aµµa(executable)
??ß???????(library)
S??d?t??(linker)
printf
hello
7
??p?? ded?µ???? (ap???)
  • ????a??? a???µ??
  • int char
  • ?a????sµ?? p??s?µa?s??
  • signed unsigned
  • ?a????sµ?? µe??????
  • short long
  • ????µ?? ????t?? ?p?d?ast????
  • float double

8
???a?a? ap??? t?p?? ded?µ????
  • char , signed char , unsigned char
  • signed short int , unsigned short int
  • signed int , unsigned int
  • signed long int , unsigned long int
  • float
  • double
  • long double
  • ?e ??????? ???µa ?sa µp????? ?a pa?a?e?f????.

9
???sµ?? µetaß??t??
  • int x
  • int x, y, z
  • double r
  • unsigned long abc
  • ??????p???s?
  • int x 1
  • int x, y 0, z 2
  • double r 1.87
  • unsigned long abc 42000000

10
Sta?e??? (i)
  • ????a?e?
  • 42 0 -1 de?ad????
  • 037 ??tad????
  • 0x1f de?ae?ad????
  • 42U 42L 42UL unsigned long
  • ????t?? ?p?d?ast????
  • 42.0 -1.3 de?ad????
  • 2.99e8 µe d??aµ? t?? 10
  • 42.0F 42.0L float long double

11
Sta?e??? (ii)
  • ?a?a?t??a
  • 'a' '0' ''
  • ??d???? ?a?a?t??e?
  • \n a??a?? ??aµµ??
  • \' ap?st??f??
  • \\ ?a?a?t??a? \ (backslash)
  • \t a??a?? st???? (tab)
  • \" e?sa??????
  • \0 ?a?a?t??a? µe ASCII 0 (null)
  • \037 µe ASCII 37 (??tad???)
  • \x1f µe ASCII 1f (de?ae?ad???)

12
Sta?e??? (iii)
  • S?µß???se????
  • "abc" "Hello world!\n" "a\"51\""
  • ????se?? sta?e???
  • const int size 10, num 5
  • const double pi 3.14159
  • const char newline '\n'

13
S????a
  • ?eta?? / ?a? /
  • include ltstdio.hgt
  • / This simple program greets the
  • world by saying "hello" /
  • void main ()
  • printf( / eh? / "Hello world!\n")
  • ?? pa?a??t? e??a? ?????
  • / Nested / comments / are wrong! /

14
??t?p?s? µe t?? printf
  • ?p??? t?p?? ded?µ????
  • int d
  • char c
  • double lf
  • string s
  • ?a??de??µa
  • printf("d lf c s\n",42, 1.2, 'a', "hello")
  • ?p?t??esµa
  • 42 1.200000 a hello

15
??sa???? µe t?? scanf
  • ?d??? ??d???? ??a t??? ap???? t?p???
  • ?a??de??µa
  • int n
  • double d
  • char c
  • scanf("d", n)
  • scanf("lf", d)
  • scanf("c", c)

16
??a p?? s???et? pa??de??µa
  • include ltstdio.hgt
  • void main ()
  • int celcius
  • double farenheit
  • printf("Give the temperature (C) ")
  • scanf("d", celcius)
  • farenheit 9.0 celcius / 5.0 32.0
  • printf("d degrees Celcius
  • "is lf degrees Farenheit",
  • celcius, farenheit)

17
?e?est?? ?a? e?f??se?? (i)
  • ????µ?t???? te?est??
  • - /
  • S?es?a??? te?est??
  • ! lt gt lt gt
  • ??????? te?est??
  • ?????? s??e??? (?a?)
  • ?????? d???e??? (?)
  • ! ?????? ????s? (???)
  • p.?. (x 3 ! 0) !finished

18
?e?est?? ?a? e?f??se?? (ii)
  • ?e?est?? bit p??? bit (bitwise)
  • s??e??? bit (AND)
  • d???e??? bit (OR)
  • ap???e?st??? d???e??? bit (XOR)
  • ????s? (NOT)
  • ltlt ???s??s? bit a??ste??
  • gtgt ???s??s? bit de???
  • ?a??de??µa
  • (0x0101 0xfff0) ltlt 2
  • ? 0x0400

19
?e?est?? ?a? e?f??se?? (iii)
  • ?e?est?? s???????
  • (a gt b) ? a b
  • ?e?est?? pa???es??
  • a-1, b5
  • ?e?est?? a???es??
  • a b1
  • a x ?s?d??aµ? µe a a x
  • ?e?est?? a???s?? ?a? µe??s??
  • a a-- t?µ? p??? t? µetaß???
  • a --a t?µ? µet? t? µetaß???

20
E?t???? ?a? ??e???? ???? (i)
  • ?e?? e?t???
  • ??t??? ??f?as??
  • a b5
  • a
  • ??t??? if
  • if (a gt b)
  • max a
  • else
  • max b

21
E?t???? ?a? ??e???? ???? (ii)
  • S???et? e?t???
  • if (a gt b)
  • min b
  • max a
  • else
  • max b
  • min a
  • ????e? ??a eµß??e?a
  • if (x lt y)
  • int temp x
  • x y
  • y temp

22
E?t???? ?a? ??e???? ???? (iii)
while (s??????) e?t???
  • ??t??? while
  • int i 1, s 0
  • while (i lt 10)
  • s i
  • i

23
E?t???? ?a? ??e???? ???? (iv)
do e?t??? while (s??????)
  • ??t??? do-while
  • int i 1, s 0
  • do
  • s i
  • while (i lt 10)

24
E?t???? ?a? ??e???? ???? (v)
for (a?????p???s? s?????? ß?µa) e?t???
  • ??t??? for
  • int i, s
  • for (i1, s0 i lt 10 i)
  • s i

25
E?t???? ?a? ??e???? ???? (vi)
  • ??t??? break
  • int s
  • for (i0, s0 i lt 10 i)
  • int x
  • scanf("d", x)
  • s x
  • printf("Sum is d\n", s)
  • if (x lt 0)
  • break

26
E?t???? ?a? ??e???? ???? (vii)
  • ??t??? continue
  • int s
  • for (i0, s0 i lt 10 i)
  • int x
  • scanf("d", x)
  • s x
  • printf("Sum is d\n", s)
  • if (x lt 0)
  • continue

27
E?t???? ?a? ??e???? ???? (viii)
  • ??t??? switch
  • switch (ch)
  • case 'a'
  • printf("alpha\n")
  • break
  • case 'b'
  • case 'c'
  • printf("beta or c\n")
  • break
  • default
  • printf("other\n")

28
E?t???? ?a? ??e???? ???? (ix)
  • ?t???te? ?a? e?t??? goto
  • int i 1, s 0
  • loop
  • s i
  • if (i lt 10)
  • goto loop
  • printf("The sum is d\n", s)
  • ??? goto d?µ?µ???? p????aµµat?sµ??!

29
??µ? t?? p?????µµat??
  • ?? p????aµµa ap?te?e?ta? ap?
  • s??a?t?se??
  • ?a??????? µetaß??t??
  • ??af???? ap? t?? Pascal
  • ????? p????aµµa, d?ad??as?e? ?a? s??a?t?se?? de
    d?af???p?????ta?
  • t? ????? p????aµµa ???µ??eta? main
  • ?? d?ad??as?e? ????? ap?t??esµa void
  • ??e? ?? s??a?t?se?? st? ?d?? ep?ped?
  • de? ep?t??p??ta? f???asµ??e? s??a?t?se??
  • µ??? p??asµa ?at? t?µ? (call by value)

30
?a??de??µa
  • int f (int x, int y)
  • return x y
  • int a
  • void p (int x)
  • a f(x, x1)
  • void main ()
  • p(6)

31
???a?e? (arrays) (i)
  • ????????a µetaß??t??
  • µe t? ?d?? ???µa,
  • µe t?? ?d?? t?p? ded?µ????,
  • se s??e??µe?e? ??se?? µ??µ??.
  • ?a??de??µa
  • int a50
  • double d15, d210
  • ? a???µ?s? a????e? ap? t? 0 !
  • for (i 0 i lt 50 i)
  • ai i

32
???a?e? (arrays) (ii)
  • ??????p???s? p??????
  • int a3 6, 7, 42
  • char c 'a', 'b', 'c', 'd', 'e'
  • S?µß???se????
  • ???a? p??a?e? ?a?a?t????
  • char s6 "abcde"
  • ?e?µat????ta? µe t?? ?e?? ?a?a?t??a '\0'
  • char s6 'a', 'b', 'c',
  • 'd', 'e', '\0'

33
???a?e? (arrays) (iii)
  • ?a??de??µa
  • int a3 5, 6, 7
  • int b3 2, 3, 1
  • int i
  • for (i 0 i lt 3 i)
  • printf("d times d is d\n",
  • ai, bi, aibi)

5 times 2 is 10 6 times 3 is 18 7 times 1 is 7
34
????d??stat?? p??a?e? (i)
  • ?a??de??µa
  • int a35
  • char c5104
  • ??????p???s?
  • int i33 1, 0, 0,
  • 0, 1, 0,
  • 0, 0, 1
  • char s10
  • "my", "name", "is", "joe"

35
????d??stat?? p??a?e? (ii)
  • ?a??de??µa p???ap?as?asµ?? p??????
  • double a34 ... ,
  • b45 ... ,
  • c35
  • int i, j, k
  • for (i0 ilt3 i)
  • for (j0 jlt5 j)
  • cij 0.0
  • for (k0 klt4 k)
  • cij aik bkj

36
??sa???? st?? d?µ?? ded?µ???? (i)
  • ???????µ??
  • ?epe?asµ??? s????? e?t???? p??,?ta?
    e?te?est???,ep?t???????? ??p??? ep???µ?t?
    ap?t??esµa
  • ?ed?µ??a e?s?d?? ?a? e??d??
  • ???e e?t??? p??pe? ?a e??a?
  • ?a?? ???sµ???
  • ap??
  • ? e?t??es? p??pe? ?a staµat?

37
??sa???? st?? d?µ?? ded?µ???? (ii)
  • ????p????t?ta
  • ??t?? e?t?µ?s?? t?? ap?d?s?? a??????µ????
    s????t?s? t?? µe??????t?? p??ß??µat?? p??
    ep??????
  • ?????? e?t??es??
  • ????t???t?ta µ??µ?? p?? apa?te?ta?
  • ????ß?? µ?t??s? p???p????t?ta?
  • t f(n)
  • ???? µe??????, s?µß???sµ?? ?, O, T
  • t O(f(n))

38
??sa???? st?? d?µ?? ded?µ???? (iii)
  • ???sµ?? t?? ???se?? ?, O, T
  • ??? ????
  • g O(f) ? ?c. ?n0. ?n gt n0. g(n) lt c f(n)
  • ??t? ????
  • g O(f) ? ?c. ?n0. ?n gt n0. g(n) gt c f(n)
  • ???? µe??????
  • g T(f) ? ?c1, c2. ?n0. ?n gt n0. c1 f(n) lt
    g(n) lt c2 f(n)
  • ???ta?? µe????? ???se?? p???p????t?ta?
  • O(1) lt O(logn) lt O(?n) lt O(n) lt O(nlogn)lt O(n2)
    lt O(n3) lt O(2n) lt O(n!) lt O(nn)

39
??sa???? st?? d?µ?? ded?µ???? (iv)
  • ?fa??es? ded?µ???? (data abstraction)
  • ??a????sµ?? ?d??t?t?? ?a? ???p???s??
  • ?d??t?te? e??? t?p?? ded?µ????? t??p?? µe t??
    ?p??? d?µ?????e? ?a?e?? ?a? ???s?µ?p??e? ded?µ??a
    a?t?? t?? t?p??
  • ???p???s? e??? t?p?? ded?µ????? t??p?? µe t??
    ?p??? a?apa??sta?ta? ta ded?µ??a st? µ??µ? t??
    ?p?????st? ?a? p????aµµat????ta? ?? d?a??s?µe?
    p???e??

40
??sa???? st?? d?µ?? ded?µ???? (v)
  • ?? ?ept?µ??e?e? ???p???s?? ep??e?????t??
    p???p????t?ta t?? a??????µ??
  • ???a? s???? ep?µ???? ? a??a?? t??p?? ???p???s??
    ??p???? t?p?? ded?µ????
  • ? afa??es? ded?µ???? e?a??st?p??e? t?? a??a???
    p?? apa?t???ta? st? p????aµµa p?? ???s?µ?p??e?
    ??a? t?p? ded?µ????, ?ta? a????e? ? t??p??
    ???p???s??

41
?f???µ???? t?p?? ded?µ????
  • ?f???µ???? t?p?? ded?µ???? ???(abstract data
    type)
  • ?a?????e? t?? ?d??t?te? t?? t?p?? ded?µ????
  • de? ?a?????e? t?? ???p???s? t??
  • ???eß????? ???sµ?? ???
  • s??ta?? ???µa t?? t?p?? ?a? ep??efa??de? t??
    p???e??
  • s?µas??????a ?a???e? p?? pe?????f??? t?
    ?e?t?????a t?? p???e??

42
?a??de??µa ??? S????a (i)
  • S??ta??
  • ???µa t?p?? set (s????a a?e?a???)
  • ?p??efa??de? p???e??
  • const set empty
  • set add (int x, set s)
  • boolean member (int x, set s)
  • set union (set s1, set s2)
  • boolean subset (set s1, set s2)
  • boolean equal (set s1, set s2)
  • S?µe??s? ?e????µe ?t? ??e? ???ste? ?at?????a ?
    t?p?? boolean.

43
?a??de??µa ??? S????a (ii)
  • S?µas??????a
  • ????µat???? ???sµ?? member
  • member(x, empty) false
  • member(x, add(x, s)) true
  • member(x, add(y, s)) member(x, s) a? x ! y
  • ????µat???? ???sµ?? union
  • union(empty, s) s
  • union(add(x, s1), s2) add(x, union(s1, s2))

44
?a??de??µa ??? S????a (iii)
  • S?µas??????a
  • ????µat???? ???sµ?? subset
  • subset(empty, s) true
  • subset(add(x, s1), s2) band(member(x, s2),
    subset(s1, s2))
  • ????µat???? ???sµ?? equal
  • equal(s1, s2) band(subset(s1, s2),
    subset(s2, s1))

45
S???e???µ???? t?p?? ded?µ????
  • S???e???µ???? t?p?? ded?µ???? S??(concrete
    data type)
  • ?a?????e? t?? ?d??t?te? t?? t?p?? ded?µ????
  • ?a?????e? epa???ß?? t?? ???p???s? t??
  • ???e ???ssa p????aµµat?sµ?? ?p?st????e?
    ???sµ????? S??, p.?.
  • ap??? t?p?? a???a??? a???µ??, p?a?µat????
    a???µ??, ?a?a?t??e?, ??????? t?µ??
  • s???et?? t?p?? p??a?e? (arrays), e???af??
    (records), de??te? (pointers), s????a (sets)

46
S??s? ??? ?a? S?? (i)
  • ??? set s????a a?e?a??? s 1, 3, 7, 9
  • ???p???s? 1 p??a?a? ap? boolean
  • ???p???s? 2 bits e??? p??a?a ap? int
  • Stat???? t??p?? ???p???s?? S??

47
S??s? ??? ?a? S?? (ii)
  • ???p???s? 3 ap?? s??dedeµ??? ??sta
  • ???aµ???? t??p?? ???p???s?? S??
  • ???p???s? 4 s?µß???se??? p?? pe????e? t?
    µa??µat??? ???sµ? t?? s??????
  • "x x1 \/ x3 \/ x7 \/ x9"
  • "x 1ltxlt10 /\ x21 /\ xltgt5"
  • ??s???? ? ???p???s? t?? p???e??
  • ?pe???s????a "x x gt 100"

48
???a?e? ?? ??? (i)
  • ?as??? p???? p??sp??as? st???e???
  • ai
  • S?????? ???p?????ta? µe ??p??? S?? p??????
    (arrays)
  • ??st?? p??sp??as?? O(1)
  • ? S?? t?? µ???d??stat?? p??a?a epa??e? ??a t??
    ???p???s? ???e ??? p??a?a
  • S????t?s? loc ?p??????e? t? ??s? e??? st???e???
    t?? ??? p??a?a st? µ???d??stat? S?? p??a?a t??
    ???p???s??

49
???a?e? ?? ??? (ii)
  • ??? p??a?a d?? d?ast?se?? n ? m
  • loc (n, m, i, j) m (i 1) j 1
  • ????µ?s? ?at? st??e?
  • loc (n, m, i, j) n (j 1) i 1

50
???a?e? ?? ??? (iii)
  • ??? ??t? t????????? p??a?a n ? n
  • loc (n, i, j) i (i 1) / 2 j 1
  • ?µ???? ??a s?µµet?????? p??a?e?

51
???a?e? ?? ??? (iv)
  • ??? t??d?a?????? p??a?a n ? n

loc (n, i, j) 2 i j 3
52
???a?e? ?? ??? (v)
  • ??? a?a??? p??a?a n ? m
  • ???p???s? µe d?ad??? p??a?a
  • ???p???s? µe t?e?? p??a?e?
  • row 1, 2, 3, 3, 4 col 1, 3, 2, 3, 5
  • val a1, a2, a3, a4, a5

53
??a??t?s? se p??a?e? (i)
  • Se???a?? a?a??t?s?
  • ?a st???e?a d?at?????ta? ?at? se???
  • ??st?? O(n)
  • ?e?t??s? st???e?? f?????? (sentinel)

7
54
??a??t?s? se p??a?e? (ii)
  • ???p???s? se C
  • int ssearch (int a, int n, int x)
  • int i
  • for (i 0 i lt n i)
  • if (ai x)
  • return i
  • return -1

55
??a??t?s? se p??a?e? (iii)
  • ???p???s? se C µe f?????
  • int ssearch_s (int a, int n, int x)
  • int i
  • an x
  • for (i 0 ai ! x i)
  • return (i lt n) ? i -1

56
??a??t?s? se p??a?e? (iv)
  • ??ad??? a?a??t?s?
  • ? p??a?a? p??pe? ?a e??a? ta????µ?µ????
  • ??st?? O(logn)
  • ???e? µ???d?? a?a??t?s??
  • ?????te?? ??st?? ? pe??ss?te??? ?????
  • ???a?e? ?ata?e?µat?sµ?? (hash tables)

57
?a????µ?s? p?????? (i)
  • ?a????µ?s? ep?????? (selection sort)
  • ?d?a
  • ??a ???e i ?at a????sa se???
  • ß?e? t? µ????te?? t?? st???e??? µet? t? ai
  • a?t?µet??es? t? µe t? ai
  • ??st?? ?(n2)
  • ?e?t??s? st???e?? f??????

58
?a????µ?s? p?????? (ii)
  • ?a????µ?s? ep??????, ???p???s? se C
  • void ssort (int a, int n)
  • int i, j
  • for (i 0 i lt n-1 i)
  • int min ai, minj i
  • for (j i1 j lt n j)
  • if (aj lt min)
  • min aminj j
  • aminj ai
  • ai min

59
?a????µ?s? p?????? (iii)
  • ?a????µ?s? e?sa????? (insertion sort)
  • ???s?µ?p????µe a?t? t?? t??p? ?ta? ta????µ??µe ta
    ?a?t?? µ?a? t??p???a?
  • ?d?a
  • ??a ???e i ap? t? de?te?? ?a? ?at a????sa
    se???
  • t?p???t?se t? ai st? s?st? t?? ??s?µeta?? t??
    st???e??? p?? e??a? p??? ap? a?t?
  • ??st?? ?(n2)
  • ?e?t??se??
  • st???e?? f??????
  • d?ad??? e?sa????

60
?a????µ?s? p?????? (iv)
  • ?a????µ?s? e?sa?????, ???p???s? se C
  • void isort (int a, int n)
  • int i, j
  • for (i 1 i lt n i)
  • int x ai
  • for (j i-1 j gt 0 j--)
  • if (x lt aj)
  • aj1 aj
  • else
  • break
  • aj1 x

61
?a????µ?s? p?????? (v)
  • ?a????µ?s? f?sa??da? (bubble sort)
  • ?d?a
  • ??a ???e i ?at a????sa se???
  • ??a ???e j gt i ?at? f?????sa se???
  • a? aj-1 gt aj a?t?µet??ese ta aj-1 ?a?
    aj
  • ??st?? ?(n2)
  • ?e?t??se??
  • staµat? a? se ??a p??asµa de? ???e? a?t?µet??es?
  • se p??? s?µe?? ????e ? te?e?ta?a a?t?µet??es?
  • a??a?? ?ate????s?? µeta?? d?ad?????? pe?asµ?t??
    (shake sort)

62
?a????µ?s? p?????? (vi)
  • ?a????µ?s? f?sa??da?, ???p???s? se C
  • void bsort (int a, int n)
  • int i, j
  • for (i 0 i lt n i)
  • for (j n-1 j gt i j--)
  • if (aj-1 gt aj)
  • int temp aj-1
  • aj-1 aj
  • aj temp

63
?a????µ?s? p?????? (vii)
  • ?a????µ?s? µe d?aµ???s? (quick sort)
  • ?d?a
  • d???e?e ??a t??a?? st???e?? x t?? p??a?a
  • d?aµ???se t?? p??a?a, µetaf????ta?
  • ta st???e?a µ????te?a t?? x st?? a???
  • ta st???e?a µe?a??te?a t?? x st? t????
  • a?ad??µ???, ta????µ?se ta d?? µ???
  • ??st?? ?(n2) st? ?e???te?? pe??pt?s? ?(n logn)
    ?at? µ?s? ???

64
?a????µ?s? p?????? (viii)
  • Quick sort, ???p???s? se C
  • void qsort (int a, int n)
  • qsort_auxil(a, 0, n-1)
  • void qsort_auxil (int a, int lower,
  • int upper)
  • if (lower lt upper)
  • int x a(lower upper) / 2
  • int i, j
  • for (i lower, j upper
  • i lt j i, j--)
  • while (ai lt x) i
  • while (aj gt x) j--

65
?a????µ?s? p?????? (ix)
  • Quick sort, ???p???s? se C (s????e?a)
  • if (i lt j)
  • int temp ai
  • ai aj
  • aj temp
  • qsort_auxil(a, lower, j)
  • qsort_auxil(a, i, upper)

66
?a????µ?s? p?????? (x)
  • ????? t??p?? ta????µ?s??
  • ?a????µ?s? t?? Shell (shell sort),??st?? ?(n2)
  • ?a????µ?s? se s??? (heap sort),??st?? ?(n
    logn)
  • ?a????µ?s? µe s?????e?s? (merge sort),??st??
    ?(n logn)

67
???sµ?? t?p??
  • S?????µa ap??? t?p??
  • typedef double real
  • real x, y
  • S?????µa s???et?? t?p??
  • typedef double vector 10
  • vector v
  • for (i 0 i lt 10 i)
  • vi i

68
?pa???µ?se?? (enumerations)
  • ? t?p?? enum
  • enum color_tag
  • GREEN, RED, BLUE, WHITE, BLACK
  • enum color_tag c
  • c GREEN
  • typedef enum
  • GREEN, RED, BLUE, WHITE, BLACK
  • color
  • color c WHITE

69
??µ?? (structures) (i)
  • ? t?p?? struct
  • struct complex_tag
  • double re, im
  • struct complex_tag z

z.re 1.0 z.im -1.0 typedef struct
complex_tag complex complex z complex zs 100
70
??µ?? (structures) (ii)
  • ?a??de??µa
  • complex complement (complex z)
  • complex result
  • result.re z.re
  • result.im -z.im
  • return result

71
??µ?? (structures) (iii)
  • ?a??de??µa
  • typedef struct
  • char firstName 30
  • char lastName 50
  • char phone 10
  • int age
  • double salary
  • record
  • record r 1000
  • r423.age 32

72
???se?? (unions)
  • ? t?p?? union
  • union number_tag
  • int i
  • double d
  • union number_tag n

n.d 1.2 printf("lf\n", n.d) n.i
42 printf("d\n", n.i) printf("lf\n", n.d)
/ wrong! /
73
?e??te? (pointers) (i)
  • ?etaß??t??
  • p?? de? pe??????? µ?a t?µ? ??p???? ap??? t?p??
    ded?µ????
  • a??? t? d?e????s? µ?a? ????? µetaß??t??
  • ?a??de??µa
  • int x, p

42
74
?e??te? (pointers) (ii)
  • ?e??t?d?t?s?
  • ? d?e????s? µ?a? µetaß??t??
  • p x
  • ?p?de??t?d?t?s?
  • t? pe??e??µe?? µ?a? d?e????s??
  • printf("d", p)
  • p 7

42
75
???a?e? ?a? de??te? (i)
  • ????µ?t??? de??t??
  • int a3 7, 6, 42
  • int p

p (a0) p a p a
printf("d\n", p) printf("d\n", (p1)) p
p2 printf("d\n", p)
76
???a?e? ?a? de??te? (ii)
  • ?s?d??aµ?a p?????? ?a? de??t??
  • ??a? p??a?a? e??a? ??a? de??t?? st? p??t?
    st???e??.
  • ai ?s?d??aµ? µe (ai)
  • ?? p??a?e? ?µ?? e??a? sta?e??? de??te?, d??ad?
    de? µp????? ?a a??????? t?µ?
  • int a3 7, 6, 42
  • int p a
  • p / correct /
  • a / wrong! /

77
???a?e? ?a? de??te? (iii)
  • ?a??de??µa a?t???af? p??????
  • int a10, b10
  • int p a, q b, i
  • / assume b initialized /
  • for (i0 ilt10 i)
  • p q

78
???a?e? ?a? de??te? (iv)
  • ?a??de??µa e?t?p?s? s?µß???se????
  • void putchar (char c)
  • char s "Hello world!\n"
  • char p
  • for (p s p ! '\0' p)
  • putchar(p)

79
?e??? de??t?? ?a? de??t?? se ?e??
  • ? ?e??? de??t?? NULL
  • ?pa???e?eta? ?a ap?de??t?d?t??e?!
  • int p NULL
  • p 42 / wrong! /
  • ? µ???? de??t?? p?? a?t?st???e? st?? ?e?d? ??????
    t?µ?, ?ta? ???s?µ?p??e?ta? se s??????
  • ? de??t?? se ?e?? void
  • Ge???? µ??f? de??t?
  • ?pa???e?eta? ?a ap?de??t?d?t??e?!
  • ?pa???e?eta? ? a???µ?t??? de??t??!

80
?e??te? a?t? pe??sµat?? µe a?af???
  • ??d??a? Pascal
  • procedure inc (var x integer)
  • begin
  • x x1
  • end
  • ... inc(a) ...
  • ?s?d??aµ?? ??d??a? C
  • void inc (int px)
  • (px)
  • ... inc(a) ...

81
?e??te? se e???af??
  • S??t?µ???af?a
  • p-gtx e??a? ?s?d??aµ? µe (p).x
  • ?a??de??µa
  • struct
  • int x, y
  • coordinates, p
  • coordinates.x 1
  • coordinates.y 3
  • p coordinates
  • printf("d\n", p-gtx)
  • printf("d\n", p-gty)

82
?etat??p?? t?p?? (i)
  • ?µµese? (coercions)
  • double d 3 (µe a???es?)
  • int x 3.14
  • int f (int x) (µe p??asµa pa?aµ?t???)
  • f(3.14)
  • ?µese? (type casting)
  • ( t?p?? ) ??f?as?
  • (double) 3
  • (int) 3.14
  • (int ) NULL

83
?etat??p?? t?p?? (ii)
  • ???ß??µa p?? µp??? ?a t?p?s? t? ap?t??esµa t??
    p?a?µat???? d?a??es?? d?? a?e?a??? a???µ??, ?????
    ??e? µetaß??t??
  • int x 5, y 3
  • ????? ??s? 1 printf("d", x/y)
  • ????? ??s? 2 printf("lf", x/y)
  • S?st?? ??se??
  • printf("lf", 1.0 x / y)
  • printf("lf", (double) x / (double) y)
  • printf("lf", (double) x / y)

84
???aµ??? pa?a????s? µ??µ?? (i)
  • S??a?t?se?? ß?ß???????? ltstdlib.hgt
  • void malloc (size_t n)
  • ???aµ??? pa?a????s? µ??µ?? µ????? n bytes.??
    ap?t??esµa p??pe? ?a µetat?ape? st? s?st?
    t?p?.?p?st??feta? NULL a? e?a?t???e? ? µ??µ?.
  • void free (void p)
  • ?p?d?sµe?s? t?? µ??µ?? st?? ?p??a de???e? t?
    p.?? p p??pe? ?a ??e? d?µ???????e? µe
    p??????µe?? ???s? st? malloc.
  • ??sa bytes ??e?????ta?
  • sizeof(type) p.?. sizeof(int)
  • sizeof(variable) p.?. sizeof(x)

85
???aµ??? pa?a????s? µ??µ?? (ii)
  • ?a??de??µa
  • int p
  • int i
  • p (int ) malloc(sizeof(int))
  • p 42
  • free(p)
  • p (int ) malloc(10 sizeof(int))
  • for (i 0 i lt 10 i)
  • pi 42
  • free(p)

86
???aµ???? S?? (i)
  • ???s?µ?p?????ta? ??a t?? ???p???s? ???
  • ?a?ade??µata
  • s??dedeµ??e? ??ste?
  • d??t?a
  • ???f??
  • ??? ???p?????ta? st? C
  • µe ?at?????? s??d?asµ? d?µ?? (struct)?a? de??t??
    (pointers), ?a?
  • µe d??aµ??? pa?a????s? µ??µ??

87
???aµ???? S?? (ii)
  • ??e??e?t?µata ??a?t? stat???? S??
  • ?e? ep?ß?????? pe?????sµ??? st? µ???st? p?????
    t?? ded?µ????
  • ? µ??µ? p?? ???s?µ?p??e?ta? e??a? a?????? t??
    p?a?µat???? p?????? t?? ded?µ????
  • ??p??e? p???e?? ???p?????ta? ap?d?t???te?a
  • ?e???e?t?µata ??a?t? stat???? S??
  • G?a sta?e?? ?a? ???st? p????? ded?µ????,
    ???s?µ?p????? s?????? pe??ss?te?? µ??µ?
  • ??p??e? p???e?? ???p?????ta? ????te?? ap?d?t???

88
???aµ???? S?? (iii)
  • ?d?a
  • ?atas?e???eta? ??a s????? ??µß??
  • ???e ??µß?? pe????e? p????f???e? ?a? s??d?sµ???
    p??? ?????? ??µß???

89
?p?? s??dedeµ??e? ??ste? (i)
  • ???a? ??aµµ???? d?at??e??
  • ???e ??µß?? pe????e? ??a s??desµ? st?? ep?µe??
    ??µß?
  • ? te?e?ta??? ??µß?? ??e? ?e?? s??desµ?

90
?p?? s??dedeµ??e? ??ste? (ii)
  • ?a??de??µa ??sta a?e?a???
  • struct node_tag
  • int data
  • struct node_tag next
  • typedef struct node_tag Node,
    LinkedList
  • ?e?? ??sta

91
?p?? s??dedeµ??e? ??ste? (iii)
  • ???s???? st???e???
  • ap?fas? p???a p??ste?e?
  • d?µ??????a???? ??µß??
  • a?t???af?p????f???a?
  • s??des????? ??µß??

i
92
?p?? s??dedeµ??e? ??ste? (iv)
  • ?fa??es? st???e???
  • ap?fas? p???st???e???a afa??e?e?
  • ?atast??f???µß??
  • s??des??p????p????µß??

93
????? (i)
  • First In First Out (FIFO)?,t? µpa??e? p??t?,
    ß?a??e? p??t?
  • ???? a?e?a???
  • ??? queue
  • const queue queueEmpty
  • void queueInsert (queue qp, int t)
  • int queueRemove (queue qp)
  • int queueHead (queue q)

94
????? (ii)
  • ???p???s? µe ap?? s??dedeµ??? ??sta

95
???p???s? ????? se C (i)
  • ???p???s? µe ap?? s??dedeµ??? ??sta
  • typedef struct list_tag
  • int data
  • struct list_tag next
  • ListNode
  • ??p?? queue
  • typedef struct
  • ListNode first
  • ListNode last
  • queue

96
???p???s? ????? se C (ii)
  • ?de?a ????
  • const queue queueEmpty NULL, NULL
  • ??sa???? st???e???
  • void queueInsert (queue qp, int t)
  • ListNode n (ListNode )
    malloc(sizeof(ListNode))
  • if (n NULL)
  • printf("Out of memory\n")
  • exit(1)

97
???p???s? ????? se C (iii)
  • ??sa???? st???e??? (s????e?a)
  • n-gtdata t
  • n-gtnext NULL
  • if (qp-gtlast NULL)
  • qp-gtfirst qp-gtlast n
  • else
  • qp-gtlast-gtnext n
  • qp-gtlast n

98
???p???s? ????? (iv)
  • ?fa??es? st???e???
  • int queueRemove (queue qp)
  • ListNode n
  • int result
  • if (qp-gtfirst NULL)
  • printf("Nothing to remove"
  • " from an empty queue\n")
  • exit(1)

99
???p???s? ????? (v)
  • ?fa??es? st???e??? (s????e?a)
  • n qp-gtfirst
  • result qp-gtfirst-gtdata
  • qp-gtfirst qp-gtfirst-gtnext
  • free(n)
  • if (qp-gtfirst NULL)
  • qp-gtlast NULL
  • return result

100
???p???s? ????? (vi)
  • ???tas? st???e???
  • int queueHead (queue q)
  • if (q.first NULL)
  • fprintf(stderr, "Nothing to see" " in
    an empty queue\n")
  • exit(1)
  • return q.first-gtdata

101
St??ße? (i)
  • Last In First Out (LIFO)?,t? µpa??e? te?e?ta??,
    ß?a??e? p??t?
  • St??ßa a?e?a???
  • ??? stack
  • const stack stackEmpty
  • void stackPush (stack sp, int t)
  • int stackPop (stack sp)
  • int stackTop (stack s)

102
St??ße? (ii)
  • ???p???s? µe ap?? s??dedeµ??? ??sta

103
???p???s? st??ßa? se C (i)
  • ??p?? stack
  • typedef ListNode stack
  • ?de?a st??ßa
  • const stack stackEmpty NULL
  • ??sa???? st???e???
  • void stackPush (stack sp, int t)
  • ListNode n (ListNode )
    malloc(sizeof(ListNode))
  • if (n NULL)
  • printf("Out of memory\n")
  • exit(1)

104
???p???s? st??ßa? se C (ii)
  • ??sa???? st???e??? (s????e?a)
  • n-gtdata t
  • n-gtnext sp
  • sp n
  • ?fa??es? st???e???
  • int stackPop (stack sp)
  • ListNode n
  • int result
  • if (sp NULL)
  • printf("Nothing to remove"
  • " from an empty stack\n")
  • exit(1)

105
???p???s? st??ßa? se C (iii)
  • ?fa??es? st???e??? (s????e?a)
  • n sp
  • result (sp)-gtdata
  • sp (sp)-gtnext
  • free(n)
  • return result

106
???p???s? st??ßa? se C (iv)
  • ???tas? st???e???
  • int stackTop (stack s)
  • if (s NULL)
  • printf("Nothing to see"
  • " in an empty stack\n")
  • exit(1)
  • return s-gtdata

107
??p?? s??dedeµ??e? ??ste? (i)
  • ?p?s?? ??aµµ???? d?at??e??
  • ??? s??desµ?? se ???e ??µß?, p??? t?? ep?µe?? ?a?
    p??? t?? p??????µe??
  • Ge???? µ??f?, p.?. ??a ???p???s? ?????

108
??p?? s??dedeµ??e? ??ste? (ii)
  • ??p?? ??µß?? DListNode
  • typedef struct DListNode_tag
  • int data
  • struct DListNode_tag next
  • struct DListNode_tag prev
  • DListNode
  • ??p?? dlist
  • typedef struct
  • DListNode first
  • DListNode last
  • dlist
  • ?de?a ??sta
  • const dlist dlistEmpty NULL, NULL

109
??p?? s??dedeµ??e? ??ste? (iii)
  • ??sa???? st???e??? st?? a???
  • void dlistInsert (dlist lp, int t)
  • DListNode n (DListNode )
    malloc(sizeof(DListNode))
  • if (n NULL)
  • fprintf(stderr, "Out of memory\n")
  • exit(1)
  • n-gtdata t

110
??p?? s??dedeµ??e? ??ste? (iv)
  • ??sa???? st???e??? st?? a??? (s????e?a)
  • if (lp-gtfirst NULL)
  • n-gtprev n-gtnext NULL
  • lp-gtfirst lp-gtlast n
  • else
  • n-gtprev NULL
  • n-gtnext lp-gtfirst
  • lp-gtfirst-gtprev n
  • lp-gtfirst n

111
???????? ??ste? (i)
  • ??p?? clist
  • typedef struct
  • ListNode first
  • ListNode last
  • clist
  • ?de?a ??sta
  • const clist clistEmpty NULL, NULL

112
???????? ??ste? (ii)
  • ??sa???? st???e???
  • void clistInsert (clist lp, int t)
  • ListNode n (ListNode )
    malloc(sizeof(ListNode))
  • if (n NULL)
  • fprintf(stderr, "Out of memory\n")
  • exit(1)
  • n-gtdata t

113
???????? ??ste? (iii)
  • ??sa???? st???e??? (s????e?a)
  • if (lp-gtfirst NULL)
  • lp-gtfirst lp-gtlast n
  • n-gtnext n
  • else
  • n-gtnext lp-gtfirst
  • lp-gtlast-gtnext n
  • lp-gtlast n

114
???????? ??ste? (iv)
  • ?fa??es? st???e???
  • int clistRemove (clist lp)
  • int result
  • if (lp-gtfirst NULL)
  • fprintf(stderr, "Nothing to remove" "
    from empty list\n")
  • exit(1)
  • result lp-gtfirst-gtdata

115
???????? ??ste? (v)
  • ?fa??es? st???e??? (s????e?a)
  • if (lp-gtfirst lp-gtlast)
  • free(lp-gtfirst)
  • lp-gtfirst lp-gtlast NULL
  • else
  • lp-gtfirst lp-gtfirst-gtnext
  • free(lp-gtlast-gtnext)
  • lp-gtlast-gtnext lp-gtfirst
  • return result

116
???????? ??ste? (vi)
  • ??t?p?s? st???e???
  • void clistPrint (clist l)
  • ListNode n
  • for (n l.first n ! NULL n
    n-gtnext)
  • printf("d\n", n-gtdata)
  • if (n-gtnext l.first)
  • break

117
?a????µ?µ??e? ??ste? (i)
  • ??p?? slist
  • typedef ListNode slist
  • ?de?a ??sta
  • const slist slistEmpty NULL
  • ??sa???? st???e???
  • void slistInsert (slist lp, int t)
  • ListNode n (ListNode )
    malloc(sizeof(ListNode))
  • if (n NULL)
  • printf("Out of memory\n")
  • exit(1)

118
?a????µ?µ??e? ??ste? (ii)
  • ??sa???? st???e??? (s????e?a)
  • n-gtdata t
  • while (lp ! NULL (lp)-gtdata lt t)
  • lp ((lp)-gtnext)
  • n-gtnext lp
  • lp n

119
?a????µ?µ??e? ??ste? (iii)
  • ?fa??es? st???e???
  • void slistRemove (slist lp, int t)
  • ListNode n
  • while (lp ! NULL (lp)-gtdata lt t)
  • lp ((lp)-gtnext)
  • if (lp NULL)
  • printf(d was not found\n, t)
  • exit(1)
  • n lp
  • lp (lp)-gtnext
  • free(n)

120
?a??µet??? t?? p?????µµat?? (i)
  • ?p??efa??da t?? p?????µµat??
  • int main (int argc, char argv)
  • ?a??µet???
  • argc ? a???µ?? t?? pa?aµ?t???
  • argvi ? i-?st? pa??µet???
  • argv0 t? ???µa t?? p?????µµat??
  • ?p?t??esµa
  • a???a??? a???µ?? p?? ep?st??feta? st? ?e?t???????
    s?st?µa
  • s?????? 0 ??a ep?t??? te?µat?sµ?

121
?a??µet??? t?? p?????µµat?? (ii)
  • ?a??de??µa
  • int main (int argc, char argv)
  • int i
  • printf("Program s called with "
  • "d parameters\n", argv0,
  • argc-1)
  • for (i 1 i lt argc i)
  • printf(" s", argvi)
  • printf("\nand will return 0\n")
  • return 0

122
S??a?t?se?? e?s?d??-e??d?? (i)
  • ?as???? s??a?t?se?? e?s?d??-e??d??
  • int printf (const char format, ...)
  • int scanf (const char format, ...)
  • ??d???? ?a?a?t??e? st? format
  • ????a??? a???µ??
  • d st? de?ad??? s?st?µa
  • u ????? p??s?µ? st? de?ad??? s?st?µa
  • o ????? p??s?µ? st? ??tad??? s?st?µa
  • x ????? p??s?µ? st? de?ae?ad??? s?st?µa

123
S??a?t?se?? e?s?d??-e??d?? (ii)
  • ??d???? ?a?a?t??e? st? format
  • ????µ?? ????t?? ?p?d?ast????f se µ??f?
    -ddd.dddddde se µ??f? -ddd.dddddd e
    /-dddg se µ??f? f ? e
  • ????? t?p??c ?a?a?t??e?s s?µß???se????p de??
    te?

124
S??a?t?se?? e?s?d??-e??d?? (iii)
  • ?a?a??a??? st? format
  • ???e??? a???µ??h a???µ?? short p.?. hd,
    hxl a???µ?? long ? double p.?. ld,
    lfL a???µ?? long double p.?. Lf
  • ????? ap?te??sµat??8d a???µ?? se µ???? 8
    ?a?a?t????20s s?µß???se??? se µ???? 20
    ?a?a?t????8d a???µ?? se µ???? 8 ?a?a?t???? µe
    08d a???µ?? se µ???? 8 ?a?a?t????, ta p??ta
    0-8d ?p?? t? 8d µe st????s? a??ste??

125
S??a?t?se?? e?s?d??-e??d?? (iv)
  • ??s?d??-???d?? ?a?a?t????
  • int putchar (int c)
  • int getchar ()
  • ??s?d??-???d?? s?µß???se????
  • int puts (const char s)
  • char gets (char s)
  • ??e???? t????? ded?µ????
  • int eof ()
  • ? sta?e?? EOF pa??st??e? t? t???? t?? ded?µ????
    ?a? ??e? t?p? int.

126
?a??de??µa
  • ??t???af? ded?µ????
  • ?? ?a?a?t??e? p?? d?aß????ta? e?t?p????ta?, µ????
    ?a pa???s?aste? t???? ded?µ????
  • void main ()
  • int c
  • while ((c getchar()) ! EOF)
  • putchar(c)

127
S??a?t?se?? d?a?e???s?? a??e??? (i)
  • ??p?? a??e???
  • FILE fp
  • ?????µa a??e???
  • FILE fopen (const char filename,
    const char mode)
  • ?a??µet??? moder a?????s? (read)w e???af?
    (write)a p??s???? (append)t ?e?µe??
    (text)b d?ad??? ded?µ??a (binary)

128
S??a?t?se?? d?a?e???s?? a??e??? (ii)
  • ??e?s?µ? a??e???
  • int fclose (FILE fp)
  • ??s?d??-???d?? ?a?a?t????
  • int fputc (int c, FILE fp)
  • int fgetc (FILE fp)
  • ??s?d??-???d?? s?µß???se????
  • int fputs (const char s, FILE fp)
  • char fgets (char s, int n, FILE
    fp)

129
S??a?t?se?? d?a?e???s?? a??e??? (iii)
  • ?as???? s??a?t?se?? e?s?d??-e??d??
  • int fprintf (FILE fp, const char
    format, ...)
  • int fscanf (FILE fp, const char
    format, ...)
  • ??e???? t????? a??e???
  • int feof (FILE fp)

130
S??a?t?se?? d?a?e???s?? a??e??? (iv)
  • ??s?d??-???d?? p????? ded?µ????
  • size_t fwrite (const void p,size_t size,
    size_t num, FILE fp)
  • size_t fread (void p,size_t size, size_t num,
    FILE fp)
  • ? a???a??? t?p?? size_t ???s?µ?p??e?ta? ??a t?
    µ?t??s? ????? µ??µ?? se bytes.

131
?a??de??µa (i)
  • ??t???af? d?ad???? a??e???
  • int main (int argc, char argv)
  • FILE fin, fout
  • unsigned char buffer1000
  • size_t count
  • fin fopen(argv1, "rb")
  • if (fin NULL)
  • return 1
  • fout fopen(argv2, "wb")
  • if (fout NULL)
  • return 2

132
?a??de??µa (ii)
  • (s??e???eta?)
  • while (!feof(fin))
  • count fread(buffer, 1,
    1000, fin)
  • fwrite(buffer, 1, count, fout)
  • fclose(fin)
  • fclose(fout)
  • return 0

133
S??a?t?se?? ß?ß???????? (i)
  • ??s?d?? ?a? ???d?? ltstdio.hgt
  • ?e????e? ??e? t?? s??a?t?se?? e?s?d??-e??d??
  • ????a????sµ??a a??e?a
  • FILE stdin t?p??? e?s?d??FILE
    stdout t?p??? ???d??FILE stderr t?p???
    ???d?? sfa?µ?t??
  • ?s?d??aµ?e?
  • printf(...) ? fprintf(stdout, ...)scanf(...) ?
    fscanf(stdin, ...)
  • ?.?p.
  • S??a?t?se?? d?a?e???s?? a??e??? µe t??a?a
    p??sßas? (random access)

134
S??a?t?se?? ß?ß???????? (ii)
  • ??a?e???s? s?µß???se???? ltstring.hgt
  • size_t strlen (const char s)
  • ??t??s? a???µ?? ?a?a?t???? t?? s?µß???se???? s.
  • char strcpy (char s1, const
    char s2)
  • ??t???af? t?? s?µß???se???? s2 st?? s1.
  • char strcat (char s1, const
    char s2)
  • ???s???? t?? s?µß???se???? s2 st? t???? t?? s1.
  • int strcmp (const char s1,
  • const char s2)
  • S?????s? t?? s?µß???se???? s1 ?a? s2.

135
S??a?t?se?? ß?ß???????? (iii)
  • ?etat??p? s?µß???se???? ltstdlib.hgt
  • int atoi (const char s)
  • ?etat??p? t?? s?µß???se???? s se int.
  • long int atol (const char s)
  • ?etat??p? t?? s?µß???se???? s se long int.
  • double atof (const char s)
  • ?etat??p? t?? s?µß???se???? s se double.

136
?a??de??µa
  • ??a d??at? ???p???s? t?? strcmp
  • int strcmp (const char s1,
  • const char s2)
  • while (s1 s2 s1 ! '\0')
  • s1 s2
  • return (s1)-(s2)
  • ?p??es? ?? t?µ?? t?? t?p?? char e??a? st?
    d??st?µa 0-255 (??? a???t????)

? while (s1 s1 s2)
137
???epe?e??ast?? (preprocessor) (i)
  • ??t??? include
  • include ltstdio.hgt
  • include "myheader.h"
  • ??t??? define
  • define MAX_CHARS 1000
  • char sMAX_CHARS
  • define INC(x) ((x))
  • INC(a)
  • INC(p)

138
???epe?e??ast?? (preprocessor) (ii)
  • ??t???? ifdef, ifndef, else ?a? endif
  • define DEBUG
  • ifdef DEBUG
  • printf("debugging is on\n")
  • else
  • printf("debugging is off\n")
  • endif
  • ifndef DEBUG
  • printf("optimizations allowed\n")
  • endif

139
??ad??? d??d?a (i)
  • Binary trees
  • ??? s??desµ?? se ???e ??µß?
  • a??ste?? ?a? de?? pa?d?
  • ???e ??µß?? ??e?0, 1 ? 2 pa?d??
  • ???a ? a?????? ??µß?? t?? d??d???
  • F???a ??µß?? ????? pa?d??
  • ????? ??µß?? a???µ?? s??d?sµ?? ap? t? ???a

140
??ad??? d??d?a (ii)
  • ??p?? ??µß?? TreeNode
  • typedef struct TreeNode_tag
  • int data
  • struct TreeNode_tag left
  • struct TreeNode_tag right
  • TreeNode
  • ??p?? tree
  • typedef TreeNode tree
  • ?e?? d??d??
  • const tree treeEmpty NULL

141
??ad??? d??d?a (iii)
  • ??sa???? se d??d?a
  • ?a????st??? ap?fas? se p??? s?µe?? t?? d??d???
    ?a e?sa??e? ? ???? ??µß??
  • ?s?????sµ??a d??d?a (balanced trees) t? ß????
    d?? f????? d?af??e? t? p??? ?at? 1
  • S????t?s? µ???st?? ß?????
  • int treeDepth (tree t)
  • if (t NULL) return 0
  • return 1 max(treeDepth(t-gtleft),
  • treeDepth(t-gtright))

142
??ad??? d??d?a (iv)
  • ??sa???? se ?s?????sµ??a d??d?a
  • void treeBalancedInsert (tree t, int d)
  • if (t NULL)
  • t (TreeNode ) malloc(sizeof(TreeN
    ode))
  • if (t NULL)
  • printf("Out of memory\n")
  • exit(1)
  • (t)-gtdata d
  • (t)-gtleft (t)-gtright NULL

143
??ad??? d??d?a (v)
  • ??sa???? se ?s?????sµ??a d??d?a (s????e?a)
  • else
  • int d1 treeDepth((t)-gtleft)
  • int d2 treeDepth((t)-gtright)
  • if (d1 lt d2)
  • treeBalancedInsert(
    ((t)-gtleft), d)
  • else
  • treeBalancedInsert(
    ((t)-gtright), d)

144
???d?a ?e????? µ??f??
  • ???e ??µß?? k ??e? nkap???????, ?p?? nk??
  • ??d???p???s? µe d?ad??? d??d?a

145
???s??s? d?ad???? d??d??? (i)
  • Se??? µe t?? ?p??a d?as?????ta? ?? ??µß??
  • ?at? ß???? (depth first)
  • ?at? p??t?? (breadth first)
  • ?at? ß????
  • 42, 10, 7, 14, 12, 30, 50, 55, 52
  • ?at? p??t??
  • 42, 10, 50, 7, 14, 55, 12, 30, 52

146
???s??s? d?ad???? d??d??? (ii)
  • ??t?p?s? ?at? ß????
  • p??? ap??, µe ???s? a?ad??µ??
  • ???p???s?
  • void treePrintDF (tree t)
  • if (t ! NULL)
  • printf("d ", t-gtdata)
  • treePrintDF(t-gtleft)
  • treePrintDF(t-gtright)

147
???s??s? d?ad???? d??d??? (iii)
  • ??t?p?s? ?at? p??t??
  • µe t? ß???e?a ????? ??a t?? ap????e?s? de??t??
    p??? t??? ??µß??? p?? de? ????µe ep?s?ef?e?
  • ???p???s?
  • void treePrintBF (tree t)
  • queue q queueEmpty
  • if (t ! NULL)
  • queueInsert(q, t)

148
???s??s? d?ad???? d??d??? (iv)
  • ??t?p?s? ?at? p??t??, ???p???s? (s????e?a)
  • while (!queueIsEmpty(q))
  • TreeNode n queueRemove(q)
  • printf("d ", n-gtdata)
  • if (n-gtleft ! NULL)
  • queueInsert(q, n-gtleft)
  • if (n-gtright ! NULL)
  • queueInsert(q, n-gtright)

149
????µ?t???? e?f??se?? (i)
  • ?a??de??µa
  • 3 (24 / 2) (9 - 3)
  • ?a??stas? se µ??f?d?ad???? d??d???
  • ?? a???µ??sta f???a
  • ?? te?est?? st????p????p??? ??µß???
  • ???s??s? ?at? ß????

150
????µ?t???? e?f??se?? (ii)
  • ???eµat??? pa??stas?
  • infix notation
  • ? te?est?? a??µesa state???µe?a
  • d?f????µe????e?????ta?pa?e???se??
  • ? s?????? µ??f???a t?? ?????p?
  • ?p?t??esµa
  • (3 (24 / 2)) (9 - 3)

151
????µ?t???? e?f??se?? (iii)
  • ????eµat??? pa??stas?
  • prefix notation
  • ? te?est?? p??? tate???µe?a
  • ??? d?f????µe??,de ??e?????ta?pa?e???se??
  • ap?? µ??a????a?????s?
  • ?p?t??esµa
  • 3 / 24 2 - 9 3

152
????µ?t???? e?f??se?? (iv)
  • ?p??eµat??? pa??stas?
  • postfix notation
  • ? te?est?? µet? tate???µe?a
  • ??? d?f????µe??,de ??e?????ta?pa?e???se??
  • ap?? µ??a????ap?t?µ?s?
  • ?p?t??esµa
  • 3 24 2 / 9 3 -

153
??ad??? d??d?a a?a??t?s?? (i)
  • Binary search trees
  • ??ad??? d??d?a µe t??pa?a??t? ?d??t?te???a ???e
    ??µß?
  • ???? ?? ??µß?? t??a??ste??? pa?d??? ?????t?µ??
    µ????te?e? ? ?se?t?? t?µ?? t?? ??µß??
  • ???? ?? ??µß?? t??de???? pa?d??? ?????t?µ??
    µe?a??te?e? ? ?se?t?? t?µ?? t?? ??µß??

154
??ad??? d??d?a a?a??t?s?? (ii)
  • ?a d?ad??? d??d?a a?a??t?s??d?e????????? t??
    a?a??t?s? st???e???
  • ??ad??µ??? a?a??t?s?
  • a? ? t?µ? p?? ??te?ta? e??a? st? ???a, ß?????e
  • a? e??a? µ????te?? ap? t?? t?µ? t?? ???a?,a??e?
    ?a a?a??t??e? st? a??ste?? pa?d?
  • a? e??a? µe?a??te?? ap? t?? t?µ? t?? ???a?,a??e?
    ?a a?a??t??e? st? de?? pa?d?
  • ??st?? a?a??t?s?? O(log n)
  • ?p? t?? p???p??es? t? d??d?? ?a e??a? ?s?????sµ???

155
??ad??? d??d?a a?a??t?s?? (iii)
  • ??a??t?s?
  • TreeNode treeSearch (tree t, int key)
  • if (t NULL)
  • return NULL / not found /
  • if (t-gtdata key)
  • return t / found /
  • if (t-gtdata gt key)
  • return treeSearch(t-gtleft, key)
  • else
  • return treeSearch(t-gtright, key)

156
?e??te? se s??a?t?se??
  • ?a??de??µa 1
  • int f (int n)
  • int (p) (int n)
  • p f
  • printf("d", (p)(3))
  • ?a??de??µa 2
  • double (q) (double) sqrt
  • ?e??te? ?s?d??aµ?? µe s??a?t?se??
  • p f
  • printf("d", p(3))

157
????????s?
  • double integral (double a, double b,
  • double (f) (double))
  • const double step 1e-6
  • doub
Write a Comment
User Comments (0)
About PowerShow.com