4/100 发布文章 -
要求:1.从文件中获得数据
2.要求能从文件的一连串字符中正确获得想要的数据
3.分析数据,从中能正确获得单词的数目
代码分析:
文件打开函数:
int file(FILE* filename)
{
char str[strsize];
int tag = begin;
FILE* fp=fopen(“filename”,“r”);
if (fp == NULL)
{
printf(“file is error”);
return -1;
}
while (!feof(fp))
{
fgets(str, strsize, fp);
while (*str != ‘\0’)
{
switch (tag)
{
case begin:if (isalpha(*str)) { tag = inword; str++; }
else { tag = outword; str++; }; break;
case inword:if (isalpha(*str) || *str == ‘-’ || *str == ‘’’) { str++; }
else { tag = outword; num++; str++; }; break;
case outword:if (isalpha(str)) { tag = inword; str++; }
else { str++; }break;
}
}
}
int cal(char str)
{
int tag;
int num = 0;
if (str == NULL)return -1;
tag = begin;
计算函数:
while (*str != ‘\0’)
{
switch (tag)
{
case begin:if (isalpha(*str)) { tag = inword; str++; }
else { tag = outword; str++; }; break;
case inword:if (isalpha(*str)||*str==’-’||*str==’’’) { str++; }
else { tag = outword; num++; str++; }; break;
case outword:if (isalpha(*str)) { tag = inword; str++; }
else { str++; }break;
}
if (*str == ‘\0’)
{
if (tag == inword) num++;
}
return num;
}