開発環境
- 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(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅤ部(言語のその他の機能)の23章(モジュールプログラミング)、23.11(プログラミング実習)、実習 23-1.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 23-1.
コード(BBEdit, Emacs)
print_pages.h
#include <fstream>
class print_fp {
std::ofstream out_file;
public:
print_fp () {}
void open_file(char *name);
void define_header(char *heading);
void print_line(char *line);
void page();
void close_file();
};
print_pages.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "print_pages.h"
void print_fp::open_file(char *name)
{
out_file.open(name);
if (out_file.fail()) {
std::cerr << "Error: Could not open " << name << std::endl;
exit (8);
}
}
void print_fp::define_header(char *heading)
{
out_file << heading << "--------------------" << std::endl;
}
void print_fp::print_line(char *line)
{
out_file << line << std::endl;
}
void print_fp::page()
{
out_file << '\f';
}
void print_fp::close_file()
{
out_file.close();
}
test_print_pages.cpp
#include "print_pages.h"
int main(int argc, char *argv[])
{
print_fp p;
char heading[] = "見出し";
char line1[] = "1行目";
char line2[] = "2行目";
char f[] = "改ページ";
p.open_file(argv[1]);
p.define_header(heading);
p.print_line(line1);
p.print_line(line2);
p.print_line(f);
p.page();
p.print_line(line1);
p.close_file();
return (0);
}
Makefile
CC=g++
CFLAGS=-g -Wall
SRC=print_pages.cpp test_print_pages.cpp
OBJ=print_pages.o test_print_pages.o
all: test_print_pages
test_print_pages: $(OBJ)
${CC} ${CFLAGS} -o test_print_pages $(OBJ)
test_print_pages.o: print_pages.h test_print_pages.cpp
${CC} $(CFLAGS) -c test_print_pages.cpp
print_pages.o: print_pages.h print_pages.cpp
${CC} $(CFLAGS) -c print_pages.cpp
clean:
rm test_print_pages test_print_pages.o print_pages.o
入出力結果(Terminal)
$ make g++ -g -Wall -c print_pages.cpp g++ -g -Wall -c test_print_pages.cpp g++ -g -Wall -o test_print_pages print_pages.o test_print_pages.o $ ./test_print_pages pages.out $ cat pages.out 見出し-------------------- 1行目 2行目 改ページ 1行目 $
0 コメント:
コメントを投稿