#4050: 為啥SE???


mark821119 (好久沒寫啦)

學校 : 國立東華大學
編號 : 12641
來源 : [134.208.3.230]
最後登入時間 :
2016-10-17 18:04:22
a017. 五則運算 | From: [59.121.151.80] | 發表日期 : 2010-07-31 18:48

本題目不開放觀看解題詳情

 

#include <stdio.h>
#include <stdlib.h>
#define N 50
#define OP 5


char op[OP] = {'(','+','-','*','/',};
int  op_priority[OP] = {0,1,1,2,2};

int priority(char c);
void to_postfix(char infix[], char postfix[]);


char stack[N]; 
int top=-1;

void push(char item){
 if (top>=N-1){
  exit(-1);
 }
 stack[++top]=item;
}

int pop(){
 if (top==-1){
  exit(-1);
 }
 return stack[top--];
}

void stackPrint(){
 int i;
 printf("stack =");
 for (i=0; i<=top; i++)
  printf(" %c", stack[i]);
 printf("\n");
}


bool IsEmpty(void)
{
    return (top < 0) ? true : false;  
}


bool IsFull()
{
   return (top >= N - 1) ? true : false;
}


char top_data()
{
   return stack[top];   
}

int priority(char c)
{
   int i;
  
   for( i=0; i < OP; i++)
      if(op[i] == c)
         return op_priority[i];
   return -1;
}


void to_postfix(char infix[], char postfix[])
{
   int i=0, j=-1;
   char x, y;

   while((x=infix[i++]) != '\0'){
      switch(x){
         case '(' : push(x);
                    break;
         case ')' : while(! IsEmpty() && (x=pop()) != '(')
                       postfix[++j]=x;
                    break;
         case '+' :
         case '-' :
         case '*' :
         case '/' : y=top_data();
                    while(priority(y) >= priority(x)){
                       postfix[++j]=pop();
                       y=top_data();
                    }
                    push(x);
                    break;
          default :
                    postfix[++j]=x;
      }
   }
   while(! IsEmpty())
      postfix[++j]=pop();
   postfix[++j]='\0';
}
bool IsDight(char c)
{
        return c>='0' && c<='9';
}
int calculate(char postfix[])
{
        int point=0;
        while(postfix[point]!='\0')
        {
                while(IsDight(postfix[point]))
                        push(postfix[point++]);
                int a=pop()-'0', b=pop()-'0',c=0;
                switch(postfix[point])
                {
                 case'+':c=b+a;
                              break;
                 case'-':c=b-a;
                              break;
                 case'*':c=b*a;
                              break;
                 case'/':c=b/a;
                              break;
                 case'%':c=b%a;
                              break;
                }
                push(c+'0');
                point++;
        }
        return pop()-'0';
}
main()
{
   char infix[50], postfix[50];
   while(scanf("%s",&infix)!=EOF)
   {
   to_postfix(infix,postfix); 
   printf("%d\n",calculate(postfix));
   }
}

 
#4051: Re:為啥SE???


past (遺憾的臭氧)

學校 : 臺北市立麗山高級中學
編號 : 8381
來源 : [111.250.64.129]
最後登入時間 :
2012-06-04 13:50:45
a017. 五則運算 | From: [65.49.2.23] | 發表日期 : 2010-07-31 22:18


http://www.programmer-club.com.tw/pc2020v5/forum/showsametitleN.asp?board_pc2020=homework&id=8240&CSS=
 
ZeroJudge Forum