C语言设计一个词典代码
时间: 2023-04-03 19:00:45 浏览: 80
很高兴回答你的问题。以下是一个简单的 C 语言词典代码示例:
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 100
#define MAX_WORD_LENGTH 20
int main() {
char words[MAX_WORDS][MAX_WORD_LENGTH];
char definitions[MAX_WORDS][MAX_WORD_LENGTH];
int num_words = ;
printf("Welcome to the dictionary!\n");
while (1) {
printf("Enter a word (or 'q' to quit): ");
char input[MAX_WORD_LENGTH];
scanf("%s", input);
if (strcmp(input, "q") == ) {
break;
}
int found = ;
for (int i = ; i < num_words; i++) {
if (strcmp(input, words[i]) == ) {
printf("%s: %s\n", words[i], definitions[i]);
found = 1;
break;
}
}
if (!found) {
printf("Word not found. Please enter a definition: ");
char definition[MAX_WORD_LENGTH];
scanf("%s", definition);
strcpy(words[num_words], input);
strcpy(definitions[num_words], definition);
num_words++;
printf("Word added to dictionary.\n");
}
}
printf("Goodbye!\n");
return ;
}
希望这个代码示例能够帮助你!