求助
时间:2011-12-04
来源:互联网
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
///
bool isShorter(const string &s1,const string &s2)
{
return s1.size() < s2.size();
}
////
bool GT4(const string &s)
{
return s.size() >= 4;
}
////
string make_plural(size_t ctr,const string &word,const string &ending)
{
return (ctr == 1) ? word : word + ending;
}
////////////////////////////
int main(int argc,char **argv)
{
if(argc < 2)
{
cerr << "NO FILE INPUT!" <<endl;
return EXIT_FAILURE;
}
ifstream inFile;
inFile.open(argv[1]);
if(!inFile)
{
cerr << "can't open the file!" <<endl;
return EXIT_FAILURE;
}
vector<string> words;
string word;
while (inFile >> word)
words.push_back(word);
sort(words.begin(),words.end());
words.erase(unique(words.begin(),words.end()),words.end());
stable_sort(words.begin(),words.end(),isShorter);
vector<string>::size_type wc = count_if (words.begin(),words.end(),GT4);
cout << wc << "" << make_plural(wc,"word","s") << "4 characters or longer" <<endl;
cout << "unique words: " <<endl;
for (vector<string>::iterator iter = words.begin();iter !=words.end();++iter)
cout << *iter << " ";
cout << endl;
return 0;
}
程序功能统计长度不小于4的单词,并输出输入序列中不重复的单词。
怎么把文件输入呢?编译运行出现:argc = 1,"NO FILE INPUT!"
哪位同仁指导一下,谢谢~
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
///
bool isShorter(const string &s1,const string &s2)
{
return s1.size() < s2.size();
}
////
bool GT4(const string &s)
{
return s.size() >= 4;
}
////
string make_plural(size_t ctr,const string &word,const string &ending)
{
return (ctr == 1) ? word : word + ending;
}
////////////////////////////
int main(int argc,char **argv)
{
if(argc < 2)
{
cerr << "NO FILE INPUT!" <<endl;
return EXIT_FAILURE;
}
ifstream inFile;
inFile.open(argv[1]);
if(!inFile)
{
cerr << "can't open the file!" <<endl;
return EXIT_FAILURE;
}
vector<string> words;
string word;
while (inFile >> word)
words.push_back(word);
sort(words.begin(),words.end());
words.erase(unique(words.begin(),words.end()),words.end());
stable_sort(words.begin(),words.end(),isShorter);
vector<string>::size_type wc = count_if (words.begin(),words.end(),GT4);
cout << wc << "" << make_plural(wc,"word","s") << "4 characters or longer" <<endl;
cout << "unique words: " <<endl;
for (vector<string>::iterator iter = words.begin();iter !=words.end();++iter)
cout << *iter << " ";
cout << endl;
return 0;
}
程序功能统计长度不小于4的单词,并输出输入序列中不重复的单词。
怎么把文件输入呢?编译运行出现:argc = 1,"NO FILE INPUT!"
哪位同仁指导一下,谢谢~
作者: yang_xiao_tao 发布时间: 2011-12-04
命令行参数输入你的文件名
作者: sduxiaoxiang 发布时间: 2011-12-04
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28