五个点全RE,求调!
查看原帖
五个点全RE,求调!
1043466
wanghaolin2013楼主2024/7/19 08:28
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
using namespace std;

int v[50000];
stack<int> a;
stack<char> b;
void jisuan(int op) {
	int c = a.top();
	a.pop();
	int d = a.top();
	a.pop();
	if (op == '+') {
		a.push(c + d);
	}
	if (op == '-') {
		a.push(c - d);
	}
	if (op == '*') {
		a.push(c * d);
	}
	if (op == '/') {
		a.push(c / d);
	}
	if (op == '^') {
		a.push(pow(c, d));
	}
	return;
}
int main() {
	v['+'] = 1;
	v['-'] = 1;
	v['*'] = 2;
	v['/'] = 2;
	v['^'] = 3;
	string s;
	cin >> s;
	//	else{
	//		for(int i=0;i<s.size();i++){
	//			if()
	//		}
	//	}
	string d = "";
	for (int i = 0; i < s.size(); i++) {
		if (s[i] >= '0' && s[i] <= '9') {
			a.push(s[i] - '0');
		} else {
			if (s[i] == '(') {
				b.push('(');
			}
			if (s[i] == ')') {
				while (1) {
					if (b.top() == ')') {
						b.pop();
						break;
					}
					jisuan(b.top());
				}
			}
			if (s[i] != '(' && s[i] != ')') {
				while (1) {
					if (v[b.top()] < v[s[i]]) {
						break;
					}
					jisuan(b.top());
				}
				b.push(s[i]);
			}
		}
	}
	while (!b.empty()) {
		if (b.top() != '(' && b.top() != ')') {
			jisuan(b.top());
			b.pop();
		}
	}
	cout << a.top();
	return 0;
}
2024/7/19 08:28
加载中...