C++数据结构——顺序栈(基本代码实现与案例)

一:enum关键字

  • 枚举 enum 是一个类型(class),可以保存一组由用户刻画的值。
  • enum 关键字的作用域是全局的,如果在enum A中声明了一个枚举类型 my_enum,则无法在enum B中声明同样的枚举类型,但是如果二者不在一个作用域里则可以声明。
    枚举变量的使用:
enum error_code{
	success, overflow, underflow
};
error_code a;
error_code b = success;

二:顺序栈基本代码

#include<iostream>
using namespace std;

enum error_code{
	success,overflow,underflow
};

class stack{
	public:
		stack();
		bool empty()const;
		bool full()const;
		error_code getTop(int &x)const;
		error_code push(const int x);
		error_code pop();
	private:
		int count;
		int data[10];
};

stack::stack(){
	count = 0;
}

bool stack::empty()const{
	return (count == 0);
}

bool stack::full()const{
	return (count == 10);
}

error_code stack::getTop(int &x)const{
	if(empty()) return underflow;
	x = data[count-1];
	return success;
}

error_code stack::push(const int x){
	if(full()) return overflow;
	data[count] = x;
	count++;
	return success;
}

error_code stack::pop(){
	if(empty()) return underflow;
	count--;
	return success;
}

bool ReferenceError(error_code a){
	if(a == overflow){
     	cout << " overflow! " << endl;
		return false;	
	}  
	if(a == underflow){
		cout<< " underflow " << endl;
		return false;
	}
	return true;
}

int main(){
	stack s;
	int top; //栈顶 
	//入栈
	for(int i = 0;i < 7;i++){
		ReferenceError(s.push(i));
	}
	//出栈 
	for(int i = 0;i < 5;i++){
		ReferenceError(s.pop());
	} 
	//取出栈顶
	ReferenceError(s.getTop(top)); 
	cout << top << endl;
	
	return 0;
}

三:顺序栈的使用

  • 案例一:设计算法将十进制转换为八进制
  • 案例二:符号"{","}","[","]","(",")"应该是互相匹配的,设计算法对以字符串形式读取的表达式S,判断其中的各括号是否是匹配的
  • 案例三:使用栈结构写一个能计算带括号的四则表达式表达式的计算器
#include<iostream>
#include<string>
using namespace std;
#include<typeinfo>

enum error_code{
	success, overflow, underflow
};

template <class T>
class stack{
public:
	stack();
	bool empty()const;
	bool full()const;
	error_code getTop(T& x)const;
	error_code push(const T x);
	error_code pop();
private:
	int count;
	int data[100];
}; 

template <class T>
stack<T>::stack()
{
	count = 0;
}

template <class T>
bool stack<T>::empty()const {
	return count == 0;
}

template <class T>
bool stack<T>::full()const {
	return count == 10;
}

template <class T>
error_code stack<T>::getTop(T& x)const {
	if (empty()) return underflow;
	x = data[count - 1];
	return success;
}

template <class T>
error_code stack<T>::push(const T x) {
	if (full()) return overflow;
	data[count] = x;
	count++;
	return success;
}

template <class T>
error_code stack<T>::pop() {
	if (empty()) return underflow;
	count--;
	return success;
}

template <class T>
bool ReferenceError(error_code a) {
	if (a == overflow) {
		cout << "overflow!" << endl;
		return false;
	}
	if (a == underflow) {
		cout << "underflow!" << endl;
		return false;
	}
	return true;
}

// 十进制转八进制 
void transform()
{
	// 初始化题目条件 
	int num, result = 0;
	cout << "请输入要转化为八进制的十进制数:";
	cin >> num;
	
	// 初始化栈结构 
	stack<int>s;
	int top; 
	
	// 开始运算 
	while(num != 0)
	{
		s.push(num % 8);
		num /= 8;
	}
	while(!s.empty())
	{
		s.getTop(top);
		s.pop();
		result = result * 10 + top; 
	}
	cout << "转成八进制为:" << result << endl;
}

// 检验符号匹配
string symbol()
{
	// 初始化题目条件
	string s;
	cout << "请输入表达式S: ";
	cin >> s;
	int length;
	length = s.length();

	// 初始化栈结构
	stack<char>a;
	char top;
	 
	// 开始运算
	for(int i = 0; i < length; i++)
	{
		if(s[i] == '(' || s[i] == '[' || s[i] == '{')
		    a.push(s[i]);
		if(s[i] == ')')
		{
			a.getTop(top);
		    if(top == '(')
		        a.pop();
		    else
				return "不匹配!!";
		}
		if(s[i] == ']')
		{
			a.getTop(top);
		    if(top == '[')
		        a.pop();
		    else
		    	return "不匹配!!";
		}
		if(s[i] == '}')
		{
			a.getTop(top);
		    if(top == '{')
		        a.pop();
		    else
		    	return "不匹配!!";
		} 
	}
	return "匹配!!";
}

// 计算器 
int getRes(int a, int b, char c) // 计算
{  
	int result;
	switch(c){
		case '+': result = a + b;
		    break;
		case '-': result = a - b;
		    break;
		case '*': result = a * b;
		    break;
		case '/': result = a / b;
		    break;
	}
	return result;
}
int getPri(char a) // 获得优先级 
{
	if(a == ')')
	    return 1; 
	if(a == '+' || a == '-')
	    return 2;
	if(a == '*' || a == '/')
	    return 3;
	if(a == '(')
	    return 4;
	else
		cout << "输入错误!!" << endl;
}
void calculator()
{
	// 初始化题目条件
	string s;
	cout << "请输入计算式:";
	cin >> s;
	int i = 0, k = 1, num1, num2;
	char sign;
	
	// 初始化栈结构
	stack<int>num;
	int top_n;
	stack<char>oper;
	char top_o;
	
	// 开始计算
	if(s[(s.length() - 1)] != '=')
	{
	    cout << "算式请以 '=' 结尾";
		return;	
	} 
	while(s[i] != '='){
		if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '(' || s[i] == ')')
		{
			l:
			if(!oper.empty())
			{
				oper.getTop(top_o);
				if(getPri(s[i]) > getPri(top_o) || top_o == '(')
			        oper.push(s[i]);
				else
				{
				    oper.getTop(top_o);
				    if(top_o != '(')
				    {
						num.getTop(top_n);
				        num1 = top_n;
				        num.pop();
				        num.getTop(top_n);  
				        num.pop();
					    num.push(getRes(top_n, num1, top_o));
					    oper.pop();
					    oper.getTop(top_o);
					    if(top_o == '(')
					    {
						    oper.pop();
						    i++;
							continue;	
						}
					    goto l;  	
					}
			   }
		    }
			if(oper.empty())
		        oper.push(s[i]); 
	        k = 1;
	    }
		else
		{
			if(k == 0)
			{
				num.getTop(top_n);
				num.pop();
			    num.push(top_n * 10 + s[i] - 48);	
			}
			if(k == 1)
			{
			    num.push(s[i] - 48);
			    k = 0;
			}
		}
		i++;
	}
	
	num.getTop(top_n);
	num1 = top_n;
	num.pop();
	num.getTop(top_n);
	oper.getTop(top_o);
    cout << "计算结果为:" << getRes(top_n, num1, top_o) << endl;
}

int main()
{
	// 十进制转八进制 
	transform();
	
    // 检验符号匹配
    cout << symbol() << endl;
    
    // 计算器 
    calculator();

	return 0;
}

更多相关内容大家可以前往我的个人博客浏览:eyes++的个人空间

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值