c语言的注释删除c语言中的注释,删除C语言程序中所有的注释语句的实现代码

http://www.open-open.com/code/view/1431333852357

2015.05一种解法非常好:状态机。在各种状态之间跳转,逻辑清晰,不易出错,出错了也容易调试。

下面把代码贴出来:

#include

int state;

int c1,c2;

void change_state(int c);

int main(int argc, const char * argv[]) {

int c;

state = 0;

c1 = 0;

c2 = 0;

while ((c=getchar())!=EOF) {

c1 = c2;

c2 = c;

change_state(c);

}

if (/* DISABLES CODE */ (0)==1) {

printf("just test://abcd");

printf("just test:/*hello*/");

}

}

/*状态机函数*/

void change_state(int c){

if (state==0) {//普通状态

if (c=='/') {

state = 1;

}else if (c=='"'){

state = 5;

putchar(c);

}else if (c=='\''){

state = 6;

putchar(c);

}

else{

state = 0;

putchar(c);

}

}else if (state==1) {//检测到1个'/'

if (c=='/') {

state = 2;

}else if (c=='*'){

state = 3;

}else{

state = 0;

putchar(c1);

putchar(c);

}

}else if (state==2) {// "//"注释状态

if (c=='\n') {

state = 0;

putchar(c);

}else{

state = 2;

}

}else if (state==3) {// "/*"注释状态

if (c=='*') {

state = 4;

}else{

state = 3;

}

}else if (state==4) {

if (c=='/') {

state = 0;

}else{

state = 3;

}

}else if (state==5){//在"字符串里

if (c=='"') {

state = 0;

putchar(c);

}else if(c=='\\'){

state = 7;

putchar(c);

}else{

state = 5;

putchar(c);

}

}else if (state==6){//在'字符里

if (c=='\'') {

state = 0;

putchar(c);

}else if(c=='\\'){

state = 8;

putchar(c);

}else{

state = 6;

putchar(c);

}

}else if (state==7){//在"字符串里的"\"

state = 5;

putchar(c);

}else if (state==8){//在'字符串里的"\"

state = 6;

putchar(c);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值