Currently finishing touches to the calculator.cpp between theme 6 & 7
## a final WORKING version of the calculator using beginners code similar to the book would solve ALL my problems to be honest##
Ok... so;
I need to declare error & keep_window_open functions before using them but I don't know what class should I use
string error(); // (string) this one????
I tried to find error() function in std facilities but it wasn't explained there so basically I have no idea what its real return class is, same goes for keep_window_open which basically calls for error
why am I seeing this ==>> ("~1") <<== on his website files? in keep_window_open("~1")
my book's brackets for keep_window_open() are empty
I might need to declare a few other things, this is the code I have so far:
#include "../../std_lib_facilities.h
//------------------------------------------------------------------------------
// main > primary > expression > term ORDER moved to primary > term > expression > main
class Token {
public:
char kind; // what kind of token
double value; // for numbers: a value
Token(char ch) // make a Token from a char
:kind(ch), value(0) { }
Token(char ch, double val) // make a Token from a char and a double
:kind(ch), value(val) { }
};
class Token_stream {
public:
Token get(); // get a Token
void putback(Token t); // put a Token back
private:
bool full{ false }; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};
//----------
void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t; // copy t to buffer
full = true; // buffer is now full
}
Token Token_stream::get() {
if (full) { // do we already have a Token ready?
full = false; // remove Token from buffer
return buffer;
}
char ch;
cin >> ch; // note that >> skips whitespace (space, newline, tab, etc.)
switch (ch) {
case ';': // for "print"
case 'q': // for "quit"
case '(': case ')': case '+': case '-': case '*': case '/':
return Token(ch); // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
cin.putback(ch); // put digit back into the input stream
double val;
cin >> val; // read a floating-point number
return Token('8', val); // let '8' represent "a number"
}
default:
error("Bad token"); // note that >> skips whitespace (space, newline, tab, etc.)
}
}
//-----------
Token_stream ts; // provides get() and putback()
double expression() // read and evaluate a Expression, declaration so that primary() can call expression()
// THIS WAS HERE DECLARED ON DOWNLOADED FILE NOT SURE IF ITS NEEDED?
double term(); // read and evaluate a Term
double primary() // read and evaluate a Primary
{
Token t = get_token();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = get_token();
if (t.kind != ')') error("')' expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the number's value
default:
error("primary expected");
}
}
//------------------------------------------------------------------------------
double term()
{
double left = primary();
Token t = get_token(); // get the next token
while (true) {
switch (t.kind) {
case '*':
left *= primary();
t = ts.get();
break;
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
default:
ts.putback(t);
return left;
}
}
}
double expression()
{
double left = term(); // read and evaluate a Term
Token t = get_token(); // get the next token
while (true) {
switch (t.kind) {
case '+':
left += term(); // evaluate Term and add
t = ts.get();
break;
case '-':
left -= term(); // evaluate Term and subtract
t = ts.get()
break;
default:
ts.putback(t); // put t back into the token stream
return left; // finally: no more + or -: return the answer
}
}
}
#include<iostream>
#include<string>
#include<stdexcept>
usingnamespace std;
// eg. error("putback() into a full buffer");inlinevoid error(const string& s)
{
throw runtime_error(s);
}
inlinevoid keep_window_open()
{
cin.clear();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
return;
}
// eg. keep_window_open("~1")inlinevoid keep_window_open(string s)
{
if (s=="") return;
cin.clear();
cin.ignore(120,'\n');
for (;;) {
cout << "Please enter " << s << " to exit\n";
string ss;
while (cin >> ss && ss!=s)
cout << "Please enter " << s << " to exit\n";
return;
}
}