想问一下第一个点RE是为什么啊
查看原帖
想问一下第一个点RE是为什么啊
263109
Lc6206楼主2024/9/26 11:24
#include<iostream>
#include<cmath>
using namespace std;

template<class T>
class Stack {
	T* data;
	int Maxsize;
	int head;
public:
	Stack(int ms);
	~Stack();
	bool empty();
	bool full();
	bool push(const T& x);
	void pop();
	T top();
	int size();
};
template<class T>
Stack<T>::Stack(int ms)
{
	data = new T[ms];
	head = -1;
	Maxsize = ms;
}
template<class T>
Stack<T>::~Stack()
{
	delete[]data;
}
template<class T>
bool Stack<T>::empty()
{
	if (head == -1)
		return true;
	return false;
}
template<class T>
bool Stack<T>::full()
{
	return head == Maxsize - 1;
}
template<class T>
bool Stack<T>::push(const T& x)
{
	if (head >= Maxsize - 1)
		return 0;
	data[++head] = x;
	return 1;
}
template<class T>
void Stack<T>::pop()
{
	head--;
}
template<class T>
T Stack<T>::top()
{
	return data[head];
}
template<class T>
int Stack<T>::size()
{
	return head + 1;
}
/**false为后面的优先级大于前面的,可以不计算
true 为后面的优先级小于等于前面的前面的需要进行计算了
优先级)>^>/*>+->(
**/
bool compare(char front, char after)
{
	if (after == '^')
		return false;
	if (((front == '+' || front == '-') && (after == '*' || after == '/')) || (front == '('))
		return false;
	return true;
}

void opera(Stack<int>& s, char op)
{
	int k = s.top();
	s.pop();
	switch (op)
	{
	case '(':
		s.push(k);
		break;
	case '+':
		k += s.top();
		s.pop();
		s.push(k);
		break;
	case '-':
		k = s.top() - k;
		s.pop();
		s.push(k);
		break;
	case '*':
		k *= s.top();
		s.pop();
		s.push(k);
		break;
	case '/':
		k = s.top() / k;
		s.pop();
		s.push(k);
		break;
	case '^':
		k = pow(s.top(), k);
		s.pop();
		s.push(k);
		break;
	}
}
int main()
{
	char s[1000];
	cin >> s;
	Stack<int> qnum(100);
	Stack<char> qop(100);
	int i = 0, left = 0, right = 0, sum = 0;
	bool negative = 0, numflag = 0;
	while (s[i] != '\0')
	{
		if (s[i] == '-' && (i == 0 || (s[i-1] == '*' || s[i-1] == '/' || s[i-1] == '+' || s[i-1] == '-' || s[i-1] == '^')))
		{
			negative = 1;
			i++;
			continue;
		}
		if (s[i] >= '0' && s[i] <= '9')
		{
			sum = sum * 10 + s[i] - '0';
			numflag = 1;
		}
		else
		{
			if (numflag)
			{
				if (negative)
				{
					qnum.push(-sum);
					cout<<sum;	
				}
				
				else
					qnum.push(sum);
				numflag = 0;
				negative = 0;
				sum = 0;
			}
		}
		if (s[i] == '*' || s[i] == '/' || s[i] == '+' || s[i] == '-' || s[i] == '^')
		{
			if (qop.empty())
				qop.push(s[i]);
			else
			{
				while (!qop.empty() && compare(qop.top(), s[i]))
				{
					opera(qnum, qop.top());
					qop.pop();
				}
				qop.push(s[i]);
			}
		}
		if (s[i] == '(')
		{
			qop.push(s[i]);
			right++;
		}
		if (s[i] == ')' && right)
		{
			while (qop.top() != '('&&!qop.empty())
			{
				opera(qnum, qop.top());
				qop.pop();
			}
			qop.pop();
			right--;
		}
		i++;
	}
	if (numflag)
	{
		if (negative)
		{
			qnum.push(-sum);
		}
		else
			qnum.push(sum);
	}
	while (!qop.empty())
	{
		opera(qnum, qop.top());
		qop.pop();
	}
	cout << qnum.top();
	return 0;
}

多余括号,负数情况都能出正确结果,RE了五六次了,求大佬指点o(╥﹏╥)o

2024/9/26 11:24
加载中...