题解 | #四则运算#

四则运算

https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e

#include <stdio.h>
int main() {
    int map[127] = { 0 };
    map['+'] = 1;
    map['-'] = 1;
    map['*'] = 2;
    map['/'] = 2;
    int a1[1001] = { 0 };
    char a2[1001] = { 0 };
    char std[1001] = { 0 };
    char std2[1500] = { 0 };
    scanf("%s", std);
    strcpy(std2, std);
    int i = 0;
    int j = 0;
    int c = 0;
    int len = strlen(std);
    for (i = 0; i < len; i++)
    {
        if (i >= 2)
        {
            if (std[i] >= '0' && std[i] <= '9' && std[i - 1] =='-'&& (std[i - 2]=='(' || std[i - 2] == '[' || std[i - 2] == '{'))
            {
                std2[i - 1] = '0';
                strcpy(&std2[i], &std[i - 1]);
            }
        }
        else if (i==1&&std[i] >= '0' && std[i] <= '9' && std[i - 1] == '-')
        {
            std2[i - 1] = '0';
            strcpy(&std2[i], &std[i - 1]);
        }
    }
    int len2 = strlen(std2);
    strcpy(std, std2);
    strcpy(&std2[1], std);
    std2[0] = '{';
    std2[len2 + 1] = '}';
    i = 0;
    j = 0;
    while (c<len2+2)
    {
        if (std2[c] >= '0' && std2[c] <= '9')
        {
            memset(std, 0, sizeof(std));
            int k = 0;
            while (std2[c] >= '0' && std2[c] <= '9')
            {
                std[k] = std2[c];
                k++;
                c++;
            }
            int len3 = strlen(std);
            int h = 0;
            for (int o = len3-1; o >= 0; o--)
            {
                if (o == len3-1)
                    h = h + (std[o] - '0');
                else
                    h = h + (std[o] - '0') * 10;
            }
            a1[i] = h;
            i++;
        }
        if ((map[std2[c]] > map[a2[j - 1]] || j - 1 < 0) && std2[c] != '(' && std2[c] != '[' && std2[c] != '{')
            {
                a2[j] = std2[c];
                j++;
                c++;
            }
        else if ((map[std2[c]] <= map[a2[j-1]] || j - 1 < 0)&& std2[c] != '(' && std2[c] != '[' && std2[c] != '{' && std2[c] != ')' && std2[c] != ']' && std2[c] != '}')
        {
            int n = 0;
            char m = a2[j-1];
            if (a2[j-1] == '+')
                n = a1[i - 2] + a1[i-1];
            else if (a2[j-1] == '-')
                n = a1[i - 2] - a1[i-1];
            else if (a2[j-1] == '*')
                n = a1[i - 2] * a1[i-1];
            else if (a2[j-1] == '/')
                n = a1[i - 2] / a1[i-1];
            a1[i - 2] = n;
            a1[i-1] = 0;
            a2[j-1] = 0;
            i = i - 1;
            j = j - 1;
        }
        else if (std2[c] == '(' || std2[c] == '[' || std2[c] == '{')
        {
            a2[j] = std2[c];
            j++;
            c++;
        }
        else if (std2[c] == ')' || std2[c] == ']' || std2[c] == '}')
        {
            if (a2[j-1] == '(' && std2[c] == ')')
            {
                a2[j-1] = 0;
                j = j - 1;
                c ++;
            }
            else if (a2[j-1] == '[' && std2[c] == ']')
            {
                a2[j-1] = 0;
                j = j - 1;
                c ++;
            }
            else if (a2[j-1] == '{' && std2[c] == '}')
            {
                a2[j-1] = 0;
                j = j - 1;
                c ++;
            }
            else {
                int n = 0;
                char m = a2[j - 1];
                if (a2[j - 1] == '+')
                    n = a1[i - 2] + a1[i - 1];
                else if (a2[j - 1] == '-')
                    n = a1[i - 2] - a1[i - 1];
                else if (a2[j - 1] == '*')
                    n = a1[i - 2] * a1[i - 1];
                else if (a2[j - 1] == '/')
                    n = a1[i - 2] / a1[i - 1];
                a1[i - 2] = n;
                a1[i - 1] = 0;
                a2[j - 1] = 0;
                i = i - 1;
                j = j - 1;
            }
        }
    }
    printf("%d", a1[0]);
    return 0;
}

全部评论

相关推荐

07-07 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务