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-

Similar documents
‚æ4›ñ

untitled

2008 IIA (program) pro(before)+gram(write) (artificial language) (programming languege) (programming) (machine language) (assembly language) ( )

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

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

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

新版明解C言語 実践編

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

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

char int float double の変数型はそれぞれ 文字あるいは小さな整数 整数 実数 より精度の高い ( 数値のより大きい より小さい ) 実数 を扱う時に用いる 備考 : 基本型の説明に示した 浮動小数点 とは数値を指数表現で表す方法である 例えば は指数表現で 3 書く

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

PowerPoint Presentation

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

講習No.12

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

tuat1.dvi

数値計算

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

2 P.S.P.T. P.S.P.T. wiki 26

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

Informatics 2014

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

gengo1-2

untitled

r07.dvi

ohp07.dvi

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

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

卒 業 研 究 報 告.PDF

P05.ppt

pptx

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

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

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in


P03.ppt

Informatics 2010.key

8 / 0 1 i++ i 1 i-- i C !!! C 2

解きながら学ぶJava入門編

P02.ppt

[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

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

cpp1.dvi

kiso2-06.key

P06.ppt

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

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

Informatics 2015

Microsoft PowerPoint - prog05.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)* (

8 if switch for while do while 2

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

(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

導入基礎演習.ppt

Java updated

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

新・明解C言語 実践編

C

計算機プログラミング

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

(1 ) scanf(

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

1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

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

プログラミング基礎

fuga scanf("%lf%*c",&fuga); 改行文字を読み捨てる 10 進数の整数 おまじない取り込んだ値を代入する変数 scanf( %d%*c,&hoge); キーボードから取り込め という命令 1: scanf 1 1: int double scanf %d %lf printf

K227 Java 2

1.ppt

ex12.dvi

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

joho07-1.ppt

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

超初心者用

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

kiso2-09.key

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

/* sansu1.c */ #include <stdio.h> main() { int a, b, c; /* a, b, c */ a = 200; b = 1300; /* a 200 */ /* b 200 */ c = a + b; /* a b c */ }

1 4 2 EP) (EP) (EP)


DOPRI5.dvi

Microsoft Word - 03

C による数値計算法入門 ( 第 2 版 ) 新装版 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 新装版 1 刷発行時のものです.

C C UNIX C ( ) 4 1 HTML 1

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

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


< F2D837C E95CF CF68A4A94C5816A2E6A>

For_Beginners_CAPL.indd

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

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

初歩のC言語ターミナル_2014_May.pages

(Version: 2017/4/18) Intel CPU 1 Intel CPU( AMD CPU) 64bit SIMD Inline Assemler Windows Visual C++ Linux gcc 2 FPU SSE2 Intel CPU do

Microsoft Word - no14.docx

prog-text.dvi

+ +

USB ID TA DUET 24:00 DUET XXX -YY.c ( ) XXX -YY.txt() XXX ID 3 YY ID 5 () #define StudentID 231

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

tuat2.dvi

double float

Transcription:

1 C IIA 1 C IIA IIA 1.1 Mac OS X 1.1.1 Mac OS X Unicode(UTF-8) UTF-8 Jedit X( ) Emacs( ) Emacs Emacs Emacs [Finder] [] Emacs dock Jedit X C 1. Jedit X Dock drag & drop Jedit X [Finder] [] Jedit X Folder 2. Dock Jedit X 3. [Jedit X] [] 4. [ ] Jedit X 5. [ ] 6. [ ] (UTF-8) Unix(LF) 7. [] 1 8. [ ] 1.1.2 terminal emulator C (Terminal.app) UTF-8 [Finder] [] [ ] Dock drag and drop 1 1

1.1 1 C IIA 1.1.3 $ cd comp3a %endminipage ~/comp3a mkdir $ mkdir comp3a $ cd comp3a C.c Emacs Cntrol x Control s 2 Emacs Control-x Control-f Control-x Control-s 1.1.4 c cc ( ) C $ cc -o -o -o cc -o o tt a.out C cc -Wall(warning all) Wall $ cc -Wall -o 2 Emacs Control- 2

1.2 C 1 C IIA $././ 1.2 C C99(ISO/IEC9899:1999) ANSI C(C89) C99 JIS X 3010:2003 1.2.1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /******************************************* * add.c * * 2 10 * *******************************************/ int a,b,sum; printf("2 a,b \n"); printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); sum = a + b; printf("a+b=%d\n",sum); 1.1 add.c (1) (2) (3) main() (4) (5) (6) (7) (8) (9) return 1.2.2 C99 37 (keyword) 3 (identifier) 32( 4 ) ANSI C 5 C99 ( IIA ) 3 3

1.3 1 C IIA 1: C99 auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while inline restrict Bool Complex Imaginary 1.3 ( ) (type) IIA 2: C Bool Bool char char signed char short int int long int long long int unsigned char unsigned short int un- signed int unsigned long int unsigned long long int enum float double long double float Complex double Complex long double Complex (basic type) 1.4 ( ) 1.4.1 4

1.4 1 C IIA 1.1 add.c 19 0 1.4.2 ( ) (identifier) 1.2 add.c int a, b, sum 3 int a, b, sum 1.4.3 C99 ANSI C ( IIA ) 3: + - ++ -- + - * / % = += -= *= /= %= &&! < <= > >= ==!= () sizeof sizeof?: & * []. ->, ~ & ^ << >> &= ^= = >>= <<= # ## 1.3 add.c 13 15 &16 = + 1.4.3.1 1.4 add.c 16 sum = a + b a + b sum 5

1.5 1 C IIA ()( ) []( ) ++ --( ). ->( ) ++ --( ) + -( )!( ) ~( ) sizeof &( ) *( ) ()( ) * / %( ) + -( ) << >>() > >= < <=( ) ==!=( ) &( AND ) ^( OR ) ( OR ) &&( AND ) ( OR )?:( ) = += -= *= /= %= >>= <<= &= ^= = ( ),( ) 1.5 C99 1.5.1 1.2 0 1. if 2. if-else 3. switch 1.5.2 for ( 1 ; 2 ; 3) while 1; while ( 2) 3 6

4: if if ( ) 0 if-else if( ) 1 else 2 0 1 0 2 1 2 switch switch( ) 2 ( case : default : ) case default 1.3 n 1 n 1. for 2. while 3. do 1.5.3 [1] Al Kelley, Ira Pohl, A Book on C, Addison Wesley, 1998, C BP 2002 ANSI C [2] 1997 JIS 1997 ANSI C(ISO/IEC 9899:1990) JIS X3010-1993 [3] C2003 C99(ISO/IEC 9899:1999) JIS X 3010:2003 [4] C http://okuyama.mt.tama.hosei.ac.jp/unix/c/ C99 [5] C 3 SOFT BANK 2007 ANSI C 7

5: for for (i=0 ; i<n ; i++) i= 0, 1,..., N 1 i= N for for for (i=m ; i<= N ; i++) i= M, M + 1,..., N i= N + 1 for for for ( 1 ; 2 ; 3) 1 1 2 0 0 3 while while( ) 0 ( ) 0 1 do do while( ); 0 ( ) 0 1 6: continue continue; continue continue break break; break switch break switch goto goto ; return return; return ; 8

2 2 2.1 (module) (divide and conquer algorithm) (stepwise refinement) (module) C 2.2 ( ) C 2.3 main() 1 C 2.3.1 add() C 9

2.3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /****************************************** * add-func.c * * 2 double * ******************************************/ double add(double x,double y); double a; a=add(1.2,2.3); printf("a=%f\n",a); double add(double x,double y) double z; z=x+y; return z; 3 main() (8-16 ) printf() (13 ) add() (18-25 ) add() 1. 6 double add(double x,double y); (function prototype) ; 2. 12 add(1.2,2.3) (function call) add() 1.2,2.3 1.2 x 2.3 y 3. 12 a=add(1.2,2.3); add(1.2,2.3) 3.5 double a 4. 18-25 (function definition) add() 5. 19-25 (function body) add() 6. 20 double z; add() double z 7. 24 return z; return z add() 10

2.3 2 add-func.c $ cc -Wall add-func.c -o add-func $./add-func a=3.500000 main() (actual arguments/actual parameters) (formal arguments/formal parameters) ( ) return (return value) void printf() 1 (char ) hello.c printf() 1 "Hello World\n"int 12 ( \n 1 ) printf() /* function printf() */ int num; num=printf("hello World\n"); printf(" %d \n",num); 2.3.2 return return return ; return; return return ( ) add-func.c add() 11

2.3 2 double add(double x,double y) return x+y; return return; 0 100 double C2F(double cel); int c; printf(" \n"); printf("----------\n"); for(c=0;c<=100;c++) printf("%4d %5.2f\n", c, C2F((double)c) ); double C2F(double cel) return cel * 9.0 / 5.0 + 32.0; 2.3.3 main() return main() main() ( ) int 0 main() $? hello.c printf("hello, world\n"); 12

2.3 2 $ cc -Wall -o hello hello.c $./hello Hello, world $ echo $? 0 main() $? /* return_test.c */ int ret; printf("main "); scanf("%d", &ret); return ret; $ cc -Wall -o return_test return_test.c $./return_test main 3 $ echo $? 3 $./return_test main -1 $ echo $? 255 $? unsigned char 2.3.4 ( ) int void 13

2.4 () 2 void add-func.c add() double ( ) add double x,double y z=x+y;return z; add(double x,double y) 2.4 () ( ) (function prototype; ) () main() include printf() <stdio.h> printf() ( ); ; add-func.c /****************************************** * add-func1.c * * * ******************************************/ double add(double x,double y) double z; z=x+y; return z; double a; a=add(1.2,2.3); /* */ printf("a=%f\n",a); 14

2.5 2 2.5 3 maximum() int maximum(int a, int b, int c); int x, y, z, max; printf(" 3 :"); scanf("%d%d%d", &x, &y, &z); max = maximum(x, y, z); printf(" %d \n", max); int maximum(int a, int b, int c) int ret = a; if ( b > ret ) ret = b; if ( c > ret ) ret = c; return ret; $ cc maximum.c -o maximum -Wall $./maximum 3 :30 25 11 30 $./maximum 3 :15 8 33 33 2.6 : call by value call by reference ( ); 15

2.6 : call by value call by reference 2 (); getchar() getchar(); /****************************************** * add-func2.c * * * ******************************************/ double add(double x,double y); double a=1.2,b=2.3,c; c=add(a,b); /* */ printf("c=%f\n",c); double add(double x,double y) double z; z=x+y; return z; add() a b a b (call by value) (call by reference) 4 (call by value) 4 scanf() 2 & (call by reference) 16

2.6 : call by value call by reference 2 /* call by value.c */ void func(int x, int y); double x=1, y=2; printf("main x=%d, y=%d\n", x, y); func(x, y); printf("main x=%d, y=%d\n", x, y); void func(double x,double y) x=3; y=0; printf("func x=%d, y=%d\n", x, y); return ; 2.1 double 2 add() 2 add-func3.c 2.2 tax() main() tax() 2.3 f(x) = 1 1 + x 2 double f(double x) 0 1 0.1 f(0.0), f(0.1),..., f(1.0) 2.4 absolute(x) = x x 0 x x < 0 double absolute(double x) double absolute() void (); 17

2.6 : call by value call by reference 2 void void void void printedge(int size); /* */ int l,i,j; printf("input size :"); scanf("%d", &l); if ( l < 3 l > 80) printf("inpossible\n"); printedge(l); /* */ for(i=2;i<l;i++) printf("*"); for(j=2;j<l;j++) printf(" "); printf("*\n"); printedge(l); /* */ void printedge(int size) int i; for(i=1;i<=size;i++) printf("*"); printf("\n"); return; 18

2.7 2 2.7 C auto, register, extern, static 4 () 4 auto register extern static auto double x, y auto double x, y; auto register CPU CPU CPU register static extern 2 extern static static int counter(void); int i; for ( i=0; i<10; i++) counter(); 19

2.8 2 int counter(void) static int counter = 0; printf(" counter %d \n", ++counter); $ cc -Wall -o static_variable static_variable.c $./static_variable counter 1 counter 2 counter 3 counter 4 counter 5 counter 6 counter 7 counter 8 counter 9 counter 10 static counter 1 2.8 ( localize ) () void func(void); int x = 5; /* main */ printf("main x %d\n", x); func(); 20

2.9 2 func(); printf("main x %d\n", x); void func(void) int x = 20; /* func */ printf("\nfunc x %d\n", x); x++; printf("\nfunc x %d\n", x); $ cc -Wall -o scope scope.c $./scope main x 5 func x 20 func x 21 func x 20 func x 21 main x 5 x 2.9 <math.h> double double #include <math.h> $ cc -lm -o -lm libm.a, libm.dylib (sqrt(-1.2) ) 21

2.9 2 7: <math.h> double acos(double x); x () cos 1 x double asin(double x); x () sin 1 x double atan(double x); x () tan 1 x double cos(double x); x cos x double sin(double x); x sin x double tan(double x); x tan x double exp(double x); x e x double log(double x); x log e x double log10(double x); x log 10 x double pow(double x,double y); x y x y double sqrt(double x); x x double fabs(double x); x x double floor(double x); x [x] 2.1 /* diagonal.c */ #include <math.h> double x,y,d; printf("="); scanf("%lf",&x); printf("="); scanf("%lf",&y); d=sqrt(x*x+y*y); printf(" =%f\n",d); $ cc -lm diagonal.c -o diagonal $./diagonal =3 =4 =5.000000 2.5 2 ax 2 + bx + c = 0, (a 0) (D = b 2 4ac ) 2.2 /* sin_curve.c */ 22

2.10 2 #include <math.h> double x, y, Pi=3.14159; int i, y2; for(x=0.0; x<=2.0 * Pi; x+=0.1) y=sin(x); y2 = (int)((y + 1.0)/2.0 * 80.0 ); for(i=0;i<y2;i++) printf(" "); printf("*\n"); 2.6 2 (x 1, y 1 ), (x 2, y 2 ) distance.c double 2.10 5 int Fact( int n ); int n; n = 5; printf("factorial of %d is %d.\n", n, Fact(n)); int Fact( int n ) if ( n == 1 ) return 1; else return n * Fact( n - 1 ); Fact() Fact() Fact() 1 23

2.10 2 n 1 1 (recursive) n main() n Fact() n 2.7 for() 64 64 n A B n - 1 A C n A B C n - 1 C B A B C 1: #include<stdio.h> int hanoi(int disks, char a, char b, char c); int disks; char a = A, b= C, c= B ; printf("?:"); scanf("%d", &disks); while (disks > 65 disks < 0 ) printf("1 64 :"); scanf("%d", &disks); 24

2.10 2 hanoi(disks, a, b, c); int hanoi(int disks, char a, char b, char c) if (disks == 1) printf(" 1 %c %c \n", a, b); else hanoi(disks - 1, a, c, b); printf(" %d %c %c \n", disks, a, b); hanoi(disks - 1, c, b, a); 25