正文

清除代码中的注释2008-04-19 23:29:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/zhaoyg/34327.html

分享到:

昨天在一个人的blog中看到他做了一个清除代码中的注释的小程序,于是我自己也来了兴趣。
以下是我自己做的,完全是我独立思考的。
不过相比之下我的代码比人家的多了很多,唉,心痛呀!!!
stack_push、stack_pop 、cope_string与cope_comment这四个函数的代码都copy于我做的那个代码缩排程序里的代码,只有局部做了些删除。
----------------------------------------------------------------------------
/*
清除代码中的注释
zhaoyg   08.4.19
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct stack
{
 char item;
 stack *next;
}stack;

void stack_push( stack **,char );
stack *stack_pop (stack **);
stack *cope_string(FILE *fout,stack *current_node_string);
stack *cope_comment(FILE *fout,stack *current_node_comment);

int main()
{
 char current_char;

 FILE *pfin,*pfout;
 stack *current_node=NULL ,*head=NULL;

 pfin = fopen("test.txt","r");
 pfout = fopen("out.txt","w");
 
////////////////////////////////////////////////////////
 while (fscanf(pfin,"%c",&current_char)!=EOF)
 {
  if (head==NULL)
  {
   stack_push(&head,current_char);
   current_node=head;
  }
  else
  {
   stack_push(&current_node,current_char);
  }
 }

 fclose(pfin);
 current_node = head;
////////////////////////////////////////////////////////

 while (current_node != NULL)
 {

  if (current_node->item == '"')    // string constant
  {
   current_node = cope_string(pfout,current_node);
   continue;
  }
  else
  {   
   // comment

   if ((current_node->item == '/' && current_node->next->item == '*')||
    (current_node->item == '/' && current_node->next->item == '/'))
   {
    current_node = cope_comment(pfout,current_node);
    continue;
   }
   else
   {
    putc(current_node->item,pfout);
    current_node = stack_pop(&current_node);
    continue;
   }
  }
 }

 system("pause");
 return 0;
}

void stack_push( stack **pointer,char current_char)
{
 if (*pointer==NULL)
 {
  *pointer = (stack*)malloc(sizeof(stack));
  (*pointer)->next = NULL;
 }
 else
 {
  (*pointer)->next = (stack*)malloc(sizeof(stack));
  *pointer=(*pointer)->next;
  (*pointer)->next=NULL;
 }
 (*pointer)->item = current_char;
}

//delet node
stack *stack_pop(stack **pointer)
{
 stack *temp;
 temp=*pointer;
 *pointer=(*pointer)->next;
 free(temp);
 
return *pointer;
}

stack *cope_comment(FILE *fout,stack *current_node_comment)
{
 char prev;
 bool bool_cppstyle_comment = 0;

 if (current_node_comment->item == '/' && current_node_comment->next->item == '/')
  bool_cppstyle_comment = 1;

 do
 {
  prev = current_node_comment->item;

  //putc( current_node_comment->item,fout );
  current_node_comment = stack_pop(&current_node_comment);

  if ( current_node_comment->item =='/' && prev == '*' )
  {
   //putc(current_node_comment->item,fout);
   return current_node_comment = stack_pop(&current_node_comment);   
  }

  if ( current_node_comment->next->item =='\n' && bool_cppstyle_comment)
  {
   //putc(current_node_comment->item,fout);
   return current_node_comment = stack_pop(&current_node_comment);
  }
 }
 while ( 1 );
}
stack *cope_string(FILE *fout,stack *current_node_string)// deal string-constant
{
 do
 {
  if (current_node_string->item =='\\')
  {
   putc(current_node_string->item,fout);
   current_node_string = stack_pop(&current_node_string);
  }

  putc(current_node_string->item,fout);
  current_node_string = stack_pop(&current_node_string);
 }
 while (  current_node_string->item !='"');

 putc( current_node_string->item , fout );

 return stack_pop(&current_node_string);
}
-----------------------------------------------------------------------------------------------------------

阅读(3100) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册