支持sin cos ln 的操作 只能是正数 然后其他的测试数据没测试 只在leetcode上提交ac了 leetcode224 basic calculator
class Solution {
public:
int transfer(char c)
{
if(c=='+'||c=='-')
return 1;
if(c=='*'||c=='/')
return 2;
if(c=='^')
return 3;
if(c=='s'||c=='c'||c=='g'||c=='n')
return 4;
if(c=='(')
return 5;
return 0;
}
void push_in(stack<char> &op_s,vector<string> &res,char c)
{
if(c=='('||op_s.empty())
{
op_s.push(c);
return;
}
if(c==')')
{
while(op_s.top()!='(')
{
string k;
k+=op_s.top();
res.push_back(k);
op_s.pop();
}
op_s.pop();
return;
}
if(transfer(c)>transfer(op_s.top())||op_s.top()=='(')
{
op_s.push(c);
return;
}
if(transfer(c)<=transfer(op_s.top()))
{
while(transfer(c)<=transfer(op_s.top())&&transfer(op_s.top())!=5)
{
string k;
k+=op_s.top();
res.push_back(k);
op_s.pop();
if(op_s.empty())
break;
}
op_s.push(c);
return ;
}
}
void show(stack<char> s)
{
cout<<"now stack is: ";
while(!s.empty())
{
cout<<s.top()<<' ';
s.pop();
}
cout<<endl;
}
vector<string> calculate1(string str)
{
stack<char> operator_s;
vector<string> res;
int i=-1;
while(str[++i]!='=')
{
if(str[i]>='0'&&str[i]<='9')
{
string num; num+=str[i];
i++;
while(str[i]=='.'||(str[i]>='0'&&str[i]<='9'))
{
num+=str[i];
i++;
}
res.push_back(num);
i--;
continue;
}
else
{
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^'||str[i]=='('||str[i]==')')
{
//show(operator_s);
push_in(operator_s, res, str[i]);
continue;
}
if(str[i]=='s'||str[i]=='c')
{
push_in(operator_s, res, str[i]);
i=i+2;
continue;
}
if(str[i]=='n'||str[i]=='g')
{
push_in(operator_s, res, str[i]);
continue;
}
}
}
while(!operator_s.empty())
{
string k;
k+=operator_s.top();
res.push_back(k);
operator_s.pop();
}
return res;
}
double result(vector<string> &a)
{
stack<string> s;
for(auto n:a)
{
if(n[0]>='0'&&n[0]<='9')
s.push(n);
else
{
if(n[0]=='+')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 = atof(s1.c_str());
double c2 = atof(s2.c_str());
double l=c1+c2;
s.push(to_string(l));
}
else if(n[0]=='-')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 = atof(s1.c_str());
double c2 = atof(s2.c_str());
double l=c2-c1;
s.push(to_string(l));
}
else if(n[0]=='*')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 = atof(s1.c_str());
double c2 = atof(s2.c_str());
double l=c1*c2;
s.push(to_string(l));
}
else if(n[0]=='/')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 = atof(s1.c_str());
double c2 = atof(s2.c_str());
double l=c2/c1;
s.push(to_string(l));
}
else if(n[0]=='^')
{
string s1=s.top(); s.pop();
string s2=s.top(); s.pop();
double c1 = atof(s1.c_str());
double c2 = atof(s2.c_str());
double l=pow(c2,c1);
//cout<<"pow"<<l<<endl;
s.push(to_string(l));
}
else if(n[0]=='s')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=sin(c1);
s.push(to_string(l));
}
else if(n[0]=='c')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=cos(c1);
s.push(to_string(l));
}
else if(n[0]=='g')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=log2(c1);
s.push(to_string(l));
}
else if(n[0]=='n')
{
string s1=s.top(); s.pop();
double c1=atof(s1.c_str());
double l=log(c1);
s.push(to_string(l));
}
}
}
return atof(s.top().c_str());
}
int calculate(string s) {
s+='=';
vector<string> a=calculate1(s);
return (int)result(a);
}
};