You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

找不到Bjarne C++ PPP书籍第6章示例代码calculator01.cpp怎么办

C++ PPP第6章calculator01.cpp 完整实现

前置说明

calculator01.cpp是《C++程序设计原理与实践》(PPP)第6章计算器示例的首个迭代版本,无需额外查找资源,对照书中代码片段即可拼接出完整可运行代码,以下是对应版本的完整实现:

完整代码

#include "std_lib_facilities.h"

class Token {
public:
    char kind;
    double value;
    Token(char ch)
        :kind(ch), value(0) { }
    Token(char ch, double val)
        :kind(ch), value(val) { }
};

Token get_token()
{
    char ch;
    cin >> ch;
    switch (ch) {
    case ';': case 'q':
    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.putback(ch);
        double val;
        cin >> val;
        return Token('8', val);
    }
    default:
        error("Bad token");
    }
}

double expression();
double term();
double primary();

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

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

double primary()
{
    Token t = get_token();
    switch (t.kind) {
    case '(':
    {
        double d = expression();
        t = get_token();
        if (t.kind != ')') error("')' expected");
        return d;
    }
    case '8':
        return t.value;
    default:
        error("primary expected");
    }
}

int main()
try {
    double val = 0;
    while (cin) {
        Token t = get_token();
        if (t.kind == 'q') break;
        if (t.kind == ';')
            cout << "= " << val << '\n';
        else
            cin.putback(t);
        val = expression();
    }
    keep_window_open();
}
catch (exception& e) {
    cerr << e.what() << '\n';
    keep_window_open();
    return 1;
}
catch (...) {
    cerr << "exception \n";
    keep_window_open();
    return 2;
}

运行注意事项

  • 代码依赖PPP配套的std_lib_facilities.h头文件,若未配置该头文件,可将代码第一行的#include "std_lib_facilities.h"替换为以下内容:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;

void keep_window_open()
{
    cin.clear();
    cin.ignore(cin.rdbuf()->in_avail());
    cin.get();
}

void error(const string& s)
{
    throw runtime_error(s);
}
  • 代码支持整数、浮点数的加减乘除运算,遵循常规运算优先级,输入表达式以;作为结束标识,输入q可退出程序
  • 示例输入:2+3*4;,输出为= 14

内容的提问来源于stack exchange,提问作者victor 0x3E7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 18:21:03