本人是个菜鸟,请教各位大师:用C++实现:自己设置一个单词库,再用cin输入一些单词,如果发现输入的单词是单词库中的,则蜂鸣器报警

liujiayin613 2012-02-03 09:04:51
用C++实现:自己设置一个单词库,再用cin输入一些单词,如果发现输入的单词是单词库中的,则蜂鸣器报警
...全文
109 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2012-02-03
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

using namespace std;

const string StdLibKey[] = {
"abort", "ios_base", "accumulate", "isalpha", "allocator", "islower", "auto_ptr", "ispunct", "back_inserter"
"isspace", "bad_alloc", "istream", "bad_cast", "istream_iterator", "bind2nd", "istringstream", "bitset"
"isupper", "boolalpha", "left", "cerr", "less_equal", "cin", "list", "copy", "logic_error", "count", "lower_bound"
"count_if", "make_pair", "cout", "map", "dec", "max", "deque", "min", "endl", "multimap", "ends", "multiset", "equal_range"
"negate", "exception", "noboolalpha", "fill", "noshowbase", "fill_n", "noshowpoint", "find", "noskipws", "find_end"
"not1", "find_first_of", "nounitbuf", "fixed", "nouppercase", "flush", "nth_element", "for_each", "oct", "front_inserter"
"of", "stream", "fstream", "ostream", "getline", "ostream_iterator", "hex", "ostringstream", "ifstream", "out_of_range"
"inner_product", "pair", "inserter", "partial_sort", "internal", "plus", "priority_queue", "sqrt", "ptrdiff_t"
"stable_sort", "queue", "stack", "range_error", "strcmp", "replace", "strcpy", "replace_copy", "string", "reverse_iterator"
"stringstream", "right", "strlen", "runtime_error", "strncpy", "scientific", "terminate", "set", "tolower", "set_difference"
"toupper", "set_intersection", "type_info", "set_union", "unexpected", "setfill", "uninitialized_copy", "setprecision"
"unitbuf", "setw", "unique", "showbase", "unique_copy", "showpoint", "upper_bound", "size_t", "uppercase", "skipws"
"vector", "sort"
};

const int StdLibKey_size = sizeof(StdLibKey) / sizeof(*StdLibKey) ;

int main()
{
string input;
cout << "请输入C++ namespace 标准库关键字:" << endl;
cin >> input;
for (int i = 0 ; i != StdLibKey_size ; ++i) {
if (input == StdLibKey[i]) {
cout << input << " 是标准库关键字";
break;
}
}
return 0;
}
LAST_MAN 2012-02-03
  • 打赏
  • 举报
回复
C++ 的容器很容易实现,楼上++
CrazyMK 2012-02-03
  • 打赏
  • 举报
回复
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
using namespace std;


int main()
{
list<string> wordList;
string word;
list<string>::iterator iter;
cout << "input words, (q) ends input" << endl;
while(cin >> word)
{
if (word == "q")
{
break;
}
wordList.push_back(word);
}
cout << "input word for check, (q) exits" << endl;
while(cin >> word)
{
if (word == "q")
{
break;
}
iter = find(wordList.begin(), wordList.end(), word);
if (iter != wordList.end())
{
cout << '\a';
}
}
wordList.clear();
return 0;
}
简单实现 =-=
灼眼的超哥 2012-02-03
  • 打赏
  • 举报
回复
功能和大致的工作流程都已经被你说出来了,剩下的就是翻译成C++语言的代码了。
如果发现输入的单词是单词库中的:
while()
{
//每循环一次读取单词库中的下一个单词,代码自己写

if(输入的单词 == 单词库中的单词)
{
//执行让蜂鸣器报警的代码
break;
}
}
灼眼的超哥 2012-02-03
  • 打赏
  • 举报
回复
反正就是用个循环,每次读取库中的一个单词,之后与输入的单词对比。
liujiayin613 2012-02-03
  • 打赏
  • 举报
回复
收益匪浅,谢谢各位大师
hongwenjun 2012-02-03
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <vector>

using namespace std;
bool findStringInVec(string str , vector<string> &StdLibKey)
{
for (auto it = StdLibKey.begin() ;it != StdLibKey.end() ; ++it) {
if (str == *it)
return true;
}
return false;
}

int main()
{
vector<string> StdLibKey = {
"abort", "ios_base", "accumulate", "isalpha", "allocator", "islower", "auto_ptr", "ispunct", "back_inserter"
"isspace", "bad_alloc", "istream", "bad_cast", "istream_iterator", "bind2nd", "istringstream", "bitset"
"isupper", "boolalpha", "left", "cerr", "less_equal", "cin", "list", "copy", "logic_error", "count", "lower_bound"
"count_if", "make_pair", "cout", "map", "dec", "max", "deque", "min", "endl", "multimap", "ends", "multiset", "equal_range"
"negate", "exception", "noboolalpha", "fill", "noshowbase", "fill_n", "noshowpoint", "find", "noskipws", "find_end"
"not1", "find_first_of", "nounitbuf", "fixed", "nouppercase", "flush", "nth_element", "for_each", "oct", "front_inserter"
"of", "stream", "fstream", "ostream", "getline", "ostream_iterator", "hex", "ostringstream", "ifstream", "out_of_range"
"inner_product", "pair", "inserter", "partial_sort", "internal", "plus", "priority_queue", "sqrt", "ptrdiff_t"
"stable_sort", "queue", "stack", "range_error", "strcmp", "replace", "strcpy", "replace_copy", "string", "reverse_iterator"
"stringstream", "right", "strlen", "runtime_error", "strncpy", "scientific", "terminate", "set", "tolower", "set_difference"
"toupper", "set_intersection", "type_info", "set_union", "unexpected", "setfill", "uninitialized_copy", "setprecision"
"unitbuf", "setw", "unique", "showbase", "unique_copy", "showpoint", "upper_bound", "size_t", "uppercase", "skipws"
"vector", "sort"
};

string input;
cout << "请输入C++ namespace 标准库关键字(输入quit退出):" << endl;
while (cin >> input) {
if (findStringInVec(input, StdLibKey))
cout << input << " 是标准库关键字\n";
else
cout << input << " NO!\n";

if (input == string("quit")) {
return -1;
}
}
return 0;
}


编译器 需要开启 C++0X 标准

64,654

社区成员

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

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