新版明解C言語 実践編

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

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

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

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

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

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

卒 業 研 究 報 告.PDF

PowerPoint Presentation

cpp1.dvi

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

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

tuat1.dvi

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

新版 明解C++入門編

r07.dvi

ohp07.dvi

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

double float

新版明解C言語入門編

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

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

‚æ4›ñ

C

lexex.dvi

P05.ppt

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

P06.ppt

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

untitled

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

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

Condition DAQ condition condition 2 3 XML key value

ex14.dvi

ex01.dvi

comment.dvi

kiso2-09.key

program.dvi

解きながら学ぶ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

ohp08.dvi

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

Taro-最大値探索法の開発(公開版

P03.ppt

Microsoft PowerPoint - kougi9.ppt

untitled

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

橡Pro PDF

P02.ppt

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

1.1 1 C IIA $ cd comp3a %endminipage ~/comp3a mkdir $ mkdir comp3a $ cd comp3a C.c Emacs Cntrol x Control s 2 Emacs Control-x Control-f Control-

slide5.pptx

解きながら学ぶJava入門編

r08.dvi

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

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


解きながら学ぶ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>

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

I J

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

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

ex01.dvi

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

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

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

明解Javaによるアルゴリズムとデータ構造

Java演習(4) -- 変数と型 --

main

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

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

1 CUI CUI CUI 1.1 cout cin redirect.cpp #i n c l u d e <s t r i n g > 3 using namespace std ; 5 6 i n t main ( void ) 7 { 8 s t r i n g s ; 10 c

8 if switch for while do while 2

tuat2.dvi

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

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

untitled


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

Microsoft Word - no14.docx

Microsoft PowerPoint - kougi8.ppt

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

1 4 2 EP) (EP) (EP)

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

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

1.3 ( ) ( ) C

memo

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

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

XMPによる並列化実装2

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

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

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

Prog1_6th

Microsoft PowerPoint - C言語の復習(配布用).ppt [互換モード]

スライド タイトルなし

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

memo

6 6.1 sound_wav_files flu00.wav.wav 44.1 khz 1/44100 spwave Text with Time spwave t T = N t N 44.1 khz t = 1 sec j t f j {f 0, f 1, f 2,, f N 1

Transcription:

2 List - "max.h" a, b max List - max "max.h" #define max(a, b) ((a) > (b)? (a) : (b)) max List -2 List -2 max #include "max.h" int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max(x, y) = %d\n", max(x, y)); "max.h" EOF

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

4 "max.h" List -2 '0x8' '0x40' Fig.-2 x, y int int x, y; Fig.-2 white-space character # List -3 fopen fclose

5 List -3 detab "detab.c" #include <stdlib.h> --- srcdst --- void detab(file *src, FILE *dst, int width) int ch; int pos = ; while ((ch = fgetc(src))!= EOF) int num; switch (ch) case '\t': num = width - (pos - ) % width; for ( ; num > 0; num--) fputc(' ', dst); detab pos++; break; "test.c" case '\n': 4 fputc(ch, dst); pos=; break; default: fputc(ch, dst); pos++; break; > detab -t4 test.cÿ 8 int main(int argc, char *argv[]) int width = 8; 8 FILE *fp; > detab test.c 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 (); else if ((fp = fopen(*argv, "r")) == NULL) fprintf(stderr, "%s\n", *argv); return (); else detab(fp, stdout, width); fp fclose(fp);

6 List -4 List - List -4 max "max.h" #define max (a, b) ((a) > (b)? (a) : (b)) max 2 TRUE #define TRUE TRUE ( ) ( List -4 max ( Fig.-3 max (a, b) ((a) > (b)? (a) : (b)) #define max(a, b) ((a) > (b)? (a) : (b)) max(x, y) (a, b) ((a) > (b)? (a) : (b))(x, y) #define max(a, b) ((a) > (b)? (a) : (b)) max(x, y) ((x) > (y)? (x) : (y)) Fig.-3

7 ( 4 4 4 4 ( z = max (x, y); max( ( ) max 4 4 z = (max)(x, y); ( ) List -5 List -5 --- --- #define max(a, b) ((a) > (b)? (a) : (b)) --- --- int (max)(int a, int b) puts("max"); return (a > b? a : b); x5ÿ y7ÿ max(x, y) = 5 max (max)(x, y) = 5 int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max(x, y) = %d\n\n", max(x, y)); printf("(max)(x, y) = %d\n", (max)(x, y));

8 # # # # include #if defined( DOHC ) #include <double.h> #else Fig.-4 #include <single.h> #endif Fig.-4 Fig.-4 #if Fig.-5 a = x; a = x; ax Fig.-5 Fig.-6 #if ax Fig.-6 #if 0 a = x; 0 #endif #if

9 #if 0 #if0 if 0 #if Fig.-7 # # if if # if Fig.-7 List -6 List -6 #if #define DEBUG 0 a5 int a = 5; int x = ; #if DEBUG == a = x; ax #endif printf("a%d\n", a); a DEBUG 0 DEBUG #if

0 2 #define para 0 #define para 0 #include "max.h" #include "max.h" "abc.h" max "max.h" #include "max.h" "max.h" #include "abc.h" "max.h""abc.h" "max.h" "def.h" 4 4 int a; #include "def.h" #include "def.h" List -7 List -7 "max.h" #if!defined( MAX) #define MAX #define max(a, b) ((a) > (b)? (a) : (b)) #endif MAX MAX max MAX!defined( MAX)

max a, b, c, d x = max(max(a, b), max(c, d)); Fig.-8 x = max(max(a, b), max(c, d)); x = ((((a) > (b)? (a) : (b))) > (((c) > (d)? (c) : (d)))? (((a) > (b)? (a) : (b))) : (((c) > (d)? (c) : (d)))); Fig.-8 x = max(max(max(a, b), c), d); Fig.-9 x = max(max(max(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.-9 > if x = a; if (b > x) x = b; 4 if (c > x) x = c; if (d > x) x = d;

2 max List -8 inline inline function List -8 // // max++ "max.h" // //--- ---// inline int max(int a, int b) return (a > b? a : b); max(x++, y) ((x++) > (y)? (x++) : (y)) 2 int function overloading "max.h" List -9 cplusplus List -9 max++ "max.h" #if defined cplusplus ++ inline int max(int a, int b) return (a > b? a : b); inline long max(long a, long b) return (a > b? a : b); inline double max(double a, double b) return (a > b? a : b); #else #define max(a, b) ((a) > (b)? (a) : (b)) #endif

3 max > int, long, double function template max "max.h" List -0 List -0 max++ "max.h" #if defined cplusplus ++ template <class Type> Type max(type a, Type b) return (a > b? a : b); #else #define max(a, b) ((a) > (b)? (a) : (b)) #endif int double List - List - // // max++ // #include <iostream> #include "max.h" using namespace std; x5ÿ y7ÿ max(x, y) = 5 int x, y; cout << "x"; cin >> x; cout << "y"; cin >> y; cout << "max(x, y) = " << max(x, y) << endl;

4 0 int x = 5; 5 initializerx 5 List -2 List -2 int n = 4.5; n = 4 printf("n = %d\n", n); int y = 97.2; int y 97.2 97

5 4 4 4 4 4 int m = 3; int x; x = 0; const int a; int b; a variable a a 053 32.5 'x' objecta b Fig.-0 Fig.-0 nameidentifier B. W. Kernighan and Dennis M. Ritchie The C Programming Language variable object

6 List -3 List -3 #include <math.h> void func(int no) register int i; auto int x = 00; printf("x = %d\n", x); for (i = 0; i < no; i++) double x = sin((double)i / no); printf("x = %f\n", x); printf("x = %d\n", x); x = 00 x = 0.000000 x = 0.099833 x = 0.98669 x = 0.295520 x = 0.38948 x = 0.479426 x = 0.564642 x = 0.64428 x = 0.77356 x = 0.783327 x = 00 func(0); func no i x func automatic storage duration Fig.- auto register Fig.- auto register register

7 func x x for x for for x x for sin((double)i / no) List -4 List -4 x = 957 int x; printf("x = %d\n", x); x 957-38 0

8 static storage duration List -5 List -5 int ft = 0; void func(void) int at = 0; static int st = 0; ft++; at++; st++; printf("ft = %d at = %d st = %d\n", ft, at, st); ft = at = st = ft = 2 at = st = 2 ft = 3 at = st = 3 ft = 4 at = st = 4 ft = 5 at = st = 5 ft = 6 at = st = 6 ft = 7 at = st = 7 ft = 8 at = st = 8 int i; for (i = 0; i < 8; i++) func(); ft st static Fig.-2 static Fig.-2

9 func ft st 0 func at func 0 ft st ft st int ft; void func(void) int at = 0; static int st; 0 void func(void) int x = sin(0.9); static int st = sin(0.9); Table - Table - main 0

20 List -6 List -6 x = 9572 int x = ; int x = x; printf("x = %d\n", x); main x x x x x x List -7 List -7 int z = ; int x = z; int z = 0; int y = z; printf x, y, z

2 int i = 5; ++ int i(5); // ++ List -8 List -8 // // ( )++ // #include <iostream> using namespace std; x = 5 y = 5 int x = 5; x5 int y(5); y5 cout << "x = " << x << endl; cout << "y = " << y << endl; Complex class Complex double re, im; public: Complex(double r, double i = 0.0) re = r; im = i; //... ; // // Complex Complex a(5.0, 7.5); // Complex b(5.0); // ( ) ( ) Complex b = 5.0; // int double

22 int a[3] =, 2, 3;, 2, 3 a, 2, 3 a[0], a[], a[2], 2, 3 List -9 List -9 int vx[3] =, 2, 3; vx[0] = ma[0] = ms[0] = vx[] = 2 ma[] = 2 ms[] = 2 int i; vx[2] = 3 ma[2] = 3 ms[2] = 3 int ma[3] =, 2, 3; static int ms[3] =, 2, 3; for (i = 0; i < 3; i++) printf("vx[%d] = %d ma[%d] = %d ms[%d] = %d\n", i, vx[i], i, ma[i], i, ms[i]); K&R static ma List -20 3 b 0 Fig.-3 b[] b[2] 0 0 Fig.-4

23 List -20 0 int i; int b[3] = ;,0,0 if (b[]!= 0 b[2]!= 0) puts(""); else for (i = 0; i < 3; i++) printf("b[%d] = %d\n", i, b[i]); b[0] = b[] = 0 b[2] = 0 int b[3] = ; int b[3] =, 0, 0; Fig.-3 int x[000] = 0; 0 0 Fig.-4 2 int c[2] =, 2, 3; int d[3]; static 0 static p.7 int x[3]; x = 0,, 2;

24 List -2 List -2 int i, j; int x[3][2] = 0,, 2, 3, 4, 5, ; for (i = 0; i < 3; i++) for (j = 0; j < 2; j++) printf("x[%d][%d] = %d\n", i, j, x[i][j]); x[0][0] = 0 x[0][] = x[][0] = 2 x[][] = 3 x[2][0] = 4 x[2][] = 5 x Fig.-5 x[0][0], x[0][], x[][0], x[][], x[2][0], x[2][] int x[3][2] = 0,, 2, 3, 4, 5, ; 0 2 3 4 5 x[0][0] x[0][] x[][0] x[][] x[2][0] x[2][] Fig.-5 int x[3][2] = 0,, 2, 3, 4, 5, ;, int x[3][2] = 0,, 2, 3, 4, 5 ; Fig.-6

25,, Fig.-6 int d[3] =, 2, 3,;, 0 Fig.-7 int x[3][2] = 0, 2, 3, 4, ; int x[3][2] = 0, 0, 2, 3, 4, 0, ; 0 0 2 3 4 0 x[0][0] x[0][] x[][0] x[][] x[2][0] x[2][] Fig.-7 Fig.-8 int x[3][2] = 0,, 2; int x[3][2] = 0,, 2, 0, 0, 0, ; 0 2 0 0 0 x[0][0] x[0][] x[][0] x[][] x[2][0] x[2][] Fig.-8

26 typedef typedef typedef typedef int INTEGER; INTEGERint INTEGER int INTEGER a; aint int a; aint typedef List -22 int 5 Int5ary typedef List -22 typedef int i; typedef int Int5ary[5]; int5 Int5ary x =, 2, 3; for (i = 0; i < 5; i++) printf("x[%d] = %d\n", i, x[i]); x[0] = x[] = 2 x[2] = 3 x[3] = 0 x[4] = 0 typedef 5 x 0 int x[5] =, 2, 3;

27 List -23 List -23 typedef int i; typedef int IntAry[]; int IntAry a =, 2, 3; 3 IntAry b =, 2, 3, 4, 5; 5 for (i = 0; i < 3; i++) printf("a[%d] = %d\n", i, a[i]); for (i = 0; i < 5; i++) printf("b[%d] = %d\n", i, b[i]); a[0] = a[] = 2 a[2] = 3 b[0] = b[] = 2 b[2] = 3 b[3] = 4 b[4] = 5 int IntAry typedef a b IntAry a 3 b 5 a, b int a[3] =, 2, 3; int b[5] =, 2, 3, 4, 5; IntAry