POJ5470去除C中的注释

去除C中的注释:
总时间限制: 1000ms 内存限制: 65536kB
描述
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/"
这种情况。
解答:

#include<iostream>
using namespace std;
int main() {
	//	string str, str_line;
	//	string file_name;
	//	fstream ifs;
	char str[300];
	string str_lines;
	int str_len, str_out = 0;
	//	cin >> file_name;
	//	ifs.open(file_name, ios::in);
	//	while (getline(ifs, str_line)) {
	while (cin.getline(str, 300)) {
		str_lines = str_lines + str;
		str_lines = str_lines + '\n';
	}
	str_len = str_lines.length();
	while (str_out < str_len) {
		if (str_lines[str_out] == '"') {
			while (!(str_lines[str_out] != '\\' && str_lines[str_out + 1] == '"')) {
				cout << str_lines[str_out++];
			}
			cout << str_lines[str_out++];
			cout << str_lines[str_out++];
		}
		else if (str_lines[str_out] == '/' && str_lines[str_out + 1] == '*') {
			str_out = str_out + 2;
			while (!(str_lines[str_out] == '*' && str_lines[str_out + 1] == '/')) {
				str_out++;
			}
			str_out += 2;
		}
		else {
			cout << str_lines[str_out++];
		}
	}
	//	ifs.close();
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值