团体作业

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
 
void CharCount();
void WordCount();
void LineCount();
 
int main(int argc,char *argv[])
{
if ((strcmp(argv[1], "-c") == 0) && (strcmp(argv[2], "H:\C\Wordcount\Wordcount\file.txt") == 0))
{
CharCount();
}
 
if ((strcmp(argv[1], "-w") == 0) && (strcmp(argv[2], "H:\C\Wordcount\Wordcount\file.txt") == 0))
 
WordCount();
}
if ((strcmp(argv[1], "-l") == 0) && (strcmp(argv[2], "H:\C\Wordcount\Wordcount\file.txt") == 0))
{
LineCount();
}
}
void CharCount()
{
FILE *fp;
int c = 0;
char ch;
if((fp = fopen("H:\C\Wordcount\Wordcount\file.txt","r")) == NULL)
{
printf("Failure!");
}
ch = fgetc(fp);
while(ch != EOF)
{
c ++;
ch = fgetc(fp);
}
printf("文件字符数为:%d.\n",c);
fclose(fp);
}
void WordCount()
{
FILE *fp;
int w = 0;
char ch;
if((fp = fopen("H:\C\Wordcount\Wordcount\file.txt","r")) == NULL)
{
printf("Failure!");
}
if(ch>='A'&&ch<='Z')
{
ch+=32;
ch = fgetc(fp);
}
else:
ch = fgetc(fp);
while(ch != EOF)
{
if ((ch >= 'a'&&ch <= 'z')||(ch >= 'A'&&ch <='Z'))
{
while ((ch >= 'a'&&ch <= 'z')||(ch >= 'A'&&ch <= 'Z')||(ch >= '0'&&ch <= '9')||ch == ' ')
{
ch = fgetc(fp);
}
w ++;
ch = fgetc(fp);
}
else
{
ch = fgetc(fp);
}
}
printf("文件单词数为:%d.\n",w);
fclose(fp);
 
}
void LineCount()
{
FILE *fp;
int l = 1;
char ch;
if((fp = fopen("H:\C\Wordcount\Wordcount\file.txt","r")) == NULL)
{
printf("Failure!");
}
ch = fgetc(fp);
while(ch != EOF)
{
if (ch == '\n')
{
l ++;
ch = fgetc(fp);
}
else
{
ch = fgetc(fp);
}
}
printf("文件有效行数为:%d.\n",l);
fclose(fp);
}
仓库代码:https://gitee.com/XMHbb/software_engineering/tree/Wordcount/

猜你喜欢

转载自www.cnblogs.com/gaofenzi/p/10284212.html