Code:
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 |
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main() {
char line[100];
char *eof;
const char lines, blank = " ", comment1 = "//", comment2 = "/*", ints = "int", longs = "long", floats = "float", doubles = "double", chars = "char", ifs = "if", elses = "else", fors = "for", switches = "swtich", semicolons = ";", structs = "struct", arrays = "[", blocks = "{";
int totalBlanks, totalComments, totalComment1, totalComment2, totalInts, totalLongs, totalFloats, totalDoubles, totalChars, totalIfs, totalElses, totalFors, totalSwitches, totalSemicolons, totalStructs, totalArrays, totalBlocks;
FILE* inFile = NULL;
const char *p = inFile;
printf("Opening file Battle_ship.txt");
inFile = fopen(inFile,"Battle_ship.cpp", "r");
if (inFile == NULL) {
printf("Could not open file Battle_ship.cpp. \n");
return -1;
}
fgets(line, 100, inFile);
while (!feof(inFile)) {
fgets(line, 100, inFile);
}
while ((p = strstr(p, blank)) != NULL) {
p += strlen(" ");
totalBlanks++;
}
while ((p = strstr(p, comment1)) != NULL) {
p += strlen("//");
totalComment1++;
}
while ((p = strstr(p, comment2)) != NULL) {
p += strlen("/*");
totalComment2++;
}
while ((inFile = strstr(p, ints)) != NULL) {
p += strlen("int");
totalInts++;
}
while ((p = strstr(p, longs)) != NULL) {
p += strlen("long");
totalLongs++;
}
while ((p = strstr(p, floats)) != NULL) {
p += strlen("float");
totalFloats++;
}
while ((p = strstr(p, doubles)) != NULL) {
p += strlen("double");
totalDoubles++;
}
while ((p = strstr(p, chars)) != NULL) {
p += strlen("char");
totalBlanks++;
}
while ((p = strstr(p, ifs)) != NULL) {
p += strlen("if");
totalIfs++;
}
while ((p = strstr(p, elses)) != NULL) {
p += strlen("else");
totalElses++;
}
while ((p = strstr(p, fors)) != NULL) {
p += strlen("for");
totalFors++;
}
while ((p = strstr(p, switches)) != NULL) {
p += strlen("switch");
totalSwitches++;
}
while ((p = strstr(p, semicolons)) != NULL) {
p += strlen(";");
totalSemicolons++;
}
while ((p = strstr(p, structs)) != NULL) {
p += strlen("struct");
totalStructs++;
}
while ((p = strstr(p, arrays)) != NULL) {
p += strlen("[");
totalArrays++;
}
while ((p = strstr(p, blocks)) != NULL) {
p += strlen("[");
totalBlocks++;
}
totalComments = totalComment1 + totalComment2;
totalArrays /= 2;
totalBlocks /= 2;
printf("%d occurences of %c\n", lines, line);
printf("%d occurences of %c\n", totalBlanks, blank);
printf("%d occurences of total comments", totalComments);
printf("%d occurences of %c\n", totalInts, ints );
printf("%d occurences of %c\n", totalLongs, longs);
printf("%d occurences of %c\n", totalFloats, floats );
printf("%d occurences of %c\n", totalDoubles, doubles);
printf("%d occurences of %c\n", totalChars, chars);
printf("%d occurences of %c\n", totalIfs, ifs);
printf("%d occurences of %c\n", totalElses, elses);
printf("%d occurences of %c\n", totalFors, fors);
printf("%d occurences of %c\n", totalSwitches, switches);
printf("%d occurences of %c\n", totalSemicolons, semicolons);
printf("%d occurences of %c\n", totalStructs, structs);
printf("%d occurences of %c\n", totalArrays, arrays);
} |