处理单词字符?再按字典顺序排列?
2007-10-22 13:42
288 查看
在C++社区里看到的一道笔试题,自己用STL实现了一下,遇到一个文件操作方面的问题:
void f(const ifstream &fin)
{
char ch = fin.get(); // 这样会出错,如果去掉const就不会出错
}
现在还弄不明白怎么会这样.
/**//*
比如文件file.txt中存放的是单词
单词使用空格,逗号和点分隔,
若文件中是I don 't know you don 't know I know
则要输出
don 't
i
know
you
也就是需要对单词排序,没搞定这道垃圾题
考查知识:文件操作,插入排序? 链表操作?
用STL中的list实现
*/
#include<iostream>
#include<fstream>
#include<list>
#include<string>
using namespace std;
void wordsort(ifstream &fin, list<string> &Q)
...{
string tmp;
char ch;
bool Inword; //判断是否在单词内
Inword = false;
tmp.clear();
while((ch = fin.get()) != EOF)
...{
switch(ch)
...{
case ' ':
case ',':
case '.':
Inword = false;
break;
default:
Inword = true;
break;
}
if(Inword == true)
...{
tmp += (ch >= 'A' && ch <= 'Z') ? ('a' + ch - 'A') : ch;
}
if(!tmp.empty() && !Inword)
...{
Q.push_back(tmp);
tmp.clear();
}
}
if(!tmp.empty())
Q.push_back(tmp);
Q.sort();
Q.unique();
}
int main()
...{
ifstream fin("a.txt");
list<string> Q;
wordsort(fin, Q);
fin.close();
for(list<string>::iterator p = Q.begin(); p != Q.end(); ++p)
cout << *p << ' ';
cout << endl;
return 0;
}
void f(const ifstream &fin)
{
char ch = fin.get(); // 这样会出错,如果去掉const就不会出错
}
现在还弄不明白怎么会这样.
/**//*
比如文件file.txt中存放的是单词
单词使用空格,逗号和点分隔,
若文件中是I don 't know you don 't know I know
则要输出
don 't
i
know
you
也就是需要对单词排序,没搞定这道垃圾题
考查知识:文件操作,插入排序? 链表操作?
用STL中的list实现
*/
#include<iostream>
#include<fstream>
#include<list>
#include<string>
using namespace std;
void wordsort(ifstream &fin, list<string> &Q)
...{
string tmp;
char ch;
bool Inword; //判断是否在单词内
Inword = false;
tmp.clear();
while((ch = fin.get()) != EOF)
...{
switch(ch)
...{
case ' ':
case ',':
case '.':
Inword = false;
break;
default:
Inword = true;
break;
}
if(Inword == true)
...{
tmp += (ch >= 'A' && ch <= 'Z') ? ('a' + ch - 'A') : ch;
}
if(!tmp.empty() && !Inword)
...{
Q.push_back(tmp);
tmp.clear();
}
}
if(!tmp.empty())
Q.push_back(tmp);
Q.sort();
Q.unique();
}
int main()
...{
ifstream fin("a.txt");
list<string> Q;
wordsort(fin, Q);
fin.close();
for(list<string>::iterator p = Q.begin(); p != Q.end(); ++p)
cout << *p << ' ';
cout << endl;
return 0;
}
相关文章推荐
- 编写一个程序,输入一串不超过60个字符组成的且不包含换行符的字符串,将字符串中的A到Z的字母按英文字典字母顺序重新排列,排列后的单词的长度要与原始句子中的长度相同,并且要求只对A到Z的字母重新排列,其
- 题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。例如输入“I am a student.”,则输出“student
- "输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。 句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。 例如输入“I am a student.”,则输出“student
- 给定一个字符串列表,找出单词中最长单词,可以用l列表中的其他单词一次构建一个字符。返回具有最小字典顺序的最长单词。
- 【代码】统计文件中,不同字符出现的次数(可排序,没有处理英文单词、数字和大小写)
- 每日一小练——按字典顺序列出所有排列
- 给你一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在给你一个字典,用户输入一个单词,让你根据字典找出这个单词有多少个兄弟单词。
- 给定一个字符串,你需要颠倒一个句子中每个单词中的字符顺序,同时保留空格和初始单词顺序。
- 每天学习一算法系列(10)(输入一句英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变,句子中单词以空格符隔开)
- 串的处理 在实际的开发工作中,对字符串的处理是最常见的编程任务。本题目即是要求程序对用户输入的串进行处理。具体规则如下: 1. 把每个单词的首字母变为大写。 2. 把数字与字母之间用下划线字符(_)分
- 从标准输入读入一个由字母构成的串(不大于30个字符)。从该串中取出3个不重复的字符,求所有的取法。取出的字符,要求按字母升序排列成一个串。不同的取法输出顺序可以不考虑。
- 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不改变
- 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
- anagrams 查找序列里具有相同字符但顺序不同的单词
- 翻转字符句子中单词顺序
- 翻转句子内单词的顺序,但单词内字符顺序不变,包括标点
- 产生所有排列---字典顺序-----2013年1月23日
- 单词在有且仅有一个字符不相同的情况下是否可以在字典里找到 hash codeforces div2 c Watto and Mechanism
- 每日一小练——按字典顺序列出全部排列
- 面试题38:翻转句子中单词的顺序,单词内字符的顺序不变