示例#1
0
int main()
{
   char expression[30],*p;
   sqstack *operator, *operand;
   int sum, flag = 0;
   int op1, op2;

   operator = (sqstack *)malloc(sizeof(sqstack));
   setnull(operator);
   operand = (sqstack *)malloc(sizeof(sqstack));
   setnull(operand);

   gets(expression);
   p = expression;

   while ( *p )
   {
      if ( (*p >= '0') && (*p <= '9') ) 
      {  
         p++;
         continue;
      }
      switch ( *p )
      {
         case '+' :
         case '-' :
         case '*' :
         case '/' :
         case ' ' :
         case '(' :
         case ')' :  
                   break;
         default  :
                   printf("Wrong Character %c in expression!\n", *p);
                   return -1;
      }
      p++;
   }

   p = expression;
   while ( *p )
   {
      if ( (*p >= '0') && (*p <= '9') )
      {
         if ( flag )
         {
            sum = 10*sum + (*p - '0');
         }
         else
         {
            sum = *p - '0';
            flag = 1;
         }
         p++;
         continue;
      }
     
      if ( flag )
      {
         printf("push data %d\n", sum);
         push_stack(operand, sum);
         flag = 0;
      }

      if ( *p == ' ' )
      {
         p++;
         continue;
      }      

      if ( empty_stack( operator ) )
      {
         printf("push %c\n", *p);
         push_stack(operator, (int)*p);
         p++;
         continue;
      }
      
      if ( *p == ')' )
      {
         printf("*** Wow, we meet ) ***\n");
         deal_bracket(operator, operand);
         p++;
         continue;
      }

      if ( (convert_operator(*p) > convert_operator((char)top_stack(operator))) || ((char)top_stack(operator) == '(') )
      {
         printf("push %c\n", *p);
         push_stack(operator, (int)*p);
      }
      else
      {
         deal_tmp(operator, operand, *p);
      }
      p++;
   }
   if ( flag )  push_stack(operand, sum);
 
   while ( !empty_stack(operator) )
   {
      op2 = pop_stack(operand);
      op1 = pop_stack(operand);
       
      switch ( (char)pop_stack(operator) )
      {
         case '+' :
                   printf("push data: %d + %d\n", op1, op2);
                   push_stack(operand, op1 + op2);
                   break;
         case '-' :
                   printf("push data: %d - %d\n", op1, op2);
                   push_stack(operand, op1 - op2);
         case '*' :
                   printf("push data: %d * %d\n", op1, op2);
                   push_stack(operand, op1 * op2);
                   break;
         case '/' :
                   printf("push data: %d / %d\n", op1, op2);         
                   push_stack(operand, op1 / op2);
                   break;
      }
   }   
        
   printf("%d\n", pop_stack(operand));

   return 0;
}
示例#2
0
int main()
{
    init();
    FILE *fp = fopen("home.css","r");
    if(fp == NULL)
    {
          printf("%s","打开文件失败");
          return 1;
    }
    char buf[10000];
    char value[10000];
    char keys[100000];
    int line_number = 0;
    int desciption_type = END_DESCIRP;
    bool desciption_begin = false;
    int bracket_type = END_DESCIRP;
    int latst_bracket_type = RIGHT_BRACKET;
    int count = 0;
    while(fgets(buf,sizeof(buf),fp)!=NULL)
    {
          line_number++;
          bool has_key = false;
          bool has_value = false;
          str_trim(buf);
          if(strlen(buf) ==0)
          {
              continue;
          }
          desciption_type = is_desciption(buf);
          if(desciption_type ==  BEGIN_DESCIRP)
          {
              desciption_begin = true;
              continue;
          }
          else if(desciption_type == END_DESCIRP )
          {
              desciption_begin = false;
              continue;
          }
          else
          {
              if(desciption_begin)
              {
                  continue;
              }
          }
          bracket_type = deal_bracket(buf);
          if(bracket_type == LEFT_BRACKET)
          {
              if(strlen(buf) <= 1)
              {
                  latst_bracket_type = LEFT_BRACKET;
                  continue;
              }
              else
              {
                  strcatN(keys, buf, strlen(buf)-1);
              }
          }
          else if(bracket_type == RIGHT_BRACKET)
          {
              count++;
              deal_keys_value(keys, value,count, line_number);
              memset(keys,0,10000);
              memset(value,0,10000);
          }
          else
          {
              if(latst_bracket_type == RIGHT_BRACKET)
              {
                 strcatN(keys, buf, strlen(buf)); 
              }
              else if(latst_bracket_type == LEFT_BRACKET)
              {
                  strcatN(value, buf, strlen(buf));  
              }
          }
          if(bracket_type != NO_BRACKET)
          {
              latst_bracket_type = bracket_type;
          }
    }
    sort_value();
    print_css();
    printf("_______________%s_______________","处理完毕");
    system("pause");
    return 0;
}