突然想試試堆疊的寫法,然後出了RE
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
void tranbin(string S);
template<class T>
class Stack{
public:
Stack(){
stack = new T[1];
top = -1;
capacity = 0;
}
bool isempty() const;
void push(const T& item);
void pop();
T &Top() const;
void print();
private:
T*stack;
int top;
int capacity;
};
int main(){
string s;
while(cin >> s){
if(s == "EOF"){
break;
}
tranbin(s);
}
return 0;
}
void tranbin(string S){
int k = S.size();
string str;
int Ar[k] , origint = 0;
for(int i = 0 ; i < k ; i++){
Ar[i] = S[i];
if(Ar[i] < 48 || Ar[i] > 57){
break;
}
}
int p = atoi(S.c_str());
Stack<int> STACK;
while(p >= 2){
if(p == 2){
STACK.push(0);
STACK.push(1);
}
else if(p == 3){
STACK.push(1);
STACK.push(1);
}
else{
STACK.push(p%2);
}
p/=2;
}
STACK.print();
}
template<class T>
void Changesize1D(T * &arr,const int& size,const int& ToSize){
T* tmp = new T[size];
tmp = arr;
arr = new T[ToSize];
for(int i=0; i< size; ++i){
arr[i] = tmp[i];
}
delete[] tmp;
}
template<class T>
inline bool Stack<T>::isempty() const {return top == -1;}
template<class T>
inline T& Stack<T>::Top() const{
return stack[top];
}
template<class T>
void Stack<T>::push(const T& item){
if(top == capacity-1){
Changesize1D(stack,capacity,capacity*2);
capacity *= 2;
}
stack[++top] = item;
}
template<class T>
void Stack<T>::pop(){
stack[top--].~T();
}
template<class T>
void Stack<T>::print(){
for(int i = top ; i >= 0 ; i--)
cout << stack[i];
cout << endl;
}