calculator 3.1 [assignment operators]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Exercise 2
Using std_lib_facilities.h by Bjarne Stroustrup.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Exercise 2

#include "std_lib_facilities.h"

struct Token {
	char kind;
	double value;
	string name;
	Token(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = '#';
const char exitprog = 'E';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqroot = 'S';
const char power = 'P';
const char reset = 'R';

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 '=':
	case 'k':
	case ',':
	{
		return Token(ch);
	}
	case '#':
	{
		return Token(let);
	}
	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) || ch == '_') { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "exit") return Token(exitprog); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqroot);
			if (s == "pow") return Token(power);
			if (s == "reset") return Token(reset);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
	}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case sqroot:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		if (d < 0.0) {
			error("negative square root");
		}
		return sqrt(d);
	}
	case power:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}

		double x = int(expression());

		t = ts.get();
		if (t.kind != ',') {
			error("',' expected");
		}

		int n = int(expression());

		t = ts.get();
		if (t.kind == ')') {
			cout << n << ", " << x << "\n";
			return pow(x, n);
		}
		else {
			error("Expected ')'");
		}
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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(char kind)
{
	Token t = ts.get();
	if (t.kind != name) { //if (t.kind != 'a')
		error("name expected in declaration");
	}

	string name = t.name;
	if (kind == let) {
		if (is_declared(name)) error(name, " declared twice");
	}
	else if (kind == reset) {
		if (!is_declared(name))
			error(name, " has not been declared");
	}
	else {
		error("unknown statement");
	}

	Token t2 = ts.get();
	if (t2.kind != '=') error("= missing in declaration of ", name);
	double d = expression();
	if (is_declared(name))
		set_value(name, d);
	else
		names.push_back(Variable(name, d));
	return d;
}

double statement()
{
	Token t = ts.get();
	double d;
	if (t.kind == let || t.kind == reset) {
		d = declaration(t.kind);
	}
	else {
		ts.unget(t);
		d = expression();
	}
	t = ts.get();
	if (t.kind != print) {
		error("Missing terminator");
	}
	return d;
}

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 == exitprog) {
			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;
}
Output:
> # x = 5;
= 5
> x + 6;
= 11
> reset x = 7;
= 7
> x + 6;
= 13
>

calculator 3.0 [allow underscores]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Exercise 1
Using std_lib_facilities.h by Bjarne Stroustrup.

Exercise 1. Allow underscores in the calculator’s variable names.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Exercise 1

#include "std_lib_facilities.h"

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

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

const char let = '#';
const char exitprog = 'E';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqroot = 'S';
const char power = 'P';

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 '=':
	case 'k':
	case ',':
	{
		return Token(ch);
	}
	case '#':
	{
		return Token(let);
	}
	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) || ch == '_') { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "exit") return Token(exitprog); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqroot);
			if (s == "pow") return Token(power);
			return Token(name, s);
		}
		error("Bad token");
		return 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;
	}
}

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0;
}

void set_value(string s, double 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)
{
	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 sqroot:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		if (d < 0.0) {
			error("negative square root");
		}
		return sqrt(d);
	}
	case power:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}

		double x = int(expression());

		t = ts.get();
		if (t.kind != ',') {
			error("',' expected");
		}

		int n = int(expression());

		t = ts.get();
		if (t.kind == ')') {
			return pow(x, n);
		}
		else {
			error("Expected ')'");
		}
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0;
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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 == exitprog) {
			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;
}
Output:
> # x_4 = 5;
= 5
> x_4 + 6;
= 11
>

bug search in Stroustrup’s calculator 2.7

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 11
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 11. Change the “quit keyword” from quit to exit. That will involve defining a string for quit just as we did for let in 7.8.2.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 11

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = '#';
const char exitprog = 'E';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqroot = 'S';
const char power = 'P';

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 '=':
		case 'k':
		case ',':
		{
			return Token(ch);
		}
		case '#':
		{
			return Token(let);
		}
		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)) { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "exit") return Token(exitprog); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqroot);
			if (s == "pow") return Token(power);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
		}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case sqroot:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		if (d < 0.0) {
			error("negative square root");
		}
		return sqrt(d);
	}
	case power:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}

		double x = expression();

		t = ts.get();
		if (t.kind != ',') {
			error("',' expected");
		}

		int n = int(expression());

		t = ts.get();
		if (t.kind == ')') {
			return pow(x, n);
		}
		else {
			error("Expected ')'");
		}
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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 == exitprog) {
			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;
}
Output:
> 5 + 6;
= 11
> exit;

bug search in Stroustrup’s calculator 2.6

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 10
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 10. Change the “declaration keyword” from let to #.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 10

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqRt = 'S';
const char power = 'P';

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 '=':
	case 'k':
	case ',':
	{
		return Token(ch);
	}
	case '#':
	{
		return Token(let);
	}
	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)) { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "quit") return Token(quit); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqRt);
			if (s == "pow") return Token(power);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
	}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case sqRt:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		if (d < 0.0) {
			error("negative square root");
		}
		return sqrt(d);
	}
	case power:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}

		double x = expression();

		t = ts.get();
		if (t.kind != ',') {
			error("',' expected");
		}

		int n = int(expression());
		t = ts.get();
		if (t.kind == ')') {
			return pow(x, n);
		}
		else {
			error("Expected ')'");
		}
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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()

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;
}
Output:
> # x = 5;
= 5
> x + 6;
= 11
>

bug search in Stroustrup’s calculator 2.5

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 9
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 9. Allow the user to use pow(x,i) to mean “Multiply x with itself i times”; for example, pow(2.5,3) is 2.5*2.5*2.5. Require i to be an integer using the technique we used for %.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 9

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqRt = 'S';
const char power = 'P';

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 '=':
	case 'k':
	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)) { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "let") return Token(let);
			if (s == "quit") return Token(quit); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqRt);
			if (s == "pow") return Token(power);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
	}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case sqRt:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		if (d < 0.0) {
			error("negative square root");
		}
		return sqrt(d);
	}
	case power:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}

		double x = int(expression());

		t = ts.get();
		if(t.kind != ',') {
			error("',' expected");
		}

		int n = int(expression());
		
		t = ts.get();
		if (t.kind == ')') {
			return pow(x, n);
		}
		else {
			error("Expected ')'");
		}
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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()

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;
}
Output:
> pow(3,5);
= 243
>

bug search in Stroustrup’s calculator 2.4

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 8
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 8. Catch attempts to take the square root of a negative number and print an appropriate error message.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 8

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqRt = 'S';

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 '=':
	case 'k':
	{
		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)) { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "let") return Token(let);
			if (s == "quit") return Token(quit); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqRt);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
	}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case sqRt:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		if (d < 0.0) {
			error("negative square root");
		}
		return sqrt(d);
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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()

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;
}
Output:
> sqrt(-32);
negative square root
>

bug search in Stroustrup’s calculator 2.3

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 7
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 7. Give the user a square root function sqrt(), for example, sqrt(2+6.7). Naturally, the value of sqrt(x) is the sqaure root of x; for example, sqrt(9) is 3. Use the standard library sqrt() function that is available through the header std_lib_facilities.h. Remember to update the comments, including the grammar.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 7

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqRt = 'S';

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 '=':
	case 'k':
	{
		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)) { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "let") return Token(let);
			if (s == "quit") return Token(quit); //Error 2: if (s == "quit") return Token(name);
			if (s == "sqrt") return Token(sqRt);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
	}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case sqRt:
	{
		t = ts.get();
		if (t.kind != '(') {
			error("'(' expected");
		}
		ts.unget(t);
		double d = primary();
		return sqrt(d);
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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()

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;
}
Output:
> sqrt(8);
= 2.82843
>

bug search in Stroustrup’s calculator 2.2

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 6
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 6. Add a predefined name k meaning 1000.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 6

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';

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 '=':
	case 'k':
	{
		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)) { //is ch a letter?
			string s;
			s += ch;
			while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
				s += ch; //Error 2: s = ch;
			}
			cin.unget(); //puts the most recently read character back into the stream
			if (s == "let") return Token(let);
			if (s == "quit") return Token(quit); //Error 2: if (s == "quit") return Token(name);
			return Token(name, s);
		}
		error("Bad token");
		return Token(' '); //Line missing
	}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
		return d; //Line missing
	}
	case '-':
	{
		return -primary();
	}
	case '+':
	{
		return primary();
	}
	case number:
	{
		return t.value;
	}
	case name:
	{
		return get_value(t.name);
	}
	default:
	{
		error("primary expected");
		return 0.0; //Line missing
	}
	}
}

double term()
{
	double left = primary();
	while (true) {
		Token t = ts.get();
		switch (t.kind) {
		case 'k':
		{
			left *= 1000;
			break;
		}
		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 != name) { //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()

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;
}
Output:
> 3k + 5k;
= 8000
>

bug search in Stroustrup’s calculator 2.1

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 5
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 5. Do the testing and fix any bugs that you missed when you commented.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 5

/*
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string val) :kind(ch), name(val) { } //Error 1: Line missing
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';

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)) { //is ch a letter?
				string s;
				s += ch;
				while (cin.get(ch) && (isalpha(ch) || isdigit(ch))) { //reads chars, strings or digits
					s += ch; //Error 2: s = ch;
				}
				cin.unget(); //puts the most recently read character back into the stream
				if (s == "let") return Token(let);
				if (s == "quit") return Token(quit); //Error 2: if (s == "quit") return Token(name);
				return Token(name, s);
			}
			error("Bad token");
			return Token(' '); //Line missing
		}
	}
}

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

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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);
	return 0.0; //Line missing
}

void set_value(string s, double 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)
{
	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"); //Error 3: if (t.kind != ')') error("'(' expected");
			return d; //Line missing
		}
		case '-':
		{
			return -primary();
		}
		case '+':
		{
			return primary();
		}
		case number:
		{
			return t.value;
		}
		case name:
		{
			return get_value(t.name);
		}
		default:
		{
			error("primary expected");
			return 0.0; //Line missing
		}
	}
}

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;
			}
			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) { //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()

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;
}
Output:
> let x = 5;
= 5
> 6 + x;
= 11
>

bug search in Stroustrup’s calculator 2.0

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 7 Drill 1
Using std_lib_facilities.h by Bjarne Stroustrup.

Drill 1. Starting from the file calculator08buggy.cpp, get the calculator to compile.

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 7 Drill 1

/*
Get the calculator to compile.
calculator08buggy.cpp
Helpful comments removed.
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(char ch) :kind(ch), value(0) { }
	Token(char ch, double val) :kind(ch), value(val) { }
	Token(char ch, string n) :kind(ch), name(n) { }
};

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

const char let = 'L';
const char quit = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';

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 == "quit") return Token(name);
			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;
}

struct Variable {
	string name;
	double value;
	Variable(string n, double v) :name(n), value(v) { }
};

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) {
			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");
	}
	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;
		}
		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()

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;
}
Output:
> 6+6=
= 12
> primary expected