| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1635 人关注过本帖
标题:[求助]一道编程题(统计字符)
只看楼主 加入收藏
pingscorpio
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-3-29
收藏
 问题点数:0 回复次数:12 
[求助]一道编程题(统计字符)

统计输入文件中出现的不同单词个数以及每个单词出现的频率,并且将这些单词按照词典排列顺序输出到文件中

输入:
以文件形式记录程序中所需要的数据。
样例输入文件(word.in)
This is a book.Its name is "C Programming".

输出:
结果放在一个文件夹中,该文件第一行为不同的单词的个数,从第二行开始则为每一个单词和其相应出现的频率,单词与频率数之间用空格符分割,单词需按字典顺序排列。

样例输出文件:
(word.out)
9
a 1
book 1
c 1
is 1
its 1
name 1
programming 1
this 1


谢谢

搜索更多相关主题的帖子: 字符 统计 
2006-03-29 19:05
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
楼主有什么不会的地方可以问,单纯求程序,恐怕会令你失望的

2006-03-29 19:21
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
同意楼上的

先要试着去写

实在有哪个地方不会了

再提出来~

有实力才会有魅力 实力来自坚持不懈的努力
2006-03-29 21:04
pingscorpio
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-3-29
收藏
得分:0 

我是不太会如何输入数据,用字符串还是别的?如何判断字符结束?
谢谢

2006-03-29 21:58
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 

[CODE]ifstream fin("x.txt"); //建立一个文件输入流
if(!fin.is_open()) //判断文件是否打开.
{
cerr<<"file open failure."<<endl;
exit(1);
}

string str;
while(fin>>str) //以空白字符为间隔,读入串,到文件尾退出循环.
{
//...
//...
}[/CODE]
大概是这样吧.


2006-03-30 10:51
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 

下面这个程序区分大小写,如果想不区分大小写,需要overload一下 "<" operator

程序代码:

#pragma warning(disable: 4786)
#include <map>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main(int argc, char* argv[])
{
string str;
map<string, int> wordCount;
map<string, int>::const_iterator itWord;

ifstream ifs(\"wordfile.txt\");

if (!ifs)
{
cout << \"Cannot open file\" << endl;
return 0;
}

while (ifs >> str)
wordCount[str] += 1;

for (itWord = wordCount.begin(); itWord != wordCount.end(); ++itWord)
cout << itWord->first << \" : \" << itWord->second << endl;

return 0;
}


http://myajax95./
2006-03-30 12:34
wanglff
Rank: 2
等 级:新手上路
威 望:5
帖 子:375
专家分:0
注 册:2005-12-21
收藏
得分:0 

我记得这个问题我以前发过
哎~~
楼主还是自己想想
写写对自己有好处
#include<iostream>
#include<string>
#include<vector>

using namespace std;

inline bool compare(const string& s1, const string& s2)
{ return s1 == s2; }

int main()
{
vector<string> words;
vector<int> times;

cout << "Please enter some words"
" ctrl + z.";

cout << endl;

string s;

// words 包含了用户输入的所有的单词( 重复的仅有一次 )
// times 包含了该单词出现的次数
while(cin >> s)
{
vector<string>::size_type i = 0;


while(i != words.size())
{
if(compare(s, words[i]))
++times[i];
else
{
words.push_back(s);
times[i] = 1;
}

++i;
}

if(words.size() == 0)
{
words.push_back(s);
times[i] = 1;
}
}

vector<string>::size_type iter = 0;

//目前为止已输出了 iter 个单词 当然还有它出现的次数
while(iter != words.size())
{
cout << "Word " << words[iter] << ' '
<< times[iter] << " time(s). ";

cout << endl;
++iter;
}

cout << endl;

return 0;
}

[此贴子已经被作者于2006-3-31 17:14:28编辑过]


自强不息:)
2006-03-31 17:01
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
楼上的程序好像没按字母排序,而且每输入一个数字就需要线性的从头到尾比较一遍。

http://myajax95./
2006-03-31 18:43
pingscorpio
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-3-29
收藏
得分:0 

为什么每次都弹出windows对话框显示错误啊?

2006-03-31 22:49
pingscorpio
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-3-29
收藏
得分:0 

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <fstream>


using namespace std;

bool ischar(char ch)
{
if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
return true;
return false;
}

int main()
{
vector<string> word;
ifstream fin("word.in");

if ( !fin )
{
cerr << "oops! unable to open file "<< endl;
}

char ch;
while ( fin )
{
string tmp;
ch = fin.get();
if (ischar(ch))
{
while( ischar(ch) )
{

if ( isupper(ch) )
{
ch = tolower(ch);
}
tmp += ch;
ch = fin.get();
}
word.push_back(tmp);
}
}

const long len = word.size();
/*for (int j = 0; j < len; ++j)
cout << word[j] << ';';*/
fin.close();

map<string,int> count;
for(int i = 0; i < len; ++i)
count[word[i]]++;

map<string,int>::iterator iter;

ofstream fout("word.out");
if ( !fout )
{
cerr << "oops! unable to open file "<< endl;
}
fout << len << endl;
for (iter = count.begin(); iter != count.end(); iter++)
fout << iter->first << " " << iter->second << endl;
return 0;
}

2006-09-01 00:07
快速回复:[求助]一道编程题(统计字符)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017747 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved