c++实现将简单的中缀表达式转化为…

不多说了,都在代码里了,假设只有加法和乘法,除法和减法类似
#include <iostream>
#include <stack>

using namespace std;

bool isNotOperator(char c) {
  return c != '*' && c != '+'&& c!= '(' && c != ')';
}

//获取符号的优先级
int getPriority(char c) {
  int priority = -1;
  switch (c) {
    case '+':
     priority = 1;
     break;
    case '*':
     priority = 2;
     break;
    case '(':
     priority = 3;
     break;
    default:
     break;
  }
  return priority;
}
int main() {
  char *s = "a+b*c+(d*e+f)*g";
  stack stk;

  while (*s != '\0' || !stk.empty()) {
    char current = *s;
    //如果字符串完毕,只有栈里面有元素,那么就全部弹出并输出
    if (current == '\0') { 
      while(!stk.empty()) {  
       cout << stk.top();
       stk.pop();
     }
 break;
    }

    // 说明不是操作符,直接输出
    if(isNotOperator(current)) {
      cout<< current;
    } else if (current ==')') {
     //1.弹出元素并输出直到'('
     while(stk.top() != '(') {
       cout << stk.top();
       stk.pop();
     }
     //2.将"("也出栈
     stk.pop();
    } else {
      //下面是普通操作符的情况
      //1.弹出栈元素直到发现优先级更低的元素
     while(!stk.empty() && getPriority(current) <=getPriority(stk.top()) && stk.top() != '(') {
cout << stk.top();
stk.pop();
     }
     //将本操作符入栈
     stk.push(current);
    }
    s++;
  }
  cout << endl;
  return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现中缀表达式转化为后缀表达式的代码: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define MAX_STACK_SIZE 100 typedef struct { char data[MAX_STACK_SIZE]; int top; } Stack; void push(Stack *s, char c) { if (s->top == MAX_STACK_SIZE - 1) { printf("Stack overflow!\n"); exit(EXIT_FAILURE); } s->data[++(s->top)] = c; } char pop(Stack *s) { if (s->top == -1) { printf("Stack underflow!\n"); exit(EXIT_FAILURE); } return s->data[(s->top)--]; } char peek(Stack *s) { if (s->top == -1) { printf("Stack underflow!\n"); exit(EXIT_FAILURE); } return s->data[s->top]; } int is_operator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } int precedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } else { return 0; } } void infix_to_postfix(char *infix, char *postfix) { Stack s; s.top = -1; int i, j; char c; for (i = 0, j = 0; infix[i] != '\0'; i++) { c = infix[i]; if (isspace(c)) { continue; } if (isdigit(c) || isalpha(c)) { postfix[j++] = c; } else if (is_operator(c)) { while (s.top != -1 && is_operator(peek(&s)) && precedence(c) <= precedence(peek(&s))) { postfix[j++] = pop(&s); } push(&s, c); } else if (c == '(') { push(&s, c); } else if (c == ')') { while (s.top != -1 && peek(&s) != '(') { postfix[j++] = pop(&s); } if (s.top == -1) { printf("Mismatched parentheses!\n"); exit(EXIT_FAILURE); } pop(&s); } else { printf("Invalid character!\n"); exit(EXIT_FAILURE); } } while (s.top != -1) { if (peek(&s) == '(') { printf("Mismatched parentheses!\n"); exit(EXIT_FAILURE); } postfix[j++] = pop(&s); } postfix[j] = '\0'; } int main() { char infix[100], postfix[100]; printf("Enter an infix expression: "); fgets(infix, 100, stdin); infix_to_postfix(infix, postfix); printf("Postfix expression: %s\n", postfix); return 0; } ``` 使用方法: 1. 编译代码:`gcc infix_to_postfix.c -o infix_to_postfix` 2. 运行程序:`./infix_to_postfix` 3. 输入中缀表达式,例如:`(a+b)*c-d/e` 4. 程序输出后缀表达式,例如:`ab+c*de/-`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值