C++ txt单词检索 作业贴=。=

evan2916 2012-05-26 03:47:41
首先声明,这是个作业贴,实现用户输入一个字符,在规定的txt中(不需用户输入是哪个txt,代码里给定),显示该字符出现了几次即可.

在VC6.0下调试,还求高手赐教,我感觉自己问题在于构造函数写错了

就三个文件

words.h:

#ifndef words_wy
#define words_wy
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class words
{
private:
string filename;
string word;
ifstream fin;
public:
words();
words(string &fn,string &wd);
int openfile(string &fn);
void setWord(string &wd);
int statistics();
};
#endif


words.cpp:

#include "words.h"
#define YES 1 /* 输入的单词未完 */
#define NO 0 /* 下面是一个新单词 */
string IntToString(int & i)
{
string s;
stringstream ss(s);
ss<<i;
return ss.str();
}

words::words()
{
filename = "";
word = "";
}

words::words(string &fn,string &wd)
{
filename = fn;
word = wd;
}

int words::openfile(string &fn)
{
fin.open("TomSawyar.txt",ios::in);
return 1;
}

void words::setWord(string &wd)
{
word = wd;
}

int words::statistics()
{
int c,i=0,nl=0, nw=0, nc=0, inword= NO;//nw:单词数,nc:出现次数
int ar[20] = {0};
string strs = "";
while ((c=getchar( ))!= EOF)
{
if(c=='\n')
{
++nl;
}
if(c==' '||c=='\n'||c=='\t'||c=='.'||c==',')
{
i=0;
inword=NO;//表示准备下一个新单词
}
else
{
ar[i] = c;
i++;
for(int j=0;j<=i;j++)
{
int &temp= ar[j];
strs+=IntToString(temp);
if(strs == word)
{
cout<<"<line"<<nl<<">"<<"has the word--"<<word<<endl;
nc++;
}
}
}
if(inword==NO)
{
inword=YES;
++nw;
}
}
cout<<"The word has__"<<nc<<"__ times in the txt"<<endl;
}






main.cpp:

#include "words.cpp"

void main()
{
string word;
cout<<"Please set the word:"<<endl;
cin>>word;
words a("TomSwayer.txt",word);
a.openfile();
a.statistics();
}
...全文
156 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
evan2916 2012-05-26
  • 打赏
  • 举报
回复
先谢谢各位的热心回答!
我对C++的了解非常粗浅,因为是作业题,先想着解决燃眉之急,
我感觉着是构造函数有问题,我们老师说是用const * char 来初始化fn

我想知道怎么修改我的构造函数,来正确的运行,

这是报错的信息:
D:\读书\C++\新建文件夹 (2)\words\main.cpp(9) : error C2664: '__thiscall words::words(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allo
cator<char> > &)' : cannot convert parameter 1 from 'char [14]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\读书\C++\新建文件夹 (2)\words\main.cpp(10) : error C2660: 'openfile' : function does not take 0 parameters
执行 cl.exe 时出错.

main.obj - 1 error(s), 0 warning(s)
bsnry 2012-05-26
  • 打赏
  • 举报
回复
没看过这本书,能否贡献代码

map 也有个find,是比较key,请问 统计单词,用key合适吗??

set好像还这个好吧


[Quote=引用 3 楼 的回复:]

我觉得楼主自己实现这个比较累啊。既然能够用到类了说明是C++的啦,那你就直接使用map来做吧,具体的参考C++ Prime里面介绍用map统计字符串的次数的
[/Quote]
bsnry 2012-05-26
  • 打赏
  • 举报
回复




不知道1楼是什么意思




string::find(目标串, int nPos=0);

用这个函数来查找,否则自己写


#include "stdafx.h"


#include <string>

#include <iostream>

using std::cout;

using std::endl;

using std::string;

int main()
{
string str="nihaohello,hello,345,hello3";
string str2="hello";

int nPos=0;

int count=0;

int i;

int len=str2.length();

while( ( i= str.find(str2 , nPos) )>0 )
{

nPos=i+ len;

count++;

}

cout<<count<<endl;

return 0;
}

W170532934 2012-05-26
  • 打赏
  • 举报
回复
我觉得楼主自己实现这个比较累啊。既然能够用到类了说明是C++的啦,那你就直接使用map来做吧,具体的参考C++ Prime里面介绍用map统计字符串的次数的
geekjack 2012-05-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

include cpp 文件这种行为 ...
[/Quote]
同楼上 还有 宏最好大写吧 比较容易看
turing-complete 2012-05-26
  • 打赏
  • 举报
回复
include cpp 文件这种行为 ...

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧