# fibonacci sequence [infinity]

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Exercise 4

#include "std_lib_facilities.h"

vector<int> myVec;
string label = "l";

void print(string& _label, vector<int>& _vecInput) {
for (int x : _vecInput) {
cout << _label << " " << x << "\n";
}
}

void fibonacci(int x, int y, vector<int>& v) {
v.push_back(x);
v.push_back(y);
while(true){
int temp;
if (x > 0 && y > 0 && x + y > 0) {
v.push_back(x + y);
temp = y;
y = x + y;
x = temp;
}
else {
break;
}
}
}

int main()
try
{
fibonacci(1, 2, myVec);
print(label, myVec);

cout << "The maximum number of the fibonacci sequence is: " << &myVec[myVec.size() - 1] << "\n";

keep_window_open();

}

catch (runtime_error e) {
cout << e.what() << "\n";
keep_window_open(".");
}
catch (...) {
cout << "Extiting" << "\n";
keep_window_open(".");
}
```
```Output:
l 1
l 2
l 3
l 5
l 8
l 13
l 21
l 34
l 55
l 89
l 144
l 233
l 377
l 610
l 987
l 1597
l 2584
l 4181
l 6765
l 10946
l 17711
l 28657
l 46368
l 75025
l 121393
l 196418
l 317811
l 514229
l 832040
l 1346269
l 2178309
l 3524578
l 5702887
l 9227465
l 14930352
l 24157817
l 39088169
l 63245986
l 102334155
l 165580141
l 267914296
l 433494437
l 701408733
l 1134903170
l 1836311903
The maximum number of the fibonacci sequence is: 000002A172660740
Please enter a character to exit
```

# fibonacci sequence

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Exercise 3

#include "std_lib_facilities.h"

void print(string& _label, vector<int>& _vecInput) {
for (int x : _vecInput) {
cout << _label << " " << x << "\n";
}
}

void fibonacci(int x, int y, vector<int>& v, int n) {
v.push_back(x);
v.push_back(y);
for (int i = 0; i < n; i++) {
int temp;
v.push_back(x + y);
temp = y;
y = x + y;
x = temp;
}
}

int main()
try
{
vector<int> myVec;
string label = "l";
int num = 20;

fibonacci(1, 2, myVec, num);

print(label, myVec);

keep_window_open();

}

catch (runtime_error e) {
cout << e.what() << "\n";
keep_window_open();
}

catch(...) {
cout << "Exiting\n";
keep_window_open();
}
```
```Output:
l 1
l 2
l 3
l 5
l 8
l 13
l 21
l 34
l 55
l 89
l 144
l 233
l 377
l 610
l 987
l 1597
l 2584
l 4181
l 6765
l 10946
l 17711
l 28657
Please enter a character to exit
```

# print function

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Exercise 2

#include "std_lib_facilities.h"

void print(string& _label, vector<int>& _vecInput) {
for (int x : _vecInput) {
cout << _label << " " << x << "\n";
}
}

int main() {
vector<int> myVector{ 1, 2, 3, 4, 5 };
string label = "l";
print(label, myVector);
keep_window_open();
}
```
```Output:
l 1
l 2
l 3
l 4
l 5
Please enter a character to exit
```

# calculator 4.2 [Token_stream]

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Exercise 1

#include "std_lib_facilities.h"

const char let = '#';
const char quitProg = 'Q';
const char print = ';';
const char number = '8';
const char name = 'a';
const char sqroot = 'S';
const char power = 'P';
const char constant = 'C';
const char reset = 'R';
const char help = '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);
};

Token Token_stream::get()
{
if (full) {
full = false; return buffer;
}
char ch;
cin.get(ch);

while (isspace(ch)) {
if (ch == '\n') return Token(print); // if newline detected, return print Token
cin.get(ch);
}

switch (ch) {
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
case ';':
case '=':
case 'k':
case ',':
{
}
case '#':
{
}
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;
}
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;
}
cin.unget(); //puts the most recently read character back into the stream
}
}
}
}

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;
bool immutable;
Variable(string n, double v, bool i) :name(n), value(v), immutable(i) { }
};

class Symbol_table {
public:
vector<Variable> var_table;
double get(string);
void set(string, double);
bool is_declared(string);
double declare(char);
bool is_immutable(string);
};

Token_stream ts;
double expression(Token_stream& ts);
double primary(Token_stream& ts);

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

void Symbol_table::set(string s, double d)
{
for (int i = 0; i <= var_table.size(); ++i) {
if (var_table[i].name == s) {
var_table[i].value = d;
return;
}
}
error("set: undefined name ", s);
}

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

double Symbol_table::declare(char kind)
{
Token t = ts.get();
if (t.kind != name) {
error("name expected in declaration");
}

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

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

bool Symbol_table::is_immutable(string s)
{
for (int i = 0; i<int(var_table.size()); ++i)
if (var_table[i].name == s && var_table[i].immutable) return true;
return false;
}

double pow_function(Token _t) {
_t = ts.get();
if (_t.kind != '(') {
error("'(' expected");
}

double x = expression(ts);

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

double n = expression(ts);
_t = ts.get();
if (_t.kind == ')') {
return pow(x, n);
}
else {
error("Expected ')'");
}
}

double sqrt_function(Token _t) {
_t = ts.get();
if (_t.kind != '(') {
error("'(' expected");
}
ts.unget(_t);
double d = primary(ts);
if (d < 0.0) {
error("negative square root");
}
return sqrt(d);
}

Symbol_table st;

double primary(Token_stream& ts)
{
Token t = ts.get();
switch (t.kind) {
case '(':
{
double d = expression(ts);
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case '-':
{
return -primary(ts);
}
case '+':
{
return primary(ts);
}
case number:
{
return t.value;
}
case sqroot:
{
return sqrt_function(t);
}
case power:
{
return pow_function(t);
}
case name:
{
return st.get(t.name);
}
default:
{
error("primary expected");
return 0.0;
}
}
}

double term(Token_stream& ts)
{
double left = primary(ts);
while (true) {
Token t = ts.get();
switch (t.kind) {
case 'k':
{
left *= 1000;
break;
}
case '*':
{
left *= primary(ts);
break;
}
case '/':
{
double d = primary(ts);
if (d == 0) error("divide by zero");
left /= d;
break;
}
case '%':
{
int i1 = narrow_cast<int>(left);
int i2 = narrow_cast<int>(primary(ts));
if (i2 == 0) error("%: divide by zero");
left = i1%i2;
break;
}

default: {
ts.unget(t);
return left;
}
}
}
}

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

double statement()
{
Token t = ts.get();
double d;
if (t.kind == let || t.kind == reset || t.kind == constant) {
d = st.declare(t.kind);
}
else {
ts.unget(t);
d = expression(ts);
}
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()
{
cout << "Welcome to my calculator.\n"
<< "Just hit enter or ; at the end of your function to calculate.\n"
<< "Enter help or Help for help or quit to exit the program.\n";
while (true) try {
cout << prompt;
Token t = ts.get();
while (t.kind == print) {
t = ts.get();
}
if (t.kind == help) {
cout << "You can use / *-+operators.\n"
<< "Declaring variables using #, for example: # x = 5.\n";
}
else {
if (t.kind == quitProg) {
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;
}
```

# simple namespace examples

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Drill 3

#include "std_lib_facilities.h"

namespace X {
int var;
void print() {
cout << var << "\n";
}
}

namespace Y {
int var;
void print() {
cout << var << "\n";
}
}

namespace Z {
int var;
void print() {
cout << var << "\n";
}
}

int main() {
X::var = 7;
X::print(); //print X's var
using namespace Y;
var = 9;
print(); //print Y's var
{	using Z::var;
using Z::print;
var = 11;
print(); //print Z's var
}
print(); //print X's var
X::print(); //print Y's var

keep_window_open();
}
```

# bug search with swap function

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Drill 2

#include "std_lib_facilities.h"

void swap_v(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}

void swap_r(int& a, int& b) {
int temp;
temp = a;
a = b;
b = temp;
}

/* Nope.
void swap_cr(const int& a,const int& b) {
int temp;
temp = a;
a = b;
b = temp;
}
*/

int main() {
//int x = 7;
//int y = 9;
//swap_v(x, y); Nope.
//swap_r(x, y); Success!
//cout << "x: " << x << " y: " << y << "\n";

//swap_v(7, 9);
//cout << "x: " << x << " y: " << y << "\n";
//swap_r(7, 9); Nope.

//const int cx = 7;
//const int cy = 9;
//swap_v(cx, cy); Nope.
//swap_r(cx, cy); //Nope.
//cout << "cx: " << cx << " cy: " << cy << "\n";

//swap_v(7.7, 9.9); Nope.
//swap_r(7.7, 9.9); Nope.

//double dx = 7.7;
//double dy = 9.9;
//swap_v(dx, dy); Nope.
//swap_r(dx, dy); Nope.
//cout << "dx: " << dx << " dy: " << dy << "\n";

keep_window_open(".");
}
```

# structuring a C++ project [main, header, source]

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

main.cpp main file

```//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 8 Drill 1

#include "myClass.h"

int foo;

int main() {
foo = 7;
print_foo();
print(99);

char cc;
cin >> cc;
return 0;
}
```

```#pragma once

#ifndef MYCLASS_H_INCLUDED
#define MYCLASS_H_INCLUDED

#include <iostream>
using namespace std;

extern int foo;
void print_foo();
void print(int);

#endif // !MY_H_INCLUDED
```

myClass_source.cpp source file

```#include "myClass.h"
using namespace std;

void print_foo() {
cout << foo;
}

void print(int _i) {
cout << _i;
}
```
```Output:
799
```