-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp1.cpp
57 lines (57 loc) · 1.34 KB
/
exp1.cpp
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile("loc1.cpp");
if (!inputFile)
{
cout << "Failed to open the input file." << endl;
return 1;
}
string line;
int codeLines = 0;
bool inMultiLineComment = false;
while (getline(inputFile, line))
{
string trimmedLine;
for (char c : line)
{
if (!isspace(c))
{
trimmedLine += c;
}
}
if (inMultiLineComment)
{
int commentEnd = trimmedLine.find("*/");
if (commentEnd == -1)
{
continue;
}
else
{
inMultiLineComment = false;
trimmedLine = trimmedLine.substr(commentEnd + 2);
}
}
if (trimmedLine.empty() || (trimmedLine.find("//") == 0))
{
continue;
}
int commentStart = trimmedLine.find("/*");
if (commentStart != -1)
{
inMultiLineComment = true;
trimmedLine = trimmedLine.substr(0, commentStart);
}
if (!trimmedLine.empty())
{
codeLines++;
}
}
cout << "Number of lines of code: " << codeLines << endl;
inputFile.close();
return 0;
}