20pts求助
查看原帖
20pts求助
965238
Fwio_楼主2023/6/8 08:49

#1,2点AC

其他全WA

#include<iostream>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#include<stack>
#include<cmath>
using namespace std; 
string read(){
	string x;
	char ch = getchar();
	while(ch == '\n' || ch == '\r' || ch == ' ') ch = getchar();
	while(ch != '\n' && ch != '\r' && ch != EOF){
		if(ch != ' ') x += ch;
		ch = getchar();
	}
	return x;
}
unordered_map<char , int> priority{{'+' , 1} , {'-' , 1} , {'*' , 2} , {'^' , 3}};
string str;
string x[30];
int n;
stack<long long> num;
stack<char> op;
string ans;
void eval(){
	long long y = num.top(); num.pop();
	long long x = num.top(); num.pop();
	char z = op.top(); op.pop();
	if(z == '+') num.push(x + y);
	else if(z == '-') num.push(x - y);
	else if(z == '*') num.push(x * y);
	else num.push((int)pow(x , y));
}
int val(string str){
	while(!num.empty()) num.pop();
	while(!op.empty()) op.pop();
	for(int i = 0;i < str.size();i++){
		if(str[i] == 'a') num.push(17LL);
		else if(str[i] == ' ') continue;
		else if(str[i] >= '0' && str[i] <= '9'){
			int j = i;
			long long v = 0;
			while(j < str.size() && str[j] >= '0' && str[j] <= '9') v = v * 10 + str[j++] - '0';
			num.push(v);
			i = j - 1;
		}
		else if(str[i] == '(') op.push(str[i]);
		else if(str[i] == ')'){
			while(!op.empty() && op.top() != '(') eval();
			op.pop();
		}
		else{
			while(!op.empty() && priority[op.top()] >= priority[str[i]]) eval();
			op.push(str[i]);
		}
	}
	while(!op.empty()) eval();
	return num.top();
}
int main(){
	str = read();
	long long k = val(str);
	cin >> n;
	for(int i = 1;i <= n;i++){
		x[i] = read();
		cout << val(x[i]) << endl;
	}
	cout << ans << endl;
	return 0;
}
2023/6/8 08:49
加载中...