NYOJ128 前缀式计算(栈的运用)

题目信息:

http://acm.nyist.net/JudgeOnline/problem.php?

pid=128

+ 2 * + 3 4 5的值就是 37,详见输入输出。

输入
有多组測试数据,每组測试数据占一行,随意两个操作符之间。随意两个操作数之间,操作数与操作符之间都有一个空格。输入的两个操作数可能是小数,数据保证输入的数都是正数。而且都小于10,操作数数目不超过500。
以EOF为输入结束的标志。
输出
对每组数据,输出该前缀表达式的值。

输出结果保留两位小数。

例子输入
+ 2 * + 3 4 5
+ 5.1 / 3 7
例子输出
37.00
5.53

题目分析:

用两个栈进行存下数字和操作,进行计算,我刚開始想的是,每当连续输入两个数即可进行计算,并将结果压入数字栈中,而且操作时。应该保证后出栈的数字在前。先出栈的在后,这样能够保证-、/的结果。

。。。。哎。不知道为什么一直Wa,最后看看别人打代码。这题能够从后往前计算。遇到操作符进行计算即可。此题须要注意就是字符串到数字的转换。


AC代码:

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<cmath>
using namespace std;
char str[1005];
stack<double> dt;
int start;

void vol(){
    int i=0,k=0;
    char a[15],b[15];
    for(;str[start]!=' ';start--){
        a[k++]=str[start];
    }
    start--;//去掉数字后面的空格
    a[k]='';
    //strrev();此函数不能用
    for(i=0;i<k/2;i++){
        char c=a[i];
        a[i]=a[k-1-i];;
        a[k-1-i]=c;
    }
    dt.push(atof(a));
}
double YunSuan(){
    double a,b;
    while(start!=-2){
        switch (str[start]){
            case '+':
                a=dt.top(); dt.pop();
                b=dt.top(); dt.pop();
                dt.push(b+a);start-=2;//去掉该字符和字符后面的空格
            break;
            case '-':
                a=dt.top(); dt.pop();
                b=dt.top(); dt.pop();
                dt.push(a-b);start-=2;
            break;
            case '*':
                a=dt.top(); dt.pop();
                b=dt.top(); dt.pop();
                dt.push(a*b);start-=2;
            break;
            case '/':
                a=dt.top(); dt.pop();
                b=dt.top(); dt.pop();
                dt.push(a/b);start-=2;
            break;
            default: vol();
        }
    }
    return dt.top();
}
int main()
{
    while(gets(str)){
        start=strlen(str)-1;
        printf("%.2lf
",YunSuan());
    }
    return 0;
}

我的代码:

int main()
{
    string str;
    int i,k;
    double a,b;
    while(getline(cin,str)){
        stack<double> dt;
        stack<char> ct;
        i=-1; k=0;
        while(i!=str.size()){
            ++i;
            if(str[i] == ' ') ++i;
            if(str[i] >= '0' && str[i] <= '9'){
                string res; double temp;
                while(i != str.size() && str[i] != ' ')
                    res += str[i++];
                sscanf(res.c_str(), "%lf", &temp);
                //cout<<temp<<endl;
                dt.push(temp),++k;
                //++k;
            }
            if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'){
                ct.push(str[i++]);
                k=0;
            }
            if(k==2&&dt.size()>1){
                a=dt.top();
                dt.pop();
                b=dt.top();
                dt.pop();
                char c=ct.top();
                ct.pop();
                if(c=='+') dt.push(b+a);
                if(c=='-') dt.push(b-a);
                if(c=='*') dt.push(b*a);
                if(c=='/') dt.push(b/a);
                k=1;
            }
        }
        while(dt.size()>1&&ct.size()){//计算最后一个
            a=dt.top();
            dt.pop();
            b=dt.top();
            dt.pop();
            char c=ct.top();
            ct.pop();
            if(c=='+') dt.push(b+a);
            if(c=='-') dt.push(b-a);
            if(c=='*') dt.push(b*a);
            if(c=='/') dt.push(b/a);
        }
        int k=1;
        if(ct.size()&&ct.top()=='-') k=-1;
        printf("%.2lf
",k*dt.top());
    }
	return 0;
}



原文地址:https://www.cnblogs.com/lcchuguo/p/5206126.html