表达式求值以及中缀式转后缀式代码

本文介绍了一种使用栈实现的表达式计算方法,包括中缀表达式到后缀表达式的转换及后缀表达式的计算过程。通过优先级判断进行运算符的处理,实现了基本算术表达式的解析与求值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>//数据结构表达式计算 
#include <algorithm>
#include <stack> 
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;

//列子 
//((1+2)*5+1)/4
//1+2*(7-4)/3 
stack<char> op;//放操作符 
stack<double> od;//放操作数 (正整数)

void deal(char str[]);
void count();
int priorityJudge(char ope1, char ope2);

int main() {
  int times;
  char str[1005];
  scanf("%d", &times);
  while (times--) {
    scanf("%s", str);
    deal(str);
  }
  return 0;
}

void deal(char str[]) {

  while (!od.empty()) od.pop();
  while (!op.empty()) op.pop();

  int i = 0, length = strlen(str);
  op.push('=');

  while (i < length) {

    if (isdigit(str[i])) {
      char s[1010];
      int count = 0;

      while (isdigit(str[i]) || str[i] == '.') {
        printf("%c", str[i]);
        s[count++] = str[i++];
      }
      s[count] = '\0';
      od.push(atof(s));

    }else {
      if (priorityJudge(op.top(), str[i]) < 0) {
        op.push(str[i]);
        i++;
      }else if(priorityJudge(op.top(), str[i]) == 0) {
        op.pop();
        i++;
      }else {
        printf("%c", op.top());
        count();
      }
    }
  }
  printf("=\n");
  printf("%.2lf\n", od.top());
}

void count() {
  double a = od.top();
  od.pop();
  double b = od.top();
  od.pop();
  char c = op.top();
  op.pop();

  if (c == '+') od.push(b + a);
  else if (c == '-') od.push(b - a);
  else if (c == '*') od.push(b * a);
  else od.push(b / a);
}

int priorityJudge(char op1, char op2) {
  if(op1 == '+' || op1 == '-') {
    if (op2 == '*' || op2 == '/' || op2 == '(') return -1;
    else return 1;
  }
  if (op1 == '*' || op1 == '/') {
    if (op2 == '(') return -1;
    else return 1;
  }
  if (op1 == '(' && op2 == ')') return 0;
  if (op1 == '=' && op2 == '=') return 0;
  return -1;
}
#include <iostream>
#include <algorithm>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;

stack<char> s;
void stringChange(string & temp, string & str);
int priorityJudge(char c);
//1.000+2/4=
//((1+2)*5+1)/4=
int main() {
    int times;
  string str, tempString = "";
  cin >> times;
  while (times--) {
    cin >> str;
    stringChange(tempString, str);
    cout << tempString << endl;

    tempString.clear();
    str.clear();
  }
    return 0;
}

void stringChange(string & temp, string & str) {//数据结构中缀式转后缀式 
  while (!s.empty()) s.pop(); 
  int i = 0, length = str.size();
  s.push('#');
  while (i < length) {
    if (str[i] == '(') s.push(str[i++]);
    else if (str[i] == ')') {
      while (s.top() != '(') {
        temp += s.top();
        temp += ' ';
        s.pop();
      }
      s.pop();
      i++;
    }else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
      while (priorityJudge(s.top()) >= priorityJudge(str[i])) {
        temp += s.top();
        temp += ' ';
        s.pop();
      }
      s.push(str[i++]);
    }else if ((str[i] <= '9' && str[i] >= '0') || str[i] == '.') {
      while ((str[i] <= '9' && str[i] >= '0') || str[i] == '.') {
        temp += str[i++];
      }
      temp += ' ';
    }else if (str[i] == ',' || str[i] == '=') i++;
  }
  while (s.top() != '#') {
    temp += s.top();
    s.pop();
    temp += ' ';
  }
  temp += '=';
}

int priorityJudge(char c) {
  if (c == '+' || c == '-') return 1;
  else if (c == '*' || c == '/') return 2;
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值