首页 > 代码库 > python面试题--去除C++源文件里的注释
python面试题--去除C++源文件里的注释
import sysdef 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 thisdef RemoveComment(file): global bhasCCommentBegin f = open(file,"r") lines = f.readlines() leng =len(lines) i=0 while i<leng: ret = HandleCComment(lines,i) if ret == -1: if bhasCCommentBegin == False: print "There must be some wrong" del lines[i] i -= 1 leng -= 1 elif ret== 0: HandleCPlusPlusComment(lines,i) else: pass i+=1 Output(lines)bhasCCommentBegin = Falsedef Output(lines): for line in lines: print line,if __name__== '__main__': RemoveComment(sys.argv[1])
python面试题--去除C++源文件里的注释
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。