解决方案bjarne stroustrup第7章钻7 sqrt()函数加上关于sqrt()函数的错误的查询

时间:2013-10-18 13:58:28

标签: c++ c++11

我以为我会发布这个,因为对于一个相对的菜鸟来说这是相对棘手的,我没有看到任何 关于这次演习的具体帖子。

另外,当我输入:'sqrt x);'我收到正确的错误消息, 但是当我输入:'sqrtx);'我收到错误:'get:undefined name sqrtx' 显然程序认为它是一个变量的声明, 有关更好的错误处理的任何建议???

这是第7章第7课的解决方案,为calc程序的用户创建sqrt():

/*
    calculator08buggy.cpp

    This program implements a basic expression calculator.
    Input from cin; output to cout.

    The grammar for input is :

    Statement:
        Expression
        Print
        Quit

    Print: 
        ;

    Quit: 
        q

    Expression:
        Term
        Expression + Term
        Expression - Term

    Term:
        Primary
        Term * Primary
        Term / Primary
        Term % Primary

    Secondary:
        primary
        sqrt ( Expression )

    Primary:
        Number
        ( Expression )
        -Primary
        +Primary

    Number:
        floating point literal

    Input comes from cin through the Token_stream called ts.

    We have inserted 3 bugs that the compiler will catch and 3 that it won't.
*/
//------------------------------------------------------------------------------
#include "std_lib_facilities.h"
//------------------------------------------------------------------------------
struct Token {
    char kind;
    double value;
    string name;
    Token() {}//SQRT OVERHAULD CODE HERE!!!
    Token(char ch) :kind(ch), value(0) { }
    Token(char ch, double val) :kind(ch), value(val) { }
    Token(char ch, string n) :kind(ch), name(n) { } //contructor for variable names 
};
//------------------------------------------------------------------------------
class Token_stream {
    bool full;
    Token buffer;
public:
    Token_stream() :full(0), buffer(0) { }

    Token get();

    void unget(Token t) { buffer=t; full=true; } 

    void ignore(char); //is called by clean_up_mess() and ignores erroneous statement until it reads a ';'
};
//------------------------------------------------------------------------------
const char let = 'L'; //this the t.kind for declartion
const string declkey = "let"; //this is string used to declar a variable
const char quit = 'Q';
const char print = ';';
const char number = '8'; //this the t.kind for a number
const char name = 'a'; //this the t.kind for a variable name
const char square_root = 'S'; //SQRT OVERHAULD CODE HERE!!!
//------------------------------------------------------------------------------
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))) s+=ch;
            cin.unget();
            if (s == "let") return Token(let);  
            if (s == "q") return Token(quit);
            if (s == "sqrt") return Token(square_root); //SQRT OVERHAULD CODE HERE!!!
            return Token(name,s);
        }
        error("Bad token");
    }
}
//------------------------------------------------------------------------------
void Token_stream::ignore(char c)// argument is 'print' or ';'
{
    if (full && c==buffer.kind) {
        full = false;
        return;
    }
    full = false;

    char ch;
    while (cin>>ch)
        if (ch==c) return;
}
//------------------------------------------------------------------------------
struct Variable {
    string name;
    double value;
    Variable(string n, double v) :name(n), value(v) { }
};
//------------------------------------------------------------------------------
vector<Variable> names; 
//------------------------------------------------------------------------------
double get_value(string s) //returns variable value if 's' matches name already in vector 'names'
{
    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)//if 's' matches name in 'names' resets value to 'd'
{
    for (int i = 0; i<=names.size(); ++i)
        if (names[i].name == s) {
            names[i].value = d;
            return;
        }
    error("set: undefined name ",s);
}
//------------------------------------------------------------------------------
bool is_declared(string s)// if 's' is already in 'names' return true/else return false
{
    for (int i = 0; i<names.size(); ++i)
        if (names[i].name == s) return true;
    return false;
}
//------------------------------------------------------------------------------
void define_name(string name, double val)
{   
    names.push_back(Variable(name,val));
}

//------------------------------------------------------------------------------
Token_stream ts; //declare ts here so following functions can use
//------------------------------------------------------------------------------
double expression();//declare here so following primary() & term () can use
//------------------------------------------------------------------------------
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 number:
        return t.value;

    case name:
        return get_value(t.name);

    case square_root:
         { ts.unget(t);
             return NULL; }                          //SQRT OVERHAULD CODE HERE!!!

    default:
        error("primary expected");
    }
}
//------------------------------------------------------------------------------
// deal with 'sqrt()'
double secondary()                             //SQRT OVERHAULD CODE HERE!!!
{
    double left = primary(); //is null if sqrt called
    Token t = ts.get();        // get the next token from token stream
    Token t2;
    double d;

        while(true) {

        switch (t.kind) {
            case square_root:

                t2 = ts.get(); //make sure '(' is next token
                if (t2.kind != '(') 
                            error("'(' missing in declaration sqrt ");

                d = expression(); 
                left = sqrt(d);

                cout << endl << "new output works: " << left << endl;

                t = ts.get();

                if (t.kind != ')') //make sure ')' is last token in call to sqrt
                        error("')' expected");

                t = ts.get();
                break;

           default: 
                ts.unget(t);    // put t back into the token stream
                return left;
        }
    }
}
//------------------------------------------------------------------------------
double term()
{
    double left = secondary();
    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;
        }
        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 != 'a') error ("name expected in declaration");
    string name = t.name;
    if (is_declared(name)) error(name, " declared twice");
    Token t2 = ts.get();
    if (t2.kind != '=') error("= missing in declaration of " ,name);
    double d = expression();
    names.push_back(Variable(name,d));
    return d;
}
//------------------------------------------------------------------------------
double statement()
{
    Token t = ts.get();
    switch(t.kind) {
    case let:
        return declaration();
    default:
        ts.unget(t);
        return expression();
    }
}
//------------------------------------------------------------------------------
void clean_up_mess()
{
    ts.ignore(print);
}
//------------------------------------------------------------------------------
const string prompt = "> ";
const string result = "= ";
//------------------------------------------------------------------------------
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()
{
    define_name("k", 1000);

    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;
    }
}
//------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

'get: undefined name sqrtx方法

返回错误get_value

Token::get方法中: 在循环while(cin.get(ch) && (isalpha(ch) || isdigit(ch))) s+=ch; ... sqrt x)

之后

s等于"sqrt",但对于"sqrtx"s等于"sqrtx",因此与sqrt不匹配并返回Token(name,s)而不是Token(square_root)

相关问题