Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
IB
3




       1




1.1
            (structure)
      (member)              1


 struct p o i n t {
        int x ;
       ...
4                                                                                                                      1

...
1.4                                                                                                                       ...
6                                                                                                                         ...
1.6                                                                                                                       ...
8                                                                                                           1



1.7      ...
1.8             :                                                               9

  2. (                               )
...
10          1



1.9
      1.9
11




          2




2.1
                                                 0                                             ...
12                                                                                                                        ...
2.4                                                                                              13



2.3.2              ...
14                                                                                                                        ...
2.5          :                                                                                                        15

...
16                                                                                     2
                                 ...
2.5           :                                                                                    17

  1                ...
18           2



2.6
      2.13
19




       3




                                                         (       )




3.1
3.1.1
                     ...
20                                                                                  3

x                                 I...
3.3                                                                                                 21



3.2.2




      ...
22                                                                           3

                                          ...
3.4            :        (     )                                                        23

          int popStack(struct c...
24                                                                        3

      3.10                                   ...
3.6           :                                                                                                           ...
26                                                                 3




         3.19

     •                            ...
27




        4




4.1
        f                              f                            f     (recursive function)
  ...
28                                                                                           4

                          ...
4.2                                                                                                                     29...
30                                                                                                                       4...
4.3                                                                                                          31

         ...
33




         5




                                                                    (         )


                  ...
34                                                                              5



5.2

                                ...
5.3                                                                                                      35

       int he...
37




          6

2


                          10,000                                             1                    ...
38                                                                              6   2



6.2
 2                           ...
6.3                                                                                                   39
                 ...
40                                                                                              6   2

          6.2


   ...
6.4                                                                                41
                                    ...
平成22年度情報通信工学実験IB実施資料
平成22年度情報通信工学実験IB実施資料
平成22年度情報通信工学実験IB実施資料
Upcoming SlideShare
Loading in …5
×

平成22年度情報通信工学実験IB実施資料

1,486 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

平成22年度情報通信工学実験IB実施資料

  1. 1. IB
  2. 2. 3 1 1.1 (structure) (member) 1 struct p o i n t { int x ; int y ; }; y x 1 tgif 1 & 1.1 1.2 int char point struct point /∗ s t r u c t p o i n t pt ∗/ struct p o i n t pt ; /∗ s t r u c t p o i n t maxpt point x 320 y 200
  3. 3. 4 1 x y 1.1 point ∗/ struct p o i n t maxpt = { 3 2 0 , 2 0 0 } ; /∗ ∗/ struct f l o a t p o i n t { float x ; float y ; } float pt ; (variable) ( ) (value) (type) *1 int i; ( )32 *2 i i i int pt 1.1 struct point pt struct point ( 32 +32 =64 ) pt struct point struct point 2 (320, 200) 1.3 x (structure member operator) “ . ” • 1 *3 • 2 struct p o i n t maxpt = { 3 2 0 , 2 0 0 } ; *1 (domain) *2 32 *3 {320, 200}.x
  4. 4. 1.4 5 /∗ x ∗/ p r i n t f ( ”%dn” , maxpt . x ) ; /∗ y ∗/ maxpt . y = 2 4 0 ; 1.4 struct p o i n t p t s [ 1 0 0 ] ; /∗ 3 pts1 ∗/ struct p o i n t p t s 1 [ ] = {{320 , 200} , {150 , 500} , {0 , 0}}; /∗ p t s 1 struct point ∗/ pts1 [ 1 ] . y = 240; 1.5 1.5.1 ( ) 1 1 *4 C 2 • (address operator) & • (indirection operator) * *5 X X X* int *pi; pi int *6 ( 1.2) *4 + *5 C segmentation fault bus error *6 C segmentation fault bus error
  5. 5. 6 1 pi 8632 8632 132 (int ) 1.2 int *pi; 1.5.2 2 • struct point pt; pt struct point (64 ) (320, 200) 2 • struct point *p; p ( 32 ) struct point • struct point *p = (struct point *)NULL; C NULL 0 32 p 0 0 int p (struct point *)NULL 0 ( , cast) • struct point *p = (struct point *)malloc(sizeof(struct point)); p malloc() – *7 ( ) – void * – ( ) sizeof ( !) sizeof(struct point) = 8(8 =64 ) malloc() sizeof(struct point) struct point * p ( 1.3) malloc() 1.5.3 1.3 x 8236 *p x (*p).x . * *7
  6. 6. 1.6 7 p (struct point * 8632 8632 malloc(sizeof(struct point)) 1.3 1.4 point2 C *8 C (*p).x p -> x 2 -> a -> b -> c (a -> b) -> c 1.6 ( ) struct p o i n t 2 { int x ; int y ; struct p o i n t 2 ∗ next ; }; struct p o i n t 2 ∗pp ; 1.4 next struct point2 pp p -> next x (pp -> next) -> x pp -> next -> x *9 *8 Lisp *9 ->
  7. 7. 8 1 1.7 : 1.2 C strcpy, strcmp, strcat • • 1.3 1.7 strcpy, strcmp, strcat my strcpy, my strcmp, my strcat strcpy my strcpy my strcpy, my strcmp, my strcat main() 1.8 : 1.8.1 • 3 1. char[256] 2. char[16] 3. int • 5 • • • 0 • 1.8.2 1.4 1.8.1 3 name, phone, number *10 1. ( ) void add(char *, char *, int); *10 name, phone, number 0 0 ( ) 1
  8. 8. 1.8 : 9 2. ( ) void delByName(char *); void delByPhone(char *); void delByNumber(int); delByName() delByPhone(), delByNumber() 3. ( ) void findByName(char *); void findByPhone(char *); void findByNumber(int); findByName() findByPhone(), findByNumber() 4. ( ) void printAll(); • • strcmp • name phone number 1.5 1.4 main() • main() 1.4( 1.7) *11 • 1.4 main() 1.6 1.8.1 1.7 1.4 1.6 1.8 1.7 main() *11 1
  9. 9. 10 1 1.9 1.9
  10. 10. 11 2 2.1 0 *1 [a1 , a2 , · · · , an ] *2 [] *3 (total order) 2 [1, 3, 2] • : 1( 1 ), 3( 2 ), 2( 3 ) • : 1( 1 ), 2( 3 ), 3( 2 ) 2.2 2.1 ML 2.2 2.3 Lisp, ML, Prolog, Java C *1 *2 I ML *3 Lisp ML “nil”
  11. 11. 12 2 F IRST (L) L EN D(L) L IN SERT (x, p, L) x L p L p 1 L p LOCAT E(x, L) L x 2 RET RIEV E(p, L) p L L p DELET E(p, L) p L L p N EXT (p, L), P REV IEOU S(p, L) p L N EXT (EN D(L), L) P REV IOU S(F IRST (L), L) L p N EXT , P REV IOU S M AKEN U LL() P RIN T LIST (L) L 2.1 nil cons(x, L) L x hd(L) L tl(L) L L@M L M 2.2 ML 2.3.1 2.3 [ a1, a2, ...., an] 2.3 1
  12. 12. 2.4 13 2.3.2 (1) ( ) 2.4 header a1 a2 a3 header 2.4 2.3.3 (2) Lisp ML [[1, 2, 3], [4, 5], nil] ML 2.5 head tail (car) (cdr) cell 1 3 5 2.5 (2) *4 2.4 *4 I
  13. 13. 14 2 1 ( ) personal_data next struct personal_data { char name[256]; char phone[16]; int number; }; 2 struct personal_data 2 struct personal_data * a, b struct personal_data *a, *b; /* malloc() a, b */ a( ) b( ) 3 2 a( ) printf() ( b ) 2.5 : 2.5.1 C C • ( ) ( 2.4 ) • int 1 • NULL NULL • 1 ( ) C 1 • 11 2.1
  14. 14. 2.5 : 15 • list.h list.c list-test.c make next 2.5.2 2.1 int 1 (struct cell) 2.2 struct cell *makeNullList(void); malloc *5 NULL , /* * Make null list and return pointer to list. */ struct cell *makeNullList(void) { struct cell *list; /* pointer to new list */ /* allocate memory for new list entry */ list = (struct cell*)malloc(sizeof(struct cell)); /* check the allocation succeeded or failed */ if (list == (struct cell*)NULL) { fprintf(stderr, makeNullList: can not allocate memory for new list.n); exit(1); } /* set next cell’s position to NULL */ list-next = (struct cell*)NULL; return list; /* return pointer to list */ } 2.3 nextCell struct cell *nextCell(struct cell *, struct cell *); 1 2 *5
  15. 15. 16 2 a /* * Return pointer to the next cell. */ struct cell *nextCell(struct cell *target, struct cell *list) { struct cell *next = (struct cell*)NULL; if (target != (struct cell*)NULL) { next = target-next; } return next; } a 2.4 struct cell *firstCell(struct cell *); struct cell *endCell(struct cell *); NULL 2.5 previousCell *6 struct cell *previousCell(struct cell*, struct cell*); 1 2 2.6 insertCell struct cell *insertCell(int, struct cell *, struct cell *); 1 2 3 2 ( 2 ) NULL *7 malloc 2.7 deleteCell struct cell *deleteCell(struct cell*, struct cell*); *6 *7 nextCell(endCell(list), list) NULL
  16. 16. 2.5 : 17 1 2 2.8 retrieveCell NULL ,0 int retrieveCell(struct cell*, struct cell*); 1 2 2.9 locateCell NULL 1 struct cell *locateCell(int, struct cell*); 1 2 /* * return position of the cell has data equals to specified as iData */ struct cell *locateCell(int data, struct cell *list) { struct cell *current; /* current cell’s pointer */ /* set pointer to current cell as pointer to first cell */ current = firstCell(list); /* while current cell’s data is not data, pursue the next cell */ while ((current != (struct cell*)NULL) (retrieveCell(current, list) != data)) { current = nextCell(current, list); } return current; /* return end cell’s pointer */ } 2.10 deleteCell deleteList void deleteList(struct cell*); 1 2.11 printList [ 1, 2, 3, 4, 5 ] void printList(struct cell*); 1 2.12 main()
  17. 17. 18 2 2.6 2.13
  18. 18. 19 3 ( ) 3.1 3.1.1 *1 LIFO(Last-In First-Out) 3.1 M AKEN U LL(S) S T OP (S) S a P OP (S) S ( ) P U SH(x, S) x S ( ) EM P T Y (S) S a 3.1 3.1.2 T OP (S) RET RIEV E(F IRST (S), S) P U SH(x, S) *1
  19. 19. 20 3 x IN SERT (x, F IRST (S), S) II 3.1.3 elements[0 · · · 99] length T OP (S) elements[length − 1] P U SH(x, S) push(x, S) { if (length == 100) { ; } else { elements[length++] = x; } } 3.2 ( ) 3.2.1 ( ) FIFO (First-In First-Out) 3.2 M AKEN U LL(Q) Q F RON T (Q) Q EN QU EU E(x, Q) x Q DEQU EU E(Q) Q Q EM P T Y (Q) Q 3.2
  20. 20. 3.3 21 3.2.2 F RON T (Q) RET RIEV E(F IRST (Q), Q) EN QU EU E(x, Q) x IN SERT (x, EN D(Q), Q) 3.2.3 EN QU EU E(x, Q) DEQU EU E(Q) elements[0 · · · 99] elements[99] elements[0] f ront length EN QU EU E(x, Q) ENQUEUE(x, Q) { if (length == 100) { ; } else { elements[(front + length) % 100] = x; length ++; } } 3.3 3.3.1 • • • • int • (MAKENULL (TOP (POP (PUSH (EMPTY 6
  21. 21. 22 3 stack.h stack.c stack-test.c make 3.3.2 3.1 ( struct cell *makeNullStack(void); /* * Make null stack and return pointer to stack */ struct cell *makeNullStack(void) { return makeNullList(); /* call makeNullList() */ } 3.2 (1 (0 int emptyStack(struct cell*); 3.3 int topStack(struct cell*); /* * Return data of the top stack */ int topStack(struct cell *stack) { return retrieveCell(firstCell(stack), stack); } 3.4
  22. 22. 3.4 : ( ) 23 int popStack(struct cell*); 3.5 void pushStack(int, struct cell*); 3.6 void deleteStack(struct cell*); 3.7 main() 3.4 : ( ) 3.4.1 • • • • int • MAKENULL FRONT DEQUEUE ENQUEUE EMPTY 6 queue.h queue.c queue-test.c make 3.8 struct cell *makeNullQueue(void); 3.9 1 0 emptyQueue int emptyQueue(struct cell*);
  23. 23. 24 3 3.10 frontQueue int frontQueue(struct cell*); 3.11 dequeue int dequeue(struct cell*); 3.12 enqueue void enqueue(int, struct cell*); 3.13 void deleleteQueue(struct cell*); 3.14 main() 3.5 : 3.15 struct array_stack *makeNullStack(void); int emptyStack(struct array_stack*); int topStack(struct array_stack*); int popStack(struct array_stack*); void pushStack(int, struct array_stack*); void deleteStack(struct array_stack*); struct array_stack #define MAX_STACK_SIZE 10 struct array_stack { int length; /* ( ) */ int elements[MAX_STACK_SIZE]; /* */ }; int 0 MAX_STACK_SIZE 3.16
  24. 24. 3.6 : 25 struct array_queue *makeNullQueue(void); int emptyQueue(struct array_queue*); int frontQueue(struct array_queue*); int dequeue(struct array_queue*); void enqueue(int, struct array_queue*); void deleteQueue(struct array_queue*); struct array_queue #define MAX_QUEUE_SIZE 10 struct array_queue { int front; /* */ int length; /* ( ) */ int elements[MAX_QUEUE_SIZE]; /* */ }; int MAX_QUEUE_SIZE-1 0 MAX_QUEUE_SIZE MAX_QUEUE_SIZE 3.6 : 2 2 32 + 9, 4 / 2 infix notation 2 prefix notation 2 postfix notation E 1. E E E 2. 2 op E1 op E2 E1 E2 op E1 , E2 E1 , E2 3. (E) E E E 3.17 32 + 3, 9 + 4 ∗ 3, (1 + 2) ∗ 4 1 1 1. push 2. op 2 x, y pop x op y 3.18 3.17
  25. 25. 26 3 3.19 • 1 +, −, ∗, / 1 1 1 1 • • / a.out % cat testdata.txt 16 3 + 3 / -2 - % ./a.out testdata.txt 8 3.1 3.6 3.7 3.20
  26. 26. 27 4 4.1 f f f (recursive function) f f f 1 f (linear recursive) 2 f (non-linear recursive) 2 1. (basis) 2. (induction step) *1 4.1.1 ML n • n=0 1 • n≥1 n ∗ (n − 1)! ML fun factorial(n) = if n = 0 then 1 else n * factorial(n - 1); L • L • L tl(L) hd(L) *1
  27. 27. 28 4 hd(L), tl(L) L ML fun reverse(L) = if L = nil then nil else reverse(tl(L)) @ [hd(L)]; 4.1.2 • 0( 1) nil • n−1 tl(L) ( ) 4.2 4.2.1 4.1 1 int sigma(int); sigma(n) = n + sigma(n − 1) = n + (n − 1) + sigma(n − 2) . . . = n + (n − 1) + · · · + sigma(1) = n + (n − 1) + · · · + 1 int sigma(int num) { int result; /* calculation result */ if (num = 1) { result = num; } else { result = num + sigma(num - 1); } return result; }
  28. 28. 4.2 29 main int main(int argc, char *argv[]) { /* check arguments */ if (argc != 2) { fprintf(stderr, Usage: %s numn, argv[0]); exit(1); } /* check argument sign */ if (atoi(argv[1]) 0) { fprintf(stderr, Error: The argument must be a positive value.n); exit(1); } /* calculation and print result */ printf(sigma(%d) = %dn, atoi(argv[1]), sigma(atoi(argv[1]))); return 0; } 4.1 ( ) 4.2 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . . 2 1 2 5 2 3, 4 2 3 2+3=5 int fibonacci(int); main() f ibonacci(n) = f ibonacci(n − 1) + f ibonacci(n − 2) = {f ibonacci(n − 2) + f ibonacci(n − 3)} + {f ibonacci(n − 3) + f ibonacci(n − 4)} . . . 4.2.2 4.3 struct cell *reverseList(struct cell*); main() *2 reverseList([3, 6, 4, 5, 2]) = reverseList([6, 4, 5, 2])@[3](@ ) = reverseList([4, 5, 2])@[6]@[3] . . . = [2, 5, 4, 6, 3] *2 printList(list)
  29. 29. 30 4 4.4 list list1 list2 void divideList(struct cell *list, struct cell *list1, struct cell *list2); list list1 list2 *3 main() ) pcList = [1, 2, 3, 4, 5]   list1 =  [1]@([3, 4, 5] divideList list1)  = [1]@([3]@([5] divideList list1))  list2 =  [2]@([3, 4, 5] divideList list2)  = [2]@([4]@([5] divideList list2)) 4.5 2 void mergeList(struct cell *list1, struct cell *list2, struct cell *merged); list1 list2 merged list1 list2 *4 main() ) list1 = [3, 6, 2] list2 = [4, 5, 7] merged = [3]@([6, 2] [4, 5, 7] mergeList merged) = [3]@([4]@([6, 2] [5, 7] mergeList merged)) = ··· 4.6 divideList mergeList mergeSort 1. divideList 2. mergeSort 3. 2 mergeList list sorted void mergeSort(struct cell *list, struct cell *sorted); main() ) list = [ 4, 7, 2, 3, 9, 1 ] mergeSort sorted sorted = [ 1, 2, 3, 4, 7, 9 ] 8 9 [4, 2, 9]→ mergeSort →[2, 4, 9] = [4, 7, 2, 3, 9, 1] → divideList → → mergeList → [1, 2, 3, 4, 7, 9] :[7, 3, 1]→ mergeSort →[1, 3, 7] ; *3 printList(list) *4 printList(list1) printList(list2)
  30. 30. 4.3 31 [4, 2, 9] mergeSort 8 9 [4, 9]→ mergeSort →[4, 9] = [4, 2, 9] → divideList → → mergeList → [2, 4, 9] : ; [2] → mergeSort → [2] 4.3 4.7
  31. 31. 33 5 ( ) (depth first search) (preorder) (inorder) (postorder) 3 5.1 2 2 2 2 ( 0 1 2) 2 5.1 N ode CREAT EN ODE(Label label, N ode lef t, N ode right) label lef t right 2 N ode LEF T N ODE(N ode n) n N ode RIGHT N ODE(N ode n) n 5.1 2 2 2 • • NULL • NULL
  32. 32. 34 5 5.2 make 5.1 struct node int 1 5.2 struct node *createNode(int, struct node*, struct node*); NULL malloc 5.3 / struct node *leftNode(struct node*); struct node *rightNode(struct node*); NULL 5.4 int labelNode(struct node*); 5.5 void preOrder(struct node*); 5.6 void inOrder(struct node*); 5.7 void postOrder(struct node*); 5.8
  33. 33. 5.3 35 int heightTree(struct node*); ) +1 heigthTree(node) = MAX( heigthTree(node ), heigthTree(node )) + 1; height tree 4 4 tree1 tree3 3 8 3 node1 node2 tree2 1 2 7 2 node4 node3 6 5 1 5.9 struct node *locateNode(int, struct node*); NULL 5.10 void deleteTree(struct node*); ) 5.11 main() 5.3 5.12
  34. 34. 37 6 2 10,000 1 10,000 1 *1 2 (binary search tree) 2 2 6.1 2 1 2 *2 • x x x • x x x 6.1 2 ML as types a in 6.1 2 *1 O(n) ( n ) *2 (total order)
  35. 35. 38 6 2 6.2 2 6.2 M EM BER(x, A) x 2 A IN SERT (x, A) x 2 A 2 M IN (A) 2 A DELET E(x, A) x 2 A 2 6.2 2 M EM BER(x, A) boolean MEMBER(x, A) { if (A == nil) { return false; /* 2 */ } else if (x == A.iData) { return true; /* A x */ } else if (x A.iData) { return MEMBER(x, A.leftTree); /* x */ } else { /* x A.iData */ return MEMBER(x, A.rightTree); /* x */ } } IN SERT (x, A), DELET E(x, A) 2 DELET E(x, A)
  36. 36. 6.3 39 1. A (A x ) 2. A a x A.iData x A x 2 A b x A.iData x A x 2 A c x == A.iData A i. A ( ) A ii. A A 2 A iii. A A 2 A iv. A y x 2 y A.lef tT ree DELET E(y, A.rigthT ree) 6.3 6.3.1 6.3.2 6.1 int BS member(int, struct node*); 1 0 /*** *** test that a node that has specified label is in the tree ***/ int BS_member(int data, struct node *tree) { int ret = 0; /* result (0: not member, 1: member) */ if (tree != (struct node*)NULL) { /* tree data */ } return ret; }
  37. 37. 40 6 2 6.2 struct node *BS insert(int, struct node*); *3 /*** *** insert a node that has specified label ***/ struct node *BS_insert(int data, struct node *tree) { if (tree == (struct node*)NULL) { /* tree */ } else { /* tree */ } return tree; } 6.3 struct node *BS min(struct node*); /*** *** get a node that has minimum label in the tree ***/ struct node *BS_min(struct node *tree) { struct node *min = (struct node*)NULL; /* pointer to minimum node (initially NULL) */ if (tree != (struct node*)NULL) { /* check tree is exist or not */ if (leftNode(tree) == (struct node*)NULL) { /* tree */ } else { /* tree */ } } return min; } 6.4 struct node *BS delete(int, struct node*); *3 NULL
  38. 38. 6.4 41 /*** *** delete a node that has a label equals to specified label ***/ struct node *BS_delete(int data, struct node *tree) { int tmp; /* temporary data */ if (tree != (struct node*)NULL) { /* check tree is exist or not */ if (labelNode(tree) data) { /* data tree */ } else if (labelNode(tree) data) { /* data tree */ } else { /* data is equal to this node, then delete this node */ if ((leftNode(tree) == (struct node*)NULL) (rightNode(tree) == (struct node*)NULL)) { /* tree */ } else if (leftNode(tree) == (struct node*)NULL) { /* tree */ } else if (rightNode(tree) == (struct node*)NULL) { /* tree */ } else { /* tree */ } } } return tree; } 6.5 void BS print(struct node*); 6.6 void BS destroy(struct node*); 6.7 main() 6.4 6.8 6.9 6 ( { | } ) 6

×