新・明解C言語 実践編

Similar documents
新版明解C言語 実践編

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

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

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

新版 明解C++入門編

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

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

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

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

double float

卒 業 研 究 報 告.PDF

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

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

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

untitled

PowerPoint Presentation

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

cpp1.dvi

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

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

BW BW

(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

lexex.dvi

tuat1.dvi

橡Pro PDF

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

C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

comment.dvi

‚æ4›ñ

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

untitled

slide5.pptx

情報処理演習 B8クラス

ohp08.dvi

Microsoft PowerPoint - 説明3_if文switch文(C_guide3)【2015新教材対応確認済み】.pptx

Cプログラミング1(再) 第2回

r07.dvi

I 2 tutimura/ I 2 p.1/??

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

ohp07.dvi

memo

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

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

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

スライド タイトルなし

r08.dvi

(STL) STL 1 (deta structure) (algorithm) (deta structure) 2 STL STL (Standard Template Library) 2.1 STL STL ( ) vector<int> x; for(int i = 0; i < 10;

C のコード例 (Z80 と同機能 ) int main(void) { int i,sum=0; for (i=1; i<=10; i++) sum=sum + i; printf ("sum=%d n",sum); 2

untitled

P05.ppt

Condition DAQ condition condition 2 3 XML key value

ex14.dvi

プログラミング基礎

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

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

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

Microsoft PowerPoint - kougi9.ppt

プログラミング演習 土曜日(Q組)

1 4 2 EP) (EP) (EP)

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

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

memo

ohp03.dvi

Prog1_6th

j x j j j + 1 l j l j = x j+1 x j, n x n x 1 = n 1 l j j=1 H j j + 1 l j l j E

gengo1-12

DVIOUT

解きながら学ぶJava入門編

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

1.3 ( ) ( ) C

ファイル入出力

Prog1_12th

ファイル入出力

C言語入門

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

PowerPoint プレゼンテーション - 物理学情報処理演習

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

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

memo

program.dvi

Microsoft Word - Training10_プリプロセッサ.docx

超初心者用

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

Microsoft Word - 【第5分科会】ConcolicTestingグループ_付録_修正_ doc

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

2006年10月5日(木)実施

新コンフィギュレータのフレームワークについて

PowerPoint Presentation

グラフと組み合わせ 課題 7 ( 解答例 ) 2013/5/27 1 列挙 n 個の文字の集合 { } S = a, a,, an の全てからなる文字列 つまり同じ文字を含まない 長さ n の文字列を列挙する 方法を考える 1. 何通りの文字列があるかを答えなさい また そのことが正しい

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裱£²²ó ¡Ý½ÉÂꣲ¤Î²òÀ⡤±é½¬£²¡Ý

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

02: 変数と標準入出力

C

gengo1-12

プログラミング基礎

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

tuat2.dvi

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè11²ó – ½ÉÂꣲ¤Î²òÀ⡤±é½¬£² –

Microsoft Word - no14.docx

Transcription:

第 1 章 見

21 1-1 見えないエラー 見 List 1-1 "max2x1.h" a, b max2 List 1-1 chap01/max2x1.h max2 "max2x1.h" #define max2(a, b) ((a) > (b)? (a) : (b)) max2 List 1-2 List 1-2 chap01/max2x1test.c max2 #include <stdio.h> #include "max2x1.h" 実行結果 int main(void) int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max2(x, y) = %d\n", max2(x, y)); return 0; "max2x1.h" EOF "max2x1.h" Fig.1-1 a

3a #define max2(a, b) ((a) > (b)? (a) : (b)) EOF b #define max2(a, b) ((a) > (b)? (a) : (b)) Ÿ EOF EOF Ÿ 1-1 Fig.1-1 #define #define #include preprocessing directive b a 4 4 4 4 4 4 4 4 4

41 見 "max2x1.h" List 1-2 '0x81' '0x40' JIS Fig.1-2 x, y int int x, y; Fig.1-2 white-space character # List 1-3 fopen fclose 8

5 List 1-3 detab #include <stdio.h> #include <stdlib.h> --- srcdst --- void detab(file *src, FILE *dst, int width) int ch; int pos = 1; chap01/detab.c 1-1 while ((ch = fgetc(src))!= EOF) int num; switch (ch) case '\t': num = width - (pos - 1) % width; for ( ; num > 0; num--) fputc(' ', dst); pos++; break; case '\n': fputc(ch, dst); pos=1; break; default: fputc(ch, dst); pos++; break; int main(int argc, char *argv[]) int width = 8; 8 FILE *fp; 実行方法 detab "test.c"4 > detab -t4 test.c Ÿ 8 > detab test.c xyz.c Ÿ > detab -t4 test.c -t8 xyz.c Ÿ if (argc < 2) detab(stdin, stdout, width); else while (--argc > 0) if (**(++argv) == '-') if (*++(*argv) == 't') width = atoi(++*argv); else fputs("\n", stderr); return 1; else if ((fp = fopen(*argv, "r")) == NULL) fprintf(stderr, "%s\n", *argv); return 1; else detab(fp, stdout, width); fp fclose(fp); return 0;

61 見落 List 1-4 List 1-1 List 1-4 chap01/max2x2.h max2 "max2x2.h" #define max2 (a, b) ((a) > (b)? (a) : (b)) "chap01/max2x2test.c" max2 object-like macro TRUE 1 4 4 #define TRUE 1 TRUE function-like macro 4 4 ( ) ( List 1-4 max2 ( Fig.1-3 a max2 max2 (a, b) ((a) > (b)? (a) : (b)) 4 4 4 4 b a max2 #define max2(a, b) ((a) > (b)? (a) : (b)) max2(x, y) (a, b) ((a) > (b)? (a) : (b))(x, y) b max2 #define max2(a, b) ((a) > (b)? (a) : (b)) max2(x, y) ((x) > (y)? (x) : (y)) Fig.1-3

7 ( 4 4 4 ( z = max2 (x, y); max2( ( ) z = (max2)(x, y); max2 max2 1-1 ( ) List 1-5 List 1-5 chap01/max2.c #include <stdio.h> --- --- #define max2(a, b) ((a) > (b)? (a) : (b)) --- --- int (max2)(int a, int b) puts("max2"); return a > b? a : b; 実行例 x15 Ÿ y7 Ÿ max2(x, y) = 15 max2 (max2)(x, y) = 15 int main(void) int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max2(x, y) = %d\n\n", max2(x, y)); printf("(max2)(x, y) = %d\n", (max2)(x, y)); return 0;

81 前処理指令内 空白 #include <stdio.h> # # # # include #if defined( DOHC ) #include <double.h> #else Fig.1-4 #include <single.h> #endif Fig.1-4 #if 指令 注釈 #if Fig.1-5 a = x; a = x; ax Fig.1-5 Fig.1-6 #if #if 0 0 a = x; #endif ax Fig.1-6 #if

9#if 0 #if0 if 0 #if Fig.1-7 # # if # if if Fig.1-7 #if List 1-6 1-1 List 1-6 #if #include <stdio.h> #define DEBUG 0 int main(void) int a = 5; int x = 1; #if DEBUG == 1 a = x; ax #endif printf("a%d\n", a); 1 chap01/debug.c 実行結果㆒ a5 実行結果㆓ a1 return 0; DEBUG 0 DEBUG 1 #if Column 1-1p.12 NDEBUG

101 ㆒ #define para 10 ㆒ #define para 10 ㆓ ㆓ #include "max2.h" #include "max2.h" "abcd.h" max2 "max2.h" #include "max2.h" "max2.h" #include "abcd.h" "max2.h""abcd.h" "max2.h" "def.h" int a; #include "def.h" #include "def.h" List 1-7 List 1-7 "max2.h" #ifndef MAX2 #define MAX2 chap01/max2.h #define max2(a, b) ((a) > (b)? (a) : (b)) #endif MAX2 MAX2 max2 2 MAX2 #ifndef MAX2 inlcude guard 7

11関数形式 実行効率 max2 a, b, c, d x = max2(max2(a, b), max2(c, d)); Fig.1-8 1-1 x = max2(max2(a, b), max2(c, d)); x = ((((a) > (b)? (a) : (b))) > (((c) > (d)? (c) : (d)))? (((a) > (b)? (a) : (b))) : (((c) > (d)? (c) : (d)))); Fig.1-8 max2 x = max2(max2(max2(a, b), c), d); Fig.1-9 x = max2(max2(max2(a, b), c), d); x = ((((((a) > (b)? (a) : (b))) > (c)? (((a) > (b)? (a) : (b))) : (c))) > (d)? (((((a) > (b)? (a) : (b))) > (c)? (((a) > (b)? (a) : (b))) : (c))) > (d)); Fig.1-9 max2 > if x = a; if (b > x) x = b; if (c > x) x = c; if (d > x) x = d;

121 関数形式 副作用 z = max2(x++, y); x y z x z = ((x++) > (y)? (x++) : y); x Column 2-2 side effect Column 1-1 NDEBUG assert List 1-6p.9 NDEBUG assert NDEBUG #define NDEBUG NDEBUG Visual Studio NDEBUG assert NDEBUG <assert.h> NDEBUG assert #define assert(ignore) ((void)0) assert

13NDEBUG assert void assert(int expression); expression 0 abort FILE LINE Assertion failed :, file, line assert List 1C-1 1-1 List 1C-1 assert chap01/assert_div.c #include <stdio.h> #include <assert.h> --- ab --- void div(int a, int b) assert(b!= 0); printf("%d%d%d%d\n", a, b, a / b, a % b); int main(void) int a, b; printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); div(a, b); 実行例 a = 7 Ÿ b = 2 Ÿ 7231 実行結果一例 a = 7 Ÿ b = 0 Ÿ Assertion failed: b!= 0, file \C\chap01\assert_div.c, line 11 return 0; div a b b 0 b!= 0 b 0 assert(b!= 0) b 0 assert

141 C++ max2 実現 max2 関数 List 1-8 inlineinline function "chap01/max2_inline_test.cpp" List 1-8 max2++ //--- ---// inline int max2(int a, int b) return a > b? a : b; chap01/max2_inline.h max > max2 int 多重定義 function overloading List 1-9 "max2_overload.h" List 1-9 chap01/max2_overload.h max2++ #ifdef cplusplus ++ inline int max2(int a, int b) return a > b? a : b; inline long max2(long a, long b) return a > b? a : b; inline double max2(double a, double b) return a > b? a : b; #else #define max2(a, b) ((a) > (b)? (a) : (b)) #endif cplusplus "chap01/max2_overload_test.cpp" int long double

15関数 function template max2 List 1-10 "max2_template.h" List 1-10 C++ chap01/max2_template.h max2++ #ifdef cplusplus ++ template <typename Type> Type max2(const Type& a, const Type& b) return a > b? a : b; #else #define max2(a, b) ((a) > (b)? (a) : (b)) #endif 1-1 List 1-11 List 1-11 max2++ #include <iostream> #include "max2_template.h" using namespace std; int main(void) int a, b; double x, y; cout << "a"; cin >> a; cout << "b"; cin >> b; cout << "x"; cin >> x; cout << "y"; cin >> y; cout << "max2(a, b) = " << max2(a, b) << '\n'; cout << "max2(x, y) = " << max2(x, y) << '\n'; return 0; chap01/max2_template_test.cpp 実行例 a15 Ÿ b7 Ÿ x1.25 Ÿ y3.14 Ÿ max2(a, b) = 15 max2(x, y) = 3.14 int double