难点在于判断输入的数字是否为负数和运算符优先级。
可以在一次循环中以获取一个数字+ 一个运算符+括号(如果有)为准则 ,如果在这次循环中先遇到‘-’号,则将它与后面的数视为负数而不是将‘-’视为减号。
在遇到运算符入栈时,要判断符号优先级,只要不高于栈顶符号优先级就先把栈顶符号取出计算,直到满足条件才入栈。
#include <stdio.h>
#include <string.h>

int main(){
    char str[1000];
    while(scanf("%s",str)!=EOF){
        float s1[500]={0};//数字栈
        char s2[500]={0};//运算符栈
        int top1=-1,top2=-1,i=0,len=strlen(str);
        while(i<=len || top1!=-1){//每次循环以一个数字与一个运算符的组合为步长(忽略括号)
            int flag=0;//判断是否是负数
            float num=0;
            if(str[i]==')' || str[i]==']'|| str[i]=='}' || i>=len){//弹出两个数与一个运算符计算,将结果推进数字栈中
                while(s2[top2]!='('){
                    if(s2[top2]=='*'){
                        s1[top1-1]*=s1[top1];
                    }
                    else if(s2[top2]=='/'){
                        s1[top1-1]/=s1[top1];
                    }
                    else if(s2[top2]=='+'){
                        s1[top1-1]+=s1[top1];
                    }
                    else if(s2[top2]=='-'){
                        s1[top1-1]-=s1[top1];
                    }
                    top1--;top2--;
                    if(top2<0){
                        break;
                    }
                }
                top2--;i++;
                if(str[i]=='+' || str[i]=='-'){//判断符号优先级,优先级不高于栈顶符号时,取出栈顶符号运算
                    while(s2[top2]!='(' && top2!=-1){
                        if(s2[top2]=='+'){
                           s1[top1-1]+=s1[top1];
                           top1--;
                        }
                        else if(s2[top2]=='-'){
                            s1[top1-1]-=s1[top1];
                            top1--;
                        }
                        else if(s2[top2]=='*'){
                           s1[top1-1]*=s1[top1];
                           top1--;
                        }
                        else if(s2[top2]=='/'){
                            s1[top1-1]/=s1[top1];
                            top1--;
                        }
                        else{
                            break;
                        }
                        top2--;
                    }
                    s2[++top2]=str[i++];
                }
                if(str[i]=='*' || str[i]=='/'){//判断符号优先级,优先级不高于栈顶符号时,取出栈顶符号运算
                    while(s2[top2]!='(' && top2!=-1){
                        if(s2[top2]=='*'){
                           s1[top1-1]*=s1[top1];
                           top1--;
                        }
                        else if(s2[top2]=='/'){
                            s1[top1-1]/=s1[top1];
                            top1--;
                        }
                        else{
                            break;
                        }
                        top2--;
                    }
                    s2[++top2]=str[i++];
                }
            }
            else{
                if(str[i]=='-'){
                    flag=1;
                    i++;
                }
                while(str[i]>='0' && str[i]<='9'){//数字入栈
                    num=num*10+str[i]-'0';
                    if(str[i+1]>='0' && str[i+1]<='9'){
                        i++;
                        continue;
                    }
                    else{
                        if(flag){
                            s1[++top1]=-num;
                        }
                        else{
                            s1[++top1]=num;
                        }
                        i++;
                        break;
                    }
                }
                if(str[i]=='+' || str[i]=='-'){//判断符号优先级,优先级不高于栈顶符号时,取出栈顶符号运算
                    while(s2[top2]!='(' && top2!=-1){
                        if(s2[top2]=='+'){
                           s1[top1-1]+=s1[top1];
                           top1--;
                        }
                        else if(s2[top2]=='-'){
                            s1[top1-1]-=s1[top1];
                            top1--;
                        }
                        else if(s2[top2]=='*'){
                           s1[top1-1]*=s1[top1];
                           top1--;
                        }
                        else if(s2[top2]=='/'){
                            s1[top1-1]/=s1[top1];
                            top1--;
                        }
                        else{
                            break;
                        }
                        top2--;
                    }
                    s2[++top2]=str[i++];
                }
                if(str[i]=='*' || str[i]=='/'){//判断符号优先级,优先级不高于栈顶符号时,取出栈顶符号运算
                    while(s2[top2]!='(' && top2!=-1){
                        if(s2[top2]=='*'){
                           s1[top1-1]*=s1[top1];
                           top1--;
                        }
                        else if(s2[top2]=='/'){
                            s1[top1-1]/=s1[top1];
                            top1--;
                        }
                        else{
                            break;
                        }
                        top2--;
                    }
                    s2[++top2]=str[i++];
                }
                if(str[i]=='(' || str[i]=='['|| str[i]=='{'){
                    s2[++top2]='(';
                    i++;
                }
            }
        }
        printf("%.f",s1[0]);//输出栈顶的结果
    }
}