其实本设计模式可以实现一些像人工智能式的问答。
这个跟人回答问题差不多,根据不同的问题作出不同的回答,而且在不同的状态,也会作出不同的回答。
如果有大量的数据,那么就可以作出千变万化的问答效果来哦。
#include <stdio.h>
enum STATE
{
HUNGARY, EAT, FULL
};
class State
{
STATE myState;
public:
State(STATE s = HUNGARY) : myState(s)
{
}
void eat()
{
switch (myState)
{
case State_2::HUNGARY:
puts("Good, I am hungary now. I would like something to eat.");
myState = EAT;
break;
case State_2::EAT:
puts("I am still eating. I am not finished, yet.");
break;
case State_2::FULL:
puts("Not, Thanks. I am full now.");
break;
default:
break;
}
}
void hungary()
{
switch (myState)
{
case State_2::HUNGARY:
puts("I am starving! I need to eat something!");
break;
case State_2::EAT:
puts("I am still hungary, I want to eat more.");
break;
case State_2::FULL:
puts("Now, I am feeling hungary again.");
myState = HUNGARY;
break;
default:
break;
}
}
void full()
{
switch (myState)
{
case State_2::HUNGARY:
puts("I am not full, I need to eat something first.");
break;
case State_2::EAT:
puts("Great, I am full now. Thank you.");
myState = FULL;
break;
case State_2::FULL:
puts("Yeah, can not be more satisfied. I am full, and full again.");
break;
default:
break;
}
}
};
int main()
{
State Bill;
puts("\nWhen Bill is in HUNGARY state:");
puts("Are you hungary, Bill?");
Bill.hungary();
puts("Are you full, Bill?");
Bill.full();
puts("Would you like something to eat, Bill?");
Bill.eat(); //Change state
puts("\nWhen Bill is in EAT state:");
puts("Would you like something to eat, Bill?");
Bill.eat();
puts("Are you hungary, Bill?");
Bill.hungary();
puts("Are you full, Bill?");
Bill.full(); //Change state
puts("\nWhen Bill is in FULL state:");
puts("Are you full, Bill?");
Bill.full();
puts("Would you like something to eat, Bill?");
Bill.eat();
puts("Are you hungary, Bill?");
Bill.hungary(); //Change state
return 0;
}
看看运行效果: