題目鏈接:點擊這裏
#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;
}