Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
cd cd ./opro
ls lsmkdir mkdir ./opro
cp cp ./file ../.mv mv ./file ./
file2cat cat ./file
今日の前半は,Linux(Unix計OS) のコマンドを使います
キーボードからの入力・画面への出力を標準入力 / 標準出力 という
cin
cout
cerr
// 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
出⼒力結果
// 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
出⼒力ファイル
// 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
出⼒力結果
// 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
パイプ
//ファイルから読み込んで改行付きで書き込む #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; }
入力ファイルと出力ファイル
エラー処理
読み込みと同時に書き込み
#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型配列)
#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 型の配列を使う
#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()
#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 番目のファイルです
出⼒力結果
#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 ファイル作成
出⼒力結果
‘ ’‘\t’ インデントに使うタブ‘\n’ 普通の改行
#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
出⼒力結果
#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 を挿入
#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
出⼒力結果
#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
出⼒力結果
改行文字だったらカウントアップ
#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
出⼒力結果
T i m e a dn a w o r d
#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
#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 型文字)
#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関数で判定
#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
出⼒力結果
#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.
出⼒力結果
#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.
出⼒力結果
小文字だったら大文字に変換する
#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.
出⼒力結果
#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(入力元,代入先);
#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
出⼒力結果
#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()
#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
出⼒力結果
#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(入力元,代入先, 区切り文字)
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
出⼒力結果
#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 存在しません
出⼒力結果