简易聊天机器人O(∩_∩)O哈哈哈~

使用说明:随便输入点什么,复制到Dev-c++上即可使用。(正在完善请多谅解︿( ̄︶ ̄)︿)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<cstdio>
 
using namespace std;
char c[1001];
int main()
{
    cout<<"My name is Jackson,your good friend!"<<endl;
    cout<<"If you have something disturbing,please tell me!QwQ"<<endl;
    if(gets(c))
    {
        cout<<"hi,what can I do for you?"<<endl;
    }
    if(gets(c))
    {
        cout<<"Do not worry,I will try my best to help you!"<<endl;
    }
    return 0;
}

  

  (*^__^*) 嘻嘻……

还有两个在网上找的,也挺好玩的^_^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
 
int main()
{
    std::string Response[]={
        "I heard you!",
        "SO, you are talking to me.",
        "CONTINUE,I am listening.",
        "Very interesting conversation.",
        "TELL me more..."
    };
    srand((unsigned) time(NULL));
    std::string sInput="";
    std::string sResponse="";
    while(1){
        std::getline(std::cin,sInput);
        int nSelection=rand()%5;
        sResponse=Response[nSelection];
        std::cout<<sResponse<<std::endl;
    }
    return 0;
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma warning(disable:4786)
 
#include<iostream>
#include<string>
#include<vector>
#include<ctime>
#include<cstdlib>
 
using namespace std;
 
const int MAX_RESP=3;
 
typedef vector<string>vstring;
 
vstring find_match(string input);
void copy(char*array[],vstring &v);
 
 
typedef struct
{
    char*input;
    char*responses[MAX_RESP];
}record;
 
record KnowledgeBase[]=
{
    {"What is your name",
    {"My name is chatterbot2.",
     "You can call me chatterbot2.",
     "Why do you want to know my name?"}
    },
 
    {"Hi",
    {"Hi there!",
     "How are you?",
     "Hi!"
    }
    },
 
    {"How are you",
    {"I'm doing fine!",
     "I'm doing well and you?",
     "Why do you want to know how am I doing?"}
    },
 
    {"Who are you",
    {"I'm an a.I program.",
     "I think that you know who I'm.",
     "Why are you asking?"}
    },
 
    {"Are you intelligent",
    {"Yes,of course.",
     "What do you think?",
     "Actualy,I'm very intelligent!"}
    },
 
    {"Are You real",
    {"Does that question really maters to you?",
     "What do you mean by that?",
     "I'm as real as I can be."}
    }
};
 
size_t nKnowledgeBaseSize = sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0]);
 
 
int main()
{
    srand((unsigned) time(NULL));
 
    std::string sInput="";
    std::string sResponse="";
 
    while(1)
    {
        cout<<">";
        getline(cin,sInput);
        vstring responses=find_match(sInput);
        if(sInput=="Bye")
        {
            cout<<"It was nice talking to you user,see you next time!"<<endl; 
            break;
        }
        else if(responses.size()==0)
        {
            cout<<"I'will try me best to help you!"<<endl;
        }
        else{
            int nSelection=rand()%MAX_RESP;
            sResponse=responses[nSelection];
            cout<<sResponse<<std::endl;
        }
    }
 
    return 0;
}
 
// make a  search for the  user's input
// inside the database of the program
vstring find_match(string input)
{
    vstring result;
    for(int i=0;i<nKnowledgeBaseSize;++i)
    
        if(string(KnowledgeBase[i].input)==input)
        {
            copy(KnowledgeBase[i].responses,result);
            return result;
        }
    }
    return result;
}
 
void copy(char  *array[],vstring &v)
{
    for(int i=0;i<MAX_RESP;++i)
    {
        v.push_back(array[i]);
    }
}

  原blog上都是大写输出,我稍微改了一下(⊙v⊙)

 ↖(^ω^)↗~~O(∩_∩)O哈哈~~

附上一个blog链接好啦~\(≧▽≦)/~啦啦啦

C++_写一个聊天机器人

posted @   aquAAA  阅读(511)  评论(1编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示