#4805: WA (line: 3)


hs01n6060 (Crazy小螃蟹)

學校 : 新北市立板橋高級中學
編號 : 13691
來源 : [220.129.67.104]
最後登入時間 :
2024-05-01 12:42:27
a017. 五則運算 | From: [203.64.161.123] | 發表日期 : 2011-01-25 15:48

大大幫忙一下:

#include <iostream>
#include <string>
using namespace std;

int main(){
    long long a[1000] = {}, i = -1, j = -1, m = 0;
    string s = "";
    char c, o[1000] = {};
    while (!cin.eof()){
          c = cin.get();
          if (isdigit (c)) s += c;
          else{
               if (s != "") {sscanf (s.c_str(), "%lld", &a[++i]); s = "";}
               if (c != ' '){
                   if (m == 1) {a[i] *= -1; m = 0;}
                   if (c == '-' && isdigit (cin.peek())) m = 1;
                   else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%')
                         if (o[j] == '*') {a[i-1] *= a[i]; i--; o[j] = c;}
                         else if (o[j] == '/') {a[i-1] /= a[i]; i--; o[j] = c;}
                         else if (o[j] == '%') {a[i-1] %= a[i]; i--; o[j] = c;}
                         else o[++j] = c;
                   if (c == '(') o[++j] = c;
                   if (c == ')')
                         for (j = j; o[j+1] != '('; j--)
                             if (o[j] == '+') {a[i-1] += a[i]; i--;}
                             else if (o[j] == '-') {a[i-1] -= a[i]; i--;}
                             else if (o[j] == '*') {a[i-1] *= a[i]; i--;}
                             else if (o[j] == '/') {a[i-1] /= a[i]; i--;}
                             else if (o[j] == '%') {a[i-1] %= a[i]; i--;}
                   if (c == '\n'){
                         for (j = j; j > -1; j--)
                             if (o[j] == '+') {a[i-1] += a[i]; i--;}
                             else if (o[j] == '-') {a[i-1] -= a[i]; i--;}
                             else if (o[j] == '*') {a[i-1] *= a[i]; i--;}
                             else if (o[j] == '/') {a[i-1] /= a[i]; i--;}
                             else if (o[j] == '%') {a[i-1] %= a[i]; i--;}
                         printf ("%d\n", a[0]);
                         i = j = -1;
                   }
               }
          }
    }
}
 

 
ZeroJudge Forum