python面试题--去除C++源文件里的注释

2015-01-27 10:12:04 · 作者: · 浏览: 8
import sys


def HandleCPlusPlusComment(lines,i):
	index = lines[i].find("//")
	if index !=-1:
		lines[i]=lines[i][0:index]
		lines[i]+="\r\n"



def HandleCComment(lines,i):
	global bhasCCommentBegin
	while True:
		if not bhasCCommentBegin:
			index = lines[i].find("/*")
			if index != -1:
				bhasCCommentBegin = True
				index2 = lines[i].find("*/",index+2)
				if index2 != -1:
					lines[i]=lines[i][0:index]+lines[i][index2+2:-1]
					bhasCCommentBegin = False #continue look for comment
				else:
					lines[i]=lines[i][0:index]  # only find "begin comment
					lines[i]+="\r\n"
					return -2
			else:
				return 0 #not find
		else:
			index2=lines[i].find("*/")
			if index2 !=-1:
				bhasCCommentBegin = False
				lines[i]=lines[i][index2+2:-1] #continue look for comment
			else:
				return -1 #should delete this



def RemoveComment(file):
	global bhasCCommentBegin
	f = open(file,"r")
	lines = f.readlines()
	leng =len(lines)
	i=0
	while i