ohp07.dvi

Similar documents
r07.dvi

ohp03.dvi

r03.dvi

ohp08.dvi

r08.dvi

r11.dvi

ohp11.dvi

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

untitled

( ) ( ) 30 ( ) 27 [1] p LIFO(last in first out, ) (push) (pup) 1

untitled

program.dvi

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

C言語によるアルゴリズムとデータ構造

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

lexex.dvi

tuat1.dvi

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

橡Pro PDF

新版明解C言語 実践編

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

cpp1.dvi

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

O(N) ( ) log 2 N

Taro-リストⅢ(公開版).jtd

XMPによる並列化実装2

BW BW

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

C

slide5.pptx

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

:30 12:00 I. I VI II. III. IV. a d V. VI

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

Taro-リストⅠ(公開版).jtd

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

Microsoft Word - C.....u.K...doc

卒 業 研 究 報 告.PDF

:30 12:00 I. I VI II. III. IV. a d V. VI

Microsoft PowerPoint - kougi9.ppt

1.3 ( ) ( ) C

PowerPoint プレゼンテーション

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

SystemC言語概論

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

file"a" file"b" fp = fopen("a", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose(fp); fp = fopen("b", "r"); while(fgets(line, BUFSIZ, fp)) {... fclose

untitled

新・明解C言語 ポインタ完全攻略

1.ppt

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裶²ó ¨¡ À©¸æ¹½Â¤¡§·«¤êÊÖ¤· ¨¡

memo

I117 II I117 PROGRAMMING PRACTICE II 2 SOFTWARE DEVELOPMENT ENV. 2 Research Center for Advanced Computing Infrastructure (RCACI) / Yasuhiro Ohara yasu

超初心者用

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

ex01.dvi

PowerPoint プレゼンテーション

char char 1 signed char unsigned char ( ; single-quote 0x27) ASCII Japan Advanced Institute of Science and Technology

Microsoft Word - Cプログラミング演習(12)

新・明解C言語 実践編

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

yacc.dvi

ex14.dvi

ex12.dvi

C B

programmingII2019-v01

Microsoft Word - no206.docx

ex01.dvi

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

Microsoft PowerPoint - H22プログラミング第一(E)#12

第3回 配列とリスト

LIFO(last in first out, ) 1 FIFO(first in first out, ) 2 2 PUSH POP : 1

PowerPoint プレゼンテーション

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

第5回お試しアカウント付き並列プログラミング講習会

joho07-1.ppt

Condition DAQ condition condition 2 3 XML key value

1. 入力した正の整数を降順に並べ換えて出力するプログラムを作成せよ プログラムは個別にコンパイルし make コマンドで実行すること 入力データは 50 以下とし 以下の数が混在しているとする 16 進数 : 先頭 1 文字が x または X( エックスの小文字か大文字 ) 8 進数 : 先頭 1

Taro-スタック(公開版).jtd

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

fp.gby

PowerPoint Presentation

第2回

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

‚æ4›ñ

プログラミング方法論 II 第 14,15 回 ( 担当 : 鈴木伸夫 ) 問題 17. x 座標と y 座標をメンバに持つ構造体 Point を作成せよ 但し座標 は double 型とする typedef struct{ (a) x; (b) y; } Point; 問題 18. 問題 17 の

文字数は1~6なので 同じ本数の枝を持つパスで生成される呪文の長さは最大で6 倍の差がある 例えば 上図のようなケースを考える 1サイクル終了した時点では スター節点のところに最強呪文として aaaaaac が求まる しかしながら サイクルを繰り返していくと やがてスター節点のところに aaaaaa

DA100データアクイジションユニット通信インタフェースユーザーズマニュアル

Taro-2分探索木Ⅱ(公開版).jtd

I void* School of Information Science, Japan Advanced Institute of Science and Technology

memo

プログラミングI第10回

[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

新・明解C言語で学ぶアルゴリズムとデータ構造

バイオプログラミング第 1 榊原康文 佐藤健吾 慶應義塾大学理工学部生命情報学科

PowerPoint Presentation

1.

text_08.dvi

Transcription:

19 7 ( ) 2019.4.20 1

(data structure) ( ) (dynamic data structure) 1 malloc C free 1 (static data structure) 2

(2) C (garbage collection GC) C GC(conservative GC) 2 2 conservative GC 3

data next p 3 5 7 9 p 3 5 7 9 p 3 5 7 9 1 1: (single linked list ) 1 (node) (cell) (C ) 4

(2) ( ) NULL( stdlib.h 0) 2 ( )3 1.0 ( NULL) node new 5

(3) plist p p = p->next; p p NULL // slistdemo1.c --- single-linked list demonstration #include <stdio.h> #include <stdlib.h> struct node { double data; struct node *next; }; typedef struct node *nodep; nodep node_new(double d, nodep n) { nodep p = (nodep)malloc(sizeof(struct node)); p->data = d; p->next = n; return p; } void plist(nodep p) { while(p!= NULL) { printf(" %g", p->data); p = p->next; } printf("\n"); 6

} nodep mklist(int n, char *a[]) { nodep p = NULL; for(int i = n-1; i >= 0; --i) { p = node_new(atof(a[i]), p); } return p; } int main(int argc, char *argv[]) { nodep p = mklist(argc-1, argv+1); plist(p); p->next->next = p->next->next->next; plist(p); p->next->next = node_new(1.0, p->next->next); plist(p); return 0; } 7

(4) ( ) mklist p NULL p = node new(, p); main argv 1 ( argc-1) 2 ( )2 ( )3 plist %./a.out 3 5 7 9 3 5 7 9 3 5 9 3 5 1 9 8

(5) mklist plist void plist(nodep p) { if(p == NULL) { printf("\n"); return; } printf(" %g", p->data); plist(p->next); } nodep mklist(int n, char *a[]) { if(n == 0) { return NULL; } return node_new(atof(*a), mklist(n-1, a+1)); } 9

(6) plist NULL mklist 0 NULL 1 a. ( 1 ) b. ( 2 ) c. 2 3 ( ) d. 2 ( 1 2 3 1 1 2 2 3 3 ) e. 10

: ( ) 1 e (enter) ( ) a (append) add (add) d (delete) p (print) q (quit) 11

: (2) %./a.out > e 1 2 3 > a 4 5 6 > p 1 2 3 4 5 6 > add 1 1 1 1 4 1 > p 2 3 4 5 5 6 > d 2 2 ( 0 ) > p 2 3 5 5 6 > q % 12

: (3) plist (include ) // listeditor.c --- single-linked list editor #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { double data; struct node *next; }; typedef struct node *nodep; nodep node_new(double d, nodep n) { nodep p = (nodep)malloc(sizeof(struct node)); p->data = d; p->next = n; return p; } nodep mklist(int n, char *a[]) { 13

if(n == 0) { return NULL; } return node_new(atof(*a), mklist(n-1, a+1)); } void plist(nodep p) { if(p == NULL) { printf("\n"); return; } printf(" %g", p->data); plist(p->next); } 14

: (4) nconc p q p next q void nconc(nodep p, nodep q) { } while(p!= NULL && p->next!= NULL) { p = p->next; } if(p!= NULL) { p->next = q; } 1 void delnode(nodep p, int n) { } while(--n > 0 && p!= NULL) { p = p->next; } if(p!= NULL && p->next!= NULL) { p->next = p->next->next; } 15

: (5) 2 NULL null 2 nodep addlist(nodep x, nodep y) { } if(x == NULL) { return y; } if(y == NULL) { return x; } return node_new(x->data+y->data, addlist(x->next, y->next)); 16

: (6) getl 1 getchar 1 1 ( ++ ) 0 false true bool getl(char s[], int lim) { } int c, i = 0; for(c = getchar(); c!= EOF && c!= \n ; c = getchar()) { } s[i++] = c; if(i+1 >= lim) { break; } s[i] = \0 ; return c!= EOF; 17

: (7) parse 1 ( ) 1 1 int parse(char *a[], char *s) { } int i, k = 0, len = strlen(s); for(i = 0; i < len; ++i) { } if(s[i] == ) { s[i] = \0 ; } if(s[i]!= \0 && (i == 0 s[i-1] == \0 )) { a[k++] = s+i; } return k; 18

: (8) main 1 1 q e mklist a list nconc add addlist d dellist p plist 19

: (9) int main(int argc, char *argv[]) { char buf[200], *cmd[20]; nodep list = NULL; while(true) { printf("> "); if(!getl(buf, 200)) { break; } int k = parse(cmd, buf); if(k > 0 && strcmp(cmd[0], "q") == 0) { break; } else if(k > 0 && strcmp(cmd[0], "e") == 0) { // enter list list = mklist(k-1, cmd+1); } else if(k > 0 && strcmp(cmd[0], "a") == 0) { // append list nconc(list, mklist(k-1, cmd+1)); } else if(k > 1 && strcmp(cmd[0], "add") == 0) { // add list list = addlist(list, mklist(k-1, cmd+1)); } else if(k > 1 && strcmp(cmd[0], "d") == 0) { // delete item delnode(list, atoi(cmd[1])); 20

} } else { plist(list); } } return 0; 21

: (10) 2 ( ) a. b. c. ( ) d. ( ) e. (1 ) f. 22

: (11) 3 p p head p p head 2: ( ) 3 1 (head) 3 ( ) 2 / / 23

1 ( ) 24

(2) 1 25

3 top push next top (top NULL ) pop top next push(1) push(2) top top top 1 2 1 pop 1 pop 2 3: istack.h 3 26

(2) // istack.h --- int type stack interface #include <stdbool.h> struct istack; typedef struct istack *istackp; istackp istack_new(int size); // allocate new stack bool istack_isempty(istackp p); // test if the stack is empty void istack_push(istackp p, int v); // push a value int istack_pop(istackp p); int istack_top(istackp p); // pop a value and return it // peek the topmost value 27

(3) 1 size( )? size size size 4 size 28

top last 2 ( ) (head) 4 ( ) top last 29

(2) last next last top next next next top last enq(1) top last enq(2) top last enq(3) 1 1 2 top last top last 1 2 3 2 3 deq 1 4: ( ) 30

(3) iqueue.h isfull // iqueue.h --- int type queue interface #include <stdbool.h> struct iqueue; typedef struct iqueue *iqueuep; iqueuep iqueue_new(int size); bool iqueue_isempty(iqueuep p); bool iqueue_isfull(iqueuep p); void iqueue_enq(iqueuep p, int v); int iqueue_deq(iqueuep p); 31

(4) 5 ( ) size isfull (#3 ) 6 ( ) size isfull (#4 ) 32

(5) 7 ( ) last ( last ) 1 size isfull last enq 1 enq 2 enq 3 deq 1 1 1 2 1 2 3 enq 4 deq 2 deq 3 deq 4 2 3 2 3 4 3 4 4 33

7A 1 6 1 ( 23:59 ) 1. sol CED /home3/staff/ka002689/prog19upload 7a 2. ( ) @@@ 3. 1 4. ( ) ( ) 5. Q1. Q2. Q3. ( ) 34

7B 1 6 ( 7A ) ( ) 2 23:69 1. sol CED /home3/staff/ka002689/prog19upload 7b 2. ( ) @@@ 3. 1 ( ) ( ) 4. 2 5. Q1. Q2. Q3. ( ) 35

: gdb? printf (debugger) ( ) ( ) gdb 36

: gdb (2) gdb gcc gcc gcc -g % gcc8 -g % gdb8 a.out gcc...... (gdb) gdb 37

: gdb (3) gdb ( 1 bt 2 ) break (b) 3 b b b : ( ) run (r)./a.out 1 2 gdb r 1 2 r quit (q) gdb 38

: gdb (4) r gdb ( bus error ) backtreace (bt) list (l) print (p) p i p a[i] p q->data 39

: gdb (5) continue (c) next (n) 1 1 (p ) step (s) next 40

: gdb (6) // factdemo.c --- two fact function for gdb exercise. #include <stdio.h> #include <stdlib.h> int fact1(int n) { } int i, r = 1; for(i = 1; i <= n; ++i) { } r *= i; return r; int fact2(int n) { if(n <= 0) { return 1; } int r = fact2(n-1); return n * r; 41

} int main(int argc, char *argv[]) { int n = atoi(argv[1]); printf("fact1(%d) == %d\n", n, fact1(n)); printf("fact2(%d) == %d\n", n, fact2(n)); return 0; } 42

: gdb (7) c ( ) % gcc8 -g factdemo.c % gdb a.out Copyright (C) 2018... Reading symbols from a.out...done. (gdb) break fact1 fact1 Breakpoint 1 at 0x40056d: file factdemo.c, line 5. (gdb) break fact2 fact2 Breakpoint 2 at 0x4005a3: file factdemo.c, line 12. (gdb) r 5 Starting program: /home3/staff/ka002689/a.out 5 43

Breakpoint 1, fact1 (n=5) at factdemo.c:5 5 int i, r = 1; (gdb) s 6 for(i = 1; i <= n; ++i) { (gdb) s 7 r *= i; (gdb) s 6 for(i = 1; i <= n; ++i) { (gdb) s 7 r *= i; (gdb) s 6 for(i = 1; i <= n; ++i) { (gdb) s 7 r *= i; (gdb) p r r $1 = 2 (gdb) p i i 44

$2 = 3 (gdb) c Continuing. fact1(5) == 120 Breakpoint 2, fact2 (n=5) at factdemo.c:12 12 if(n <= 0) { return 1; } (gdb) c Continuing. Breakpoint 2, fact2 (n=4) at factdemo.c:12 12 if(n <= 0) { return 1; } (gdb) c Continuing. Breakpoint 2, fact2 (n=3) at factdemo.c:12 12 if(n <= 0) { return 1; } (gdb) c Continuing. Breakpoint 2, fact2 (n=2) at factdemo.c:12 45

12 if(n <= 0) { return 1; } (gdb) c Continuing. Breakpoint 2, fact2 (n=1) at factdemo.c:12 12 if(n <= 0) { return 1; } (gdb) c Continuing. Breakpoint 2, fact2 (n=0) at factdemo.c:12 12 if(n <= 0) { return 1; } (gdb) bt #0 fact2 (n=0) at factdemo.c:12 #1 0x00000000004005bd in fact2 (n=1) at factdemo.c:13 #2 0x00000000004005bd in fact2 (n=2) at factdemo.c:13 #3 0x00000000004005bd in fact2 (n=3) at factdemo.c:13 #4 0x00000000004005bd in fact2 (n=4) at factdemo.c:13 #5 0x00000000004005bd in fact2 (n=5) at factdemo.c:13 #6 0x0000000000400618 in main (argc=3, argv=0x7fffffffd7f8) at factdemo.c 46

(gdb) s 15 } (gdb) s 14 return n * r; (gdb) p r $3 = 1 (gdb) p n $4 = 1 (gdb) s 15 } (gdb) s 14 return n * r; (gdb) s 15 } (gdb) s 14 return n * r; (gdb) p r r n r 47

$5 = 2 (gdb) p n n $6 = 3 (gdb) c Continuing. fact2(5) == 120 [Inferior 1 (process 44205) exited normally] (gdb) q 48