2014年2月18日火曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅡ部(シンプルなプログラミング)の10章(C++ プリプロセッサ)、10.1(#define文)、設問 10-1を解いてみる。

その他参考書籍

設問 10-1

コード(BBEdit, Emacs)

sample.cpp

#include <iostream>

#define FIRST_PART 7
#define LAST_PART 5
/* 括弧を付け加える */
#define ALL_PARTS (FIRST_PART + LAST_PART)

int main() 
{
    /* 括弧を付け加える前の、問題のコードのままだと、ALL_PARTS * ALL_PARTSは
     * 7 + 5 * 7 + 5
     * と展開されて、結果は47となる
     * 括弧を書き加えて修正したので、ALL_PARTS * ALL_PARTSは
     * (7 + 5) * (7 + 5)
     * と展開されて、評価結果は114となる
     */
    std::cout << "The square of all the parts is " << ALL_PARTS * ALL_PARTS <<
              '\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
The square of all the parts is 144
$

0 コメント:

コメントを投稿