2014年2月12日水曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅡ部(シンプルなプログラミング)の9章(変数のスコープと関数)、9.8(プログラミング実習)、実習 9-1.を解いてみる。

その他参考書籍

実習 9-1.

コード(BBEdit, Emacs)

sample.cpp

#include <iostream>

/*  word_count -- 文字列中の「ワード」の数を数える(ここでワードとは、愛児のこと
 * 
 *  string -- 「ワード」の数を数える文字列
 * 
 *  戻り値
 *    「ワード」の数
 */
int word_count(char string[])
{
    int count = 0;
    int i = 0;
    while (string[i] != '\0') {
        if (string[i] >= 'A' && string[i] <= 'z') {
            ++count;
        }
        ++i;
    }
    return count;
}

int main()
{
    char s1[] = "";
    char s2[] = "c";
    char s3[] = "cpp";
    char s4[] = " c cpp ";
    std::cout << "ワード数\n" <<
            s1 << ": " << word_count(s1) << '\n' <<
            s2 << ": " << word_count(s2) << '\n' <<
            s3 << ": " << word_count(s3) << '\n' <<
            s4 << ": " << word_count(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)

a$ make && ./sample
g++ -g -Wall -o sample sample.cpp
ワード数
: 0
c: 1
cpp: 3
 c cpp : 4
$

0 コメント:

コメントを投稿