開発環境
- OS X Mavericks - Apple, ときどき
Windows 8.1 + Cygwin64, MinGW (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅤ部(言語のその他の機能)の22章(例外)、22.3(プログラミング実習)、実習 22-4.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 22-4.
コード(BBEdit, Emacs)
sample447_4.cpp
#include <iostream>
class empty_err {
public:
const std::string what;
empty_err(const std::string& i_what) : what(i_what) {}
};
class count {
public:
int vowel;
int consonant;
count () {
vowel = 0;
consonant = 0;
}
void p() {
std::cout << "vowel: " << vowel << ", consonant: " << consonant <<
std::endl;
}
};
count count_letter(const std::string s)
{
if (s == "")
throw empty_err("文字列が指定されていません");
count c;
int i;
for (i = 0; s[i] != '\0'; ++i)
switch (s[i]) {
case 'a':
case 'e':
case 'i':
case 'u':
case 'o':
++c.vowel;
break;
default:
++c.consonant;
break;
}
return c;
}
int main(int argc, char *argv[])
{
count c;
while (true) {
std::string s;
std::cout << "文字列を入力: ";
std::cin >> s;
try {
c = count_letter(s);
c.p();
}
catch (empty_err& err) {
std::cerr << "Error: " << err.what << std::endl;
exit (8);
}
}
return (0);
}
Makefile
CC=g++
CFLAGS=-g -Wall
all: sample447_4
sample447_4: sample447_4.cpp
${CC} ${CFLAGS} -o sample447_4 sample447_4.cpp
clean:
rm sample447_4
入出力結果(Terminal)
$ make && ./sample447_4 g++ -g -Wall -o sample447_4 sample447_4.cpp 文字列を入力: scheme vowel: 2, consonant: 4 文字列を入力: python vowel: 1, consonant: 5 文字列を入力: c vowel: 0, consonant: 1 文字列を入力: a vowel: 1, consonant: 0 文字列を入力: Error: 文字列が指定されていません $
0 コメント:
コメントを投稿