-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb85ff5
commit 46eab24
Showing
3 changed files
with
147 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
|
||
|
||
> * Ver 2.0 重构了部分方法和文件结构 | ||
* ver 2.1 添加了完整的进制转换系统 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#ifndef STACK_H_ | ||
#define STACK_H_ | ||
|
||
#define DEFAULT_CAPACITY 3 | ||
typedef int Rank; | ||
|
||
template <typename T> | ||
class Stack | ||
{ | ||
private: | ||
T *_elem; | ||
Rank _size; | ||
Rank _capacity; | ||
public: | ||
Stack(int n = 0,T ele = 0){ | ||
_elem = new T[_capacity = DEFAULT_CAPACITY << 1]; | ||
for(_size = 0;_size < n;_size++) | ||
_elem[_size] = ele; | ||
expand(); | ||
}; | ||
|
||
~Stack(){delete [] _elem;}; | ||
|
||
void expand(); | ||
void shrink(); | ||
bool empty(){return (_size == 0);} | ||
Rank size(){return _size;} | ||
void push(T const&e); | ||
T pop(); | ||
T & top(); | ||
|
||
}; | ||
|
||
|
||
template <typename T> | ||
void Stack<T>::expand(){ | ||
if(_size < _capacity) return; | ||
if(_capacity < DEFAULT_CAPACITY) _capacity = DEFAULT_CAPACITY; | ||
T *oldelem = _elem; | ||
_elem = new T[_capacity <<= 1]; | ||
for(int i = 0;i < _size; ++i){ | ||
_elem[i] = oldelem[i]; | ||
} | ||
delete [] oldelem; | ||
} | ||
|
||
template <typename T> | ||
void Stack<T>::shrink(){ | ||
if(_capacity < DEFAULT_CAPACITY << 1) return; | ||
if(_size << 2 > _capacity) return; | ||
T *oldelem = _elem; | ||
_elem = new T[_capacity >>= 1]; | ||
for(int i = 0;i < _size; ++i){ | ||
_elem[i] = oldelem[i]; | ||
} | ||
delete [] oldelem; | ||
|
||
} | ||
|
||
template <typename T> | ||
void Stack<T>::push(T const&e){ | ||
_elem[_size++] = e; | ||
} | ||
|
||
template <typename T> | ||
T Stack<T>::pop(){ | ||
shrink(); | ||
return _elem[_size-- - 1]; | ||
} | ||
|
||
template <typename T> | ||
T & Stack<T>::top(){ | ||
return _elem[_size - 1]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,94 @@ | ||
#include <cmath> | ||
#include <string> | ||
#include <iostream> | ||
#include <cstring> | ||
#include <cstdio> | ||
#include <cstdlib> | ||
#include <cmath> | ||
#include <algorithm> | ||
#include "Stack.h" | ||
|
||
|
||
#ifndef EPS | ||
#define EPS 0.0001 | ||
#define EPS 0.000001 | ||
#endif | ||
|
||
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}; | ||
|
||
double convert_to_R(double num,double R) | ||
{ | ||
int num_int = (int)floor(num); | ||
double num_flo = num - num_int; | ||
double ans; | ||
for(int j = 0;num_int != 0;++j) | ||
{ | ||
ans = ans + pow(10,j) * floor(fmod(num_int,R)); | ||
num_int = (int) (num_int / R); | ||
//if (num_int == 0) break; | ||
} | ||
double num_flo_flo; | ||
for(int j = 1;num_flo_flo < EPS;++j) | ||
{ | ||
ans += pow(10,-j)*(floor(num_flo*R)); | ||
num_flo *= R; | ||
num_flo_flo = num_flo - floor(num_flo); | ||
//if (num_int == 0) break;; | ||
} | ||
return ans; | ||
|
||
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; | ||
num = upper(num); | ||
char *pha = (char *)num.c_str(); | ||
char *p = pha; | ||
int i = 0; | ||
for (p = pha + num.size() - 1; p != pha - 1; --p) | ||
{ | ||
for (int j = 0; j < 16; ++j) | ||
{ | ||
if(*p == digit[j]) | ||
ans += num_dig[j] * pow(R1 , i); | ||
} | ||
i++; | ||
} | ||
return ans; | ||
} | ||
|
||
double numconvert(double num,double R1,double R2) | ||
string convert(string num,int R1,int R2) | ||
{ | ||
if (R1 == 10) return convert_to_R(num,R2); | ||
return 0; //Coding | ||
|
||
int number; | ||
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()]; | ||
for (int i = 0;!answer.empty(); ++i) | ||
{ | ||
str[i] = answer.pop(); | ||
} | ||
string op = str; | ||
return op; | ||
} | ||
|
||
void Numsys() | ||
{ | ||
double former,later,r1,r2; | ||
int r1,r2; | ||
string former; | ||
string later; | ||
|
||
do | ||
{ | ||
cout << "Input number and R1:" ; | ||
cin >> former; | ||
if (former == 0) break; | ||
cin >> former; | ||
if(former == "q") break; | ||
cin >> r1; | ||
if (r1 == 0) { | ||
cout << "R1 shouldn't be 0." << endl; | ||
continue; | ||
} | ||
cout << "To R2:"; | ||
cin >> r2; | ||
later = numconvert(former,r1,r2); | ||
cout << "The anwser is:" << later << "(" << r2 << ")" << endl; | ||
cout << "0 to quit." << endl; | ||
}while (former != 0); | ||
if (r2 == 0) { | ||
cout << "R2 shouldn't be 0." << endl; | ||
continue; | ||
} | ||
else if(r2 == 10){ | ||
cout << "The answer is:" << convert_to_10(former,r1) << "(" << r2 << ")" << endl; | ||
} | ||
else { | ||
later = convert(former,r1,r2); | ||
cout << "The answer is:" << later << "(" << r2 << ")" << endl; | ||
} | ||
cout << "q to quit." << endl; | ||
}while (former != "q"); | ||
} |