ohp03.dvi

Similar documents
r03.dvi

ohp08.dvi

r07.dvi

ohp07.dvi

r08.dvi

ohp11.dvi

r11.dvi

橡Pro PDF

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)* (

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

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

joho07-1.ppt

超初心者用

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

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

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) * *

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

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

ex14.dvi

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

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

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

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

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

tuat1.dvi

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

& & a a * * ptr p int a ; int *a ; int a ; int a int *a

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

BW BW

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 >>

‚æ2›ñ C„¾„ê‡Ìš|

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

Condition DAQ condition condition 2 3 XML key value

C B

卒 業 研 究 報 告.PDF

lexex.dvi

ex12.dvi

O(N) ( ) log 2 N

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

program.dvi

1.3 ( ) ( ) C

(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

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

untitled

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

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

programmingII2019-v01

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

cpp1.dvi

‚æ4›ñ

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

ポインタ変数

新・明解C言語 実践編

PowerPoint Presentation

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 <<

yacc.dvi

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

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

slide5.pptx

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

Microsoft PowerPoint - CproNt02.ppt [互換モード]

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

fp.gby

1.ppt

Prog1_15th

CM-3G 周辺モジュール拡張技術文書 MS5607センサ(温度、気圧)

PowerPoint プレゼンテーション

新版明解C言語 実践編

K227 Java 2

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

ポインタ変数

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

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 今日の講義講義で学ぶ内容 switch 文 論理演算子 条件演算子 条件判断文 3 switch 文 switch 文 式が case のラベルと一致する場所から直後の break; まで処理しますどれにも一致致しない場合 def

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

comment.dvi

第2回

XMPによる並列化実装2

PowerPoint Presentation

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

02: 変数と標準入出力

WinHPC ppt

Cプログラミング - 第8回 構造体と再帰

ex01.dvi

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

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

untitled

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225

A/B (2010/10/08) Ver kurino/2010/soft/soft.html A/B

ex01.dvi

解きながら学ぶC++入門編

* ライブラリ関数 islower(),toupper() を使ったプログラム 1 /* 2 Program : trupper.c 3 Student-ID : K 4 Author : TOUME, Kouta 5 Comments : Used Library function i

double float

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の break; までを処理し どれにも一致しない場合 default; から直後の break; までを処理する 但し 式や値 1

gengo1-12

pptx

#include <stdio.h> 2 #include <stdlib.h> 3 #include <GL/glut.h> 4 Program 1 (OpenGL GameSample001) 5 // 6 static bool KeyUpON = false; // 7 sta

P05.ppt

スライド タイトルなし

gengo1-12

Transcription:

19 3 ( ) 2019.4.20 CS 1

(comand line arguments) Unix./a.out aa bbb ccc ( ) C main void int main(int argc, char *argv[]) {... 2

(2) argc argv argc ( ) argv (C char ) ( 1) argc 4 argv NULL. / a. o u t \0 a a \0 b b b \0 c c c \0 1: 3

(3) // argdemo1.c --- print argc/argv as string #include <stdio.h> int main(int argc, char *argv[]) { for(int i = 0; i < argc; ++i) { printf("%s\n", argv[i]); return 0; 0... "..." 1 (C "..." Ruby... ) %./a.out aa bbb ccc./a.out aa bbb ccc 4

(4) 1 (stdlib.h ) int atoi(char *s) double atof(char *s) 1 5

(5) // argdemo2.c --- sum of argument strings (as int) #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int sum = 0; for(int i = 1; i < argc; ++i) { sum += atof(argv[i]); printf("sum = %d\n", sum); return 0; %./a.out 3 4 5 sum = 12 scanf 6

(stack) LIFO(last-in, first-out) 2 1 3 3 2 1 3 1 2 3 push pop top 2 1 isempty 2: push pop ( ) isempty 7

(2) ( ) top (push pop )? push pop ( ) 8

1 arr ptr push pop (ptr 0 ) arr[ptr++] = value; // same as: arr[ptr] = value; ++ptr; value = arr[--ptr]; // same as: --ptr; value = arr[ptr]; arr push 3 arr 3 ptr 3 pop ptr 3: push 1 pop 1 ( 3) ptr ptr 1 9

(2) push pop // 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 10

(3) typedef istackp struct istack* 1 top ptr 1 1 typedef int *intp intp 11

(4) // istack.c --- int type stack impl. with array #include <stdlib.h> #include "istack.h" struct istack { int ptr; int *arr; ; istackp istack_new(int size) { istackp p = (istackp)malloc(sizeof(struct istack)); p->ptr = 0; p->arr = (int*)malloc(size * sizeof(int)); return p; bool istack_isempty(istackp p) { return p->ptr <= 0; void istack_push(istackp p, int v) { p->arr[p->ptr++] = v; int istack_pop(istackp p) { return p->arr[--(p->ptr)]; int istack_top(istackp p) { return p->arr[p->ptr - 1]; 12

(5) // reversestr.c --- print argv[1] in reverse order #include <stdio.h> #include <stdlib.h> #include "istack.h" int main(int argc, char *argv[]) { istackp s = istack_new(200); char *t = argv[1]; for(int i = 0; t[i]!= \0 ; ++i) { istack_push(s, t[i]); while(!istack_isempty(s)) { putchar(istack_pop(s)); putchar( \n ); return 0; 13

(6)./a.out argv[1] t ( \0 ) putchar( ) 1 %./a.out this is a pen..nep a si siht 14

(7) expect int // test_istack.c --- unit test for istack. #include <stdio.h> #include <stdlib.h> #include "istack.h" void expect_int(int i1, int i2, char *msg) { printf("%s %d:%d %s\n", (i1==i2)?"ok":"ng", i1, i2, msg); int main(int argc, char *argv[]) { struct istack *s = istack_new(200); istack_push(s, 1); istack_push(s, 2); istack_push(s, 3); expect_int(istack_pop(s), 3, "push 1 2 3 ; pop"); expect_int(istack_pop(s), 2, "pop"); istack_push(s, 4); expect_int(istack_pop(s), 4, "push 4; pop"); 15

expect_int(istack_pop(s), 1, "pop"); return 0; %./a.out OK 3:3 push 1 2 3 ; pop OK 2:2 pop OK 4:4 push 4; pop OK 1:1 pop 16

(1) (2) (3) ( 4) OK [ { ( ) ( ( ) ) ] NG ) ( { { [ ( ] ) 4: 17

(2) 5 ( ) ( $ ) stack ( 1 + 2 ) * ( ( 3-1 ) / 2 ) $ ( ) * ( ( 3-1 ) / 2 ) $ ( ( 3-1 ) / 2 ) $ ( ( 3-1 ) / 2 ) ( ( ) / 2 ) ( ) $ $ $ $ 5: 18

(3) ( ) C balance1 c ( ) ( ( ) ) ( ( ) main 1 balance1 OK NG 19

(4) // barance1.c --- see if parentheses are balanced in input #include <stdio.h> #include <stdlib.h> #include "istack.h" bool balance1(char *t) { istackp s = istack_new(200); for(int i = 0; t[i]!= \0 ; ++i) { char c = t[i]; if(c == ( ) { istack_push(s, c); else if(c == ) ) { if(istack_isempty(s)) { return false; istack_pop(s); return istack_isempty(s); 20

int main(int argc, char *argv[]) { for(int i = 1; i < argc; ++i) { printf("%s : %s\n", argv[i], balance1(argv[i])?"ok":"ng"); return 0; %./a.out ((a)) (a)) ((a)) : OK (a)) : NG 21

(5) bool2str expect bool // test_barance1.c --- unit test for balance1 #include <stdio.h> #include <stdlib.h> #include "istack.h" char *bool2str(bool b) { return b? "true" : "false"; void expect_bool(bool b1, bool b2, char *msg) { printf("%s %s:%s %s\n", (b1==b2)?"ok":"ng", (balance1 here) int main(void) { bool2str(b1), bool2str(b2), msg); expect_bool(balance1("((a)())"), true, "((a)())"); expect_bool(balance1(")(a)()("), false, ")(a)()("); expect_bool(balance1("((a)()"), false, "((a)()"); 22

expect_bool(balance1("(a)())"), false, "(a)())"); expect_bool(balance1("(((())))"), true, "(((())))"); return 0; %./a.out OK true:true ((a)()) OK false:false )(a)()( OK false:false ((a)() OK false:false (a)()) OK true:true (((()))) 23

(6) 1 a. () [] { ( [(] OK ) b. ( [(] [()] OK ) c. ( ) ( [(]) OK [{)] ) : 3 24

(7) d. ( : -1 1 ) e. 25

( ) (infix notation) 3 * 2 + 4 3 + 2 * 4 * (postfix notation) 2 2 ( ) 3 2 * 4 + 6 4 + 10 3 2 4 3 2 4 * + 3 8 + 11 3 2 4 26

(2) 4 2 3 3 4 6 6 10 2 3 3 2 3 8 3 11 3 2 * 4 + 3 2 4 * + 6: (1) (2) 2 ( 6) 27

(3) output stack input 2 + 3 * 4 + 1 $ 2 + 3 * 4 + 1 $ 2 + 3 * 4 + 1 $ 2 3 + * 4 + 1 $ 2 3 + * 4 + 1 $ 2 3 4 + * + 1 $ 2 3 4 * + + 1 $ 2 3 4 * + + 1 $ 2 3 4 * + + 1 $ 2 3 4 * + 1 + $ 7: 28

(4) 2 C + - 1 * / % 2 ^ 3 ( C ^ ) 29

(5) postfix1 ( ) C *u++ = ; 30

(6) // postfix1.c -- convert infix to postfix (w/o parentheses) #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include "istack.h" int operprec(int c) { switch(c) { case + : case - : return 1; case * : case / : case % : return 2; case ^ : return 3; default: return 0; void postfix1(char *t, char *u) { istackp s = istack_new(200); for(int i = 0; t[i]!= \0 ; ++i) { 31

char c = t[i]; if(isdigit(c)) { *u++ = c; continue; while(!istack_isempty(s) && operprec(istack_top(s)) >= operprec(c)) { *u++ = istack_pop(s); istack_push(s, c); while(!istack_isempty(s)) { *u++ = istack_pop(s); *u++ = \0 ; int main(int argc, char *argv[]) { char buf[200]; for(int i = 1; i < argc; ++i) { postfix1(argv[i], buf); printf("%s => %s\n", argv[i], buf); 32

return 0; 33

(7) isdigit ctype.h include (0 9) continue %./a.out 1+2*3+4 1+2*3+4 => 123*+4+ expect str strcmp string.h include strcmp 0 OK NG 34

(8) // test_postfix1.c --- unit test of postfix1 #include <ctype.h> #include <stdio.h> #include <string.h> #include "istack.h" (operprec, postfix1 here) void expect_str(char *s1, char *s2, char *msg) { printf("%s %s : %s %s\n", strcmp(s1, s2)?"ng":"ok", s1, s2, msg); int main(void) { char buf[200]; postfix1("1+2*3", buf); expect_str(buf, "123*+", "1+2*3 => 123*+"); postfix1("2*3+1", buf); expect_str(buf, "23*1+", "2*3+1 => 23*1+"); return 0; 35

(9) 2 a. 2^3^4 (2 3 ) 4 2 (34) ( ) b. ( : ) c. 1 2 36

(10) d. ( ) e. 37

38

(2) // pstack.h --- pointer type stack interface #include <stdbool.h> struct pstack; typedef struct pstack *pstackp; pstackp pstack_new(int size); bool pstack_isempty(pstackp p); bool pstack_isfull(pstackp p); void pstack_push(pstackp p, void *v); void *pstack_pop(pstackp p); void *pstack_top(pstackp p); void* void* 39

(3) // pstack.c --- pointer type stack impl. with array #include <stdlib.h> #include "pstack.h" struct pstack { int ptr, lim; void **arr; ; pstackp pstack_new(int size) { pstackp p = (pstackp)malloc(sizeof(struct pstack)); p->ptr = 0; p->lim = size; p->arr = (void**)malloc(size * sizeof(void*)) return p; bool pstack_isempty(pstackp p) { return p->ptr <= 0; bool pstack_isfull(pstackp p) { return p->ptr >= p->lim; void pstack_push(pstackp p, void *v) { p->arr[p->ptr++] = v; void *pstack_pop(pstackp p) { return p->arr[--(p->ptr)]; void *pstack_top(pstackp p) { return p->arr[p->ptr - 1]; 40

(4) void* void** // reversefile.c --- input a flie and output upside-down #include <stdio.h> #include <stdlib.h> #include <string.h> #include "pstack.h" 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; int main(void) { char buf[200]; 41

pstackp s = pstack_new(200); while(getl(buf, 200)) { char *t = (char*)malloc(strlen(buf)+1); strcpy(t, buf); pstack_push(s, t); while(!pstack_isempty(s)) { char *t = (char*)pstack_pop(s); printf("%s\n", t); free(t); return 0; 42

(5) getl 1 EOF( ) 2 main getl buf +1( ) free %./a.out this is a pen. that is a dog. how are you? ^D Ctrl-D how are you? that is a dog. this is a pen. 2 for c i for 43

(6) 3 44

1 2 2 1 1 45

this is a pen. this is a pen. --- current position --- that is a dog today is fine. yeserday snowed backward upper stack this is a pen. that is a dog --- current position --- insert that is a dog how are you? --- current position --- today is fine. yeserday snowed this is a pen. that is a dog today is fine. --- current position --- forward today is fine. yeserday snowed lower stack delete this is a pen. that is a dog --- current position --- yeserday snowed yeserday snowed 8: 2 46

(2) 4 2 5 6 47

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

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