1. 程式人生 > >關於 switch 函式的用法,新手求教

關於 switch 函式的用法,新手求教

程式碼如下:

#include <stdio.h>
enum{COPY,START,COMMENT,END};
int main(void)
{
    char c;
    int state=COPY;
    while((c=getchar())!=EOF)
    {
        switch(state)
        case COPY:             這個地方正確
           if(c=='/')
               state=START;
            else
                putchar(c);
            break;
        case START:            錯誤case label not within a switch statement
            if(c=='*')
                state=COMMENT;
            else
                {
                    putchar('/');
                    state=(c=='/')?START:(putchar(c),COPY);
                }
            break;
        case COMMENT:          錯誤case label not within a switch statement
            if(c=='*')
                state=END;
            else
                putchar(c);
            break;
        case END:               錯誤case label not within a switch statement
            if(c=='/')
                state=COPY;
            else if(c=='*')
                state=END;
            else
                state=COMMENT;
            break;
    }
return 0;
}