数学表达式-实现中缀表达式转后缀表达式 (用C语言 不直接使用队列栈)

这是一个C语言程序,用于将中缀数学表达式转换为后缀表达式并计算其值。程序处理了加、减、乘、除、取余和指数运算,并考虑了括号的优先级。程序读取输入的中缀表达式,将其转换为后缀表达式,然后根据后缀表达式计算表达式的值。
摘要由CSDN通过智能技术生成

给你一个数学表达式,请你求出它的值。
表达式中可能含有以下运算符:”+”、”-“、”*“、”/“、”%”(取余)、”^”(指数)、”(“、”)”,其中优先级为: 括号 > “^” > “*” = “/“ = “%” > “+” = “-“。

 

// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
#include <stdio.h>
#include <math.h>

int middle2post_expre(char *post_expre, char *symbol_expre) 
{
    char str_char;
    int symbol_index = 0;
    int post_index = 0;
    
    while (scanf_s("%c", &str_char, 1) == 1) {
        if (str_char == ' ') {
            continue;
        } else if (str_char == '$') {
           while (symbol_index > 0 && symbol_expre[symbol_index - 1] != ' ') {
               post_expre[post_index++] = symbol_expre[--symbol_index];
               symbol_expre[symbol_index] = ' ';
           }
           return 0;
        } else if (str_char == '(') {
            symbol_expre[symbol_index++] = str_char;
        } else if ((str_char >= '0') && (str_char <= '9&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值