写一个查找单词c++代码
时间: 2023-04-08 07:02:43 浏览: 68
我可以回答这个问题。以下是一个简单的查找单词c的代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[100], word[10];
int i, j, len, word_len, count = 0;
printf("Enter a string: ");
gets(str);
printf("Enter the word to search: ");
gets(word);
len = strlen(str);
word_len = strlen(word);
for (i = 0; i <= len - word_len; i++) {
for (j = 0; j < word_len; j++) {
if (str[i + j] != word[j])
break;
}
if (j == word_len) {
count++;
}
}
printf("'%s' appears %d times in '%s'.", word, count, str);
return 0;
}
```
注意:这只是一个简单的示例代码,实际应用中可能需要考虑更多的情况和优化。
相关问题
单词检查c++怎么写,有没有代码
实现单词检查可以使用哈希表或者字典树(Trie)等数据结构。这里提供一个使用字典树实现单词检查的 C++ 代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MAX_NODE = 26; // 字典树最大节点数
class TrieNode {
public:
TrieNode() : is_word(false), children(MAX_NODE, nullptr) {}
bool is_word; // 标记该节点是否为单词结尾
vector<TrieNode*> children; // 子节点指针数组
};
class Trie {
public:
Trie() : root(new TrieNode()) {}
void insert(const string& word) {
TrieNode* p = root;
for (char c : word) {
int index = c - 'a';
if (!p->children[index]) {
p->children[index] = new TrieNode();
}
p = p->children[index];
}
p->is_word = true;
}
bool search(const string& word) {
TrieNode* p = root;
for (char c : word) {
int index = c - 'a';
if (!p->children[index]) {
return false;
}
p = p->children[index];
}
return p->is_word;
}
private:
TrieNode* root; // 根节点指针
};
int main() {
Trie trie;
trie.insert("hello");
trie.insert("world");
cout << trie.search("hello") << endl; // 输出 1 (true)
cout << trie.search("world") << endl; // 输出 1 (true)
cout << trie.search("hi") << endl; // 输出 0 (false)
return 0;
}
```
上述代码中,`TrieNode` 表示字典树的节点,包含一个布尔型变量 `is_word` 表示该节点是否为单词结尾,以及一个指向子节点的指针数组 `children`。`Trie` 类表示字典树,包含一个根节点指针 `root` 和 `insert` 和 `search` 两个方法。`insert` 方法用于向字典树中插入单词,`search` 方法用于查找单词是否在字典树中。在 `insert` 方法中,我们遍历单词的每个字符,若该字符对应的子节点为空,则创建一个新的节点,并将指针存入 `children` 数组中;最后将最后一个节点标记为单词结尾。在 `search` 方法中,我们同样遍历单词的每个字符,若某个字符对应的子节点不存在,则说明单词不在字典树中,返回 `false`;否则继续遍历,直到遍历完整个单词,如果最后一个节点被标记为单词结尾,则说明单词存在于字典树中,返回 `true`。
设计一份C++的代码
好的,这里是一个简单的 C++ 代码示例,实现了您提出的英文字典软件的要求,包括查找单词、添加单词、删除单词、查找相近单词和凯撒编码破译功能。请注意,此代码仅作为示例,实际应用中可能需要更复杂的实现和更完善的异常处理。
```c++
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
// 定义字典类型为 map<string, string>
typedef map<string, string> Dictionary;
// 定义相似度类型为 map<string, double>
typedef map<string, double> Similarity;
// 定义单词列表类型为 vector<string>
typedef vector<string> WordList;
// 读取字典文件,返回字典对象
Dictionary readDictionary(const string& fileName) {
Dictionary dict;
ifstream file(fileName);
if (file.is_open()) {
string line;
while (getline(file, line)) {
string word, meaning;
size_t pos = line.find(' ');
if (pos != string::npos) {
word = line.substr(0, pos);
meaning = line.substr(pos + 1);
dict[word] = meaning;
}
}
file.close();
}
return dict;
}
// 写入字典文件,将字典对象写入指定文件
void writeDictionary(const string& fileName, const Dictionary& dict) {
ofstream file(fileName);
if (file.is_open()) {
for (auto it = dict.begin(); it != dict.end(); ++it) {
file << it->first << " " << it->second << endl;
}
file.close();
}
}
// 查找单词,返回单词的中文解释
string searchWord(const Dictionary& dict, const string& word) {
auto it = dict.find(word);
if (it != dict.end()) {
return it->second;
} else {
return "未找到单词 " + word + " 的中文解释。";
}
}
// 添加单词,将单词和中文解释添加到字典中
void addWord(Dictionary& dict, const string& word, const string& meaning) {
dict[word] = meaning;
}
// 删除单词,从字典中删除指定单词
bool removeWord(Dictionary& dict, const string& word) {
auto it = dict.find(word);
if (it != dict.end()) {
dict.erase(it);
return true;
} else {
return false;
}
}
// 计算两个单词的相似度,返回相似度值
double calculateSimilarity(const string& word1, const string& word2) {
int len1 = word1.length();
int len2 = word2.length();
int maxLen = max(len1, len2);
int minLen = min(len1, len2);
int diffLen = maxLen - minLen;
int sameLen = minLen;
for (int i = 0; i < minLen; i++) {
if (word1[i] != word2[i]) {
diffLen++;
} else {
sameLen++;
}
}
return static_cast<double>(sameLen) / maxLen;
}
// 查找相近单词,返回与指定单词相似度最高的前 n 个单词
WordList searchSimilarWords(const Dictionary& dict, const string& word, int n) {
Similarity sim;
for (auto it = dict.begin(); it != dict.end(); ++it) {
double similarity = calculateSimilarity(word, it->first);
sim[it->first] = similarity;
}
WordList words;
for (int i = 0; i < n; i++) {
double maxSimilarity = 0.0;
string maxWord = "";
for (auto it = sim.begin(); it != sim.end(); ++it) {
if (find(words.begin(), words.end(), it->first) == words.end() && it->second > maxSimilarity) {
maxSimilarity = it->second;
maxWord = it->first;
}
}
if (!maxWord.empty()) {
words.push_back(maxWord);
} else {
break;
}
}
return words;
}
// 凯撒加密算法,将指定字符串加密 n 位
string caesarEncrypt(const string& str, int n) {
string result = "";
for (char c : str) {
if (isalpha(c)) {
char base = isupper(c) ? 'A' : 'a';
char offset = (c - base + n) % 26;
result += base + offset;
} else {
result += c;
}
}
return result;
}
// 凯撒解密算法,将指定字符串解密 n 位
string caesarDecrypt(const string& str, int n) {
return caesarEncrypt(str, 26 - n);
}
// 破译凯撒加密算法,返回可能的明文列表
WordList crackCaesar(const string& str) {
WordList words;
for (int i = 0; i < 26; i++) {
string plainText = caesarDecrypt(str, i);
words.push_back(plainText);
}
return words;
}
int main() {
// 读取字典文件
Dictionary dict = readDictionary("dictionary.txt");
// 查找单词
cout << searchWord(dict, "hello") << endl;
// 添加单词
addWord(dict, "world", "世界");
writeDictionary("dictionary.txt", dict);
// 删除单词
removeWord(dict, "world");
writeDictionary("dictionary.txt", dict);
// 查找相近单词
WordList similarWords = searchSimilarWords(dict, "go", 5);
for (string word : similarWords) {
cout << word << endl;
}
// 凯撒加密和解密
cout << caesarEncrypt("good", 4) << endl;
cout << caesarDecrypt("kssh", 4) << endl;
// 破译凯撒加密
WordList plainTexts = crackCaesar("kssh");
for (string plainText : plainTexts) {
cout << plainText << endl;
}
return 0;
}
```
以上是一个简单的 C++ 代码示例,实现了您提出的英文字典软件的要求,包括查找单词、添加单词、删除单词、查找相近单词和凯撒编码破译功能。希望对您有所帮助!