我正在尝试计算源代码中某些关键字的数量。我对我的生活只是不知道该怎么做。
例如,我的代码就是这样做
number of total lines number and percentage of blank lines number and percentage of comments (start with // or /*) number and percentages of ints, longs, floats, doubles, char number and percentages of if's number and percentage of else's number and percentage of for's number and percentage of switch number and percentage of semicolons number and percentage of structs number and percentage of arrays (contains [ or ], divide count by 2) number of blocks (contains { or }, divide count by 2)
这是我到目前为止做的一点点
int main()
{
FILE * bsfile;
bsfile = fopen("bship.txt", "r");
char singleLine[150];
while (!feof(bsfile)) {
fgets(singleLine, 150, bsfile);
puts(singleLine);
}
fclose(bsfile);
return 0;
}
它几乎将整个文件写入控制台,而不是关闭。如果有人可以通过向我解释如何找到ifs的数量来帮助我,那将对我有很大的帮助并让我开始。我试过谷歌这样,没有运气我怎么能让这个工作。我知道我想使用带有for()的计数器?
答案 0 :(得分:0)
您正在做的是从文件中读取一行并将其放入字符串中。现在fgets()在循环中使用相同的字符串,这将保留覆盖前一行。这不是将文件数据复制到字符串中的安全方法。您没有注意到这一点,因为您在将其添加到字符串后打印每行。如果你在while循环后使用了put,你就会意识到这个问题。
你应该使用双指针指向字符,并为每一行动态分配二维数组,并通过每个1D数组使用fgets继续获取行,或者使用单个指针并将整个数据复制到使用fgetc()动态分配的单个字符串中
将数据输入字符串后,将strstr()与搜索字符串一起使用来计算特定关键字的重复次数并完成工作。那么你唯一需要做的就是计算每个关键字的百分比,这不是一项艰巨的工作。
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *bk;
bk = fopen("brick.c", "r");
char singleLine[1000];
int a= 0;
int totalLines = 0;
int numberofifs = 0;
int blankLine = 0;
int comLines = 0;
int intLines = 0;
int longLines = 0;
int floatLines = 0;
int doubleLines = 0;
int charLines = 0;
int ifLines = 0;
int elseLines = 0;
int forLines = 0;
int switchLines = 0;
int semiLines = 0;
int structLines = 0;
int arrayLines = 0;
int blockLines = 0;
float percentofbl;
float percentofcl;
float percentofint;
float percentoflong;
float percentoffloat;
float percentofdouble;
float percentofchar;
float percentofif;
float percentofelse;
float percentoffor;
float percentofswitch;
float percentofsemi;
float percentofstruct;
float percentofarray;
float percentofblock;
while (!feof(bk)) {
fgets(singleLine, 1000, bk);
totalLines++;
for( a =0; singleLine[a] != '\0'; a++){ // Loops to scan the whole source code//
// scanning blank lines //
if (a == 0 && singleLine[a] == '\n' && singleLine[a+1] == '\0')
blankLine++;
// scanning comment lines //
if ( singleLine[a] == '/' && ( singleLine[a+1] == '/' || singleLine[a+1] == '*'))
comLines++;
// scanning int line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'i' && singleLine[a+2] == 'n' && singleLine[a+3] == 't')
intLines++;
// scanning longs line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'l' && singleLine[a+2] == 'o' && singleLine[a+3] == 'n'
&& singleLine[a+4] == 'g')
longLines++;
// scanning floats line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'f' && singleLine[a+2] == 'l' && singleLine[a+3] == 'o'
&& singleLine[a+4] == 'a' && singleLine[a+5] == 't')
floatLines++;
// scanning doubles line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'd' && singleLine[a+2] == 'o' && singleLine[a+3] == 'u'
&& singleLine[a+4] == 'b' && singleLine[a+5] == 'l' && singleLine[a+5] == 'e')
doubleLines++;
// scanning chars line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'c' && singleLine[a+2] == 'h' && singleLine[a+3] == 'a'
&& singleLine[a+4] == 'r')
charLines++;
// scanning if's line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'i' && singleLine[a+2] == 'f')
ifLines++;
// scanning else's line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'e' && singleLine[a+2] == 'l' && singleLine[a+3] == 's'
&& singleLine[a+4] == 'e')
elseLines++;
// scanning for line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 'f' && singleLine[a+2] == 'o' && singleLine[a+3] == 'r')
forLines++;
// scanning switch line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 's' && singleLine[a+2] == 'w' && singleLine[a+3] == 'i'
&& singleLine[a+4] == 't' && singleLine[a+5] == 'c' && singleLine[a+5] == 'h')
switchLines++;
// scanning semicolons line //
if (singleLine[a] == ';')
semiLines++;
// scanning structs line //
if ((singleLine[a] == ' ' || singleLine[a] == '\t') && singleLine[a+1] == 's' && singleLine[a+2] == 't' && singleLine[a+3] == 'r'
&& singleLine[a+4] == 'u' && singleLine[a+5] == 'c' && singleLine[a+5] == 't')
structLines++;
// scanning arrays [ line //
if (singleLine[a] == '[')
arrayLines++;
// scanning blocks { line //
if (singleLine[a] == '{')
blockLines++;
percentofbl = (float)blankLine/totalLines * 100;
percentofcl = (float)comLines/totalLines * 100;
percentofint = (float)intLines/totalLines * 100;
percentoflong = (float)longLines/totalLines * 100;
percentoffloat = (float)floatLines/totalLines * 100;
percentofdouble = (float)doubleLines/totalLines * 100;
percentofchar = (float)charLines/totalLines * 100;
percentofif = (float)ifLines/totalLines * 100;
percentofelse = (float)elseLines/totalLines * 100;
percentoffor = (float)forLines/totalLines * 100;
percentofswitch = (float)switchLines/totalLines * 100;
percentofsemi = (float)semiLines/totalLines * 100;
percentofstruct = (float)structLines/totalLines * 100;
percentofarray = (float)arrayLines/totalLines * 100;
percentofblock = (float)blockLines/totalLines * 100;
}
}
printf("The total number of lines is %i\n",totalLines);
printf("The total number of blank lines is %i and percentage is %lf \n",blankLine, percentofbl );
printf("The total number of comment lines is %i and percentage is %lf \n", comLines, percentofcl);
printf("The total number of int lines is %i and percentage is %lf \n", intLines, percentofint);
printf("The total number of long lines is %i and percentage is %lf \n", longLines, percentoflong);
printf("The total number of float lines is %i and percentage is %lf \n", floatLines, percentoffloat);
printf("The total number of double lines is %i and percentage is %lf \n", doubleLines, percentofdouble);
printf("The total number of char lines is %i and percentage is %lf \n", charLines, percentofchar);
printf("The total number of if lines is %i and percentage is %lf \n", ifLines, percentofif);
printf("The total number of else lines is %i and percentage is %lf \n", elseLines, percentofelse);
printf("The total number of for lines is %i and percentage is %lf \n", forLines, percentoffor);
printf("The total number of switch lines is %i and percentage is %lf \n", switchLines, percentofswitch);
printf("The total number of semicolon lines is %i and percentage is %lf \n", semiLines, percentofsemi);
printf("The total number of structs lines is %i and percentage is %lf \n", structLines, percentofstruct);
printf("The total number of arrays lines is %i and percentage is %lf \n", arrayLines, percentofarray);
printf("The total number of blocks lines is %i and percentage is %lf \n", blockLines, percentofblock);
fclose(bk);
return 0;
}