删除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);
}
}
以本段代码作为输入,结果如下:
[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);
}
感谢@roma823 及其文章:http://blog.csdn.net/roma823/article/details/6364849
原文链接: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
相关文章推荐
- 编写一个删除C语言源程序中的所有注释语句,要正确处理带引号的字符串和字符常量。
- 【1-23】编写一个删除C语言程序中所有的注释语句。要正确处理带引号的字符串与字符常量。再c语言中,注释不允许嵌套。
- 第7周 C语言程序设计(新2版) 练习1-23 删除C语言程序中所有的注释语句(代码有问题?)
- 编写一个删除C语言程序中所有注释语句,正确处理带引号的字符串与字符常量
- C语言删除文件夹下所有代码的注释for Mac
- 编写一个删除c语言中的注释语句/* */的程序
- 删除C语言程序中所有的注释语句,代码实现
- C程序设计语言练习1-23 编写一个删除C语言程序中所有的注释语句
- 【K&R 习题 1-23】删除C/C++语言中的程序中的所有注释语句
- 删除C语言程序中所有的注释语句(有疑问)
- 编写一个删除C语言程序中所有注释语句的程序
- 编写一个删除c语言程序文件中所有的注释语句
- 练习1-23:编写一个删除C语言程序中所有的注释语句。要正确处理带引号的字符串与字符常量。在C语言程序中,注释不允许嵌套
- SqlServer删除所有存储过程和所有表、查询表是否存在指定的记录及UPDATE语句
- 一次性删除数据库所有表和所有存储过程 SQL语句
- sql 语句 写一个函数T-sql函数elimination(a,b)将出现在第一个字符串中的第二个字符串中的所有字符删除,即:select dbo.elimination('abcdefg123
- 用正则删除vb代码中所有注释的方法
- 一次性删除数据库所有表和所有存储过程 SQL语句
- 一条删除所有表中数据的SQL语句
- 自改的删除数据库中所有外键语句