AOJ 0087 Strange Mathematical Expression
こんにちは川です。
今回は特にしていません。
スタックを使いました。
それだけです。
#include<bits/stdc++.h>
using
namespace
std;
int
main()
{
string str;
while
(getline(cin,str)){
stack<
double
> st;
for
(
int
i=0,point=0;;i++){
if
(str[i]==
' '
||str[i]==
'\0'
){
if
(i-point==1){
if
(str[point]-
'0'
>9||str[point]-
'0'
<0){
double
righ=st.top();
st.pop();
double
lef=st.top();
st.pop();
if
(str[point]==
'+'
)st.push(lef+righ);
else
if
(str[point]==
'-'
)st.push(lef-righ);
else
if
(str[point]==
'*'
)st.push(lef*righ);
else
st.push(lef/righ);
}
else
st.push(str[point]-
'0'
);
}
else
{
double
now=0;
bool
kawa=0;
if
(str[point]==
'-'
){
point++;
kawa=1;
}
for
(
int
j=point;j<i;j++)now+=(str[j]-
'0'
)*
pow
(10,i-j-1);
if
(kawa)now*=-1;
st.push(now);
}
if
(str[i]==
'\0'
)
break
;
point=i+1;
}
}
printf
(
"%lf\n"
,st.top());
}
}