跳到正文

个人记录

7.20-晚上河西教室1 --栈进阶--表达式问题

#SUSPENDED#约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年7月20日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 6 分钟

版本

  1. v1当前

课堂笔记

笔记

课堂习题

U445194 栈进阶—3 后缀表达式求值

思路:用栈,将字符转成数字,然后入栈,进行运算,最后输出栈顶。

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
stack<ll>x;
string a;
int main()
{
	while(1)
	{
		cin>>a;
		if(a=="@")break;
		if(a[0]>='0'&&a[0]<='9')
		{
			ll num=stoi(a);
			x.push(num);
		}
		if(a[0]=='+')
		{
			ll num1=x.top();
			x.pop();
			ll num2=x.top();
			x.pop();
			x.push(num2+num1);
		}
		if(a[0]=='-')
		{
			ll num1=x.top();
			x.pop();
			ll num2=x.top();
			x.pop();
			x.push(num2-num1);
		}
		if(a[0]=='*')
		{
			ll num1=x.top();
			x.pop();
			ll num2=x.top();
			x.pop();
			x.push(num2*num1);
		}
		if(a[0]=='/')
		{
			ll num1=x.top();
			x.pop();
			ll num2=x.top();
			x.pop();
			x.push(num2/num1);
		}
	}
	cout<<x.top();
	return 0;
}

U445233 栈进阶—5 表达式求值

思路:用栈,输入数字和字符,进行运算,最后出栈。

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
stack<int>x;
char c;
int a;
int main()
{
    cin>>a;
    x.push(a%10000);
    
    while(cin>>c>>a)
    {
        if(c=='+')x.push(a);
        else
        {
            int num1=x.top();
			x.pop();
			x.push(a*num1%10000);
        }
    }
    int t=0;
    int s=x.size();
	while(s--)
    {
        t+=x.top();
        x.pop();
    }
    cout<<t%10000;
	return 0;
}

U445242 栈进阶—6 中缀转后缀表达式

思路:用栈,进行匹配,还要处理运算符优先级

C++
#include <bits/stdc++.h>
using namespace std;
stack <char> st;
string s;
map<char,int>mp;
int main()
{
	mp['(']=0,mp['+']=1,mp['-']=1,mp['*']=2,mp['/']=2;
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]>='a'&&s[i]<='z'){
			cout<<s[i];
		}else{
			if(s[i]=='('){
				st.push(s[i]);
			}
			else if(s[i]==')'){
				while(st.top()!='('){
					cout<<st.top();
					st.pop();
				}
				st.pop();
			}else {
				while(st.size()!=0 && mp[s[i]]<=mp[st.top()]){
					cout<<st.top();
					st.pop();
					if(st.size()==0){
						break;
					}
				}
				st.push(s[i]);
			}
		}
	}
	while(st.size()>=1){
		cout<<st.top();
		st.pop();
	}
	return 0;
}

P1739 表达式括号匹配

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
stack<int>x;
char c;
int a;
int main()
{
    cin>>a;
    x.push(a%10000);
    
    while(cin>>c>>a)
    {
        if(c=='+')x.push(a);
        else
        {
            int num1=x.top();
			x.pop();
			x.push(a*num1%10000);
        }
    }
    int t=0;
    int s=x.size();
	while(s--)
    {
        t+=x.top();
        x.pop();
    }
    cout<<t%10000;
	return 0;
}

P1981 [NOIP 2013 普及组] 表达式求值

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
stack<int>x;
char c;
int a;
int main()
{
    cin>>a;
    x.push(a%10000);
    
    while(cin>>c>>a)
    {
        if(c=='+')x.push(a);
        else
        {
            int num1=x.top();
			x.pop();
			x.push(a*num1%10000);
        }
    }
    int t=0;
    int s=x.size();
	while(s--)
    {
        t+=x.top();
        x.pop();
    }
    cout<<t%10000;
	return 0;
}
原文