2012年9月14日

UVa 397 - Equation Elation

Suck problem...
#include <cstdio>
#include <string>
#include <vector>
using std::string;
using std::vector;

class C {
public:
  C (int value, int start, int end) {
    this -> value = value;
    this -> start = start;
    this -> end = end;
  }
  int value, start, end;
};

void handle(string &s) {
  for (int i = 0, cmd = 0; s[i]; i++) {
    if ('0' <= s[i] && s[i] <= '9') {
      cmd = 0;
      if (s[i + 1] != ' ' && (s[i + 1] < '0' || s[i + 1] > '9'))
        s.insert(s.begin() + ++i, ' ');
    } else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
      if (cmd) {
        if (s[i] == '+') 
          s.erase(s.begin() + i--);
        cmd = 0;
      } else {
        cmd = s[i];
        if (s[i + 1] != ' ')
          s.insert(s.begin() + ++i, ' ');
      }
    } else if (s[i] == '=') {
      if (s[i + 1] != ' ') 
        s.insert(s.begin() + ++i, ' ');
      break;
    }
  }
  puts(s.c_str());
}

void cal(C a, C b, char op, string &s) {
  int n1 = a.value;
  int i1 = a.start - (n1 < 0);
  int n2 = b.value;
  int i2 = b.end;
  int n;
  char N[100];
  switch (op) {
  case '+':
    n = n1 + n2;
    break;
  case '-':
    n = n1 - n2;
    break;
  case '*':
    n = n1 * n2;
    break;
  case '/':
    n = n1 / n2;
    break;
  }
  sprintf(N, "%d", n);
  s.erase(s.begin() + i1, s.begin() + i2 + 1);
  s.insert(i1, N);
  puts(s.c_str());
}

int main() {
  int first = 1;
  char str[1000];
  while (gets(str)) {
    if (!first)
      puts("");
    first = 0;
    string s(str);
    handle(s);
    vector<C> num, op;
    int cmd = 0, tmp = 0, start = 0, end = 0, neg = 0;;
    for (int i = 0; s[i]; i++) {
      if ('0' <= s[i] && s[i] <= '9') {
        if (!i || (s[i - 1] < '0' || s[i - 1] > '9')) 
          start = i;
        end = i;
        tmp = tmp * 10 + s[i] - '0';
      } else if (s[i] == '+' || s[i] == '-') {
        if (s[i] == '-' && '0' <= s[i + 1] && s[i + 1] <= '9') {
          neg = 1;
        } else {
          op.push_back(C(s[i], i, i));
          tmp = 0;
        }
      } else if (s[i] == '*' || s[i] == '/') {
        op.push_back(C(s[i], i, i));
        cmd = s[i];
        tmp = 0;
      } else if (s[i] == ' ') {
        if (neg) {
          tmp *= -1;
          neg = 0;
        }
        if ('0' <= s[i - 1] && s[i - 1] <= '9')
          num.push_back(C(tmp, start, end));
        tmp = 0;
        start = 0;
        end = 0;
        if (num.size() > 1 && (num.back().start > op.back().end) && (op.back().value == '*' || op.back().value == '/')) {
          cal(num[num.size() - 2], num[num.size() - 1], op.back().value, s);
          num.erase(num.begin(), num.end());
          op.erase(op.begin(), op.end());
          i = -1;
          continue;
        }
      } else if (s[i] == '=') {
        if (!op.size() && num.size() <= 1) 
          break;
        cal(num[0], num[1], op[0].value, s);
        num.erase(num.begin(), num.end());
        op.erase(op.begin(), op.end());
        i = -1;
        continue;
      }
    }
  }
  return 0;
}

沒有留言:

張貼留言