第7章練習問題1~3

  • 投稿日:
  • by
  • カテゴリ:

 前回、デバッグしたつもりのソースコードを使って練習問題を解くことにする。回答例http://www.stroustrup.com/Programming/Solutions/Ch7/e7-1.cppの方が、練習問題1~3を一度に解いていたので、これにならうことにする。練習問題1~3は、下記の通り。

1.電卓プログラムの変数名でアンダースコアを使用できるようにする。
2.代入演算子=を追加し、letを使って変数を宣言した後に、その値を変更できるようにする。これがなぜ便利なのか、そしてどのような場合に問題の原因になるかについて話し合う。
3.実際に値を変更できない名前付き定数を追加する。

 練習問題1は簡単だ。 Token_stream::get()で、2文字目以降をアルファベットか数字か、となっている部分に'_'を追加すればいい。
 練習問題2は、変数名が初めてletされたときは登録し、それ以外のときは値をセットする。declaration()を少し手を加えた。
 練習問題3は、少し複雑である。問題文にヒントがあって、それを引用する。

定数と変数を区別するメンバーをVariableに追加し、それをset_value関数でチェックする必要がある。単にpiとeを定数として定義するのではなく、ユーザーが定数を定義できるようにしたい場合は、ユーザーがそれを表現するためのconst pi = 3.14;のような表記を追加する必要がある。

 定数ならtrueとなるメンバーを追加し、定数と変数とは別の関数を呼び出すように変えてみた。

#include "..\..\std_lib_facilities.h"

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char CONSTANT = 'c'; /*練習問題3*/

const string prompt = "> ";
const string result = "= ";

class Token {
public:
    char kind;
    double value;
    string name;

    Token() :kind(0) { }
    Token(char ch) :kind{ ch } { }
    Token(char ch, double val) :kind{ ch }, value{ val } { }
    Token(char ch, string n) :kind{ ch }, name{ n } { }
};

class Token_stream {
public:
    Token_stream() :full{ 0 }, buffer{ 0 } { }

    Token get();
    void unget(Token t) { buffer = t; full = true; };
    void ignore(char c);

private:
    bool full;
    Token buffer;
};

 

Token Token_stream::get()
{
    if (full) { full = false; return buffer; }
    char ch;
    cin >> ch;
    switch (ch) {
    case '(':
    case ')':
    case '+':
    case '-':
    case '*':
    case '/':
    case '%':
    case ';':
    case '=':
        return Token(ch);
    case '.':
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    {    cin.unget();
    double val;
    cin >> val;
    return Token(number, val);
    }
    default:
        if (isalpha(ch)) {
            string s;
            s += ch;
            while (cin.get(ch) && (isalpha(ch) || isdigit(ch)) || ch=='_') s += ch; /*練習問題1*/
            cin.unget();
            if (s == "let") return Token(let);
            if (s == "quit") return Token(quit);
            if (s == "const") return Token(CONSTANT); /*練習問題3*/
            return Token(name, s);
        }
        error("Bad token");
    }
}


void Token_stream::ignore(char c)
{
    if (full && c == buffer.kind) {
        full = false;
        return;
    }
    full = false;

    char ch;
    while (cin >> ch)
        if (ch == c) return;
}

class Variable {
public:
    string name;
    double value;
    bool constant; /*練習問題3*/
    Variable(string n, double v, bool f) :name{ n }, value{ v }, constant{ f } { }
};

vector<Variable> names;

double get_value(string s)
{
    for (int i = 0; i<names.size(); ++i)
        if (names[i].name == s) return names[i].value;
    error("get: undefined name ", s);
}

void set_value(string s, double d)
{
    for (int i = 0; i <= names.size(); ++i)
        if (names[i].name == s) {
            if (names[i].constant) error("定数には代入できません");  /*練習問題3*/
            names[i].value = d;
            return;
        }
    error("set: undefined name ", s);
}

bool is_declared(string s)
{
    for (int i = 0; i<names.size(); ++i)
        if (names[i].name == s) return true;
    return false;
}

Token_stream ts;

double expression();

double primary()
{
    Token t = ts.get();
    switch (t.kind) {
    case '(':
    {    double d = expression();
    t = ts.get();
    if (t.kind != ')') error("'(' expected");
    return d;
    }
    case '-':
        return -primary();
    case '+':
        return primary();
    case number:
        return t.value;
    case name:
        return get_value(t.name);
    default:
        error("primary expected");
    }
}

double term()
{
    double left = primary();
    while (true) {
        Token t = ts.get();
        switch (t.kind) {
        case '*':
            left *= primary();
            break;
        case '/':
        {    double d = primary();
        if (d == 0) error("divide by zero");
        left /= d;
        break;
        }
        case '%':
        {    double d = primary();
        if (d == 0) error("divide by zero");
        left = fmod(left, d);
        break;
        }
        default:
            ts.unget(t);
            return left;
        }
    }
}

double expression()
{
    double left = term();
    while (true) {
        Token t = ts.get();
        switch (t.kind) {
        case '+':
            left += term();
            break;
        case '-':
            left -= term();
            break;
        default:
            ts.unget(t);
            return left;
        }
    }
}

double declaration()
{
    Token t = ts.get();
    if (t.kind != name) error("name expected in declaration");
    string name = t.name;
    bool not_declared = !is_declared(name); /*練習問題2*/
    Token t2 = ts.get();
    if (t2.kind != '=') error("= missing in declaration of ", name);
    double d = expression();
    if (not_declared) names.push_back(Variable(name, d, false)); /*練習問題2*/
    else set_value(name, d); /*練習問題2*/
    return d;
}

double const_set() /*練習問題3*/
{
    Token t = ts.get();
    if (t.kind != name) error("name expected in declaration");
    string name = t.name;
    if (is_declared(name)) error("定数が既に宣言されています");
    Token t2 = ts.get();
    if (t2.kind != '=') error("= missing in declaration of ", name);
    double d = expression();
    names.push_back(Variable(name, d, true));
    return d;
}

double statement()
{
    Token t = ts.get();
    switch (t.kind) {
    case let:
        return declaration();
    case CONSTANT:  /*練習問題3*/
        return const_set();
    default:
        ts.unget(t);
        return expression();
    }
}

void clean_up_mess()
{
    ts.ignore(print);
}

 

void calculate()
{
    while (true) try {
        cout << prompt;
        Token t = ts.get();
        while (t.kind == print) t = ts.get();
        if (t.kind == quit) return;
        ts.unget(t);
        cout << result << statement() << endl;
    }
    catch (runtime_error& e) {
        cerr << e.what() << endl;
        clean_up_mess();
    }
}

int main()

try {
    calculate();
    return 0;
}
catch (exception& e) {
    cerr << "exception: " << e.what() << endl;
    char c;
    while (cin >> c&& c != ';');
    return 1;
}
catch (...) {
    cerr << "exception\n";
    char c;
    while (cin >> c && c != ';');
    return 2;
}