1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | // Code derived from Stroustrup's PPP2 book
// From Chs. 6 & 7, The Calculator Project - CLI edition
// -and beginning on p 174
#pragma once
#include <string>
#include "calc_util.h"
using std::string;
//------------------------------------------------------------------------------
/** the type that represents a mathematical token obtained from cin
*/
class Token {
public:
/** Param'd ctor
*
* @param ch
*/
Token(char const ch);
/** Param'd ctor
*
* @param ch
* @param val
*/
Token(char const ch, double const val);
/** Param'd ctor
*
* @param ch
* @param n
*/
Token(char const ch, std::string const& n);
char kind; // what kind of token
double value; // for numbers: a value
string name; // for names: name itself
};
//------------------------------------------------------------------------------
/** type that contains a 1x buffer of Token, reads from cin
* @note provides get(), putback(), and ignore()
*/
class Token_stream {
public:
/** ctor
*/
Token_stream();
/** get the next Token; either from the internal buffer, or from cin
*
* @return the next Token
* @note throws runtime_error on ill-formed token
*/
Token get();
/** put a Token back
*
* @param t
* @note throws runtime_error on already-full buffer
*/
void putback(Token const t);
/** discard all tokens up to and including a c
*
* @param c
* @note used for error-correction
*/
void ignore(char const c);
private:
bool full; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};
//------------------------------------------------------------------------------
/** a simple mathematical calculator
*/
class Calculator {
public:
/** ctor
*/
Calculator();
/** begin the processing loop of Calculator
*/
void run();
private:
/** pre-establish mathematical constant declarations, such as 'pi' and 'e'
*/
void set_constants();
/** parse the next primary
*
* @return the derived value
* @note deal with numbers and parentheses
* @note throws runtime_error on missing primary, or on missing ')' token
*/
double primary();
/** parse the next term
*
* @return the derived value
* @note deal with *, /, and %
* @note throws runtime_error on divide by zero
*/
double term();
/** parse the next expression
*
* @return the derived value
* @note deal with + and -
*/
double expression();
/** makes a declaration for k, whose name will be the following expression
*
* @param k - will be "let" or "con"(stant)
* @return the derived expression
* @note defines a new Variable, on success
* @note throws runtime_error on non-named Token, or on missing '=' in
* declaration statement
*/
double declaration(Token const k);
/** get the next Token, parsing either it's declaration or it's expression
*
* @return the evaluated value
*/
double statement();
/** error-correction, ignores "prints"
*/
void clean_up_mess();
Token_stream ts; // provides get(), putback(), and ignore()
};
|
main_calculator.cpp • Calculator.cpp • calc_util.h • calc_util.cpp