HDU 1237 简单计算器 (stack)

                                               简单计算器

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27155    Accepted Submission(s): 9861

Problem Description

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

Input

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

Output

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

Sample Input

1 + 2

4 + 2 * 5 - 7 / 11

0

Sample Output

3.00

13.36

Method1:

计算器的运算优先级用栈来实现最适合不过了,只要在入栈之前控制好

* / + -这样的优先级顺序,getchar来读入每个运算符以及吃掉空格即可。

/*
  Problem : 1237 ( 简单计算器 )     Judge Status : Accepted
  RunId : 27199222    Language : G++    Author : html_11
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mod 998244353
#define INF 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;is;
int main(){
    int n;
    double m;
    while(scanf("%d", &n) != EOF){
        char c = getchar();
        if(c == '\n' && n == 0) break;
        s.push(n);
        c = getchar();
        while(scanf("%d", &n) != EOF){
            if(c == '*'){
                m = s.top();
                m *= n;
                s.pop();    //这里要记得出栈
                s.push(m);
            }
            else if(c == '/'){
                m = s.top();
                m /= n;
                s.pop();  //这里记得出栈
                s.push(m);
            }
            else if(c == '+'){
                s.push(n);
            }
            else if(c == '-'){
                s.push(0 - n);
            }
            c = getchar();
            if(c == '\n') break;
            c = getchar();
        }
        double res = 0;
        while(!s.empty()){
            res += s.top();
            s.pop();        //栈要清空:细节
        }
        printf("%.2lf\n", res);
    }
    return 0;
}

 Method2:

直接读取一行数据,处理每个取值细节

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;
int main() {
	string str;
	while (getline(cin, str)) {
		if (str == "0") return 0;
		//cout << str << endl;
		stack S;
		int len = str.length();
		for (int i = 0; i < len; i++) {
			if (str[i] == ' ') continue;
			int v = 0;
			bool sign = false;
			while (isalnum(str[i]) && i < len) {
				v = v * 10 + str[i] - '0';
				i++;
				sign = true;
			}
			if(sign) S.push(v);	//这里一定要标记判断,不然会把不存在的0压入栈影响结果
			if (i < len && str[i] == ' ') i++;
			if (str[i] == '*') {
				i += 2;						//跳空格
				double p = S.top();
				S.pop();
				v = 0;
				while (isalnum(str[i]) && i < len) {	//数值取所有位数
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(p * (double)v);
			}
			else if (str[i] == '/') {
				i += 2;
				double p = S.top();
				S.pop();
				v = 0;
				while (str[i] >= '0' && str[i] <= '9' && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(p / v);
			}
			else if (str[i] == '-') {
				i += 2;
				v = 0;
				while (str[i] >= '0' && str[i] <= '9' && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(-v);
			}
			else if (str[i] == '+') {
				i += 2;
				v = 0;
				while (str[i] >= '0' && str[i] <= '9' && i < len) {
					v = v * 10 + str[i] - '0';
					i++;
				}
				S.push(v);
			}
		}
		double res = 0;
		while (!S.empty()) {
			//printf("%f\n", S.top());
			res += S.top();
			S.pop();
		}
		printf("%.2f\n", res);
	}
	return 0;
}

 

你可能感兴趣的:(STL)