https://rentry.org/PPP2_Calculator_h

// 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.cppCalculator.cppcalc_util.hcalc_util.cpp

Edit
Pub: 09 Mar 2023 05:04 UTC
Edit: 03 May 2023 10:40 UTC
Views: 523