UVa 327 - Evaluating Simple C Expressions

来源:互联网 发布:网易博客软件 编辑:程序博客网 时间:2024/05/17 01:44

传送门UVa 327 - Evaluating Simple C Expressions

虽然这题是在树的专题里面, 但是我看了半天还是找不到和树的联系.

只能用自己的方法做了.

如果路过的各位知道怎么用树的方法做, 求指点.....

题意挺简单, 就不说了.

我的思路就按题目里说的

1. 先扫描一遍, 干掉前缀, 干掉的同时对应的值++

2.再扫描一遍, 干掉后缀, 同时值--

3.计算值并输出.


我开了两个字符数组, 两个int数组.

int数组, 一个是用来计算值的时候用的, 另一个是最后输出各字母的值时候用的.

char数组, 一个用来存储原来的算式, 另一个为了计算.


详情见代码

#include <cstdio>#include <cstring>#include <cctype>#include <vector>using namespace std;char in[200];vector<char> temp;     //存储的是不带空格的数组.int num[26];    //储存的是计算用值int num2[26];   //储存输出用值int number;const char word[] = "abcdefghijklmnopqrstuvwxyz";void Initial();void EraseEmpty();void ErasePrefix();void ErasePostfix();void Output();int main(){    //freopen("input.txt", "r", stdin);    int i;    while (fgets(in, 200, stdin) != NULL)    {        number = 0;        Initial();        EraseEmpty();   //消去空格        ErasePrefix();  //消去前缀.        ErasePostfix();  //消去后缀, 这时只剩下一个基本的表达式.        Output();        memset(in, 0, sizeof(in));    }    return 0;}void Output(){    int i, j;    number = num[temp[0] - 97];    for (i = 0; i < temp.size(); i++)    {        if (temp[i] == '+')            number += num[temp[i + 1] - 97];        else if (temp[i] == '-')            number -= num[temp[i + 1] - 97];    }    printf("Expression: %s", in);    printf("    value = %d\n", number);    for (i = 0; i < 26; i++)    {        for (j = 0; j < temp.size(); j++)            if (word[i] == temp[j])                printf("    %c = %d\n", temp[j], num2[temp[j] - 97]);    }}void Initial(){    temp.clear();    for (int i = 0; i < 26; i++)    {        num[i] = i + 1;        num2[i] = i + 1;    }}void EraseEmpty(){    int i;    for (i = 0; i < strlen(in); i++)    {        if (in[i] == ' ')            continue;        else            temp.push_back(in[i]);    }}void ErasePrefix(){    int i;    for (i = 0; i < temp.size(); i++)    {        if (isalpha(temp[i]))        {            if (i == 0)         //因为要检查前两个字符, 如果第一个就是字母会越界.                continue;            else            {                if (temp[i - 1] == '-' && temp[i - 2] == '-')                {                    num[temp[i] - 97]--;                    num2[temp[i] - 97]--;                    temp.erase(temp.begin() + i - 2, temp.begin() + i);                }                else if (temp[i - 1] == '+' && temp[i - 2] == '+')                {                    num2[temp[i] - 97]++;                    num[temp[i] - 97]++;                    temp.erase(temp.begin() + i - 2, temp.begin() + i);                }            }        }    }}void ErasePostfix(){    int i;    for (i = 0; i < temp.size(); i++)    {        if (isalpha(temp[i]))        {            if (i == temp.size() - 1)   //同理, 防止越界                break;            else            {                if (temp[i + 1] == '-' && temp[i + 2] == '-')                {                    num2[temp[i] - 97]--;                    temp.erase(temp.begin() + i + 1, temp.begin() + i + 3);     //干掉                }                else if (temp[i + 1] == '+' && temp[i + 2] == '+')                {                    num2[temp[i] - 97]++;                    temp.erase(temp.begin() + i + 1, temp.begin() + i + 3);                }            }        }    }}






0 0
原创粉丝点击