Problem p00087 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "math.h"
#include "stdio.h"
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;

/** string stream
        lexical-cast **/

/** Problem0087 : Strange Mathematical Expression **/
int main() {
  stack<double> stk;
  string s;
  int tmp;

  while (1) {
    getline(cin, s);
    if (cin.eof())
      break;

    s.push_back(' ');

    tmp = 0;
    for (int i = 0; i < s.length(); i++) {
      if ('0' <= s[i] && s[i] <= '9') {
        if (tmp >= 0) {
          tmp *= 10;
          tmp += s[i] - '0';
        } else {
          tmp *= 10;
          tmp -= s[i] - '0';
        }
      } else if (tmp != 0 && s[i] == ' ') {
        stk.push(tmp);
        tmp = 0;
      } else if (s[i] == '+') {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a + b);
      } else if (s[i] == '-') {
        if (s[i + 1] == ' ') {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(b - a);
        } else {
          tmp *= 10;
          tmp += -(s[i + 1] - '0');
          i++;
        }
      } else if (s[i] == '*') {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a * b);
      } else if (s[i] == '/') {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(b / a);
      }
    }
    printf("%.6f\\n", stk.top());
  }
}

Error: TLE

Fixed submission:

#include "math.h"
#include "stdio.h"
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;

/** string stream
        lexical-cast **/

/** Problem0087 : Strange Mathematical Expression **/
int main() {
  stack<double> stk;
  string s;
  int tmp;

  while (1) {
    getline(cin, s);
    s.push_back(' ');

    tmp = 0;
    for (int i = 0; i < s.length(); i++) {
      if ('0' <= s[i] && s[i] <= '9') {
        if (tmp >= 0) {
          tmp *= 10;
          tmp += s[i] - '0';
        } else {
          tmp *= 10;
          tmp -= s[i] - '0';
        }
      } else if (tmp != 0 && s[i] == ' ') {
        stk.push(tmp);
        tmp = 0;
      } else if (s[i] == '+') {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a + b);
      } else if (s[i] == '-') {
        if (s[i + 1] == ' ') {
          double a = stk.top();
          stk.pop();
          double b = stk.top();
          stk.pop();
          stk.push(b - a);
        } else {
          tmp *= 10;
          tmp += -(s[i + 1] - '0');
          i++;
        }
      } else if (s[i] == '*') {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a * b);
      } else if (s[i] == '/') {
        double a = stk.top();
        stk.pop();
        double b = stk.top();
        stk.pop();
        stk.push(a / b);
      }
    }
    printf("%.6f\\n", stk.top());
  }
}