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

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

stack<char> op;//放操作符 
stack<long long> od;//放操作数 (正整数) 
//列子 
//((1+2)*5+1)/4
//1+2*(7-4)/3 
void deal(string str);
void count();
int priorityJudge(char c);

int main() {
  int times;
  string str;
  cin >> times;
  while (times--) {
    cin >> str;
    deal(str);
    printf("%lld\n", od.top());
  }
  return 0;
}

void deal(string str) {
  while (!op.empty()) op.pop();
  while (!od.empty()) od.pop();
  int i = 0, length = str.size();
  op.push('#'); 
  while (i < length) {
    if (str[i] <= '9' && str[i] >= '0') {
      int value = 0;
      while (str[i] <= '9' && str[i] >= '0') {
        value = value * 10 + (str[i] - '0');
        i++;
      }
      od.push(value);
    }else if (str[i] == '(') {
      op.push('(');
      i++;
    }else if (str[i] == ')') {
      count();

      op.pop();

      if (op.top() != '#' && op.top() != '(') count();
      i++;
    }else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/') {
      if (priorityJudge(op.top()) >= priorityJudge(str[i])) {
        count();
      }
      op.push(str[i++]);
    }
  }
  while (op.top() != '#') {
    count();
  }
}

void count() {
  long long a = od.top();
  od.pop();
  long long 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 c) {//运算符优先级
  if (c == '+' || c == '-') return 1;
  else if (c == '*' || c == '/') return 2;
  return 0;
}
#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;
}

猜你喜欢

转载自blog.csdn.net/zwhsoul/article/details/80018155