问题 1507: [蓝桥杯][算法提高VIP]去注释

题目链接:点击这里

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>

using namespace std;

int main()
{
	char c;
	char pre = 'x';
	bool dan = false, duo = false;
	
	while((c = cin.get()) != EOF)
	{
		if(dan)             //进入单行注释 
		{
			if(c=='\n')     //遇到换行结束
			{
				cout<<endl;
				dan = false;
			}
			continue;
		}
		
		if(duo)             //进入多行注释
		{
			if(pre == '*' && c == '/')  duo = false;    //遇到*/结束
			else    pre = c;    //此时需要记录前一个字符作为判断
			continue;
		}
		
		if(c == '/')
		{
		    c = getchar();
			if(c == '/')    dan = true;
			else if(c == '*')   duo = true;
			else cout<<"/"<<c;
		}
		else
		    cout<<c;
	}
	
	return 0;
}
发布了822 篇原创文章 · 获赞 127 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104943823