https://rentry.org/PPP2_calc_util_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>

using std::string;

//------------------------------------------------------------------------------

char const   number   = '8';    // t.kind==number means that t is a number Token
char const   quit     = 'q';    // t.kind==quit means that t is a quit Token
char const   print    = ';';    // t.kind==print means that t is a print Token
char const   name     = 'a';    // name token
char const   let      = 'L';    // declaration token
char const   con      = 'C';    // const declaration token
string const declkey  = "let";  // declaration keyword
string const constkey = "const";  // const keyword
string const prompt   = "> ";  // used to indicate we're waiting on user input
string const result   = "= ";  // used to indicate that what follows is a result

//------------------------------------------------------------------------------
/** the type used to populate the std::vector var_table
 */
class Variable {
 public:
  /** Param'd ctor
   *
   * @param n
   * @param v
   * @param va - variable (true) or constant (false); defaults to true
   */
  Variable(std::string const& n, double const v, bool const va = true);

  string name;
  double value;
  bool   var;  // variable (true) or constant (false)
};

//------------------------------------------------------------------------------

/** add (s, val, var) to var_table
 *
 * @param s
 * @param val
 * @param var - variable (true) or constant (false); defaults to true
 * @return val
 * @note throws runtime_error on twice-declared variable
 */
double define_name(std::string const& s, double const val,
                   bool const var = true);

/** is var already declared (stored) in var_table?
 *
 * @param var
 * @return true if it is
 */
bool is_declared(std::string const& var);

/** return the value of the Variable named s
 *
 * @param s
 * @return s' value
 * @note throws runtime_error on undefined variable
 */
double get_value(std::string const& s);

/** assign the value d to the Variable named s
 *
 * @param s
 * @param d
 * @note throws runtime_error on assigning to const, or on undefined variable
 */
void set_value(std::string const& s, double const d);

//------------------------------------------------------------------------------

/** throws a runtime_error with message s1
 *
 * @param s1
 */
void error(std::string const& s1);

/** throws a runtime_error with message s1 + s2
 *
 * @param s1
 * @param s2
 */
void error(std::string const& s1, std::string const& s2);

/** run-time checked narrowing cast (type conversion)
 *
 * @return value a, of the type R specified for the narrowing cast
 * @note throws runtime_error on information loss
 */
template <class R, class A>
R narrow_cast(A const& a)
{
  R r = R(a);
  if (A(r) != a)
    error(string{"narrow_cast() info loss"});

  return r;
}

main_calculator.cppCalculator.hCalculator.cppcalc_util.cpp

Edit
Pub: 09 Mar 2023 05:06 UTC
Edit: 03 May 2023 10:44 UTC
Views: 420