開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅡ部(シンプルなプログラミング)の9章(変数のスコープと関数)、9.8(プログラミング実習)、実習 9-6.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 9-6.
コード(BBEdit, Emacs)
sample.cpp
#include <string>
#include <iostream>
/* hy2un -- 文字列中のハイフン(-)をアンダースコア(_)に変換した文字列を返す関数
*
* 引数
* s -- 文字列(std::string)
*
* 戻り値
* 文字列(std::string)
*/
std::string hy2un(std::string s)
{
int i;
for (i = 0; i < s.length(); ++i) {
if (s.at(i) == '-') {
s[i] = '_';
}
}
return s;
}
int main()
{
std::string s1 = "-cpp-";
std::string s2 = "cpp-cpp";
std::string s3;
std::string s4;
s3 = hy2un(s1);
s4 = hy2un(s2);
std::cout << "置換前: " << s1 << '\n';
std::cout << "置換後: " << s3 << '\n';
std::cout << "置換前: " << s2 << '\n';
std::cout << "置換後: " << s4 << '\n';
return (0);
}
Makefile
# # FSFのg++コンパイラ用のMakefile # CC=g++ CFLAGS=-g -Wall all: sample sample: sample.cpp $(CC) $(CFLAGS) -o sample sample.cpp clean: rm sample
入出力結果(Terminal)
$ make && ./sample g++ -g -Wall -o sample sample.cpp 置換前: -cpp- 置換後: _cpp_ 置換前: cpp-cpp 置換後: cpp_cpp $
0 コメント:
コメントを投稿