posts - 64, comments - 4, trackbacks - 0, articles - 0

hdu_3528 The Simple Programming Language

Posted on 2010-08-16 13:26 acronix 阅读(219) 评论(0)  编辑 收藏 引用 所属分类: hzshuai解题报告

题意:给你一串只含有关键字write,if,else的语句,要你编程处理并输出结果,无结果则不输出

分析:本题最大的难点就是对字符串的处理。
    首先,以if() ..... else ..... 为基结构,碰到write("....")便输出。而碰到嵌套语句.....不断利用递归调用寻找基语句。同时,注意if(0)的情况,在if(0).....else之间的.....通通略过。我做的时候,碰到问题有换行,处理空格,以及对字符串末尾的'\0'的处理。棘手的就是要清楚的看到指针变换的过程与指向位置。


#include <cstdio>
#include 
<string.h>
#include 
<stdlib.h>
#include 
<ctype.h>

char s[5000],cmd[20];

void work(char *
p)
{
     sscanf(p,
"%s"
,cmd);
     
char *=
 cmd;
     
if (cmd[0== 'w')   //当keyword == "write" 

     {
        
while (*!= '"')    q ++
;  
        q 
++
;
        
while (*!= '"'
)
        {
              putchar(
*q);   q ++

        }
        putchar(
'\n'
);
        
while (s[0!= '\0'
)
              gets(s);
     }
     
else if (cmd[0= 'i'//当 key words == "if" 

          {
             
while (*!= '(')  p ++
;
             p 
++
;
             
             
if (*== '1')//当为true时 

             {
                    p 
= strchr(p,' '
);
                    
                    
while  (p != NULL &&*== ' ')  p ++
;
                    
if (p ==
 NULL)
                    {
                       gets(s); 
                       
if (s[0!= '\0'
)
                          work(s);
                       
else return
 ;   
                    }
                    
else
 work(p);
             }
             
else{     //当为false时 

                     int cnt = 0;
                     
                     
//一个 do-while循环忽略if(0)..else之间的语句 

                     do{
                       p 
= strchr(p,' '
);
                      
                       
while (p != NULL && *== ' '
)
                            p 
++
;
                       
                       
if (p == NULL || *== '\0'
)
                       {
                           gets(s);
                           
if (s[0== '\0'
)
                              
return
 ;
                           p 
=
 s;
                       }
                       sscanf(p,
"%s"
,cmd);
                       
if (cmd[0== 'i'
)
                         cnt 
++
;
                       
else if (cmd[0== 'e'
)
                                cnt 
--
;
                       
while (p != NULL && *== ' '
)
                            p 
++
;
                     }
while (cnt >= 0
);
                     p 
= strchr(p,' '
);
                     
while  (p!= NULL && *== ' '
)  
                        p 
++
;
                        
                     
if (p == NULL || *== '\0'
)
                       {
                           gets(s);
                           
if (s[0== '\0'
)
                              
return
 ;
                           p 
=
 s;  
                       } 
                    
                     work(p);
                     
                  }
          }
          
else {//当key words为else 

                   p = strchr(p,' ');
                   
while  (p != NULL && *== ' ')  p ++
;
                   
if (p == NULL || *== '\0'
)
                       {
                           gets(s);
                           
if (s[0== '\0'
)
                              
return
 ;
                           p 
=
 s;
                       } 
                   work(p);
                   
               }
}

int
 main()
{
    
while (gets(s) && s[0!= '\0'
)
    {
          work(s);
    }
    
return 0
;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理