#include <iostream>
#include <string>
#include <iomanip>
#include <stack>
#include <ctype.h>
using namespace std;
int cmp(char ch)
{
switch(ch)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default : return 0;
}
}
void change(string &s1, string &s2)
{
stack <char> s;
s.push('#');
int i = 0;
while(i < s1.length() - 1)
{
if(s1[i] == '(')
{
s.push(s1[i++]);
}
else if(s1[i] == ')')
{
while(s.top() != '(')
{
s2 += s.top();
s2 += ' ';
s.pop();
}
s.pop();
i ++;
}
else if(s1[i] == '+' || s1[i] == '-' || s1[i] == '*' || s1[i] == '/')
{
while(cmp(s.top()) >= cmp(s1[i]))
{
s2 += s.top();
s2 += ' ';
s.pop();
}
s.push(s1[i]);
i ++;
}
else
{
while(isdigit(s1[i]) || s1[i] == '.')
{
s2 += s1[i++];
}
s2 += ' ';
}
}
while(s.top() != '#')
{
s2 += s.top();
s2 += ' ';
s.pop();
}
}
double value(string s2)
{
stack <double> s;
double x, y;
int i = 0;
while(i < s2.length())
{
if(s2[i] == ' ')
{
i++;
continue;
}
switch(s2[i])
{
case '+': x = s.top(); s.pop(); x += s.top(); s.pop(); i++; break;
case '-': x = s.top(); s.pop(); x = s.top() - x; s.pop(); i++; break;
case '*': x = s.top(); s.pop(); x *= s.top(); s.pop(); i++; break;
case '/': x = s.top(); s.pop(); x = s.top() / x; s.pop(); i++; break;
default:
{
x = 0;
while(isdigit(s2[i]))
{
x = x * 10 + s2[i] - '0';
i ++;
}
if(s2[i] == '.')
{
double k = 10.0;
y = 0;
i++;
while('0' <= s2[i]&&s2[i] <= '9')
{
y += ((s2[i]-'0')/k);
i++;
k *= 10;
}
x += y;
}
}
}
s.push(x);
}
return s.top();
}
int main(int argc, char const *argv[])
{
int n;
string s1, s2;
cin >> n;
while(n--)
{
cin >> s1;
s2 = "";
change(s1, s2);
cout << fixed << setprecision(2) << value(s2) << endl;
}
return 0;
#include <string>
#include <iomanip>
#include <stack>
#include <ctype.h>
using namespace std;
int cmp(char ch)
{
switch(ch)
{
case '+': return 1;
case '-': return 1;
case '*': return 2;
case '/': return 2;
default : return 0;
}
}
void change(string &s1, string &s2)
{
stack <char> s;
s.push('#');
int i = 0;
while(i < s1.length() - 1)
{
if(s1[i] == '(')
{
s.push(s1[i++]);
}
else if(s1[i] == ')')
{
while(s.top() != '(')
{
s2 += s.top();
s2 += ' ';
s.pop();
}
s.pop();
i ++;
}
else if(s1[i] == '+' || s1[i] == '-' || s1[i] == '*' || s1[i] == '/')
{
while(cmp(s.top()) >= cmp(s1[i]))
{
s2 += s.top();
s2 += ' ';
s.pop();
}
s.push(s1[i]);
i ++;
}
else
{
while(isdigit(s1[i]) || s1[i] == '.')
{
s2 += s1[i++];
}
s2 += ' ';
}
}
while(s.top() != '#')
{
s2 += s.top();
s2 += ' ';
s.pop();
}
}
double value(string s2)
{
stack <double> s;
double x, y;
int i = 0;
while(i < s2.length())
{
if(s2[i] == ' ')
{
i++;
continue;
}
switch(s2[i])
{
case '+': x = s.top(); s.pop(); x += s.top(); s.pop(); i++; break;
case '-': x = s.top(); s.pop(); x = s.top() - x; s.pop(); i++; break;
case '*': x = s.top(); s.pop(); x *= s.top(); s.pop(); i++; break;
case '/': x = s.top(); s.pop(); x = s.top() / x; s.pop(); i++; break;
default:
{
x = 0;
while(isdigit(s2[i]))
{
x = x * 10 + s2[i] - '0';
i ++;
}
if(s2[i] == '.')
{
double k = 10.0;
y = 0;
i++;
while('0' <= s2[i]&&s2[i] <= '9')
{
y += ((s2[i]-'0')/k);
i++;
k *= 10;
}
x += y;
}
}
}
s.push(x);
}
return s.top();
}
int main(int argc, char const *argv[])
{
int n;
string s1, s2;
cin >> n;
while(n--)
{
cin >> s1;
s2 = "";
change(s1, s2);
cout << fixed << setprecision(2) << value(s2) << endl;
}
return 0;
}
心得:做表达式求值的实验,学习逆波兰算法。又是题解http://blog.csdn.net/sevenmit/article/details/9026687,想用自己的方法来,结果WA,果然按照别人的思路就无法提升自己的能力吗,这道理应该也适合别的方面吧。什么也不说了,这狗屎天气真的是冷冷冷死我啦!!!!