#2
zmbilx2016-02-10 11:27
程序代码: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> //一个机器人及基本例子 #define MAX_RESP 3 typedef struct A { 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]); void chatterbot1() { char *Response[]={"我听到了!", "你是在和我说话。", "继续说,我在听。", "哈哈,真是非常有趣的谈话。", "后来呢?" }; char sInput[5] = {0}; char *sResponse = NULL; int nSelection; srand((unsigned) time(NULL)); while(1) { printf(">"); scanf("%4s",&sInput); fflush(stdin); if(sInput[0] == 0x71 || sInput[0] == 0x51){ printf("和你聊天真的很愉快,下次再见。\n"); _sleep(1000); break; } nSelection = rand() % 5; sResponse = Response[nSelection]; printf("%s\n", sResponse); } } record *find_match(char *input) { unsigned int i; for(i=0; i<nKnowledgeBaseSize; ++i) { if( !stricmp( KnowledgeBase[i].input, input)) return &KnowledgeBase[i]; } return NULL; } void chatterbot2() { char sInput[20] = {0}; char *sResponse = NULL; record *responses; srand((unsigned) time(NULL)); while(1){ printf(">"); scanf("%19[^\n]", sInput); fflush(stdin); responses = find_match(sInput); if(!strcmp("BYE", sInput)){ printf("和你聊天太愉快了,下次再来!\n"); _sleep(1000); break; } else if( !responses ){ printf("我,我,我不确定我是否能理解你说的是什么...\n"); } else { int nSelection = rand() % MAX_RESP; sResponse = responses->responses[nSelection]; printf("%s\n", sResponse); } } } int main() { chatterbot1(); chatterbot2(); return 0; } VC++6.0 测试 只有本站会员才能查看附件,请 登录 [此贴子已经被作者于2016-2-10 11:28编辑过] |
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//一个机器人及基本例子
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);
}
}
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 nKnowlegeBaseSize = 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);
}
}
}
int main()
{
chatterbot1();
chatterbot2();
return 0;
}
E:\新建文件夹 (2)\qq机器人\main.c|34|error: variably modified 'responses' at file scope|
E:\新建文件夹 (2)\qq机器人\main.c|84|error: 'nKnowledgeBaseSize' undeclared (first use in this function)|