-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint_NonComment.cpp
More file actions
83 lines (70 loc) · 1.48 KB
/
Print_NonComment.cpp
File metadata and controls
83 lines (70 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Implement a method called printNonComments() which prints out a extract of text with comments removed.
For example, the input:
hello /* this is a
multi line comment */ all
Should produce:
hello
all
You have access to a method called getNextLine() which returns the next line in the input string.
*/
// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "vector"
#include "iostream"
using namespace std;
int i = 0;
vector<string> vec;
string getNextLine()
{
return vec[i++];
}
void printNonComment()
{
bool inComment = false, hasEnter = false;
string s = getNextLine();
while (!s.empty())
{
for (int i = 0; i < s.size(); i++)
{
if (!inComment)
{
if (s[i] == '/'&&i + 1 < s.size() && s[i + 1] == '*')
{
inComment = true;
i++;
continue;
}
cout << s[i];
}
else
{
if (s[i] == '*'&&i + 1 < s.size() && s[i + 1] == '/')
{
inComment = false;
i++;
int j = i + 1;
if (hasEnter)
while (j < s.size() && s[j] == ' ')s.erase(s.begin() + j);
hasEnter = false;
continue;
}
}
}
if (!(inComment&&hasEnter))
{
cout << endl;
}
if (inComment)hasEnter = true;
s = getNextLine();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vec.push_back("hello /* this /*is amul");
vec.push_back("ti line comment */ all");
vec.push_back("");
printNonComment();
return 0;
}