robot.cpp

来自「自动聊天机器人」· C++ 代码 · 共 76 行

CPP
76
字号
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Robot.h"
#include "Robotconfig.h"
//---------------------------------------------------------------------------

__fastcall ChatRobot::ChatRobot( const AnsiString &name, ChatRobot::SEX sex, int age )
    {
       this->Name = name;
       this->Sex  = sex;
       this->Age  = age;
       this->mp_Logger = NULL;
    }
//---------------------------------------------------------------------------
__fastcall ChatRobot::ChatRobot()
    {
       this->Name = DEF_ROBOT_NAME;
       this->Sex  = DEF_ROBOT_SEX;           //不带参数的构造函数,;
       this->Age  = DEF_ROBOT_AGE;
       this->mp_Logger = NULL;
    }
//---------------------------------------------------------------------------
void __fastcall ChatRobot::SetLogger( IInfoLog *logger )
 {
        this->mp_Logger = logger;       //机器人通过一个中间接口来发送消息;
 }
//---------------------------------------------------------------------------
void __fastcall ChatRobot::Talk( const AnsiString &Info )
{
      TStringList *strlist;
      strlist = new TStringList();
      int len;
      AnsiString filename = ExtractFilePath( Application->ExeName );
      filename = filename + "dialog.txt";
      strlist->LoadFromFile(filename);

      if(this->mp_Logger == NULL) return;

      for( len = 0; len < strlist->Count; len++ )    //len列表中的行
            if( Info.Pos( strlist->Names[len] ) > 0 )    //返回是否匹配用户发送过来的关键字值
            { //ShowMessage(strlist->Names[len]);
            break;
            }
       if( len < strlist->Count && len >= 0 )
       this->mp_Logger->Log( Now(), this->Name, strlist->Values[strlist->Names[len]] );
      else
       {    //len = random( strlist->Count );     //随机产生一个不大于strlist->Count的数
            this->mp_Logger->Log( Now(), this->Name, strlist->Values[strlist->Names[16]] );
       }

       delete  strlist;

}
//---------------------------------------------------------------------------
void __fastcall ChatRobot::Introduce( void )
{
   AnsiString s;

   if( this->mp_Logger == NULL ) return;

   s = "您好,我叫" + this->Name + ",今年" + this->Age + "岁,";
   switch( this->Sex )
   {
      case ChatRobot::Male :
           s = s + "是一位帅哥哦!";
           break;
      case ChatRobot::Female :
           s = s + "是一位美女哦!";
           break;
      default:
           s = s + "性别未明!";
   }

   this->mp_Logger->Log( Now(), this->Name, s );
}

#pragma package(smart_init)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?