r03.dvi

Similar documents
ohp03.dvi

ohp08.dvi

r07.dvi

ohp07.dvi

r08.dvi

ohp11.dvi

r11.dvi

橡Pro PDF

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

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

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

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

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

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

超初心者用

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

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

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

joho07-1.ppt

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

untitled

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

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

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

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

O(N) ( ) log 2 N

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

BW BW

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

tuat1.dvi

Condition DAQ condition condition 2 3 XML key value

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

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

program.dvi

cpp1.dvi

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

C B

卒 業 研 究 報 告.PDF

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

lexex.dvi

untitled

(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

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

1.3 ( ) ( ) C

ex12.dvi

programmingII2019-v01

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 - CproNt02.ppt [互換モード]

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

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

‚æ4›ñ

fp.gby

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

PowerPoint Presentation

K227 Java 2

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

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

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

ポインタ変数

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

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

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

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

yacc.dvi

新・明解C言語 実践編

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

double float

Prog1_15th

新版明解C言語 実践編

1.ppt

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

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

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

( ) 1 1: 1 #include <s t d i o. h> 2 #include <GL/ g l u t. h> 3 #include <math. h> 4 #include <s t d l i b. h> 5 #include <time. h>

第2回

pptx

02: 変数と標準入出力

Microsoft PowerPoint - kougi9.ppt

Jacques Garrigue

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

ポインタ変数


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

P03.ppt

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

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

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

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

haskell.gby

PowerPoint Presentation

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

org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t exp

design_pattern.key

PowerPoint プレゼンテーション

slide5.pptx

break 文 switch ブロック内の実行中の処理を強制的に終了し ブロックから抜けます switch(i) 強制終了 ソースコード例ソースファイル名 :Sample7_1.java // 入力値の判定 import java.io.*; class Sample7_1 public stati

untitled

A/B (2018/06/08) Ver kurino/2018/soft/soft.html A/B

Transcription:

19 ( ) 019.4.0 CS 1 (comand line arguments) Unix./a.out aa bbb ccc ( ) C main void... 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: // argdemo1.c --- print argc/argv as string for(int i = 0; i < argc; ++i) { printf("%s\n", argv[i]); 0... "..." 1 (C "..." Ruby... ) 1

%./a.out aa bbb ccc./a.out aa bbb ccc 1 (stdlib.h ) int atoi(char *s) double atof(char *s) 1 // argdemo.c --- sum of argument strings (as int) int sum = 0; for(int i = 1; i < argc; ++i) { sum += atof(argv[i]); printf("sum = %d\n", sum); %./a.out 4 5 sum = 1 scanf.1 (stack) LIFO(last-in, first-out) 1 1 1 push pop top 1 isempty : push pop ( ) isempty ( ) top (push pop )

? push pop ( ). 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 arr ptr pop ptr : push 1 pop 1 ( ) ptr ptr 1 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); // pop a value and return it int istack_top(istackp p); // peek the topmost value typedef istackp struct istack* top ptr 1 // istack.c --- int type stack impl. with array struct istack { int ptr; int *arr; ; istackp istack_new(int size) { istackp p = (istackp)malloc(sizeof(struct istack)); 1 typedef int *intp intp 1

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]; // reversestr.c --- print argv[1] in reverse order istackp s = istack_new(00); 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 );./a.out argv[1] t ( \0 ) putchar( 1 %./a.out.nep a si siht expect int // test_istack.c --- unit test for istack. void expect_int(int i1, int i, char *msg) { printf("%s %d:%d %s\n", (i1==i)?"ok":"ng", i1, i, msg); struct istack *s = istack_new(00); istack_push(s, 1); istack_push(s, ); istack_push(s, ); expect_int(istack_pop(s),, "push 1 ; pop"); expect_int(istack_pop(s),, "pop"); istack_push(s, 4); expect_int(istack_pop(s), 4, "push 4; pop"); expect_int(istack_pop(s), 1, "pop"); 4

%./a.out OK : push 1 ; pop OK : pop OK 4:4 push 4; pop OK 1:1 pop.1 (1) () () ( 4) OK [ { ( ) ( ( ) ) ] NG ) ( { { [ ( ] ) 4: 5 ( ) ( ) stack ( 1 + ) * ( ( - 1 ) / ) ( ) * ( ( - 1 ) / ) ( ( - 1 ) / ) ( ( - 1 ) / ) ( ( ) / ) ( ) 5: ( ) C balance1 c ( ) 5

( ( ) ) ( ( ) main 1 balance1 OK NG // barance1.c --- see if parentheses are balanced in input bool balance1(char *t) { istackp s = istack_new(00); 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); for(int i = 1; i < argc; ++i) { printf("%s : %s\n", argv[i], balance1(argv[i])?"ok":"ng"); %./a.out ((a)) (a)) ((a)) : OK (a)) : NG boolstr expect bool // test_barance1.c --- unit test for balance1 char *boolstr(bool b) { return b? "true" : "false"; void expect_bool(bool b1, bool b, char *msg) { printf("%s %s:%s %s\n", (b1==b)?"ok":"ng", boolstr(b1), boolstr(b), msg); (balance1 here) 6

int main(void) { expect_bool(balance1("((a)())"), true, "((a)())"); expect_bool(balance1(")(a)()("), false, ")(a)()("); expect_bool(balance1("((a)()"), false, "((a)()"); expect_bool(balance1("(a)())"), false, "(a)())"); expect_bool(balance1("(((())))"), true, "(((())))"); %./a.out OK true:true ((a)()) OK false:false )(a)()( OK false:false ((a)() OK false:false (a)()) OK true:true (((()))) 1 a. () [] { ( [(] OK ) b. ( [(] [()] OK ) c. ( ) ( [(]) OK [{)] ) : d. ( : -1 1 ) e.. ( ) (infix notation) * + 4 + * 4 * (postfix notation) ( ) 7

* 4 + 6 4 + 10 4 4 * + 8 + 11 4 4 4 8 6 6 10 11 * 4 + 4 * + 6: (1) () ( 6) output stack input + * 4 + 1 + * 4 + 1 + * 4 + 1 + * 4 + 1 + * 4 + 1 4 + * + 1 4 * + + 1 4 * + + 1 4 * + + 1 4 * + 1 + 7: C + - 1 * / % ^ ( C ^ ) postfix1 ( ) C *u++ = ; // postfix1.c -- convert infix to postfix (w/o parentheses) #include <ctype.h> 8

int operprec(int c) { switch(c) { case + : case - : return 1; case * : case / : case % : return ; case ^ : return ; default: void postfix1(char *t, char *u) { istackp s = istack_new(00); for(int i = 0; t[i]!= \0 ; ++i) { 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 ; char buf[00]; for(int i = 1; i < argc; ++i) { postfix1(argv[i], buf); printf("%s => %s\n", argv[i], buf); isdigit ctype.h include (0 9) continue %./a.out 1+*+4 1+*+4 => 1*+4+ expect str strcmp string.h include strcmp 0 OK NG // test_postfix1.c --- unit test of postfix1 #include <ctype.h> #include <string.h> 9

(operprec, postfix1 here) void expect_str(char *s1, char *s, char *msg) { printf("%s %s : %s %s\n", strcmp(s1, s)?"ng":"ok", s1, s, msg); int main(void) { char buf[00]; postfix1("1+*", buf); expect_str(buf, "1*+", "1+* => 1*+"); postfix1("*+1", buf); expect_str(buf, "*1+", "*+1 => *1+"); a. ^^4 ( ) 4 (4) ( ) b. ( : ) c. 1 d. ( ) e. 4 4.1 // 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* 10

// pstack.c --- pointer type stack impl. with array #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]; void* void** // reversefile.c --- input a flie and output upside-down #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[00]; pstackp s = pstack_new(00); while(getl(buf, 00)) { 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); getl 1 EOF( ) main getl buf +1( ) for c i for 11

free %./a.out that is a dog. how are you? ^D Ctrl-D how are you? that is a dog. 4. 1 1 1 --- current position --- that is a dog backward upper stack insert that is a dog how are you? today is fine. --- current position --- yeserday snowed that is a dog today is fine. --- current position --- yeserday snowed today is fine. that is a dog yeserday snowed today is fine. --- current position --- forward lower stack delete that is a dog --- current position --- yeserday snowed yeserday snowed 8: 4 5 6 1

A 1 6 1 ( :59 ) 1. sol CED /home/staff/ka00689/prog19upload a. ( ) @@@. 1 4. ( ) ( ) 5. Q1. Q. Q. ( ) B 1 6 ( A ) ( ) :59 1. sol CED /home/staff/ka00689/prog19upload b. ( ) @@@. 1 ( ) ( ) 4. 5. Q1. Q. Q. ( ) 1