プログラミング初級 第 13 回 2017 年 7 月 10 日 標準ライブラリ関数 1
標準ライブラリ関数とは 関数には (1) 自分で作る関数 (2) はじめから用意されている関数特に C 言語用用意されているもの : 標準ライブラリ関数 文字列の代入文字列の長さを求める文字列の比較文字列の連結 strcpy strlen strcmp strcat 2
文字列の操作 - 具体例を通して (141 ページ ) #include <stdio.h> #include <string.h> int main(void) { char s1[80],s2[80]; int nn; strcpy(s1, abcde ); strcpy(s2, FGHIJ ); printf( s1=%s s2=%s n,s1,s2); nn=strlen(s1); printf( len=%d n, nn); if(strcmp(s1, s2) > 0) printf( 左辺が大 n ); if(strcmp(s1, s2) < 0) printf( 右辺が大 n ); } if(strcmp(s1, s2) == 0) printf( 等しい n ); if(strcmp(s1, s2)!= 0) printf( 等しくない n ); strcat(s1, s2); printf( s1+s2=%s n,s1); return 0; 3
文字列の操作 - 具体例を通して (141 ページ ) #include <stdio.h> #include <string.h> int main(void) { char s1[80],s2[80]; int nn; strcpy(s1, abcde ); strcpy(s2, FGHIJ ); printf( s1=%s s2=%s n,s1,s2); 文字列処理をするときはつける ( と覚える ) < 文字列の代入 > if(strcmp(s1, s2) == 0) printf( 等しい n ); if(strcmp(s1, s2)!= 0) printf( 等しくない n ); strcpy( 代入される文字列変数 代入する文字列 ) nn=strlen(s1); printf( len=%d n, nn); strcat(s1, s2); printf( s1+s2=%s n,s1); if(strcmp(s1, s1 s2) [0] > 0) [1] [2] [3] [4] return [5] 0; [6] [7] [8] [9] printf( 左辺が大 n ); } if(strcmp(s1, s2) a< 0) b c d e 0 printf( 右辺が大 n ); s2 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] F G H I J 0 4
文字列の操作 - 具体例を通して (141 ページ ) #include <stdio.h> #include <string.h> int main(void) { char s1[80],s2[80]; int nn; strcpy(s1, abcde ); strcpy(s2, FGHIJ ); printf( s1=%s s2=%s n,s1,s2); nn=strlen(s1); printf( len=%d n, nn); if(strcmp(s1, s2) == 0) printf( 等しい n ); if(strcmp(s1, s2)!= 0) printf( 等しくない n ); < 文字列の長さを求める > strcat(s1, s2); printf( s1+s2=%s n,s1); n=strlen( 文字列 もしくは文字列変数 ); if(strcmp(s1, s1 s2) [0] > 0) [1] [2] [3] [4] return [5] 0; [6] [7] [8] [9] printf( 左辺が大 n ); } if(strcmp(s1, s2) a< 0) b c d e 0 printf( 右辺が大 n ); 5
文字列の操作 - 具体例を通して (141 ページ ) #include <stdio.h> #include <string.h> < 文字列の比較 > strcmp( 文字列 1 文字列 2); int main(void) { char s1[80],s2[80]; int nn; if(strcmp(s1,s2)==0) if(strcmp(s1,s2)!=0) if(strcmp(s1, abcde )==0) if(strcmp(s1, FGHIJ )!=0) strcpy(s1, abcde ); strcpy(s2, FGHIJ ); printf( s1=%s s2=%s n,s1,s2); nn=strlen(s1); printf( len=%d n, nn); if(strcmp(s1, s2) > 0) printf( 左辺が大 n ); if(strcmp(s1, s2) < 0) printf( 右辺が大 n ); } if(strcmp(s1, s2) == 0) printf( 等しい n ); if(strcmp(s1, s2)!= 0) printf( 等しくない n ); strcat(s1, s2); printf( s1+s2=%s n,s1); return 0; 6
#include <stdio.h> #include <string.h> 文字列の操作 - 具体例を通して (141 ページ ) s1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] a b c d e 0 F G H I J int main(void) { char s1[80],s2[80]; int nn; s2 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] F G H I J 0 strcpy(s1, abcde ); strcpy(s2, FGHIJ ); printf( s1=%s s2=%s n,s1,s2); < 文字列の連結 > nn=strlen(s1); printf( len=%d n, nn); strcat( 文字列変数 連結する文字列 ); if(strcmp(s1, s2) == 0) printf( 等しい n ); if(strcmp(s1, s2)!= 0) printf( 等しくない n ); strcat(s1, s2); printf( s1+s2=%s n,s1); strcpy(s1, XYZ ); strcat(a, ABCDE ); if(strcmp(s1, s2) > 0) printf( 左辺が大 n ); if(strcmp(s1, s2) < 0) printf( 右辺が大 n ); } return 0; 7
文字列を扱う 数学関数を使う 8
数学関数を使う - 具体例を通して (146 ページ ) #include <stdio.h> #include <math.h> int main(void) { printf( sin 30=%f n, sin(30.0*3.14159/180.0)); printf( cos 30=%f n, cos(30.0*3.14159/180.0)); printf( tan 45=%f n, tan(45.0*3.14159/180.0)); printf( 2 の 3 乗 =%f n,pow(2.0,3.0); printf( 1000 の常用対数 =%f n,log10(1000.0)); printf( 10 の平方根 =%f n, sqrt(10.0)); } return 0; 9
数学関数を使う - 具体例を通して (146 ページ ) #include <stdio.h> #include <math.h> 数学関数を用いるときはつける ( と覚える ) int main(void) { printf( sin 30=%f n, sin(30.0*3.14159/180.0)); printf( cos 30=%f n, cos(30.0*3.14159/180.0)); printf( tan 45=%f n, tan(45.0*3.14159/180.0)); printf( 2 の 3 乗 =%f n,pow(2.0,3.0); printf( 1000 の常用対数 =%f n,log10(1000.0)); printf( 10 の平方根 =%f n, sqrt(10.0)); } return 0; サイン コサイン タンジェント sin(30.0 * 3.14159 / 180.0) cos(30.0 * 3.14159 / 180.0) tan(45.0 * 3.14159 / 180.0) 10
数学関数を使う - 縁がないと思ったら大間違い (x,y) 11
数学関数を使う - 縁がないと思ったら大間違い (x,y) m 12
数学関数を使う - 縁がないと思ったら大間違い m sinθ (x,y) m m θ m cosθ 13
数学関数を使う - 縁がないと思ったら大間違い (x,y) (x,y ) m sinθ m θ m cosθ x = x + m cosθ y = y + m sinθ 14
代表的な数学関数 サイン sin x sin(x); コサイン cos x cos(x); タンジェント tan x tan(x); 累乗値 x y pow(x,y); 常用対数 log 10 x log10(x); 平方根 x sqrt(x); 15
数学関数を使う - 具体例を通して (146 ページ ) #include <stdio.h> #include <math.h> int main(void) { printf( sin 30=%f n, sin(30.0*3.14159/180.0)); printf( cos 30=%f n, cos(30.0*3.14159/180.0)); printf( tan 45=%f n, tan(45.0*3.14159/180.0)); printf( 2 の 3 乗 =%f n,pow(2.0,3.0); printf( 1000 の常用対数 =%f n,log10(1000.0)); printf( 10 の平方根 =%f n, sqrt(10.0)); } return 0; 16
数学関数を用いるときの注意 1. 三角関数を使うとき 角度はラジアンで表現する! ラジアン : 角度を表す単位 0 度 =0 ラジアン 180 度 =3.14159 ラジアン ( つまり円周率 π ラジアン ) 360 度 =6.28318 ラジアン ( つまり 2π ラジアン ) では任意の角度 X 度は? X/180.0*3.14159 ラジアン 2. 数学関数は引数も戻り値も double 型! double 型戻り値 = func(double 型の引数 ); double 型戻り値 = func(double 型の引数,double 型の引数 ); 17
文字列を扱う 数学関数を使う その他 18
その他 入出力用の関数 putchar(), getchar(), puts(), gets(), printf(), scanf() ファイル入出力用の関数 fopen(), fclose(), fputc(), fgetc(), fputs(), fgets(), fprintf(), fscanf() ( 詳細については次章で扱う ) 19
その他 文字処理用関数 isalnum(c), isalpha(c), isdigit(c), islower(c), isupper(c), isxdigit(c), tolower(c), toupper(c) #include <stdio.h> #include <string.h> #include <ctype.h> int main(void) { int i,n; char s1[80]; < プログラム例 > 文字列 s1 内の文字を全て大文字に変換する } strcpy(s1,"abcdefghijklmn"); n=strlen(s1); for(i=0;i<n;i++) s1[i]=toupper(s1[i]); printf("%s n",s1); return 0; 20