#include
#include
#include
#include
#include
#include
#include
using namespace std;
vector > f(string s)
{
vector > v;
char ch[100];
string str;
int i=0, j;
while (s[i] != '\0')
{
if (s[i] == '(')
{
str = s[i++] + '\0';
v.push_back(make_pair (1, str));
}
else if (s[i] == ')')
{
str = s[i++] + '\0';
v.push_back(make_pair < int, string>(2, str));
}
else if (s[i] == '+' || s[i] == '-')
{
str = s[i++] + '\0';
v.push_back(make_pair < int, string>(3, str));
}
else if (s[i] == '*' || s[i] == '/')
{
str = s[i++] + '\0';
v.push_back(make_pair < int, string>(4, str));
}
else if(s[i] == ' ')
{
i++;
continue;
}
else if (s[i] <= '9' && s[i] >= '0' || s[i] == '.')
{
bool dian;
if(s[i] == '.')
dian = true;
j = 1;
ch[0] = s[i++];
int k = 1;
while(s[i] <= '9' && s[i] >= '0' || s[i] == '.')
{
if(s[i] == '.' && dian)
{
cout<<"输入2个小数点了"< while(1);
}
ch[j] = s[i];
++j;
++i;
}
ch[j] = '\0';
v.push_back(make_pair < int, string >(0, ch));
dian = false;
}
}
return v;
}
bool bijiao(int n, string a)
{
int m;
if(a == "+" || a == "-")
m=3;
else if(a == "*" || a == "/")
m=4;
else if(a == "#")
m=-1;
return n>m?true:false;
}
stack f_nbl(vector > v)
{
stack s, f;
f.push("#");
for(vector >::iterator xi = v.begin(); xi != v.end(); ++xi)
{
if(xi->first == 0)
s.push(xi->second);
else
{
if(xi->first == 1)
f.push(xi->second);
else if(xi->first == 2)
{
while(f.top() != "(")
{
s.push(f.top());
f.pop();
}
f.pop();
}
else if(f.top() == "(")
f.push(xi->second);
else
{
if(f.top() == "#")
{
f.push(xi->second);
}
else
{
if(bijiao(xi->first, f.top()))
f.push(xi->second);
else
{
while(!bijiao(xi->first, f.top()))
{
s.push(f.top());
f.pop();
}
f.push(xi->second);
}
}
}
}
}
while(f.top() != "#")
{
s.push(f.top());
f.pop();
}
f.pop();
while(!s.empty())
{
f.push(s.top());
s.pop();
}
return f;
}
double f_js(stack s)
{
double a, b;
string ch;
stack p;
while(!s.empty())
{
while(s.top() != "+" && s.top() != "-" && s.top() != "*" && s.top() != "/")
{
p.push(atof(s.top().c_str()));
if (s.size())
s.pop();
}
ch = s.top();
if (s.size())
s.pop();
b = p.size() ? p.top() : 0;
if (p.size())
p.pop();
a = p.size() ? p.top() : 0;
if (p.size())
p.pop();
switch(ch[0])
{
case '+' : p.push(a + b); break;
case '-' : p.push(a - b); break;
case '*' : p.push(a * b); break;
case '/' : if(b==0.0)
{
cout<<"除数不能为0"< while(1);
}
else
p.push(a / b);
break;
}
}
return p.size() ? p.top() : 0;
}
int main()
{
string s;
while (s != "quit")
{
cout<<"请输入算式:"< cin>>s;
cout< }
return 0;
}
