跳到正文

个人记录

7.21

Owen0130约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年7月21日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟

版本

  1. v1当前

后缀表达式求值

#include<bits/stdc++.h>

using namespace std;

stack<int> a;

int k,ss;

string s[1005];

int main(){

C++
while(cin>>s[++k]){//输入
    if(s[k]=="@") break;
};
for(int i=1;i<=k;i++){
	if(s[i]=="/"){
        int x=a.top();
        a.pop();//出栈
        int y=a.top();
        a.pop();
        a.push(y/x);
    }else if(s[i]=="*"){
        int x=a.top();
        a.pop();
        int y=a.top();
        a.pop();
        a.push(y*x);
    }else if(s[i]=="+"){
        int x=a.top();
        a.pop();
        int y=a.top();
        a.pop();
        a.push(y+x);
    }else if(s[i]=="-"){
        int x=a.top();
        a.pop();
        int y=a.top();
        a.pop();
        a.push(y-x);
    }else{
        if(s[i]=="@") break;
        a.push(stoi(s[i]));
    }
}
cout<<a.top();//输出
return 0;

}

表达式求值

#include<bits/stdc++.h>

using namespace std;

int a[1000005],k=2,s,m;

int main(){

C++
char x;
cin>>a[1];
while(cin>>x>>m){//输入
	a[k]=m%10000;//输出后四位
	if(x=='*'){
		a[k-1]=a[k]*a[k-1]%10000;
	}else{
		k++;
	}
}
for(int i=1;i<k;i++){
	s+=a[i];
    s=s%10000;
}
cout<<s%10000;
return 0;

}

中缀v转后缀表达式

#include<bits/stdc++.h>

using namespace std;

string s;

stack<char> a;

int yx(char o){

代码
if(a.top()=='*'||a.top()=='/'){
    return 1;
}else{
    if(o=='+'||o=='-') return 1;
    return 0;
}

}

int main(){

C++
cin>>s;
for(int i=0;i<s.size();i++){
	if(s[i]>='a'&&s[i]<='z'){
		cout<<s[i];
	}else if(s[i]==')'){
		while(a.size()&&a.top()!='('){
			cout<<a.top();
			a.pop();
		}
        if(a.size()>0&&a.top()=='(') a.pop();
	}else{
        while(a.size()>0){
            if(a.size()&&s[i]!='('&&a.top()!='('&&yx(s[i])){
                cout<<a.top();
                a.pop();
            }else{
                break;
            }
        }
		a.push(s[i]);
	}
}
while(a.size()){
	cout<<a.top();
	a.pop();
}
return 0;

}

原文