昨天在一个人的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",¤t_char)!=EOF) { if (head==NULL) { stack_push(&head,current_char); current_node=head; } else { stack_push(¤t_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(¤t_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 nodestack *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(¤t_node_comment); if ( current_node_comment->item =='/' && prev == '*' ) { //putc(current_node_comment->item,fout); return current_node_comment = stack_pop(¤t_node_comment); } if ( current_node_comment->next->item =='\n' && bool_cppstyle_comment) { //putc(current_node_comment->item,fout); return current_node_comment = stack_pop(¤t_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(¤t_node_string); } putc(current_node_string->item,fout); current_node_string = stack_pop(¤t_node_string); } while ( current_node_string->item !='"'); putc( current_node_string->item , fout ); return stack_pop(¤t_node_string);}-----------------------------------------------------------------------------------------------------------

评论