首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何把程序的注释部分去掉

2012-02-05 
怎么把程序的注释部分去掉?#include stdafx.h#includefstream#includestdio.husing namespace stdi

怎么把程序的注释部分去掉?
#include "stdafx.h"
#include<fstream>
#include<stdio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile;
ofstream outfile;
infile.open("EX1_test.c");
outfile.open("result.txt");


  char c,s,a[1000]; int ap;

  s=ap=0; /* Initial statu -- NORMAL mode */

  while ((c=infile.get()) != EOF) {
  switch (s) {
/* NORMAL */
  case 0 : if (c == '/') s=1;/* SEARCH mode */
else outfile<<c;/* NORMAL mode */
  if (c == '"') s=4;/* STRING mode */
if (c == '\'') s=6; /* ' CHAR mode */
break; 
/* SEARCH */
  case 1 : if (c == '*') {
s=2;/* COMMENT mode */
  a[ap++]='/'; a[ap++]='*';/* Buff the comment */
} else {
outfile<<'/';
if (c!='/') {
outfile<<c;/* NORMAL mode */
  s=0;
}
}
  break;
/* COMMENT */
  case 2 : if ((a[ap++]=c) == '*') s=3;/* RETURN mode */
break; 
/* RETURN */
  case 3 : if ((a[ap++]=c) == '/') {
ap=s=0;/* NORMAL mode */
} else if (c != '*') s=2;/* COMMENT mode */
break;/* RETURN mode */
/* STRING */
  case 4 : outfile<<c;/* STRING mode */
if (c == '"') s=0;/* NORMAL mode */
if (c == '\\') s=5; /* \ CHAR mode */
break;
/* " ...\" ..." */
  case 5 : outfile<<c; s=4;/* STRING mode */
break;

/* ' CHAR */
  case 6 : outfile<<c;
if (c == '\'') s=0;/* end CHAR mode */
if (c == '\\') s=7;
break;
/* '\?' */
  case 7 : outfile<<c; s=6;/* in CHAR mode */
break;
  } /* switch */
  } /* while */
  for (c=0; c<ap; c++) outfile<<a[c];/* Print the Buff */

return 0;
}
这段代码不是很懂,是实现去掉源程序的/*.....*/注释工程的
还有,想请问一下,如果要把源程序的//.......形式的注释也去掉,和程序中出现的"....."出现的字符穿用一个字母表示,比如$,如string="hello!"变成string=$ 则需要怎么修改程序呢?谢谢

[解决办法]
读一行,碰到//,就截取包括它后面的
碰到"。。。",用一个字符替换

热点排行