Skip to content

Commit

Permalink
Ver 3.1 update
Browse files Browse the repository at this point in the history
  • Loading branch information
ChestnutHeng committed Oct 28, 2015
1 parent 3b56665 commit 2763805
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* ver 2.1 添加了完整的进制转换系统

> * Ver 3.0 添加了表达式计算功能
* ver 3.1 在进制转换系统中添加了小数和负数的进制转换



Expand Down
91 changes: 70 additions & 21 deletions numsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <algorithm>
#include "Stack.h"


#ifndef EPS
#define EPS 0.000001
#endif
Expand All @@ -17,55 +16,105 @@ using namespace std;

static char digit[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
static char num_dig[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int dot = 0;
float decimal = 0;
int integer = 0;

string upper(string str){
transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
int convert_to_10(string num,int R1){
int ans = 0;
float convert_to_10(string num,int R1){
float ans = 0;
dot = 0;
decimal = 0;
integer = 0;
bool isPos = true;
bool isDes = false;
int num_size = num.size();

num = upper(num);
char *pha = (char *)num.c_str();
char *old_pha = (char *)num.c_str();
char *pha = old_pha;
if(*pha == '-') {
isPos = false;
num_size--;
pha++;
}
char *p = pha;
int i = 0;
for (p = pha + num.size() - 1; p != pha - 1; --p)

for (p = pha + num_size - 1; p != pha - 1; --p){
if(*p == '.') {
dot = pha + num_size - 1 - p;
isDes = true;
}
}

int i = num_size - 1 - dot;
if(isDes) i--;
for (p = pha;*p != '.' && *p != '\0'; ++p)
{
for (int j = 0; j < 16; ++j)
{
if(*p == digit[j])
integer += num_dig[j] * pow(R1 ,i);
}

i--;
}

if(isDes) p++;
i = -1;
for (;*p != '\0'; ++p)
{
for (int j = 0; j < 16; ++j)
{
if(*p == digit[j])
ans += num_dig[j] * pow(R1 , i);
decimal += ((float)num_dig[j] )* pow(R1 ,i);
}
i++;
i--;
}

ans = integer + decimal;
if(!isPos) ans = -ans;
return ans;
}

string convert(string num,int R1,int R2)
{

int number;
float flo_num;
Stack <char> answer;
number = convert_to_10(num,R1);
cout << "Notes:" << number << "(10)" << endl;
while(number != 0){
answer.push(digit[number % R2]);
number = number / R2;
}
char *str = new char[answer.size()];

flo_num = convert_to_10(num,R1);
cout << "Notes:" << flo_num << "(10)" << endl;

while(integer != 0){
answer.push(digit[integer % R2]);
integer = integer / R2;
}

string str;
for (int i = 0;!answer.empty(); ++i)
{
str[i] = answer.pop();
str.push_back(answer.pop());
}

if(dot > 0){
str.push_back('.');
while(decimal != 0){
decimal = decimal * R2;
str.push_back(digit[(int)decimal]);
decimal -= (int)decimal;
}
}
string op = str;
return op;
return str;
}

void Numsys()
{
int r1,r2;
string former;
string later;

do
{
cout << ">>>Input number and R1:" ;
Expand Down

0 comments on commit 2763805

Please sign in to comment.