mirror of
https://github.com/louisrubet/rpn
synced 2025-01-01 18:20:06 +01:00
binary base functions (not yet entries)
This commit is contained in:
parent
bc45462ffb
commit
a24e53f8dd
4 changed files with 122 additions and 32 deletions
19
src/rpn-binary.h
Normal file
19
src/rpn-binary.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
void dec()
|
||||
{
|
||||
binary::s_mode = binary::dec;
|
||||
}
|
||||
|
||||
void hex()
|
||||
{
|
||||
binary::s_mode = binary::hex;
|
||||
}
|
||||
|
||||
void oct()
|
||||
{
|
||||
binary::s_mode = binary::oct;
|
||||
}
|
||||
|
||||
void bin()
|
||||
{
|
||||
binary::s_mode = binary::bin;
|
||||
}
|
|
@ -31,6 +31,13 @@ program::keyword_t program::_keywords[] =
|
|||
{ cmd_keyword, "sqrt", &program::squareroot, "unarity operator square root" },
|
||||
{ cmd_keyword, "sq", &program::square, "unarity operator square" },
|
||||
|
||||
//BINARY
|
||||
{ cmd_undef, "", NULL, "\nBINARY"},
|
||||
{ cmd_keyword, "dec", &program::dec, "decimal representation for binaries" },
|
||||
{ cmd_keyword, "hex", &program::hex, "hexadecimal representation for binaries" },
|
||||
{ cmd_keyword, "oct", &program::oct, "octal representation for binaries" },
|
||||
{ cmd_keyword, "bin", &program::bin, "binary representation for binaries" },
|
||||
|
||||
//TEST
|
||||
{ cmd_undef, "", NULL, "\nTEST"},
|
||||
{ cmd_keyword, ">", &program::sup, "binary operator >" },
|
||||
|
|
|
@ -33,14 +33,23 @@ void help()
|
|||
cout<<endl;
|
||||
cout<<"Current verbosity is "<<g_verbose<<endl;
|
||||
cout<<"Current float mode is ";
|
||||
switch(g_float_mode)
|
||||
switch(number::s_mode)
|
||||
{
|
||||
case mode_std: cout << "'std'"; break;
|
||||
case mode_fix: cout << "'fix'"; break;
|
||||
case mode_sci: cout << "'sci'"; break;
|
||||
case number::std: cout << "'std'"; break;
|
||||
case number::fix: cout << "'fix'"; break;
|
||||
case number::sci: cout << "'sci'"; break;
|
||||
default: cout << "unknown"; break;
|
||||
}
|
||||
cout<<endl<<"Current float precision is "<<number::s_current_precision<<endl;
|
||||
cout<<"Current binary mode is ";
|
||||
switch(binary::s_mode)
|
||||
{
|
||||
case binary::dec: cout << "'dec'"; break;
|
||||
case binary::hex: cout << "'hex'"; break;
|
||||
case binary::oct: cout << "'oct'"; break;
|
||||
case binary::bin: cout << "'bin'"; break;
|
||||
default: cout << "unknown"; break;
|
||||
}
|
||||
cout<<endl<<"Current float precision is "<<g_current_precision<<endl;
|
||||
cout<<endl<<endl;
|
||||
}
|
||||
|
||||
|
@ -49,12 +58,12 @@ void std()
|
|||
if (stack_size()>=1)
|
||||
{
|
||||
ARG_IS_OF_TYPE(0, cmd_number);
|
||||
g_default_precision = (int)getf();
|
||||
number::s_default_precision = (int)getf();
|
||||
}
|
||||
g_current_precision = g_default_precision;
|
||||
g_float_mode = mode_std;
|
||||
number::s_current_precision = number::s_default_precision;
|
||||
number::s_mode = number::std;
|
||||
|
||||
cout.precision(g_current_precision);
|
||||
cout.precision(number::s_current_precision);
|
||||
cout.unsetf(ios_base::floatfield);
|
||||
}
|
||||
|
||||
|
@ -63,10 +72,10 @@ void fix()
|
|||
MIN_ARGUMENTS(1);
|
||||
ARG_IS_OF_TYPE(0, cmd_number);
|
||||
|
||||
g_current_precision = (int)getf();
|
||||
g_float_mode = mode_fix;
|
||||
number::s_current_precision = (int)getf();
|
||||
number::s_mode = number::fix;
|
||||
|
||||
cout << setprecision(g_current_precision) << fixed;
|
||||
cout << setprecision(number::s_current_precision) << fixed;
|
||||
}
|
||||
|
||||
void sci()
|
||||
|
@ -74,10 +83,10 @@ void sci()
|
|||
MIN_ARGUMENTS(1);
|
||||
ARG_IS_OF_TYPE(0, cmd_number);
|
||||
|
||||
g_current_precision = (int)getf();
|
||||
g_float_mode = mode_sci;
|
||||
number::s_current_precision = (int)getf();
|
||||
number::s_mode = number::sci;
|
||||
|
||||
cout << setprecision(g_current_precision) << scientific;
|
||||
cout << setprecision(number::s_current_precision) << scientific;
|
||||
}
|
||||
|
||||
void rpn_version()
|
||||
|
|
89
src/rpn.cpp
89
src/rpn.cpp
|
@ -34,6 +34,7 @@
|
|||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
//#include <regex>
|
||||
using namespace std;
|
||||
|
||||
#include "stack.h"
|
||||
|
@ -45,23 +46,10 @@ static const string g_show_stack_separator = ":\t";
|
|||
//
|
||||
static int g_verbose = 0;
|
||||
|
||||
//
|
||||
static int g_default_precision = 20;
|
||||
static int g_current_precision = g_default_precision;
|
||||
|
||||
//
|
||||
static const char version[] = "1.0";
|
||||
static const char uname[] = "rpn v1.0, (c) 2013 <louis@rubet.fr>";
|
||||
|
||||
//
|
||||
typedef enum {
|
||||
mode_std,
|
||||
mode_fix,
|
||||
mode_sci
|
||||
} float_mode;
|
||||
static float_mode g_default_float_mode = mode_std;
|
||||
static float_mode g_float_mode = g_default_float_mode;
|
||||
|
||||
typedef enum {
|
||||
ret_ok,
|
||||
ret_unknown_err,
|
||||
|
@ -85,6 +73,7 @@ const char* ret_value_string[ret_max] = {
|
|||
typedef enum {
|
||||
cmd_undef,
|
||||
cmd_number,/* floating value to put in stack */
|
||||
cmd_binary,/* binary (integer) value to put in stack */
|
||||
cmd_symbol,/* symbol value to put in stack */
|
||||
cmd_keyword,/* langage keyword */
|
||||
cmd_branch,/* langage branch keyword */
|
||||
|
@ -92,11 +81,12 @@ typedef enum {
|
|||
} cmd_type_t;
|
||||
|
||||
const char* cmd_type_string[cmd_max] = {
|
||||
"undef", "number", "symbol", "keyword", "keyword"
|
||||
"undef", "number", "binary", "symbol", "keyword", "keyword"
|
||||
};
|
||||
|
||||
//
|
||||
typedef long double floating_t;
|
||||
typedef long long integer_t;
|
||||
class program;
|
||||
class object;
|
||||
class branch;
|
||||
|
@ -122,7 +112,68 @@ public:
|
|||
number(floating_t value) : object(cmd_number) { _value = value; }
|
||||
virtual void show(ostream& stream = cout) { stream << _value; }
|
||||
floating_t _value;
|
||||
|
||||
// representation mode
|
||||
typedef enum {
|
||||
std,
|
||||
fix,
|
||||
sci
|
||||
} mode_enum;
|
||||
static mode_enum s_default_mode;
|
||||
static mode_enum s_mode;
|
||||
|
||||
// precision
|
||||
static int s_default_precision;
|
||||
static int s_current_precision;
|
||||
};
|
||||
number::mode_enum number::s_default_mode = number::std;
|
||||
number::mode_enum number::s_mode = number::s_default_mode;
|
||||
int number::s_default_precision = 20;
|
||||
int number::s_current_precision = number::s_default_precision;
|
||||
|
||||
class binary : public object
|
||||
{
|
||||
public:
|
||||
binary(integer_t value) : object(cmd_binary) { _value = value; }
|
||||
|
||||
virtual void show(ostream& stream = cout)
|
||||
{
|
||||
cout << "# ";
|
||||
switch(s_mode)
|
||||
{
|
||||
case dec: cout<<std::right<<std::setw(8)<<std::dec<<_value<<" d"; break;
|
||||
case hex: cout<<std::right<<std::setw(8)<<std::hex<<_value<<" h"; break;
|
||||
case oct: cout<<std::right<<std::setw(8)<<std::oct<<_value<<" o"; break;
|
||||
case bin:
|
||||
{
|
||||
string mybin;
|
||||
for (int i = (int)(log((floating_t)_value) / log(2.)); i>=0; i--)
|
||||
{
|
||||
if (_value & (1 << i))
|
||||
mybin+='1';
|
||||
else
|
||||
mybin+='0';
|
||||
}
|
||||
cout<<std::right<<std::setw(16)<<std::oct<<mybin<<" b";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
integer_t _value;
|
||||
|
||||
// representation mode
|
||||
typedef enum {
|
||||
dec,
|
||||
hex,
|
||||
oct,
|
||||
bin,
|
||||
} binary_enum;
|
||||
static binary_enum s_default_mode;
|
||||
static binary_enum s_mode;
|
||||
};
|
||||
binary::binary_enum binary::s_default_mode = binary::dec;
|
||||
binary::binary_enum binary::s_mode = binary::s_default_mode;
|
||||
|
||||
class symbol : public object
|
||||
{
|
||||
|
@ -206,7 +257,7 @@ public:
|
|||
}
|
||||
|
||||
// not a command, but a stack entry, manage it
|
||||
if (type == cmd_number)
|
||||
if ((type == cmd_number) || (type == cmd_binary))
|
||||
{
|
||||
stk.push_back(seq_obj(i), seq_len(i), type);
|
||||
i++;
|
||||
|
@ -691,6 +742,7 @@ private:
|
|||
// keywords implementation
|
||||
#include "rpn-general.h"
|
||||
#include "rpn-algebra.h"
|
||||
#include "rpn-binary.h"
|
||||
#include "rpn-test.h"
|
||||
#include "rpn-stack.h"
|
||||
#include "rpn-branch.h"
|
||||
|
@ -708,8 +760,11 @@ private:
|
|||
static void apply_default(void)
|
||||
{
|
||||
//default precision
|
||||
cout << setprecision(g_default_precision);
|
||||
g_float_mode = g_default_float_mode;
|
||||
cout << setprecision(number::s_default_precision);
|
||||
number::s_mode = number::s_default_mode;
|
||||
|
||||
//default binary mode
|
||||
binary::s_mode = binary::s_default_mode;
|
||||
}
|
||||
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue