计算表达式值

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

const int N = 1e3 + 10;

stack<int> op, br;
stack<double> num;
char str[N];

void initStack() {
  while (!op.empty()) op.pop();
  while (!br.empty()) br.pop();
  while (!num.empty()) num.pop();
}

int getType(char c) {
  if (c >= '0' && c <= '9')
    return 0;
  else if (c == '+' || c == '-')
    return 1;
  else if (c == '*' || c == '/')
    return 1;
  else if (c == '(' || c == ')')
    return 2;
  else if (c == '[' || c == ']') 
    return 2;
  else if (c == '{' || c == '}')
    return 2;
  else
    return -1;
}

int transOp(char c) {
  if (c == '+')
    return 0;
  else if (c == '-')
    return 1;
  else if (c == '*')
    return 2;
  else if (c == '/')
    return 3;
  return -1;
}

int transBr(char c) {
  if (c == '(')
    return 0;
  else if (c == ')')
    return 1;
  else if (c == '[')
    return 2;
  else if (c == ']')
    return 3;
  else if (c == '{')
    return 4;
  else if (c == '}')
    return 5;
  else
    return -1;
}

bool cal(int fp) {
  double num2, num1;
  if (!num.empty()) { // get second number
    num2 = num.top();
    num.pop();
  }
  else {
    printf("Wrong position of operator\n");
    return false;
  }
  if (!num.empty()) { // get first number
    num1 = num.top();
    num.pop();
  }
  else {            
    printf("Wrong position of operator\n");
    return false;
  }
  double ans = 0;
  if (fp % 4 == 0) {
    ans = num1 + num2;
  }
  else if (fp % 4 == 1) {
    ans = num1 - num2;
  }
  else if (fp % 4 == 2) {
    ans = num1 * num2;
  }
  else if (fp % 4 == 3) {
    if (num2 == 0) {
      printf("Divide zero\n");
      return false;
    }
    ans = num1 / num2;
  }
  num.push(ans);
  return true;
}

bool solve(double &res) {
  initStack();
  int len = strlen(str);

  int plus = 0; // operator addition
  for (int i = 0; i < len; i++) {
    int type = getType(str[i]);
    if (type == 0) { // number
      double sum = str[i++] - '0';
      double pl = 10;
      while (i < len && (type = getType(str[i])) == 0) { // before decimal
        sum = sum * pl + str[i] - '0';
        i++;
      }
      if (i < len && str[i] == '.') { // decimal
        pl = 0.1;
        i++;
        while (i < len && (type = getType(str[i])) == 0) { // after decimal
          sum = sum + (str[i] - '0') * pl;
          i++;
          pl *= 0.1;
        }
      }
      else if (i < len && type == -1) { // illegal character
        printf("Illegal character: %c\n", str[i]);
        return false;
      }
      num.push(sum);
      i--;
    }
    else if (type == 1) { // operator
      int p = transOp(str[i]) + plus;
      while (!op.empty()) {
        int fp = op.top();
        if (fp >= p) { // now operator is lower
          op.pop();
          if (!cal(fp))
            return false;
        }
        else { // now operator is higher
          break;
        }
      }
      op.push(p);
    }
    else if (type == 2) { // bracket
      int t = transBr(str[i]);
      if (t % 2) { // right bracket
        if (br.empty()) {
          printf("Brackets are not mactch\n");
          return false;
        }
        else {
          int ft = br.top();
          br.pop();
          if (t - 1 != ft) {
            printf("Brackets are not mactch\n");
            return false;
          }
        }
        plus -= 4;
      }
      else { // left bracket
        plus += 4;
        br.push(t);
      }
    }
    else { // illegal character
      printf("Illegal character: %c\n", str[i]);
      return false;
    }
  }
  if (!br.empty()) { // check bracket
    printf("Brackets are not mactch\n");
    return false;
  }
  while (!op.empty()) { // calculate rest
    int fp = op.top();
    op.pop();
    if (!cal(fp))
      return false;
  }
  if (num.empty()) { // no number left
    printf("No Number\n");
    return false;
  }
  else if (num.size() > 1) { // too many numbers
    printf("Too many numbers\n");
    return false;
  }
  res = num.top();
  return true;
}

int main() {
  scanf("%s", str);
  double res = -1;
  if (solve(res))
    printf("%.3lf\n", res);
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值