• 検索結果がありません。

PowerPoint プレゼンテーション

N/A
N/A
Protected

Academic year: 2021

シェア "PowerPoint プレゼンテーション"

Copied!
20
0
0

読み込み中.... (全文を見る)

全文

(1)

プログラミング初級

第13回 2017年7月10日

標準ライブラリ関数

(2)

標準ライブラリ関数とは

関数には

(1)自分で作る関数

(2)はじめから用意されている関数

特にC言語用用意されているもの:標準ライブラリ関数

文字列の代入

strcpy

文字列の長さを求める

strlen

文字列の比較

strcmp

文字列の連結

strcat

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

(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”); 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; }

文字列処理をするときはつ

ける(と覚える)

<文字列の代入>

strcpy(代入される文字列変数、代入する文字列)

a

b

c

d

e

¥0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s1

s2

(5)

文字列の操作

-具体例を通して(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; }

<文字列の長さを求める>

n=strlen(“文字列”もしくは文字列変数);

a

b

c

d

e

¥0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s1

(6)

文字列の操作

-具体例を通して(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; }

<文字列の比較>

strcmp(文字列1、文字列2);

if(strcmp(s1,s2)==0)

if(strcmp(s1,s2)!=0)

if(strcmp(s1,”abcde”)==0)

if(strcmp(s1,”FGHIJ”)!=0)

(7)

文字列の操作

-具体例を通して(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; }

<文字列の連結>

strcat(文字列変数、連結する文字列);

strcpy(s1,”XYZ”);

strcat(a,”ABCDE”);

a

b

c

d

e

¥0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s1

F

G

H

I

J

¥0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s2

a

b

c

d

e

F

G

H

I

J

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s1

F

G

H

I

J

¥0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

s2

(8)

• 文字列を扱う

• 数学関数を使う

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

(10)

数学関数を使う

-具体例を通して(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)

(11)

数学関数を使う

-縁がないと思ったら大間違い

(12)

数学関数を使う

-縁がないと思ったら大間違い

(x,y)

(13)

数学関数を使う

-縁がないと思ったら大間違い

(x,y)

m

θ

m・cosθ

m・sinθ

m

(14)

数学関数を使う

-縁がないと思ったら大間違い

(x,y)

(x’,y’)

m

θ

m・cosθ

m・sinθ

x’ = x + m・cosθ

(15)

代表的な数学関数

サイン

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

(16)

数学関数を使う

-具体例を通して(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; }

(17)

数学関数を用いるときの注意

1.三角関数を使うとき、角度はラジアンで表現する!

ラジアン:角度を表す単位

0度=0ラジアン

180度=3.14159…ラジアン(つまり円周率πラジアン)

360度=6.28318…ラジアン(つまり2πラジアン)

では

任意の角度X度は? X/180.0*3.14159ラジアン

2.数学関数は引数も戻り値もdouble型!

double型戻り値 = func(double型の引数);

(18)

• 文字列を扱う

• 数学関数を使う

• その他

(19)

その他

入出力用の関数

putchar(), getchar(), puts(), gets(),

printf(), scanf()

ファイル入出力用の関数

fopen(), fclose(), fputc(), fgetc(),

fputs(), fgets(), fprintf(), fscanf()

(詳細については次章で扱う)

(20)

その他

文字処理用関数

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]; strcpy(s1,"ABCdefGHIjklmn"); n=strlen(s1); for(i=0;i<n;i++) s1[i]=toupper(s1[i]);

<プログラム例>

文字列s1内の文字を

全て大文字に変換する

参照

関連したドキュメント

CIとDIは共通の指標を採用しており、採用系列数は先行指数 11、一致指数 10、遅行指数9 の 30 系列である(2017

前章 / 節からの流れで、計算可能な関数のもつ性質を抽象的に捉えることから始めよう。話を 単純にするために、以下では次のような型のプログラム を考える。 は部分関数 (

チューリング機械の原論文 [14]

東京都は他の道府県とは値が離れているように見える。相関係数はこう

これはつまり十進法ではなく、一進法を用いて自然数を表記するということである。とは いえ数が大きくなると見にくくなるので、.. 0, 1,

(注)本報告書に掲載している数値は端数を四捨五入しているため、表中の数値の合計が表に示されている合計

例えば、総トン数 499 トン・積載トン数 1600 トン主機関 1471kW(2000PS)の内航貨 物船では、燃料油の加熱に使用される電力は

「特殊用塩特定販売業者」となった者は、税関長に対し、塩の種類別の受入数量、販売数