【資料結構】棧的應用--數制轉換(c++)

zhaoyaqian552發表於2015-06-01

標頭檔案:


#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

template<class Type>
class SeqStack
{
public:
	SeqStack(size_t sz = INIT_SZ);
	~SeqStack();
public:
	bool empty()const;
	bool full()const;
	void show()const;
	bool push(const Type &x);
	bool pop();
	void gettop(Type &x);
	int length()const;
	void clear();
	void destory();
	void quit_system(Type &x);
private:
	enum{ INIT_SZ = 64 };
	Type *base;
	int capacity;
	int top;
};

template<class Type>
SeqStack<Type>::SeqStack(size_t sz = INIT_SZ)
{
	capacity = sz > INIT_SZ ? sz : INIT_SZ;
	base = new Type[capacity];
	assert(base != NULL);
	top = 0;
}

template<class Type>
SeqStack<Type>::~SeqStack()
{
	destory();
}

// 判斷棧是否滿了
template<class Type>
bool SeqStack<Type>::full()const
{
	return (top >= capacity);
}

// 判斷是否為空棧
template<class Type>
bool SeqStack<Type>::empty()const
{
	return (top == 0);
}

// 顯示
template<class Type>
void SeqStack<Type>::show()const
{
	if (top == 0)
	{
		cout << "the stack is empty!" << endl;
		return;
	}
	for (int i = top - 1; i >= 0; --i)
	{
		cout << base[i] << endl;
	}
}

// 入棧
template<class Type>
bool SeqStack<Type>::push(const Type &x)
{
	if (full())
	{
		cout << "the stack is full,can not enter!" << endl;
		return false;
	}
	else
	{
		base[top] = x;
		top++;
		return true;
	}
}

// 出棧
template<class Type>
bool SeqStack<Type>::pop()
{
	if (empty())
	{
		cout << "the stack is empty,can not pop!" << endl;
		return false;
	}
	else
	{
		top--;
		return true;
	}
}

// 獲得棧頂元素
template<class Type>
void SeqStack<Type>::gettop(Type &x)
{
	x = base[top - 1];
}

// 求棧長度
template<class Type>
int SeqStack<Type>::length()const
{
	return top;
}

// 清空棧
template<class Type>
void SeqStack<Type>::clear()
{
	top = 0;
}

// 摧毀棧
template<class Type>
void SeqStack<Type>::destory()
{
	delete[]base;
	base = NULL;
	capacity = top = 0;
}

// 退出系統
template<class Type>
void SeqStack<Type>::quit_system(Type &x)
{
	x = 0;
}


主函式:


#include "ConNum.h"

int main()
{
	SeqStack<int> mystack;
	int input = 1;
	int value;
	int rem;  //餘數
	while (input)
	{
		cout << "***********************************************************" << endl;
		cout << "*  [1] decimal to binary         [2] decimal to octonary  *" << endl;
		cout << "*  [3] decimal to hexadecimal    [4] quit system          *" << endl;
		cout << "***********************************************************" << endl;
		cout << "please choose :";
		cin >> input;
		switch (input)
		{
			case 1:
				cout << "please enter the decimal number:";
				cin >> value;
				mystack.push('#');
				while (value != 0)
				{
					rem = value % 2;
					mystack.push(rem);
					value = value / 2;
				}
				while (mystack.gettop(rem),rem != '#')
				{
					cout << rem;
					mystack.pop();
				}
				cout << endl;
				break;
			case 2:
				cout << "please enter the decimal number:";
				cin >> value;
				mystack.push('#');
				while (value != 0)
				{
					rem = value % 8;
					mystack.push(rem);
					value = value / 8;
				}
				while (mystack.gettop(rem), rem != '#')
				{
					cout << rem;
					mystack.pop();
				}
				cout << endl;
				break;
			case 3:
				cout << "please enter the decimal number:";
				cin >> value;
				mystack.push('#');
				while (value != 0)
				{
					rem = value % 16;
					mystack.push(rem);
					value = value / 16;
				}
				while (mystack.gettop(rem), rem != '#')
				{
					if (rem > 9)
					{
						rem = 'A' + (rem - 10);
						cout << (char)rem;
					}
					else
					{
						cout << rem;
					}
					mystack.pop();
				}
				cout << endl;
				break;
			case 4:
				mystack.quit_system(input);
				break;
			default:
				break;
		}
	}
	return 0;
}



相關文章