河南省第四届acm省赛 C:表达式求值 逆波兰表达式

来源:互联网 发布:淘宝怎样看拍卖车 编辑:程序博客网 时间:2024/05/16 07:29

题目:

https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1457

题意:

Description

Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等。经过训练,Dr.Kong设计的机器人卡多甚至会计算一种嵌套的更复杂的表达式。
假设表达式可以简单定义为:
1. 一个正的十进制数 x 是一个表达式。
2. 如果 x 和 y 是 表达式,则 函数min(x,y )也是表达式,其值为x,y 中的最小数。
3. 如果 x 和 y 是 表达式,则 函数max(x,y )也是表达式,其值为x,y 中的最大数。
4.如果 x 和 y 是 表达式,则 函数add(x,y )也是表达式,其值为x,y 之和。
例如, 表达式 max(add(1,2),7) 的值为 7。
请你编写程序,对于给定的一组表达式,帮助 Dr.Kong 算出正确答案,以便校对卡多计算的正误。
Input

第一行: N 表示要计算的表达式个数 (1≤ N ≤ 10)
接下来有N行, 每行是一个字符串,表示待求值的表达式
(表达式中不会有多余的空格,每行不超过300个字符,表达式中出现的十进制数都不
超过1000。)
Output

输出有N行,每一行对应一个表达式的值。
Sample Input
3
add(1,2)
max(1,999)
add(min(1,1000),add(100,99))

Sample Output
3
999
200

思路:

把给定的字符串转换为中缀表达式,把add(x, y)转换为x op y,min和max同理。然后用逆波兰表达式求解

#include <bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int, int> P;const int N = 1010;int precedence(char ch){    if(ch == '>' || ch == '<' || ch == '+') return 1;    else return 0;}void reverse_polish(char s[], char sta[]){    char ts[N];    int k = 0, top = 0;    ts[top++] = '@';    for(int i = 0; s[i]; i++)    {        if(s[i] == '(') ts[top++] = s[i];        else if(s[i] == ')')        {            while(ts[top-1] != '(') sta[k++] = ts[--top];            --top;        }        else if(s[i] == '>' || s[i] == '<' || s[i] == '+')        {            while(precedence(ts[top-1]) >= precedence(s[i])) sta[k++] = ts[--top];            ts[top++] = s[i];        }        else if(s[i] >= '0' && s[i] <= '9')        {            while(s[i] >= '0' && s[i] <= '9') sta[k++] = s[i++];            i--;            sta[k++] = ' ';        }    }    while(ts[top-1] != '@') sta[k++] = ts[--top];    sta[k++] = '\0';}int Operator(int num1, int num2, char ch){    int res;    if(ch == '+') res = num1 + num2;    else if(ch == '<') res = min(num1, num2);    else if(ch == '>') res = max(num1, num2);    return res;}void solve(char s[]){    int ts[N];    int top = 0;    for(int i = 0; s[i]; i++)    {        if(s[i] >= '0' && s[i] <= '9')        {            int num = 0;            while(s[i] >= '0' && s[i] <= '9') num = num * 10 + s[i] - '0', i++;            i--;            ts[top++] = num;        }        else if(s[i] != ' ')        {            int num1 = ts[--top], num2 = ts[--top];            ts[top++] = Operator(num2, num1, s[i]);        }    }    printf("%d\n", ts[0]);}int main(){    char str[N], s[N], sta[N];    int t;    scanf("%d", &t);    while(t--)    {        scanf("%s", str);        for(int i = 0; str[i]; i++)        {            if(str[i] == ',')                for(int j = i-1; j >= 0; j--)                {                    if(str[j] == 'n')                    {                        str[i] = '<';                        str[j] = str[j-1] = str[j-2] = '?';                        break;                    }                    else if(str[j] == 'x')                    {                        str[i] = '>';                        str[j] = str[j-1] = str[j-2] = '?';                        break;                    }                    else if(str[j] == 'd')                    {                        str[i] = '+';                        str[j] = str[j-1] = str[j-2] = '?';                        break;                    }                }        }        memset(s, 0, sizeof s);        int k = 0;        for(int i = 0; str[i]; i++)            if(str[i] != '?') s[k++] = str[i];        //printf("%s\n", s);        reverse_polish(s, sta);        solve(sta);    }    return 0;}
0 0
原创粉丝点击