还是太简单了,再复杂一点
时间: 2024-03-18 20:42:29 浏览: 79
好的,这是一个更为复杂的基于C语言的人工智能代码:
```
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_INPUT_LENGTH 1000
#define MAX_RESPONSE_LENGTH 1000
#define MAX_KNOWLEDGE_BASE_ENTRIES 1000
#define MAX_TOPIC_LENGTH 100
typedef struct {
char* input;
char* output;
} knowledgeBaseEntry;
typedef struct {
char* topic;
knowledgeBaseEntry* entries;
int numEntries;
} knowledgeBase;
knowledgeBaseEntry* createKnowledgeBaseEntry(char* input, char* output) {
knowledgeBaseEntry* entry = (knowledgeBaseEntry*) malloc(sizeof(knowledgeBaseEntry));
entry->input = (char*) malloc(strlen(input)+1);
strcpy(entry->input, input);
entry->output = (char*) malloc(strlen(output)+1);
strcpy(entry->output, output);
return entry;
}
void freeKnowledgeBaseEntry(knowledgeBaseEntry* entry) {
free(entry->input);
free(entry->output);
free(entry);
}
knowledgeBase* createKnowledgeBase(char* topic) {
knowledgeBase* kb = (knowledgeBase*) malloc(sizeof(knowledgeBase));
kb->topic = (char*) malloc(strlen(topic)+1);
strcpy(kb->topic, topic);
kb->entries = (knowledgeBaseEntry*) malloc(MAX_KNOWLEDGE_BASE_ENTRIES*sizeof(knowledgeBaseEntry));
kb->numEntries = 0;
return kb;
}
void freeKnowledgeBase(knowledgeBase* kb) {
int i;
for(i = 0; i < kb->numEntries; i++) {
freeKnowledgeBaseEntry(&kb->entries[i]);
}
free(kb->entries);
free(kb->topic);
free(kb);
}
void addKnowledgeBaseEntry(knowledgeBase* kb, char* input, char* output) {
kb->entries[kb->numEntries] = *createKnowledgeBaseEntry(input, output);
kb->numEntries++;
}
int getKnowledgeBaseEntry(knowledgeBase* kb, char* input, knowledgeBaseEntry* entry) {
int i;
for(i = 0; i < kb->numEntries; i++) {
if(strstr(input, kb->entries[i].input) != NULL) {
*entry = kb->entries[i];
return 1;
}
}
return 0;
}
knowledgeBase* createKnowledgeBaseFromFile(char* filename) {
FILE* fp = fopen(filename, "r");
if(fp == NULL) {
printf("无法打开文件 %s !\n", filename);
return NULL;
}
char topic[MAX_TOPIC_LENGTH];
fscanf(fp, "%s", topic);
knowledgeBase* kb = createKnowledgeBase(topic);
char input[MAX_INPUT_LENGTH];
char output[MAX_RESPONSE_LENGTH];
while(fscanf(fp, "%s %[^\n]", input, output) != EOF) {
addKnowledgeBaseEntry(kb, input, output);
}
fclose(fp);
return kb;
}
void printKnowledgeBase(knowledgeBase* kb) {
printf("主题:%s\n", kb->topic);
printf("知识库:\n");
int i;
for(i = 0; i < kb->numEntries; i++) {
printf("%s -> %s\n", kb->entries[i].input, kb->entries[i].output);
}
}
void trim(char* str) {
int i;
int begin = 0;
int end = strlen(str) - 1;
while (isspace(str[begin])) {
begin++;
}
while ((end >= begin) && isspace(str[end])) {
end--;
}
for (i = begin; i <= end; i++) {
str[i - begin] = str[i];
}
str[i - begin] = '\0';
}
int main() {
knowledgeBase* kb = createKnowledgeBaseFromFile("knowledge_base.txt");
if(kb == NULL) {
return 1;
}
printKnowledgeBase(kb);
char input[MAX_INPUT_LENGTH];
char response[MAX_RESPONSE_LENGTH];
knowledgeBaseEntry entry;
printf("你好,我是一个基于C语言的AI程序。请输入你的问题:\n");
while(1) {
fgets(input, MAX_INPUT_LENGTH, stdin);
trim(input);
if(strcmp(input, "exit") == 0) {
printf("再见,祝你有一个愉快的一天!\n");
break;
}
if(getKnowledgeBaseEntry(kb, input, &entry)) {
strcpy(response, entry.output);
}
else {
strcpy(response, "抱歉,我不知道怎么回答你的问题。");
}
printf("%s\n", response);
}
freeKnowledgeBase(kb);
return 0;
}
```
这个代码比之前的代码更为复杂,它使用了动态内存分配来存储AI的知识库,可以从文件中读取知识库。知识库被组织成主题,每个主题包含一个或多个知识库条目。每个知识库条目包含一个输入和一个回复。
此外,这个程序还实现了一个简单的字符串修剪功能,用于去除用户输入中的空格和换行符。
最后,这个程序还实现了一个免费函数,用于释放动态分配的内存。
阅读全文