栈-中缀变成后缀-C++实现

#include <iostream>
#include<ctype.h>
#include "Stack.h"
using namespace std;
int value(char ch) {
    if (ch == '+' || ch == '-') {
        return 1;
    }
    else if (ch == '*' || ch == '/') {
        return 2;
    }
    else if (ch == '(') {
        return 0;//'('的优先级要比/*-+的低,保证/*-+在'('上面
    }
    else if (ch == ')') {
        return -1;//')'的优先级要比'(','+','-','*','/'的低,保证弹出其余所有符号!!
    }
}
void test() {
    const char* arr = "a+b*(c+d)+e*g/h-i";//abcd+*+eg*h/+i-
    //const char* arr = "a+b*(c+d)";
    //const char* arr = "a+b*c+(d*e+f)*g";
    Stack<char> s(strlen(arr) + 1);
    for (int i = 0; arr[i] != '\0'; i++) {
        if (isalpha(arr[i])) {
            cout << arr[i];
        }
        else if (arr[i] == '(' && !s.isFull()) {
            s.push(arr[i]);
        }
        else if (arr[i] == ')') {
            while (!s.isEmpty()) {
                if (s.top() == '(') {
                    s.pop();//只弹,不输出
                }
                else {
                    cout << s.pop();//弹出并输出
                }
                continue;
            }
        }//下面是遇到操作符的情况
        else {
            if (s.isEmpty()) {
                s.push(arr[i]);//空栈就直接压栈就行
                continue;
            }
            else {
                while (!s.isEmpty()&&s.top()!='('&&s.top()!=')'
                    &&value(s.top()) >= value(arr[i])) {//相同及更优先级的都会被弹出
                    cout << s.pop();//弹出并输出
                }
                s.push(arr[i]);//弹出完毕之后就压栈
            }
        }
    }
    while (!s.isEmpty()) {
        if (s.top() == '(' || s.top() == ')'){
            s.pop();//控制字符输出,不输出'('和')'
        }
        else {
            cout << s.pop();
        }
    }
}
int main()
{
    test();
    return 0;
}

下面是栈的模板,都类似

#pragma once
#include<iostream>
using namespace std;
template<typename T>
class Stack
{
private:
	typedef T* PtrT;
	typedef unsigned int size_;
	const int TOS = -1;//top of stack
	PtrT  m_array;
	size_ m_size;
	int   m_pos;
public:
	Stack(size_ capacity);
	bool  push(const T&);
	T&    pop();
	T&    top()const;
	bool  isFull() const {return m_pos == m_size - 1;}
	bool  isEmpty()const {return m_pos == TOS;}
	size_ size()  const {return m_size;}
	void  clear();
	static void Error(const char* msg) {
		cerr << msg << endl;
	}
	static void FatalError(const char* msg,int signal=1) {
		cerr << msg << endl;
		exit(signal);
	}
};
template<typename T>
Stack<T>::Stack(size_ capacity){
	if ((m_array = new T[capacity]) == nullptr) {
		FatalError("Out of space!!");
	}
	this->m_pos = TOS;
	m_size = capacity;
}
template<typename T>
bool Stack<T>::push(const T& o) {
	if (!isFull()) {
		m_array[++m_pos] = o;
		return true;
	}
	else {
		Error("The Stack is full!!");
		return false;
	}
}
template<typename T>
T& Stack<T>::pop() {
	if (!isEmpty()) {
		return m_array[m_pos--];
	}
	else {
		Error("Can't pop the empty stack!!");
	}
}
template<typename T>
T& Stack<T>::top() const{
	if (!isEmpty()) {
		return m_array[m_pos];
	}
	else {
		Error("The stack is empty!!");
	}
}
template<typename T>
void Stack<T>::clear() {
	delete m_array;
	m_pos = TOS;
	m_size = 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿维的博客日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值