去除C程序中的注释
一、问题简述
- 在给出一段c语言代码后,删除其中的注释部分,其余部分不变。C语言中的注释为/**/,不考虑c++中的单行注释//。
- 确保给出的c程序的正确性。
二、背景题目(北大OJ平台)
-
描述
C程序的注释用/*…*/来表示。请写一个程序,将输入的C程序源代码中的注释去掉,输出去掉注释之后的源代码。用于测试的C代码保证符合语法,不使用C++的//注释语法。注意,C语言不允许出现嵌套注释。具体来说,对于/*/**/"*/",如果不允许嵌套注释,那么它表示字符串"*/";如果允许嵌套注释,它表示一个引号"。还请注意,字符串中出现的注释符/*属于字符串的一部分,注释中出现的双引号"属于注释的一部分。
-
输入
符合语法的C代码文本文件。代码每行不超过200个字符。
-
输出
去掉注释后的C代码。要求只能去掉注释,不可以做其他的修改,比如调整缩进,去除注释之外的换行符等。
-
样例输入
#include #include #include /*Hash Search: Hash function: division method; handling collisions: open addressing's linear probing. In this exercise, M is the basic area's length, all keys are non negative integers.*/ #define M 11 int hash(int key) { return key % M; } void init_hash(int* hashtable) { int i; for(i = 0; i < M; ++i) { hashtable[i] = -1; } } /*return value: 1:found, *position is the key's index; 0:not found, *position is where to insert the key; -1:overflow. */ int search_hash(int* hashtable, int key, int* position) { int i, h = hash(key); for(i = 0; i < M; ++i) { if(key == hashtable[h]) { *position = h; return 1; } if(-1 == hashtable[h]) { *position = h; return 0; } h = (h+1) % M; } *position = -1; return -1; } /*return value: 1:inserted, 0:overflow*/ int insert_hash(int* hashtable, int key) { int position, result; result = search_hash(hashtable, key, &position); if(-1 == result) return 0; hashtable[position] = key; return 1; } void main() { int hashtable[M]; init_hash(hashtable); srand(time(NULL)); int i, j, key; for(i = 0; i < 8; ++i) /*make a hash table with 8 elements*/ { key = rand() % 50; insert_hash(hashtable, key); printf("Insert %d\n", key); for(j = 0; j < M; ++j) printf("%3d", hashtable[j]); printf("\n"); } printf("Please input the key to search:\n"); scanf("%d", &key); i = search_hash(hashtable, key, &j); if(1 == i) printf("Found!Its index is %d\n", j); else printf("Not found!\n"); }
-
样例输出
#include #include #include #define M 11 int hash(int key) { return key % M; } void init_hash(int* hashtable) { int i; for(i = 0; i < M; ++i) { hashtable[i] = -1; } } int search_hash(int* hashtable, int key, int* position) { int i, h = hash(key); for(i = 0; i < M; ++i) { if(key == hashtable[h]) { *position = h; return 1; } if(-1 == hashtable[h]) { *position = h; return 0; } h = (h+1) % M; } *position = -1; return -1; } int insert_hash(int* hashtable, int key) { int position, result; result = search_hash(hashtable, key, &position); if(-1 == result) return 0; hashtable[position] = key; return 1; } void main() { int hashtable[M]; init_hash(hashtable); srand(time(NULL)); int i, j, key; for(i = 0; i < 8; ++i) { key = rand() % 50; insert_hash(hashtable, key); printf("Insert %d\n", key); for(j = 0; j < M; ++j) printf("%3d", hashtable[j]); printf("\n"); } printf("Please input the key to search:\n"); scanf("%d", &key); i = search_hash(hashtable, key, &j); if(1 == i) printf("Found!Its index is %d\n", j); else printf("Not found!\n"); }
-
提示
注意字符串,字符,转义字符的情况。
看看自己有没有考虑"a\"/*ccc*/"
这种情况。
三、算法分析(c++)
-
使用getchar()函数逐一输入给定的c程序代码。
-
将可能出现的情况分为三类。
-
当输入的字符ch为双引号(”)或者单引号(‘)时,表示到来的是字符串或字符,那么只需要将匹配到第二个双引号(“)或单引号(’)之前的字符原样输出(引号也要输出);
当输入的字符ch是转义符时,因为要将整个转义符输出,所以在匹配到一个斜杠(\)的时候就要把下一个字符也输出;
(以上可合为一个if分支,同时判断输入字符是否为单双引号,并在该if分支中判断是否遇到转义符,因为以确保c程序的正确性,所以其他地方不会遇到转义符) -
当遇到注释符(/*)时,只接收字符不输出字符,相当于删除了c程序中的注释部分。
此时可以定义两个中间变量,第二个中间变量赋值给第一个中间变量,然后用第二个中间变量来获取字符,避免两个字符同时获取时跳过结束的注释符(*/)。
-
当以上两种情况都不符合,那此时获取的字符只需要原样输出即可。
-
-
注意:在转义符那里需要格外留意。
- 对于"a\"/*ccc*/"来说:
- 若不考虑转义符的输出:“a\”"
- 正确结果为:“a\”/*ccc*/"
- 原因在于若不考虑转义符,在获取字符时会把a后面的转义符(\")看作(\)和(“),此时会与第一个双引号匹配,后面的注释也就不属于字符串的内容了,就不会被输出。
- 对于"a\"/*ccc*/"来说:
四、代码实现
#include<iostream>
#include<string>
using namespace std;
int main() {
char ch;
while ((ch=getchar()) != EOF) {
if (ch == '"'||ch=='\'') {
char ch1;
putchar(ch);
while ((ch1 = getchar()) != ch) {
putchar(ch1);
if (ch1 == '\\') {
char cha;
cha = getchar();
putchar(cha);
}
}
putchar(ch1);
}
else if (ch == '/') {
char ch2;
ch2 = getchar();
if (ch2 == '*') {
char tmp1, tmp2;
tmp1 = getchar();
tmp2 = getchar();
while (tmp1!='*'||tmp2!='/')
{
tmp1 = tmp2;
tmp2 = getchar();
}
}
else {
putchar(ch);
putchar(ch2);
}
}
else {
putchar(ch);
}
}
return 0;
}