中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

刪除C語言程序中所有的注釋語句的實現(xiàn)代碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
一種解法非常好:狀態(tài)機。在各種狀態(tài)之間跳轉(zhuǎn),邏輯清晰,不易出錯,出錯了也容易調(diào)試。

下面把代碼貼出來:

    #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*/");  
        }  
    }  
      
    /*狀態(tài)機函數(shù)*/  
    void change_state(int c){  
        if (state==0) {//普通狀態(tài)  
            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) {// "//"注釋狀態(tài)  
            if (c=='\n') {  
                state = 0;  
                putchar(c);  
            }else{  
                state = 2;  
            }  
        }else if (state==3) {// "/*"注釋狀態(tài)  
            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);  
        }  
    }  

標簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:使用python3.4解析xml文件(sax、dom、etree)

下一篇:php發(fā)送郵件函數(shù),支持html和普通文本