您的位置:首页 > 编程语言 > C语言/C++

删除c语言中的所有注释语句

2015-01-16 10:57 429 查看

原文链接:http://lixing123.com/archives/310

学习《C程序设计语言》到第1章最后,有一道题目:

[html]
view plaincopyprint?





编写一个删除C语言程序中所有的注释语句。要正确处理带引号的字符串与字符常量。在C语言中,注释不允许嵌套。  

下面把代码贴出来:

[cpp]
view plaincopyprint?





#include <stdio.h>  
  
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;  

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

#include <stdio.h>

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


以本段代码作为输入,结果如下:

[cpp]
view plaincopyprint?





#include <stdio.h>  
  
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 ( (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) {  
        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);  
    }  

#include <stdio.h>

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 ( (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 (
b2ec
state==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);
}
perfect!

感谢@roma823 及其文章:http://blog.csdn.net/roma823/article/details/6364849
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐