洛谷 P1449 后缀表达式(栈 STL stack)

菜鸟生成记(54)

首先了解一下后缀表达式这里–>(大佬博客)很详细;

通过这一题又学到一个知识点–后缀表达式

后缀表达式理解后这一题就简单多了,STL大法暴力模拟;

#include <iostream>
#include<vector>
#include<string>
#include <stack>
#include <algorithm>
using namespace std;
int fun(string s)//判断纯数字 
{
	for(int i=0;i<s.length();i++)
	if(!isdigit(s[i]))//isdigit判断数字字符 
	return 0;
	return 1;
}
int fun1(string s)//判断纯符号串 
{
	for(int i=0;i<s.length();i++)
	if(isdigit(s[i]))//isdigit判断数字字符 
	return 0;
	return 1;
}
void f(string &num1,string &str,string t)//将符号串和数字串分开 
{//例如:-*7 分成-* 和 7 
	int i=0;
	for(i=0;i<t.length();i++)
	if(isdigit(t[i]))//遇到第一个数字字符结束 
	break;
	str=t.substr(0,i);//截取符号串 (0开始截取i个字符)
	num1=t.substr(i);//截取数字串(i开始截取到尾部)
	//两者都为引用类型,回传到主函数 
}
/*
10.28.30./*7.-@
*/
int main()//
{
	string s1,t,num1,str;
	int x,y,sum,num=0;
	vector<string>s;
	stack<int>st;//stl栈 
	cin>>s1;
	for(int i=0;i<s1.length();i++)//将s1串分割 
	{
		/*
		10.28.30./*7.-@分成
		10
		28
		30
		/*
		7
		- 
		*/
		if(s1[i]!='.'&&s1[i]!='@')
		t.push_back(s1[i]);//非分割和结束符,t串尾部入栈 
		else
		{
			if(fun(t))//纯符号串 
			{
				s.push_back(t);
			}
			else if(fun1(t))//纯数字串 
			{
				s.push_back(t);
			}
			else if(!fun(t)&&!fun1(t))//两者都有的杂串 
			{
				f(num1,str,t);//符号与数字分离 
				s.push_back(str);//符号串s容器尾部入栈 
				s.push_back(num1);//数字串s容器尾部入栈 	
			}	
			t.clear();//清空t串 
		}
	}
	for(int i=0;i<s.size();i++)
	{
		num=0;
		if(fun(s[i]))//数字 
		{//转化为数字(int) 
			for(int j=0;j<=s[i].length()-1;j++)
			{
				num=num*10+(s[i][j]-'0');//
			}
			st.push(num);//尾部入栈 
		}
		else//符号 
		{//一个符号串中可能有多个符号,
		//循环 
			for(int j=0;j<s[i].size();j++)
			{
				y=st.top(),st.pop();//出栈 
				x=st.top(),st.pop();//出栈 
				switch(s[i][j])
				{
					case '+':
						sum=x+y;
					break;
					case '-':
						sum=x-y;
					break;
					case '*':
						sum=x*y;
					break;
					case '/':
						sum=x/y;
					break;
				}
				st.push(sum);//x,y的运算结果入栈 
			}
		}
	}
	cout<<st.top();//最后的栈顶元素就是后缀表达式的结果 
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用C++ STL中的来实现中缀表达式转后缀表达式,可以按照以下步骤进行: 1. 创建一个空的,用于存储运算符。 2. 遍历中缀表达式的每个字符: - 如果是操作数(数字),直接将其输出到后缀表达式。 - 如果是左括号 '(',将其推入中。 - 如果是右括号 ')',则将中的运算符依次弹出并输出到后缀表达式,直到遇到左括号 '('。注意:左括号 '('不输出到后缀表达式,也不入。 - 如果是运算符 (+, -, *, /, ^),则: - 如果为空,或者顶元素为左括号 '(',则将运算符推入中。 - 否则,比较当前运算符与顶运算符的优先级。如果当前运算符的优先级小于或等于顶运算符的优先级,则将顶运算符弹出并输出到后缀表达式,直到为空或者顶运算符为左括号 '(',然后将当前运算符推入中。 3. 遍历完中缀表达式后,如果中还有运算符,依次弹出并输出到后缀表达式。 下面是一个用C++ STL中的实现中缀转后缀的示例代码: ```cpp #include <iostream> #include <stack> #include <string> int getPriority(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else if (op == '^') return 3; else return 0; } std::string infixToPostfix(const std::string& infix) { std::stack<char> opStack; std::string postfix; for (char ch : infix) { if (isdigit(ch)) { postfix += ch; } else if (ch == '(') { opStack.push(ch); } else if (ch == ')') { while (!opStack.empty() && opStack.top() != '(') { postfix += opStack.top(); opStack.pop(); } if (!opStack.empty() && opStack.top() == '(') { opStack.pop(); } } else { while (!opStack.empty() && getPriority(ch) <= getPriority(opStack.top())) { postfix += opStack.top(); opStack.pop(); } opStack.push(ch); } } while (!opStack.empty()) { postfix += opStack.top(); opStack.pop(); } return postfix; } int main() { std::string infix = "5 + 2 * (3 - 1)"; std::string postfix = infixToPostfix(infix); std::cout << "Infix expression: " << infix << std::endl; std::cout << "Postfix expression: " << postfix << std::endl; return 0; } ``` 在上述示例中,我们定义了一个`getPriority`函数来获取运算符的优先级。`infixToPostfix`函数使用一个循环来遍历中缀表达式的每个字符,并根据字符的类型执行相应的操作。最后,我们通过调用`infixToPostfix`函数将中缀表达式转换为后缀表达式,并输出结果。 希望这个示例能帮助你理解如何使用C++ STL中的来实现中缀表达式转后缀表达式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值