本人是个菜鸟,请教各位大师:用C++实现:自己设置一个单词库,再用cin输入一些单词,如果发现输入的单词是单词库中的,则蜂鸣器报警
用C++实现:自己设置一个单词库,再用cin输入一些单词,如果发现输入的单词是单词库中的,则蜂鸣器报警
[解决办法]
功能和大致的工作流程都已经被你说出来了,剩下的就是翻译成C++语言的代码了。
如果发现输入的单词是单词库中的:
- C/C++ code
while(){ //每循环一次读取单词库中的下一个单词,代码自己写 if(输入的单词 == 单词库中的单词) { //执行让蜂鸣器报警的代码 break; }}
[解决办法]
#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;
}
简单实现 =-=
[解决办法]
- C/C++ code
#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;}
[解决办法]
- C/C++ code
#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;}