编译原理——删除注释

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

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值