题解 | #表达式求值#

表达式求值

http://www.nowcoder.com/practice/9566499a2e1546c0a257e885dfdbf30d

#include <string.h>
#include <ctype.h>

int func1(char ch[101]);

int func2(int *num, char *str, int i);

int main(void)
{
    char strnum[101][101] = {'\0'};
    char str[101] = {'\0'};
    char *str_p = str;
    char ch;
    int i = 0, j = 0, flag = 0, flagcp = 0, fl = 0;
    int num[101] = {0};
    int *num_p = num;
    int hash[101] = {0};
    int hashcp[101] = {0};
    int tmp[101] = {0};
    int *tmp_p = tmp;
    char strtmp[101] = {'\0'};
    char *strtmp_p = strtmp;
    int val;

    while ((ch = (char) getchar()) != '\n')
    {
        if (isalnum(ch))
        {
            strnum[i][j] = ch;
            j++;
            if (flag != 0)
                hash[i] = flag;
        } else if (ch == '(')
        {
            if (fl > 0)
                flag++;
            hash[i] = flag + 1;
            flag += 1;
            flagcp += 1;
        } else if (ch == ')')
        {
            flag -= 1;
            fl = 1;
        } else
        {
            str[i] = ch;
            i++;
            j = 0;
        }
    }

    for (int k = 0; k <= i; ++k)
    {
        num[k] = func1(strnum[k]);
        hashcp[k] = hash[k];
    }

    int x = 0;
    int y = flagcp;
    for (int k = 0; k < y; ++k)
    {
        if (flagcp == 0)
            break;
        for (int a = 0; a <= i; ++a)
        {
            if (hash[a] == flagcp)
            {
                tmp[x] = num[a];
                if (hash[a] == flagcp && hash[a + 1] == flagcp)
                {
                    strtmp[x] = str[a];
                    str[a] = '*';
                }
                num[a] = 1;
                hash[a]--;
                x++;
            }
            if (hashcp[a - 1] == flagcp && hashcp[a] == flagcp && hashcp[a + 1] != flagcp)
            {
                num[a] = func2(tmp_p, strtmp_p, x - 1);
                x = 0;
                memset(tmp, 0, sizeof(tmp));
                memset(strtmp, '\0', sizeof(strtmp));
            }
        }
        flagcp--;
        memcpy(hashcp, hash, sizeof(hash));
    }
    val = func2(num_p, str_p, i);

    printf("%d", val);

    return 0;
}

int func1(char ch[101])
{
    int val = 0;
    int len = (int) strlen(ch);

    for (int i = len - 1, j = 1; i >= 0; --i, j *= 10)
    {
        val += (ch[i] - '0') * j;
    }
    return val;
}

int func2(int *num, char *str, int i)
{
    int val;
    for (int k = 0; k <= i - 1; ++k)
    {
        if (*(str + k) == '*')
        {
            *(num + k + 1) = *(num + k) * *(num + k + 1);
            if (*(str + k - 1) == '-')
                *(num + k + 1) = 0 - *(num + k + 1);
            *(num + k) = 0;
            *(str + k) = '+';
        } else if (*(str + k) == '/')
        {
            *(num + k + 1) = *(num + k) / *(num + k + 1);
            if (*(str + k - 1) == '-')
                *(num + k + 1) = 0 - *(num + k + 1);
            *(num + k) = 0;
            *(str + k) = '+';
        }
    }

    val = num[0];

    for (int a = 0; a <= i - 1; ++a)
    {
        if (*(str + a) == '+')
        {
            val += *(num + a + 1);
        } else if (*(str + a) == '-')
        {
            val -= *(num + a + 1);
        }
    }
    return val;
}

全部评论

相关推荐

与火:这不接? 留子的钱不挣白不挣
点赞 评论 收藏
分享
一名愚蠢的人类:多少games小鬼留下了羡慕的泪水
投递荣耀等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务