r08.dvi

Similar documents
ohp08.dvi

ohp03.dvi

r03.dvi

r07.dvi

ohp07.dvi

ohp11.dvi

r11.dvi

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

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

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

untitled

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

卒 業 研 究 報 告.PDF

超初心者用

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

Microsoft PowerPoint - kougi9.ppt

ex14.dvi

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

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

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

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

1.ppt

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

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

double float

lexex.dvi

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

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

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

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

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

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

O(N) ( ) log 2 N

memo

ポインタ変数

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

slide5.pptx

untitled

untitled

橡Pro PDF

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

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

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

1.3 ( ) ( ) C

新・明解C言語 実践編

新版明解C言語 実践編

ex12.dvi

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

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

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

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

PowerPoint Presentation

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

(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

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

memo

fp.gby

Prog1_15th

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

C B

r1.dvi

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

Condition DAQ condition condition 2 3 XML key value

GNU Emacs GNU Emacs

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

programmingII2019-v01

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

ファイル入出力

file:///D|/C言語の擬似クラス.txt

ex01.dvi

17 1. strucr Potter ( ) Harry Potter and the Philosopher s Stone 1997 English Title : Harry Potter and the Philosopher s Stone : : 1997 #include<stdio

ポインタ変数

tuat1.dvi


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

‚æ4›ñ

Microsoft Word - no206.docx

memo

XMPによる並列化実装2

joho07-1.ppt

第7章 有限要素法のプログラミング

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

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

I J

cpp1.dvi

haskell.gby

Taro-ファイル処理(公開版).jtd

C ( ) C ( ) C C C C C 1 Fortran Character*72 name Integer age Real income 3 1 C mandata mandata ( ) name age income mandata ( ) mandat

1. ファイルにアクセスするには ファイルにアクセスするには 1. ファイルを開く 2. アクセスする 3. ファイルを閉じるという手順を踏まなければなりません 1.1. ファイルを読み込む まずはファイルの内容を画面に表示させるプログラムを作りましょう 開始 FILE *fp char fname

ex01.dvi

プログラミング及び演習 第1回 講義概容・実行制御

WinHPC ppt

10

Jacques Garrigue

(300, 150) 120 getchar() HgBox(x, y, w, h) (x, y), w, h #include <stdio.h> #include <handy.h> int main(void) { int i; double w, h; } HgO

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

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

slide4.pptx

Transcription:

19 8 ( ) 019.4.0 1 1.1 (linked list) ( ) next ( 1) (head) (tail) ( ) top head tail head data next 1: NULL nil ( ) NULL ( NULL ) ( 1 ) (double linked list ) ( ) 1 next 1 prev 1

head cur tail head cur prev data next : ( ) 1 ( ) 1. 100 (100 ) 1 100 MAXSTR // ebuf.h --- editor buffer API. #include <stdbool.h> #define MAXSTR 100 struct ebuf; typedef struct ebuf *ebufp; ebufp ebuf_new(); // create ebuf bool ebuf_iseof(ebufp e); // see if current line is bool ebuf_forward(ebufp e); // forward 1 line bool ebuf_backward(ebufp e); // backward 1 line void ebuf_top(ebufp e); // go to top char *ebuf_str(ebufp e); // obtain current line string void ebuf_insert(ebufp e, char *s); // insert a line (end of file ) ( ) ebuf iseof ebuf top ebuf forward ebuf backward 1 / false ebuf str ( ) ebuf insert // ebufdemo.c --- demonstration of ebuf. #include <stdio.h>

#include "ebuf.h" int main(void) { ebufp e = ebuf_new(); ebuf_insert(e, "abc"); ebuf_insert(e, "def"); ebuf_insert(e, "ghi"); ebuf_top(e); while(!ebuf_iseof(e)) { printf("%s\n", ebuf_str(e)); ebuf_forward(e); while(ebuf_backward(e)) { printf("%s\n", ebuf_str(e)); return 0; % gcc8 ebufdemo.c ebuf.c %./a.out abc def ghi ghi def abc ebuf line char head cur struct ebuf prev str struct line next : ( ) ebuf head cur prev next ebuf // ebuf.c --- editor buffer implementation #include <stdlib.h> #include <string.h> #include "ebuf.h" struct line { struct line *prev, *next; char str[maxstr];

; struct ebuf { struct line *head, *cur; ; ebufp ebuf_new() { ebufp r = (ebufp)malloc(sizeof(struct ebuf)); r->head = (struct line*)malloc(sizeof(struct line)); r->cur = r->head->next = r->head->prev = r->head; strcpy(r->head->str, ""); return r; bool ebuf_iseof(ebufp e) { return e->cur == e->head; bool ebuf_forward(ebufp e) { if(e->cur == e->head) { return false; e->cur = e->cur->next; return true; bool ebuf_backward(ebufp e) { if(e->cur->prev == e->head) { return false; e->cur = e->cur->prev; return true; void ebuf_top(ebufp e) { e->cur = e->head->next; char *ebuf_str(ebufp e) { return e->cur->str; void ebuf_insert(ebufp e, char *s) { struct line *p = (struct line*)malloc(sizeof(struct line)); strncpy(p->str, s, MAXSTR); p->str[maxstr-1] = \0 ; p->prev = e->cur->prev; p->next = e->cur; e->cur->prev->next = p; e->cur->prev = p; head cur head 1 cur cur->next 1 ( ) cur cur->str strncpy (100 ) 1 1 ( 4) cur->prev cur->prev->next p cur p->prev p->str p->next cur->prev 4: #0 expect str // test_ebuf.c --- unit test for cbuf. 4

#include <stdio.h> #include <string.h> #include "ebuf.h" 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) { ebufp e = ebuf_new(); ebuf_insert(e, "abc"); ebuf_insert(e, "def"); ebuf_top(e); expect_str(ebuf_str(e), "abc", "line 1: abc"); ebuf_forward(e); expect_str(ebuf_str(e), "def", "line : def"); ebuf_insert(e, "ghi"); ebuf_top(e); ebuf_forward(e); expect_str(ebuf_str(e), "ghi", "new line : ghi"); ebuf_forward(e); expect_str(ebuf_str(e), "def", "new line : def"); return 0; %./a.out OK abc : abc line 1: abc OK def : def line : def OK ghi : ghi new line : ghi OK def : def new line : def 1 OK a. ebuf replace b. ebuf delete c. ebuf yank ( 5 ) 1 d 1 d 1 d 1 ff 1 y 1 y 1 y 1 5: d. ebuf copy ( 1 ) 5

e. 5 ebuf swap ( ) f. ( ) g..1 (line editor) ( ) (screen editor) ( ) ( Unix ed ) q p t f 1 b 1 i ( ) f p % gcc8 editor.c ebuf.c %./a.out > ithis is a pen. > ithat is a dog. > t > p This is a pen. > f 1 > ihow are you? > t > p This is a pen. > How are you? > That is a dog. 6

> > q ( ) getl // editor.c --- a simple line editor. #include <stdio.h> #include "ebuf.h" bool getl(char s[], int lim) { int c, i = 0; for(c = getchar(); c!= && c!= \n ; c = getchar()) { s[i++] = c; if(i+1 >= lim) { break; s[i] = \0 ; return c!= ; int main(void) { char buf[00]; ebufp e = ebuf_new(); printf("> "); while(getl(buf, 00)) { if(buf[0] == q ) { // quit break; else if(buf[0] == p ) { // print printf(" %s\n", ebuf_str(e)); else if(buf[0] == t ) { // top ebuf_top(e); else if(buf[0] == f ) { // fwd ebuf_forward(e); else if(buf[0] == b ) { // back ebuf_backward(e); else if(buf[0] == i ) { // insert ebuf_insert(e, buf+1); else { // other --- fwd and print ebuf_forward(e); printf(" %s\n", ebuf_str(e)); printf("> "); return 0; 1. 7

FILE *f = foepn(, ); FILE* ( : ) NULL printf fprintf(,, ) 1 fgets(,, ) 1 fgets ( ) NULL fclose( ) ( ) fopen fgets (NULL ) ( ebuf bool readfile(ebufp e, char *fname) { char str[00]; FILE *f = fopen(fname, "r"); if(f == NULL) { return false; while(fgets(str, 00, f)!= NULL) { int len = strlen(str); if(len > 0) { str[len-1] = \0 ; ebuf_insert(e, str); fclose(f); return true; fopen fprintf bool writefile(ebufp e, char *fname) { FILE *f = fopen(fname, "w"); if(f == NULL) { return false; ebuf_top(e); while(!ebuf_iseof(e)) { fprintf(f, "%s\n", ebuf_str(e)); ebuf_forward(e); fclose(f); return true; main i readfile(e, buf+1) writefile(e, buf+1) (false ) OK / a. r w 8

b. f b ( ) b. d c. s d. 1c y e. 1d x f. option.1 ncurses Unix ( ) ncurses // ncursesdemo.c --- show usage of ncurses. #include <ncurses.h> #include <stdlib.h> int main(void) { initscr(); noecho(); cbreak(); system("stty raw"); clear(); move(10, 10); addstr("press any key"); refresh(); int ch = getch(); addch( a ); addch( b ); refresh(); ch = getch(); move(10, 15); insch( a ); insch( b ); refresh(); ch = getch(); delch(); move(10, 0); clrtoeol(); refresh(); ch = getch(); endwin(); return 0; ncurses.h stdlib.h initscr() ncurses noecho ( ) cbreak 1 ( ) system Unix ^C (^C ) endwin ncurses refresh getch 1 refresh move(y, X) Y X addch(c) addstr(s) 1 insch(c) 1 delch() 9

clear() cleartoeol() refresh() ( ) getch() 1 -lncurses % gcc8 ncursesdemo.c -lncurses %./a.out 10 press any key press any keyab pressba any keyab 0 pressa any ncurses. 1 1 ( ) ( readfile writefile // sedit.c --- very primitive screen editor w/ ncurses. #include <stdio.h> #include <stdbool.h> #include <ncurses.h> #include <stdlib.h> #include <string.h> #include "ebuf.h" // readfile, writefile here p 1 void inschar(char *s, char ch, int p, int len) { int i; for(i = len + 1; i > p; --i) { s[i] = s[i-1]; s[p] = ch; void delchar(char *s, int p, int len) { int i; for(i = p; i < len; ++i) { s[i] = s[i+1]; 10

ncurses handleline (1 ) false true ( ) bool handleline(int c, char *s, int *pos, int *len) { if(c >= && c <= ~ ) { if(*len >= MAXSTR-1) { return false; insch(c); inschar(s, c, *pos, (*len)++); move(10, ++(*pos)); else if(c == B - @ ) { if(*pos > 0) { move(10, --(*pos)); else if(c == F - @ ) { if(*pos < *len) { move(10, ++(*pos)); else if(c == H - @ ) { if(*pos <= 0) { return false; move(10, *pos - 1); delch(); delchar(s, --(*pos), (*len)--); else { return false; return true; MAXSTR-1 OK 1 (insch move ) ^F ^B ( @ ) 0 BS ( ^H ) 1 1 false main ebuf readfile show 1 ( true ) ncurses show show false refresh int main(int argc, char *argv[]) { ebufp e = ebuf_new(); if(!readfile(e, argv[1])) { printf("?\n"); return 1; ebuf_top(e); int len, pos, ch; char *str; bool show = true; initscr(); noecho(); cbreak(); system("stty raw"); clear(); 11

while(true) { if(show) { str = ebuf_str(e); len = strlen(str); pos = 0; move(10, 0); addstr(str); clrtoeol(); move(10, 0); show = false; refresh(); ch = getch(); if(handleline(ch, str, &pos, &len)) { // do nothing else if(ch == J - @ ) { ebuf_forward(e); ebuf_insert(e, ""); ebuf_backward(e); show = true; else if(ch == N - @ ) { ebuf_forward(e); show = true; else if(ch == P - @ ) { ebuf_backward(e); show = true; else if(ch == Z - @ ) { break; endwin(); writefile(e, argv[1]); return 0; handleline OK OK ( ^J 1 ) (^N ^P) show ^Z curses ebuf 1 4 1 a. 1 ( 5 ) 10 b. emacs c. ^J d. ^H e. ^F ^B ( : handleline ) f. 1 g. h. 1

? initscr() getmaxyx(stdscr, height, width); height width 1 8A 1 6 1 ( :59 ) 1. sol CED /home/staff/ka00689/prog19upload 8a. ( ) @@@. 1 4. ( ) ( ) 5. Q1. Q. Q. ( ) 8B 1 6 ( 8A ) ( ) :69 1. sol CED /home/staff/ka00689/prog19upload 8b. ( ) @@@. 1 ( ) ( ) 4. 5. Q1. Q. Q. ( ) 1 & 1