?? 1237 簡單計算器.cpp
字號:
/*
1237 簡單計算器
Time Limit : 1000 ms Memory Limit : 32768 K Output Limit : 256 K
GUN C++
*/
#include<stack>
#include<cstdio>
using namespace std;
stack<double> s1;
stack<char> s2;
inline void get(double&lf, char& ch)
{
scanf("%lf",&lf);
ch = getchar();
while(ch==' ' || ch=='\t')
ch=getchar();
}
int index(char ch)
{
if(ch == '+' || ch == '-') return 1;
else if(ch == '*' || ch == '/') return 2;
else return 0;
}
inline double operate(double & a, double & b, char ch)
{
switch(ch)
{
case '/':
return a / b;
case '-':
return a - b;
case '*':
return a * b;
case '+':
return a + b;
}
}
int main()
{
double lf, temp;
char ch, ch2;
while(true)
{
while(!s1.empty()) s1.pop();
while(!s2.empty()) s2.pop();
get(lf, ch);
if(lf == 0.0 && (ch == '\n' || ch == EOF)) break;
while(true)
{
if(s2.empty() || index(s2.top()) < index(ch))
{
s1.push(lf);
s2.push(ch);
}
else
{
while(!s2.empty() && index(s2.top()) >= index(ch))
{
temp = s1.top();
s1.pop();
ch2 = s2.top();
s2.pop();
temp = operate(temp, lf, ch2);
lf = temp;
}
s1.push(lf);
s2.push(ch);
}
if(ch == '\n') break;
get(lf, ch);
}
printf("%.2lf\n",s1.top());
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -