编译原理——删除注释

 在学 编译原理,写了个删除注释的小程序

#include "stdio.h"
#include 
"string.h"
void  main()
{
        FILE 
*fIn,*fTemp,*
fOut;
        
char ch1,ch2,buffer[100
];
        
int
 i,len;
        printf(
"___________________________________________________ "
);
        printf(
"*************************************************** "
);
        printf(
" 这是一个用于删除C语言源程序中注释的小程序! "
);
        printf(
" Made by zs,on Mar 27th,2008. "
);
        printf(
"*************************************************** "
);
        printf(
"___________________________________________________ "
);
        
        
//删除'//'型注释

        if((fIn=fopen("input.txt","r"))==NULL)
        
{
            printf(
"can not open this file! "
);
        }

        fTemp
=fopen("tempoutput.txt","w");
    
        
while (!
feof(fIn))
        
{
            i
=0
;
            fgets(buffer,
100
,fIn);
            len
=
strlen(buffer);
            
while(len--
)
            
{
                ch1
=
buffer[i];
                ch2
=buffer[i+1
];
                i
++
;
                
if (ch1=='/'&&ch2=='/'
)
                
{
                    fputc(
' '
,fTemp);
                    
break
;
                }

                fputc(ch1,fTemp);
            }

        }

        fclose(fIn);
        fclose(fTemp);
    
        
//删除'/**/'型注释
        if((fTemp=fopen("tempoutput.txt","r"))==NULL)
        
{
                printf(
"can not open this file! "
);
        }

        fOut
=fopen("output.txt","w");

        
while ((ch1 = fgetc(fTemp)) !=
 EOF)
        
{        
            
if (ch1== '/'
)
            
{   
                ch2
=
 fgetc(fTemp);
                
if (ch2 == '*'
)
                    
{
                        ch1
=
 fgetc(fTemp);
                        ch2
=
 fgetc(fTemp);
                        
while (ch1 != '*' &&ch2 != '/'
)
                        
{
                            ch1
=
ch2;
                            ch2
=
fgetc(fTemp);
                        
                        }

                    }

                
else
                    
{
                        fputc(ch1,fOut);
                        fputc(ch2,fOut);
                    }
    
            }
    
            
else

                fputc(ch1,fOut);
        
        }

       fclose(fTemp);
       fclose(fOut);
}

你可能感兴趣的:(编译原理,buffer,null,语言,file,c)