52

OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 2: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 3: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 4: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 5: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

cd cd ./opro

ls lsmkdir mkdir ./opro

cp cp ./file ../.mv mv ./file ./

file2cat cat ./file

今日の前半は,Linux(Unix計OS) のコマンドを使います

Page 6: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

キーボードからの入力・画面への出力を標準入力 / 標準出力 という

cin

cout

cerr

Page 7: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

// cout と cerr int main(){

cout << “COUT” << endl; for(int i = 0; i < 5; i++){ cout << i << “ ”;

} cout << endl;

cerr << “CERR” << endl; for(int i = 0; i < 5; i++){ cerr << i << “ ”;

} cerr << endl; return 0;

}

% ./cerr COUT 0 1 2 3 4

CERR 0 1 2 3 4

出⼒力結果

Page 8: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

// cout と cerr int main(){

cout << “COUT” << endl; for(int i = 0; i < 5; i++){ cout << i << “ ”;

} cout << endl;

cerr << “CERR” << endl; for(int i = 0; i < 5; i++){ cerr << i << “ ”;

} cerr << endl; return 0;

}

% ./cerr > error.txt CERR 0 1 2 3 4

出⼒力結果

% cat error.txt COUT 0 1 2 3 4

出⼒力ファイル

Page 9: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 10: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

// 1 から 10 まで出力する proA int main(){

for(int i = 1; i <= 10; i++){ cout << i << endl;

} return 0;

}

// 入力の合計値を表示する proB int main(){

int input, sum = 0; while(cin >> input){ sum += input;

}

cout << sum << endl; return 0;

}% ./proA 1 2 3 -中略- 9 10

出⼒力結果

% ./proB 1 2 3 4 5 (ctrl+D) 15

出⼒力結果

Page 11: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

// 1 から 10 まで出力する proA int main(){

for(int i = 1; i <= 10; i++){ cout << i << endl;

} return 0;

}

// 入力の合計値を表示する proB int main(){

int input, sum = 0; while(cin >> input){ sum += input;

}

cout << sum << endl; return 0;

}

% ./proA | ./proB 55

パイプ

Page 12: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 13: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 14: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 15: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

//ファイルから読み込んで改行付きで書き込む #include<iostream> #include<fstream>

using namespace std;

int main(){

ifstream ff("in.dat"); ofstream fo("out.dat");

if(!ff || !fo){ cerr << "Can not Open" << endl; return 1; }

string st; while(ff >> st){ fo << st << endl; } return 0; }

入力ファイルと出力ファイル

エラー処理

読み込みと同時に書き込み

Page 16: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){ string s =“oput.dat”; ofstream fout(s);

if(!fout){ cerr << “cannot open” << endl; retrun 1; } for(int i=1; i <=5; i++) fout << i << endl; return 0; }

ofstream オブジェクト名(string 型変数)

ifstream オブジェクト名(string 型変数)

ofstream オブジェクト名(char型配列)

ifstream オブジェクト名(char型配列)

Page 17: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<fstream>

using namespace std;

int main(){ char c[] =“oput.dat”; ofstream fout(c);

if(!fout){ cerr << “cannot open” << endl; return 1; } for(int i=1; i <=5; i++) fout << i << endl; return 0; }

char 型の配列を使う

Page 18: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){ string s = “oput.dat”; ofstream fout(s.c_str());

if(!fout){ cerr << “cannot open” << endl; return 1; } for(int i=1; i <=5; i++) fout << i << endl; return 0; }

string型変数 . c_str()

Page 19: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){ string s[3] = {“o1.dat”, “o2.dat”, “o3.dat”};

for(int i = 0; i < 3; i++){ ofstream fout(s[i].c_str());

if(!fout){ cerr << “cannot open” << endl; return 1;

}

fout << i + 1 <<“ 番目のファイルです”<< endl; }

return 0; }

%./str_ex1 %cat o1.dat 1 番目のファイルです %cat o2.dat 2 番目のファイルです %cat o3.dat 3 番目のファイルです

出⼒力結果

Page 20: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){ string s;

cin >> s; ofstream fout(s.c_str());

if(!fout){ cerr << “Can not open” << endl; return 1;

}

fout << “ファイル作成”<< endl; return 0; }

%./str_ex2 file2.dat

%cat file2.dat ファイル作成

出⼒力結果

Page 21: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 22: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 23: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 24: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

‘ ’‘\t’ インデントに使うタブ‘\n’ 普通の改行

Page 25: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ char c;

while (cin >> noskipws >> c){

cout << ch; }

return 0; }

%./ws1 This is a pen This is a pen

出⼒力結果

Page 26: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ char c;

while (cin >> noskipws >> c){

cout << ch; }

return 0; }

%./ws1 This is a pen This is a pen

出⼒力結果

iomanip をインクルード

noskipws を挿入

Page 27: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ int n = 0;

char c; while (cin >> noskipws >> c){

n++; }

cout << n << endl; return 0; }

%./ws2 This is a pen[Enter] [ctrl+D] 14

%./ws2 This is a pen[Enter] [Enter] [Enter] [ctrl+D] 16

出⼒力結果

Page 28: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ int n = 0;

char c; while (cin >> noskipws >> c){

if(c == ‘\n’){ n++;

} }

cout << n << endl; return 0; }

%./ws3 This is a pen[Enter] I Like It [Enter] [ctrl+D] 2

出⼒力結果

改行文字だったらカウントアップ

Page 29: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ int n = 0; bool flag = false; char c; while (cin >> noskipws >> c){

if(c == ‘ ’ || c == ‘\n’ || c == ‘\t’){ flag = false;

}else if(!flag){ flag = true; n++;

} } cout << n << endl;

return 0; }

%./ws4 Time and a word[Enter] [ctrl+D] 4

出⼒力結果

Page 30: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

T i m e a dn a w o r d

Page 31: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ int n = 0; bool flag = false; char c; while (cin >> noskipws >> c){

if(c == ‘ ’ || c == ‘\n’ || c == ‘\t’){ flag = false;

}else if(!flag){ flag = true; n++;

} } cout << n << endl;

return 0; }

%./ws4 Time and a word[Enter] [ctrl+D] 4

出⼒力結果

空白文字だったら flag を false にしておく

直前が空白文字(flag == false) だったら単語数 + 1

Page 32: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip>

using namespace std;

int main(){ int n = 0; bool flag = false; char c; while (cin >> noskipws >> c){

if(c == ‘ ’ || c == ‘\n’ || c == ‘\t’){ flag = false;

}else if(!flag){ flag = true; n++;

} } cout << n << endl;

return 0; }

%./ws4 Time and a word[Enter] [ctrl+D] 4

出⼒力結果

長くていちいち書いていられない!

#include<cctype> &

isspace(char 型文字)

Page 33: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip> #include<cctype> using namespace std;

int main(){ int n = 0; bool flag = false; char c; while (cin >> noskipws >> c){

if(isspace(c)){ flag = false;

}else if(!flag){ flag = true; n++;

} } cout << n << endl;

return 0; }

%./ws5 Time and a word[Enter] [ctrl+D] 4

出⼒力結果

isspace(char型 文字) : 文字が空白文字なら : true 文字が空白文字でないなら : false

cctype をinclude

isspace関数で判定

Page 34: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<iomanip> #include<cctype> using namespace std;

int main(){ bool flag = false; char c; while (cin >> noskipws >> c){

if(isspace(c)){ flag = false;

}else if(!flag){ flag = true;

if( c >= ‘a’ && c <= ‘z’){ c = ‘A’ + (c - ‘a’); }

} cout << c << endl;

} return 0; }

%./ws6 this is apple[Enter] [ctrl+D] This Is Apple

出⼒力結果

Page 35: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 36: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 37: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string>

using namespace std;

int main(){

string str; int i = 0; while (cin >> str){

cout << i << “単語目 : ”; cout << str << endl; i++;

} return 0; }

% cat apple.dat This is an apple.

⼊入⼒力テキスト

%./st1 < apple.dat 1 単語目 : This 2 単語目 : is 3 単語目 : an 4 単語目 : apple.

出⼒力結果

Page 38: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string>

using namespace std;

int main(){

string str; while (cin >> str){

for(int i = 0; i < str.size(); i++){ if(str[i] >= ‘a’ && str[i] <= ‘z’){ str[i] = ‘A’ + (str[i] - ‘a’); }

}

cout << str << endl; }

return 0; }

% cat apple.dat This is an apple.

⼊入⼒力テキスト

%./st2 < apple.dat THIS IS AN APPLE.

出⼒力結果

小文字だったら大文字に変換する

Page 39: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string>

using namespace std;

int main(){

string line;  int num = 0; while (getline(cin,line)){

num ++; cout << num << “ : ” << line << endl;

} return 0; }

% cat gl.dat Hello. How are you? I’m fine.

⼊入⼒力テキスト

%./st3 < gl.dat 1 : Hello. 2 : How are you? 3 : I’m fine.

出⼒力結果

Page 40: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string>

using namespace std;

int main(){

string line;  int num = 0; while (getline(cin,line)){

num ++; cout << num << “ : ” << line << endl;

} return 0; }

getline(入力元,代入先);

Page 41: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){

ifstream ff(“in.dat”); int x,y; string line; 

while (ff >> x >> y && getline(ff,line)){ cout << x * y << “ : ” << line << endl;

} return 0; }

% cat in.dat 32 300 White Chocolate 42 430 Orange Cookie 53 380 Lemon Macaroons

⼊入⼒力テキスト

%./st4 9600 : White Chocolate 18060 : Orange Cookie 20140 : Lemon Macaroons

出⼒力結果

Page 42: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){

ifstream ff(“in.dat”); int x,y; string line; 

while (ff >> x >> y && getline(ff,line)){ cout << x * y << “ : ” << line << endl;

} return 0; }

% cat in.dat 32 300 White Chocolate 42 430 Orange Cookie 53 380 Lemon Macaroons

⼊入⼒力テキスト

ファイルで getline()

Page 43: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){

ifstream ff(“ABC.dat”); string line; 

while (getline(ff,line,’A’)){ cout << line << endl;

} return 0; }

% cat ABC.dat ABC DEA BCD EAB CDE ABC

⼊入⼒力テキスト

%./st5 BC DE    

BCD      E B CDE BC

出⼒力結果

Page 44: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string> #include<fstream>

using namespace std;

int main(){

ifstream ff(“ABC.dat”); string line; 

while (getline(ff,line,’A’)){ cout << line << endl;

} return 0; }

% cat ABC.dat ABC DEA BCD EAB CDE ABC

⼊入⼒力テキスト

%./st5 BC DE    

BCD      E B CDE BC

出⼒力結果getline(入力元,代入先, 区切り文字)

Page 45: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

I ah v ae

g do npo esの中身

s[0] s[1] s[2] s[3]

s[15]s[14]s[13]s[12]s[11]s[10]s[9]

s[8]s[7]s[6]s[5]s[4]

s[16]

\0s[17]

#include<iostream> #include<string>

using namespace std;

int main(){ string s = “I have a good pen”; string fd = “pen”;

cout << “I : ” << s.find(“I”) << endl; cout << “pen : ” << s.find(sd) << endl; cout << “have: ” << s.find(“have”) << endl;

return 0; }

%./st6 I : 0 pen : 14 have : 2

出⼒力結果

Page 46: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\

#include<iostream> #include<string>

using namespace std;

int main(){ string s = “I have a good pen”;

int i = s.find(“had”); cout << “had : ” << i << endl;

if( i == string::npos){ cout << “存在しません” << endl;

}

return 0; }

%./st7 had : 429467295 存在しません

出⼒力結果

Page 47: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 48: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 49: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 50: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 51: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\
Page 52: OE UFSN F k= A W S I ]iui.ci.seikei.ac.jp/~takase/wp/wp-content/uploads/2015/...6ÇR¤m¸ × Û f F f 3 6ÇR¤m¸ × Û ] ' f UkW« ' P Û f F f 3æ{ ] ' f UkW« 'Ã8© Ú, ,ÿ. P/\