#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stack>
#define N 1000
using namespace Std;
Char s[n],b[3];
stack<double>num;
stack<char>st;
int main ()
{
while (gets (s))
{
int K=strlen (s);
Double a1,a2;
Char s1[10],s2[10];
for (int i=k-1;i>=0;i--)
{
if (s[i]== ') continue;
if (s[i]>= ' 0 ' &&s[i]<= ' 9 ' | | s[i]== '. ') Come on backwards.
{
int l=0;
while (s[i]!= ")
{
St.push (S[i]);
i--;
}
while (!st.empty ())
{
B[l]=st.top ();
St.pop ();
l++;
}
b[l]= ' + ';
Num.push (Atof (b));
}
Else
{
Switch (S[i])
{
Case ' + ':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2+A1);
Break
Case '-':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A1-A2);//order cannot be wrong
Break
Case ' * ':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A2*A1);
Break
Case '/':
A1=num.top ();
Num.pop ();
A2=num.top ();
Num.pop ();
Num.push (A1/A2);//order cannot be wrong
Break
}
}
}
printf ("%0.2lf\n", Num.top ());
Num.pop ();
}
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nanyang OJ Prefix-type calculation topic 218