C/C++ 中缀表达式转换成后缀表达式并求值

本文介绍了如何将中缀表达式转换为后缀表达式,并详细阐述了使用栈数据结构在C/C++中进行后缀表达式的计算方法,包括表达式的括号匹配、运算符优先级处理等关键步骤。
摘要由CSDN通过智能技术生成
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 

#define STACK_INIT_SIZE 100 
#define STACKINCREMENT 10 

typedef struct{ 
    char *base; 
    char *top; 
    int stacksize; 
}SqStack1; 

typedef struct{ 
    double *base; 
    double *top; 
    int stacksize; 
}SqStack2; 

SqStack1 OPTR; 
SqStack2 OPND; 

double EvaluateExpression(); 
void CHAR_InitStack(SqStack1 &S); 
void DOUBLE_InitStack(SqStack2 &S); 
char CHAR_GetTop(SqStack1 S); 
double DOUBLE_GetTop(SqStack2 S); 
int CHAR_Push(SqStack1 &S , char e); 
int DOUBLE_Push(SqStack2 &S , double e); 
char CHAR_Pop(SqStack1 &S , char &e); 
double DOUBLE_Pop(SqStack2 &S , double &e); 
char Precede(char a,char b); 
double Operate ( double d ,char e,double f); 
int In(char c); 


int main(void) 
{ 
    double result; 
    printf("*************************\n"); 
    printf("*      表达式计算       *\n"); 
    printf("*************************\n"); 
    printf("请输入一组表达式(以#号结束):\n"); 
    result = EvaluateExpression(); 
    printf("\n\n计算结果是(结果保留两位小数):%.2f\n\n\n",result); 
} 

void CHAR_InitStack(SqStack1 &S) 
{ 
    S.base=(char *)malloc(STACK_INIT_SIZE * sizeof(char)); 
    if(!S.base)exit(OVERFLOW); 
    S.top = S.base; 
    S.stacksize = STAC
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
以下是中缀表达式转换成后缀表达式求值的C代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define STACK_SIZE 100 typedef struct { int top; int items[STACK_SIZE]; } Stack; void push(Stack *s, int item) { if (s->top >= STACK_SIZE) { printf("Stack overflow\n"); exit(1); } s->items[++(s->top)] = item; } int pop(Stack *s) { if (s->top <= -1) { printf("Stack underflow\n"); exit(1); } return s->items[(s->top)--]; } int priority(char operator) { switch(operator) { case '+': case '-': return 1; case '*': case '/': return 2; case '^': return 3; default: return 0; } } void infixToPostfix(char infix[], char postfix[]) { Stack s; s.top = -1; int i, j; for (i = 0, j = 0; infix[i] != '\0'; i++) { if (isdigit(infix[i])) { // operand postfix[j++] = infix[i]; } else if (infix[i] == '(') { // left parenthesis push(&s, infix[i]); } else if (infix[i] == ')') { // right parenthesis while (s.items[s.top] != '(') { postfix[j++] = pop(&s); } pop(&s); // discard the left parenthesis } else { // operator while (s.top != -1 && s.items[s.top] != '(' && priority(infix[i]) <= priority(s.items[s.top])) { postfix[j++] = pop(&s); } push(&s, infix[i]); } } // pop any remaining operators from the stack while (s.top != -1) { postfix[j++] = pop(&s); } postfix[j] = '\0'; // terminate the postfix string } int evaluatePostfix(char postfix[]) { Stack s; s.top = -1; int i, op1, op2, result; for (i = 0; postfix[i] != '\0'; i++) { if (isdigit(postfix[i])) { // operand push(&s, postfix[i] - '0'); } else { // operator op2 = pop(&s); op1 = pop(&s); switch (postfix[i]) { case '+': result = op1 + op2; break; case '-': result = op1 - op2; break; case '*': result = op1 * op2; break; case '/': result = op1 / op2; break; case '^': result = 1; for (int j = 0; j < op2; j++) { result *= op1; } break; } push(&s, result); } } return pop(&s); } int main() { char infix[STACK_SIZE]; char postfix[STACK_SIZE]; printf("Enter an infix expression: "); scanf("%s", infix); infixToPostfix(infix, postfix); printf("Postfix expression: %s\n", postfix); printf("Result: %d\n", evaluatePostfix(postfix)); return 0; } ``` 此代码实现了中缀表达式转换成后缀表达式求值的功能。其中,`infixToPostfix()`函数将中缀表达式转换成后缀表达式,`evaluatePostfix()`函数对后缀表达式求值,`priority()`函数返回操作符的优先级,`push()`和`pop()`函数分别实现栈的入栈和出栈操作。在`main()`函数中,用户输入一个中缀表达式,程序将其转换成后缀表达式并输出,然后求出后缀表达式的值并输出。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡宝全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值