用一个结构体type0是字符,type1是数字,然后建一个vector。把所有字符数字数字化(比如‘1’,‘2’,‘3’变成123),然后用栈每找到一个括号对就solve那一段。solve直接用insert和erase,把那一段变成数字。先扫一遍^,然后/*然后+-,同样找到一个符号就把左右和符号本身erase和insert成一个数字。
注意细节处理比如负号,EOF,多余括号。好久没打这种暴力模拟了。。。
C++ 代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<stack>
//#pragma G++ optimize(2)
using namespace std;
const int maxn = 1e6+5;
struct node{
bool Type;//0 char 1 int
int num;
char ch;
void print()
{
if(Type==0) printf("%c", ch);
else printf("%d", num);
}
};
vector <node> s;
stack <char> stk1;
stack <int> stk2;
node addnode(bool Type, int num, char ch)
{
node tmp;
tmp.Type = Type;
tmp.num = num;
tmp.ch = ch;
return tmp;
}
inline int poww(int a, int b)//快速幂
{
int ans = 1;
while(b)
{
if(b&1)
{
ans *= a;
--b;
}
else
{
a *= a;
b >>= 1;
}
}
return ans;
}
inline void Solve(int l, int r)//把l和r之间的表达式合并为一个数
{
for(register int i=l; i<=r; ++i)
{
if(s[i].ch == '^')
{
int tmp1 = s[i-1].num;
int tmp2 = s[i+1].num;
s.erase(s.begin()+i-1, s.begin()+i+2);
s.insert(s.begin()+i-1, addnode(1, poww(tmp1, tmp2), 0));
r-=2;
i-=1;
}
}
for(register int i=l; i<=r; ++i)
{
if(s[i].ch == '*')
{
int tmp1 = s[i-1].num;
int tmp2 = s[i+1].num;
s.erase(s.begin()+i-1, s.begin()+i+2);
s.insert(s.begin()+i-1, addnode(1, tmp1 * tmp2, 0));
r-=2;
i-=1;
}
else if(s[i].ch == '/')
{
int tmp1 = s[i-1].num;
int tmp2 = s[i+1].num;
s.erase(s.begin()+i-1, s.begin()+i+2);
s.insert(s.begin()+i-1, addnode(1, tmp1 / tmp2, 0));
r-=2;
i-=1;
}
}
for(register int i=l; i<=r; ++i)
{
if(s[i].ch == '+')
{
int tmp1 = s[i-1].num;
int tmp2 = s[i+1].num;
s.erase(s.begin()+i-1, s.begin()+i+2);
s.insert(s.begin()+i-1, addnode(1, tmp1 + tmp2, 0));
r-=2;
i-=1;
}
else if(s[i].ch == '-')
{
int tmp1 = s[i-1].num;
int tmp2 = s[i+1].num;
s.erase(s.begin()+i-1, s.begin()+i+2);
s.insert(s.begin()+i-1, addnode(1, tmp1 - tmp2, 0));
r-=2;
i-=1;
}
}
}
int main()
{
// freopen("input.txt", "r", stdin);
bool isread = 0;
char ch;
bool iseof = 0;
//读入
char lstch='(';
while(1)
{
if(iseof) break;
if(!isread)
{
if(scanf("%c", &ch)==EOF) break;
}
int num=0;
if(ch=='\n' || ch=='\r') break;
if(ch=='-' && lstch=='(')
{
s.push_back(addnode(1, 0, 0));
s.push_back(addnode(0, 0, '-'));
lstch = ch;
continue;
}
lstch = ch;
if(isdigit(ch))
{
num = num * 10 + ch - '0';
isread = 1;
while(isdigit(ch))
{
if(scanf("%c", &ch)==EOF)
{
iseof = 1;
break;
}
if(!isdigit(ch)) break;
lstch = ch;
num = num * 10 + ch - '0';
}
s.push_back(addnode(1, num, 0));
}
else
{
isread = 0;
s.push_back(addnode(0, 0, ch));
}
}
//用栈找括号并solve,容易发现找到的括号里面没有括号,stk2记录左括号在vector中的位置
for(register unsigned int i=0; i<s.size(); ++i)
{
if(s[i].ch == '(')
{
stk1.push('(');
stk2.push(i);
}
else if(s[i].ch == ')')
{
if(!stk1.empty() && stk1.top()=='(')
{
Solve(stk2.top()+1, i-1);
s.erase(s.begin()+stk2.top());
s.erase(s.begin()+stk2.top()+1);
i = stk2.top();
stk1.pop(); stk2.pop();
}
else
{
s.erase(s.begin()+i);//多余右括号删掉
--i;
}
}
}
//处理多余左括号
while(!stk1.empty())
{
s.erase(s.begin()+stk2.top());
stk1.pop();
stk2.pop();
}
Solve(0, s.size()-1);
printf("%d", s[0].num);
return 0;
}