zl程序教程

您现在的位置是:首页 >  其他

当前栏目

数据结构 求表达式串的后缀表达式和值 (栈+模拟)

模拟数据结构 表达式 后缀
2023-09-11 14:17:18 时间

Description

基于任意给定的表达式串(包含的运算有加(+)、减(-)、乘(*)、除(/)、圆括号等,例如:输入3.4+5.6*(4.2-1),求其后缀表达式,并求表达式的计算结果。

Input

有多组输入数据,每一行一组输入,请处理到文件结束(EOF)。
每行一个表达式串(最多1000个字符),中间没有空格等其他无关字符。

Output

输出为三行。第一行输出每组的 case,第二行是所求的后缀表达式,第三行是所求的表达式的值。只有每个数字后面会有空格,没有多余空格输出,后缀表达式中的数字按给出字符串中的数字 原样输出,表达式的值中不要有无意义的0出现,每组输出后边都有一个空行。格式见Sample。

Sample Input

5+10*(5+6)-10
5.0+10.00*(5+6.000)-10.00000

Sample Output

Case #1: 5 10 5 6 +*+10 -
The answer is 105.
 
Case #2: 5.0 10.00 5 6.000 +*+10.00000 -
The answer is 105.

HINT

考察知识点:栈, 时间复杂度O(n),空间复杂度O(n)

Append Code

析:用栈来计算,按照转化规则,要么自己推一下也是可以,不会看这http://www.cnblogs.com/dwtfukgv/articles/5875526.html

其他的就很简单了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
 
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
stack<string> num;
stack<char> mark;
char s[maxn];
vector<string> ans;
int priv[maxn];
 
double calc(double a, double b, char op){
    if(op == '+')  return a + b;
    if(op == '-')  return a - b;
    if(op == '*')  return a * b;
    if(op == '/')  return a / b;
}
 
double solve(){
    string str = (string)s;
    stack<double> num;
    priv['+'] = priv['-'] = 3;
    priv['*'] = priv['/'] = 2;
    priv['('] = 10;
    double x, y, t = 0;
    char last = 0;
    for(int i = 0; i < n; ++i){
        if(isdigit(s[i])){
            num.push(atof(str.c_str()+i));
            for(; i+1 < n && isdigit(str[i+1]); i++);
            if(i+1 < n && str[i+1] == '.')
                for(i++; i+1 < n && isdigit(str[i+1]); i++);
        }
        else if(str[i] == '(')  mark.push(str[i]);
        else if(str[i] == ')'){
            while(mark.top() != '('){
                    y = num.top();  num.pop();
                    x = num.top();  num.pop();
                    char op = mark.top();  mark.pop();
                    num.push(calc(x, y, op));
            }
            mark.pop();
        }
        else if(str[i] == '-' && (last == 0 || last == '(')){
                    num.push(0.0);
                    mark.push('-');
        }
        else if(priv[str[i]] > 0){
            while(mark.size() > 0 && priv[str[i]] >= priv[mark.top()]){
                y = num.top();  num.pop();
                x = num.top();  num.pop();
                char op = mark.top();  mark.pop();
                num.push(calc(x, y, op));
            }
            mark.push(str[i]);
        }
        else continue;
        last = str[i];
    }
 
    while(mark.size() > 0){
        y = num.top();  num.pop();
        x = num.top();  num.pop();
        char op = mark.top();  mark.pop();
        num.push(calc(x, y, op));
    }
    return num.top();
}
 
int main(){
    int kase = 0;
    while(scanf("%s", s) == 1){
        printf("Case #%d:\n", ++kase);
        n = strlen(s);
        ans.clear();
        for(int i = 0; i < n; ++i){
            if(isdigit(s[i])){
                string ss = "";
                int j;
                for(j = i; j < n; ++j){
                    if(isdigit(s[j]) || s[j] == '.') ss += s[j];
                    else  break;
                }
                i = j-1;
                num.push(ss);
            }
            else if(s[i] == '('){
                mark.push(s[i]);
            }
            else if(s[i] == ')'){
                while(true){
                    char ch = mark.top();  mark.pop();
                    if(ch == '(')  break;
                    string s1; s1.push_back(ch);
                    num.push(s1);
                }
            }
            else{
                if(mark.empty()){  mark.push(s[i]);  continue; }
                while(!mark.empty()){
                    char ch = mark.top();
                    string s1; s1.push_back(ch);
 
                    if(s1[0] == '('){  mark.push(s[i]);  break; }
                    if((s1[0] == '+' || s1[0] == '-') && (s[i] == '*' || s[i] == '/')){  mark.push(s[i]);  break; }
                    else{
                        num.push(s1);
                        mark.pop();
                    }
                }
                if(mark.empty()){  mark.push(s[i]); continue; }
            }
        }
        while(!mark.empty()){
            char ch = mark.top();mark.pop();
            string s1; s1.push_back(ch);
            num.push(s1);
        }
        while(!num.empty())  ans.push_back(num.top()), num.pop();
        for(int i = ans.size()-1; i >= 0; --i){
            if(isdigit(ans[i][0]))  cout << ans[i] << " ";
            else cout << ans[i];
        }
        cout << endl;
        printf("The answer is %g.\n\n", solve());
    }
    return 0;
}