四则运算重写了一遍。。
不调试还是出bug
唉~~
#include <iostream>
#include <vector>
#include <string>
#include<stack>
using namespace std;
double calculate(string s,int& i,char flag)
{
stack<double>st;
while(i<s.size())
{
double num=0;
while(i<s.size()&&isdigit(s[i]))
{
num=num*10+s[i]-'0';
i++;
}
if(s[i]=='(')
{
i++;
num= calculate(s,i,'+');
}
if(flag=='+')
st.push(num);
if(flag=='-')
st.push(-1*num);
if(flag=='*')
{ double temp=st.top()*num;
st.pop();
st.push(temp);}
if(flag=='/')
{ double temp=st.top()/num;
st.pop();
st.push(temp);}
if(s[i]==')')
{
i++;
break;
}
if(i<s.size())
flag = s[i];
i++;
}
double res=0;
while(!st.empty()){
res+=st.top();
st.pop();
}
return res;
}
int main(){
string s;
cin>>s;
stack<double>st;
int i =0;
char flag = '+';
double res=calculate(s,i,flag);;
cout<<res<<endl;
return 0;
}