在线等!!! 题目:统计一篇英文文章中的单词数和行数,写了代码,却无法统计单词数,代码如下:
#include<iostream>#include<fstream>usingnamespacestd;intmain(){ifstreamin("D:\\day.tx...
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("D:\\day.txt");
if(!in)
{
cerr<<"not open \n";
return 1;
}
bool isalph(char );
bool change=false;
int line=0,word=0,i;
char c[1000];
while(!in.eof())
{
in.getline(c,1000);
line++;
i=0;
while(c[i]!=0)
{
if(!isalph(c[i]))
change=false;
else if(isalph(c[i]) && change==false)
{
word++;
change=true;
}
i++;
}
}
cout<<"Line="<<line<<endl;
cout<<"word="<<word<<endl;
return 0;
}
bool isalph(char c)
{
return ((c>='a'&&c<='z')&&(c>='A'&&c<='Z'));
} 展开
#include <fstream>
using namespace std;
int main()
{
ifstream in("D:\\day.txt");
if(!in)
{
cerr<<"not open \n";
return 1;
}
bool isalph(char );
bool change=false;
int line=0,word=0,i;
char c[1000];
while(!in.eof())
{
in.getline(c,1000);
line++;
i=0;
while(c[i]!=0)
{
if(!isalph(c[i]))
change=false;
else if(isalph(c[i]) && change==false)
{
word++;
change=true;
}
i++;
}
}
cout<<"Line="<<line<<endl;
cout<<"word="<<word<<endl;
return 0;
}
bool isalph(char c)
{
return ((c>='a'&&c<='z')&&(c>='A'&&c<='Z'));
} 展开
1个回答
展开全部
#include <iostream>
#include <fstream>
using namespace std;
bool isalph(char);
int main()
{
ifstream in("D:\\day.txt");
if(!in)
{
cerr<<"not open \n";
return 1;
}
int line=0,word=0,i;
char c[1000];
while(!in.eof())
{
in.getline(c,1000);
line++;
i=0;
while(c[i]!=0)
{
if(isalph(c[i]))
{
word++;
}
i++;
}
}
cout<<"Line="<<line<<endl;
cout<<"word="<<word<<endl;
return 0;
}
bool isalph(char c)
{
return ((c>='a'&&c<='z')||(c>='A'&&c<='Z'));
}
不明白楼主设置的change变量有何用? return ((c>='a'&&c<='z')&&(c>='A'&&c<='Z'))应该为return ((c>='a'&&c<='z')||(c>='A'&&c<='Z'))。函数声明 bool isalph(char)应该在main函数之前。
#include <fstream>
using namespace std;
bool isalph(char);
int main()
{
ifstream in("D:\\day.txt");
if(!in)
{
cerr<<"not open \n";
return 1;
}
int line=0,word=0,i;
char c[1000];
while(!in.eof())
{
in.getline(c,1000);
line++;
i=0;
while(c[i]!=0)
{
if(isalph(c[i]))
{
word++;
}
i++;
}
}
cout<<"Line="<<line<<endl;
cout<<"word="<<word<<endl;
return 0;
}
bool isalph(char c)
{
return ((c>='a'&&c<='z')||(c>='A'&&c<='Z'));
}
不明白楼主设置的change变量有何用? return ((c>='a'&&c<='z')&&(c>='A'&&c<='Z'))应该为return ((c>='a'&&c<='z')||(c>='A'&&c<='Z'))。函数声明 bool isalph(char)应该在main函数之前。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询