我编写了 ac 代码来计算文件中的字符数、位数和行数。不幸的是,行数没有给出确切的计数。我写了下面的代码。
#include<stdio.h>
void scan();
FILE *fp;
int numbercount=0,textcount=0,spacecount=0,newlinecount=0,specialcount=0;
int main(int argc,char *argv[])
{
if(argc<2)
{
printf("\n Enter the filename through the command line ! ");
}
else
{
fp=fopen( argv[1],"r");
if(fp==NULL)
printf("\n Cannot Open the file ");
else
scan();
}
}
void scan()
{
char ch;
while(1)
{
ch=fgetc(fp);
if((ch>=65 && ch<=90)||(ch>=97 && ch<=122))
{
textcount++;
}
else if(ch>=48&&ch<=57)
{
numbercount++;
}
else if(ch==','||ch=='!'||ch=='?'||ch=='.')
{
specialcount++;
}
else if(ch==' ')
{
spacecount++;
}
else if(ch=='\n')
{
newlinecount++;
}
else if(ch==EOF)
break;
}
printf("\n The count of charecters in the text = %d ",textcount);
printf("\n The count of numbers in the text = %d ",numbercount);
printf("\n The count of special charecters in the text = %d",specialcount);
printf("\n The count of newlines = %d ",newlinecount);
printf("\n The number of spaces = %d \n",spacecount);
}
我已将输入文本文件内容如下http://pastebin.com/GXVdqfzT给出,代码将行数设为 6 而不是 11。是否有合适的方法来计算行数。