刪除C語言程序中所有的注釋語句,代碼實現(xiàn)
來源:程序員人生 發(fā)布時間:2015-01-19 09:05:54 閱讀次數(shù):2588次
原文鏈接:http://lixing123.com/archives/310
學(xué)習(xí)《C程序設(shè)計語言》到第1章最后,有1道題目:
編寫1個刪除C語言程序中所有的注釋語句。要正確處理帶引號的字符串與字符常量。在C語言中,注釋不允許嵌套。
Exercise 1⑵3. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments don't nest.
剛開始,我用1種brute-force的方式,對每一個字符進(jìn)行遍歷,然落后行判斷,有無進(jìn)入注釋。
這樣做有1個非常麻煩的問題:只有連續(xù)檢測到“//”或"/*"時,才肯定進(jìn)入了注釋狀態(tài);如果我們只檢測到1個"/",后面就是其它字符串的話,還得將這個單獨的"/"打印出來。
而且有許多的分支狀態(tài),如果用if/else的話,會難以理解,并容易出錯。
在網(wǎng)上搜了1下,發(fā)現(xiàn)有1種解法非常好:狀態(tài)機(jī)。在各種狀態(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)機(jī)函數(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=='
') {
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);
}
}
以本段代碼作為輸入,結(jié)果以下:
#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=='
') {
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
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈