#4820: 為什麼RE??


popular10347 (ICPC// 哪時能唸到高等演算法T^T)

學校 : 元智大學
編號 : 11351
來源 : [1.169.118.99]
最後登入時間 :
2012-10-29 00:22:54
d285. 00727 - Postfix Expression -- UVa727 | From: [125.231.171.52] | 發表日期 : 2011-01-27 13:29

我的code為什麼會RE??

不是說最多50行??

麻煩大大們看一下

謝謝 ! 

#include<iostream>
using namespace std;

int index_post;
int index_stack;
char postfix[101];
char stack[101];

bool is_operator(char);
int value(char);

int main()
{
 
      char ch;
      char infix[101];
 
      int n;
      while(cin >> n)
     {
           cin.get();
           for(int i=0;i<n;i++)
          {
                 index_post=-1;
                 index_stack=-1;
                 int index=0;
                 while(ch=getchar())
                 {
                      if(ch != '\n')
                             infix[index++]=ch;
                      else
                             break;
                      char c=getchar();
                 }
   
                 for(int j=0;j<index;j++)
                {
                        if(!is_operator(infix[j]))
                                 postfix[++index_post]=infix[j];
                        else
                        {
                                 if(index_stack == -1 || infix[j] == '(')
                                        stack[++index_stack]=infix[j];
                                 else if(infix[j] != ')')
                                 {
                                        while(value(infix[j])<value(stack[index_stack]))
                                       {
                                               postfix[++index_post]=stack[index_stack];
                                               index_stack--;
                                        }
      
                                        stack[++index_stack]=infix[j];
                                 }

                                 else
                                {
                                        while(stack[index_stack] != '(')
                                       {
                                                 postfix[++index_post]=stack[index_stack];
                                                 index_stack--;
                                        }

                                         index_stack--;
                                }
                       }
                }
   
               while(index_stack>-1)
              {
                      postfix[++index_post]=stack[index_stack];
                      index_stack--;
              }

              for(int k=0;k<=index_post;k++)
                      cout << postfix[k];
             cout << endl;
         }
     }
 

   // system("pause");
      return 0;
}

bool is_operator(char ch)
{
      if(ch>='0' && ch<='9')
           return false;
      else
           return true;
}

int value(char ch)
{
         switch(ch)
         {
             case '(':
                     return 0;
            case '+':
            case '-':
                    return 1;
            case '*':
            case '/':
                   return 2;
         }
}

 

 
ZeroJudge Forum