输入字符串表达式,用栈计算出表达式的值

/*思路: 边解析边用两个栈来做
    s1: 用来放操作数
    s2:用来放运算符
    
    s1:来一个操作数,入一个栈
    
    s2:
        入栈条件:栈为空的时候直接入栈
        栈不为空的情况下:
            
            如果来的这个运算符(也就是即将入栈的运算符)
            优先级 高于
            栈顶运算符
            动作:入栈
            
            while()
            如果来的这个运算符(也就是即将入栈的运算符)
            优先级 低于  <=
            栈顶运算符
            动作:干掉栈顶元素(s2栈顶出栈op,同时要从s1出栈两个数据a b,
                                计算 c = a op b,最后,再把计算结果c入栈s1)
                                这里需要注意ab哪一个是第一个操作数
            最终动作:入栈
    
    全部入栈后:s1 和 s2 同时出栈并且计算,直到s2为空    (s2栈顶出栈op,
                                        同时要从s1出栈两个数据a b,
                                        计算 c = a op b,最后,再把计算结果c入栈s1)
    
    最后结果:S1的栈顶元素*/

#include "SeqStack.h"

int main()
{
    SeqStack *S1 = (SeqStack*)malloc(sizeof(SeqStack));
    SeqStack *S2 = (SeqStack*)malloc(sizeof(SeqStack));
    S1 = InitStack(20);
    S2 = InitStack(20);
    char *str = "2-2*8+3/2";
    printf("%s",str);
    //printf("55\n");
    while(*str)
    {
        if(*str >= '0' && *str <= '9')
        {
            Push(S1,*str);
            //printf("11\n");
        }
        else
        {
            //printf("77\n");
            if(S2 ->top == -1)
            {
                //printf("7788\n");
                Push(S2,*str);
                //printf("66\n");
            }
            else
            {
                if((S2 ->elem[S2 ->top] == '+' || S2 ->elem[S2 ->top] == '-') && (*str == '*' || *str == '/'))
                {
                    Push(S2,*str);
                    //printf("33\n");
                }
                else
                {
                    char a,b,c;
                    int d;
                    Pop(S2,&c);
                    Pop(S1,&a);
                    Pop(S1,&b);
                    //printf("22\n");
                    switch (c)
                    {
                        case '+': d = (b -48) + (a - 48);break;
                        case '-': d = (b -48) - (a - 48);break;
                        case '*': d = (b -48) * (a - 48);break;
                        case '/': d = (b -48) / (a - 48);break;
                    }
                    //printf("d : %d\n",d);
                    //printf("c : %c\n",c);
                    Push(S1,d + 48);
                    continue;
                }
            }
        }
        str++;
    }
    char a,b,c;
    int d;
    if(*str == '\0')
    {
        while(S1 ->top != -1 && S2 ->top != -1)
        {

            Pop(S2,&c);
            Pop(S1,&a);
            Pop(S1,&b);
            switch (c)
            {
                case '+': d = (b -48) + (a - 48);break;
                case '-': d = (b -48) - (a - 48);break;
                case '*': d = (b -48) * (a - 48);break;
                case '/': d = (b -48) / (a - 48);break;
            }
            //printf("d : %d\n",d);
            Push(S1,d + 48);
        }
    }
    printf(" = %d\n",d);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用栈来实现字符串表达式计算,具体实现过程如下: 1. 创建一个操作数一个运算符。 2. 将字符串表达式转换为一个字符列表,遍历该列表。 3. 如果当前字符是数字,则将其转换为数字并压入操作数。 4. 如果当前字符是运算符,则将其压入运算符。 5. 如果当前字符是右括号,则弹运算符的运算符和操作数的操作数,并进行运算,直到遇到左括号。 6. 遍历完字符列表后,依次弹运算符的运算符和操作数的操作数,并进行运算,直到运算符为空。 7. 最后操作数的唯一元素就是表达式。 下面是一个示例代码: ```python def calculate(expr): # 创建操作数和运算符 operand_stack = [] operator_stack = [] # 运算符优先级 priority = {'+': 1, '-': 1, '*': 2, '/': 2} # 将字符串表达式转换为字符列表 tokens = list(expr) # 遍历字符列表 for token in tokens: if token.isdigit(): # 如果是数字,将其转换为整数并压入操作数 operand_stack.append(int(token)) elif token in priority: # 如果是运算符,比较其与顶运算符的优先级 while operator_stack and priority[token] <= priority[operator_stack[-1]]: # 如果顶运算符优先级大于等于当前运算符,则弹顶运算符和操作数,进行运算 operator = operator_stack.pop() operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = eval('{}{}{}'.format(operand1, operator, operand2)) operand_stack.append(result) # 将当前运算符压入运算符 operator_stack.append(token) elif token == '(': # 如果是左括号,将其压入运算符 operator_stack.append(token) elif token == ')': # 如果是右括号,弹运算符的运算符和操作数,并进行运算,直到遇到左括号 while operator_stack[-1] != '(': operator = operator_stack.pop() operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = eval('{}{}{}'.format(operand1, operator, operand2)) operand_stack.append(result) operator_stack.pop() # 依次弹运算符的运算符和操作数,并进行运算,直到运算符为空 while operator_stack: operator = operator_stack.pop() operand2 = operand_stack.pop() operand1 = operand_stack.pop() result = eval('{}{}{}'.format(operand1, operator, operand2)) operand_stack.append(result) # 操作数的唯一元素就是表达式 return operand_stack[0] # 测试代码 expr = '3+2*4-(5+1)/2' result = calculate(expr) print('{}={}'.format(expr, result)) ``` 输出结果: ``` 3+2*4-(5+1)/2=8 ``` 注意,上述示例代码的 `eval` 函数是执行字符串表达式的函数,可以替换为自己实现的计算函数。另外,上述代码只支持整数运算,如果需要支持浮点数运算,可以将操作数的元素改为浮点数类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值