From a24e53f8dd084a8e5ab7eb1d905b0226e9af67b6 Mon Sep 17 00:00:00 2001 From: louis Date: Sat, 7 Feb 2015 14:04:10 +0100 Subject: [PATCH] binary base functions (not yet entries) --- src/rpn-binary.h | 19 ++++++++++ src/rpn-cmd.h | 7 ++++ src/rpn-general.h | 39 +++++++++++++-------- src/rpn.cpp | 89 ++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 122 insertions(+), 32 deletions(-) create mode 100644 src/rpn-binary.h diff --git a/src/rpn-binary.h b/src/rpn-binary.h new file mode 100644 index 0000000..d0f2759 --- /dev/null +++ b/src/rpn-binary.h @@ -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; +} diff --git a/src/rpn-cmd.h b/src/rpn-cmd.h index d7e86d0..46663f4 100644 --- a/src/rpn-cmd.h +++ b/src/rpn-cmd.h @@ -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 >" }, diff --git a/src/rpn-general.h b/src/rpn-general.h index 435dfa8..2f88aa5 100644 --- a/src/rpn-general.h +++ b/src/rpn-general.h @@ -33,14 +33,23 @@ void help() cout<=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() diff --git a/src/rpn.cpp b/src/rpn.cpp index 4bc769a..f0a9787 100644 --- a/src/rpn.cpp +++ b/src/rpn.cpp @@ -34,6 +34,7 @@ #include #include #include +//#include 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 "; -// -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<=0; i--) + { + if (_value & (1 << i)) + mybin+='1'; + else + mybin+='0'; + } + cout<