救救
查看原帖
救救
991851
X_weijifen_swl楼主2024/7/9 11:09
#include<bits/stdc++.h>
using namespace std;
typedef long long lld;
//----------------------------------
vector<char>k;
stack<char>s;
map<char,int>fu;
string c;
int main()
{
	fu['(']=0;
	fu['|']=1;
	fu['&']=2;
	cin>>c;
	int len=c.length()-1;
	int k_len=0;
	cout<<"输入成功"<<endl;
	for(int i=0;i<=len;i++){
		if(c[i]=='0'||c[i]=='1'){
			k.push_back(c[i]-'0');
			k_len++;
		}else{
			if(c[i]=='('){
				s.push(c[i]);
			}else if(c[i]==')'){
				while(s.top()!='(')
				{
					k.push_back(s.top());
					s.pop();
					k_len++;
				}
			}else if(fu[s.top()]>=fu[c[i]]){
				k.push_back(c[i]);
				k.push_back(s.top());
				s.pop();
				k_len+=2;
			}
		}
	}
	cout<<"转后缀成功"<<endl;
	bool x,y;
	int a_cnt,b_cnt;
	a_cnt=b_cnt=0;
	while(!s.empty())s.pop();
	for(int i=1;i<=k_len;i++){
		if(k[i]=='0'||k[i]=='1'){
			s.push(k[i]);
		}
		if(k[i]=='&'){
			x=s.top()-'0';s.pop();
			y=s.top()-'0';s.pop();
			a_cnt+= y==0 ;
			s.push(x&y+'0');	
		}
		if(k[i]=='|'){
			x=s.top()-'0';s.pop();
			y=s.top()-'0';s.pop();
			b_cnt+= y==1 ;
			s.push(x|y+'0');
		}
		//if(k[i]=='!')
	}
	cout<<"计算&统计成功"<<endl;
	cout<<s.top()<<endl;
	cout<<a_cnt<<" "<<b_cnt;
	s.pop();
	cout<<endl<<"输出成功"; 
	return 0;
}

2024/7/9 11:09
加载中...