#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
/*
描述:这是一个聊天机器人程序一个非常基本的例子 chatterbot1
*/
void chatterbot1()
{
char* Response[] = {
"我听到了!",
"你是在和我说话。",
"继续说,我在听。",
"哈哈,真是非常有趣的谈话。",
"后来呢?"
};
srand((unsigned) time(NULL));
char sInput[5] = {0};
char* sResponse = NULL;
while(1) {
printf(">");
scanf("%4s", &sInput);
fflush(stdin);
if(sInput[0] == 0x71 || sInput[0] == 0x51 ){
printf("和你聊天真的很愉快, 下次再见。\n");
_sleep(1000);
break;
}
int nSelection = rand() % 5;
sResponse = Response[nSelection];
printf("%s\n", sResponse);
}
}
/*
描述:这是一个改进版聊天机器人程序 chatterbot2
*/
const int MAX_RESP = 3;
typedef struct {
char *input;
char *responses[MAX_RESP];
}record;
record KnowledgeBase[] = {
{"WHAT IS YOUR NAME",
{"嗯,我的名字叫瞎聊。",
"你可以叫我公主...,是啊,我是大小姐啊。",
"为什么你想知道我的名字?下一步是要交换QQ号吗?"}
},
{"HI",
{"哈哈,好啊!",
"你是谁?",
"噢,你好!"}
},
{"HOW ARE YOU",
{"我干得不坏!",
"你干得怎么样?",
"为什么你会想知道我是怎么做到的?"}
},
{"WHO ARE YOU",
{"我尼玛就是个程序啊。",
"表说,我知道你知道我是谁。",
"为什么还问?"}
},
{"ARE YOU INTELLIGENT",
{"是的,当然是的。",
"你是怎么想的?",
"哈哈,事实上我确实很聪明。"}
},
{"ARE YOU REAL",
{"这对你真的是个问题吗?",
"啥意思啊?你?",
"我尽量让我看起来像个真正的人类。"}
}
};
size_t nKnowledgeBaseSize = sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0]);
record* find_match(char*
input) {
for(int i = 0; i < nKnowledgeBaseSize;
++i) {
if( !stricmp( KnowledgeBase[i].input, input ) )
return &KnowledgeBase[i];
}
return NULL;
}
void chatterbot2()
{
srand((unsigned) time(NULL));
char sInput[20] = {0};
char* sResponse = NULL;
while(1) {
printf(">");
scanf("%19[^\n]", &sInput);
fflush(stdin);
record* responses = find_match(sInput);
if(sInput == "BYE") {
printf("和你聊天太愉快了,下次再来!\n");
_sleep(1000);
break;
}
else if( !responses )
{
printf("我,我,我不确定我是否能理解你说的是什么 ...\n");
}
else {
int nSelection = rand()
% MAX_RESP;
sResponse = responses->responses[nSelection];
printf("%s\n", sResponse);
}
}
}
// 太久不写代码了,我不确信 main 是否是这样声明的...
int main()
{
chatterbot1();
chatterbot2();
return 0;
}
//我是来学习的