開発環境
- 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-3.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 23-3.
コード(BBEdit, Emacs)
symbol_table.h
#include <string>
class symbols {
private:
public:
class symbol {
public:
std::string data;
private:
symbol *next_ptr;
friend class symbols;
};
symbol *first_ptr;
symbols() {first_ptr = NULL;}
void enter(const std::string& s);
int lookup(const std::string& name);
void remove(const std::string& name);
void p();
};
symbol_table.cpp
#include <iostream>
#include "symbol_table.h"
void symbols::enter(const std::string& s)
{
symbol *new_ptr;
new_ptr = new symbol;
new_ptr->data = s;
new_ptr->next_ptr = first_ptr;
first_ptr = new_ptr;
}
int symbols::lookup(const std::string& name)
{
symbol *current_ptr;
current_ptr = first_ptr;
while (current_ptr != NULL && current_ptr->data != name)
current_ptr = current_ptr->next_ptr;
return (current_ptr != NULL);
}
void symbols::remove(const std::string& name)
{
symbol *current_ptr;
symbol *previous_ptr;
current_ptr = first_ptr;
previous_ptr = first_ptr;
while (current_ptr != NULL && current_ptr->data != name) {
previous_ptr = current_ptr;
current_ptr = current_ptr->next_ptr;
}
if (current_ptr != NULL) {
if (current_ptr == first_ptr)
first_ptr = current_ptr->next_ptr;
else
previous_ptr->next_ptr = current_ptr->next_ptr;
delete current_ptr;
current_ptr = NULL;
}
}
void symbols::p()
{
symbol *current_ptr;
std::cout << "シンボルテーブル" << std::endl;
for (current_ptr = first_ptr; current_ptr != NULL;
current_ptr = current_ptr->next_ptr)
std::cout << current_ptr->data;
std::cout << std::endl;
}
test_symbol_table.cpp
#include "symbol_table.h"
int main(int argc, char *argv[])
{
symbols s;
s.p();
s.enter("a");
s.p();
s.enter("a");
s.p();
s.enter("b");
s.p();
s.enter("c");
s.p();
s.enter("d");
s.p();
s.enter("e");
s.p();
s.remove("a");
s.p();
s.remove("b");
s.p();
s.remove("e");
s.p();
s.remove("xyz");
s.p();
return (0);
}
Makefile
CC=g++
CFLAGS=-g -Wall
SRC=symbol_table.cpp test_symbol_table.cpp
OBJ=symbol_table.o test_symbol_table.o
all: test_symbol_table
test_symbol_table: $(OBJ)
${CC} ${CFLAGS} -o test_symbol_table $(OBJ)
test_symbol_table.o: symbol_table.h test_symbol_table.cpp
${CC} $(CFLAGS) -c test_symbol_table.cpp
symbol_table.o: symbol_table.h symbol_table.cpp
${CC} $(CFLAGS) -c symbol_table.cpp
clean:
rm test_symbol_table test_symbol_table.o symbol_table.o
入出力結果(Terminal)
$ make && ./test_symbol_table g++ -g -Wall -c symbol_table.cpp g++ -g -Wall -o test_symbol_table symbol_table.o test_symbol_table.o シンボルテーブル シンボルテーブル a シンボルテーブル aa シンボルテーブル baa シンボルテーブル cbaa シンボルテーブル dcbaa シンボルテーブル edcbaa シンボルテーブル edcba シンボルテーブル edca シンボルテーブル dca シンボルテーブル dca $
0 コメント:
コメントを投稿