链表实现简单的聊天机器人

#define MAXLEN 1024
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<vector>
#include<fstream>
#include<windows.h>
#include<time.h>  
using namespace std;

typedef struct node {
	string key;
	vector<string>val;
	node *next;
}Node;

const string Emoticonss[10] = { "⊙▽⊙",""," ̄▽ ̄"," _(:3 」∠)_",
								"(>▽<)","= ̄ω ̄=","( ̄ˇ ̄) ",
								"(⊙_⊙)","( ̄. ̄) "," ̄□ ̄"
};

Node *creatlink();
void learn(Node *head);
void del(Node *head);
void Home(Node *head);
void chat(Node *head);
void signout(Node *head);
void travel(Node *head);
void save(Node *head);
void load(Node *head);
void tailinsert(Node*p,Node *q,string s);

int main()
{
	Node *head;

	system("color f0");
	head=creatlink();
	while (1) 
		Home(head);
	return 0;
}
	
Node *creatlink() {
	Node *head = NULL;

	head = (Node *)malloc(sizeof(Node));
	memset(head, 0, (sizeof(Node)));
	head->next = NULL;
	return head;
}

void Home(Node *head) {
	int x;

	cout << "小C:想让我干嘛呢?" << endl;
	cout << "1.聊天" << endl;
	cout << "2.调教" << endl;
	cout << "3.载入记忆" << endl;
	cout << "4.删除记忆" << endl;
	cout << "5.显示记忆" << endl;
	cout << "6.退出" << endl;
	cin >> x;
	switch (x) {
		case 1:chat(head); break;
		case 2:learn(head); break;
		case 3:load(head); break;
		case 4:del(head); break;
		case 5:travel(head); break;
		case 6:signout(head); break;
		default:cout << "小C:请输入正确的数字!!(>_<)" << endl; break;
	}
}

void signout(Node *head) {
	Node *p = head;
	char ch;
	
	cout << "小C:需要保存记忆吗?(Y/N)" << endl;
	cin >> ch;
	if (ch == 'Y' || ch == 'y')
		save(head);
	while (head) {
		head = head->next;
		free(p);
		p = head;
	}
	cout << "小C:挥挥~" << endl;
	Sleep(2000);
	exit(0);
}

void learn(Node *head) {
	Node *q=head;
	string in,out;

	while (q->next)
		q = q->next;
	cout << "小C:如果不想调教了请输入0哦" << endl;
	cout << endl;
	while (1) {
		bool flag = true;
		cout << "小C:请教我知识点!!" << endl;
		cin >> in;
		if (in == "0") {
			system("cls");
			break;
		}
			
		cout << "小C:如果您提到"<<"\""<< in << "\""<< "我该说什么呢?"<<endl;
		cin >> out;
		if (head->next!=NULL) {
			Node *r = head->next;
			while (r) {
				if (r->key == in) {
					r->val.push_back(out);
					flag= false;
					break;
				}
				r = r->next;
			}
		}
		
		if (flag) {
			Node *p = (Node *)malloc(sizeof(Node));

			tailinsert(p, q, in);
			p->val.push_back(out);
		}
		cout << "小C:好的我记住辣!" << endl;
		cout << endl;
	}
}

void chat(Node *head) {
	Node *p;
	string in;
	
	cout << "小C:不想聊了请输入0哦!" << endl;
	cout << endl;
	while (1) {
	bool flag = false;
		cout << "我:";
		cin >> in;
		if (in == "0") {
			system("cls");
			break;
		}
		Sleep(1000);
		srand((unsigned)time(NULL));
		p=head->next;
		while (p) {
			if (in.find(p->key)!= string::npos){
				flag = true;
				int idx = rand()%p->val.size();
				cout << "小C: " << (p->val)[idx];
				break;
			}
			p = p->next;
		}
		if (!flag)
			cout << "小C:我不知道...";
		if(rand()%2)
			cout << Emoticonss[rand() % 10] << endl;
		else
			cout << endl;
		cout << endl;
	}
}

void del(Node *head) {
	string in;
	bool flag = false;

	cout << "小C:请输入要删除的记忆" << endl;
	cin >> in;
	Node *p=head->next, *q=head;

	while (p) {
		if (p->key == in) {
			q->next = p->next;
			cout <<"小C:删除成功!" << endl;
			flag = true;
			Sleep(2000);
			system("cls");
			break;
		}
		q = p;
		p = p->next;
	}
	if (!flag) {
		cout <<"小C:未找到此项记忆=.=" << endl;
		Sleep(2000);
		system("cls");
	}
}

void travel(Node *head) {
	Node *p = head->next;
	if (!p) {
		cout << "小C:感觉身体被掏空..." << endl;
		Sleep(2000);
		system("cls");
		return ;
	}
	while (p) {
		cout << "知识点:" << p->key << " " << "回应:";
		for (int i = 0; i < p->val.size(); i++)
			cout << p->val[i] << " ";
		cout << endl;
		p = p->next;
	}
	cout << endl;
	Sleep(2000);
	system("cls");
}

void save(Node *head) {
	ofstream out("Memory.txt");
	if (out.is_open())
	{
		Node *p = head->next;
		while (p) {
			out << p->key ;
			out << endl;
			for (int i = 0; i < p->val.size(); i++)
				out<< p->val[i] << " ";
			out << endl;
			p = p->next;
		}
		out.close();
	}
}

void load(Node *head) {
	int cnt = 0;
	char buffer[MAXLEN];
	string key_word,val_word,result;
	vector<string>res;
	ifstream in("Memory.txt");
	
	if (!in.is_open())
		cout << "未找到记忆文件..." << endl;
	else {
		while (!in.eof()) {
			in.getline(buffer, MAXLEN);
			if (cnt % 2 == 0)
				key_word = buffer;
			else {
				val_word = buffer;
				stringstream input(val_word);

				while (input >> result)
					res.push_back(result);
				Node *q = head;
				bool flag = true;

				while (q->next)
					q = q->next;
				if (head->next != NULL) {
					Node *r = head->next;
					while (r) {
						if (r->key == key_word) {
							for(int i=0;i<res.size();i++)
								r->val.push_back(res[i]);
							flag = false;
							break;
						}
							r = r->next;
					}
				}
				if (flag) {
					Node *p = (Node *)malloc(sizeof(Node));

					tailinsert(p, q, key_word);
					for (int i = 0; i<res.size(); i++)
						p->val.push_back(res[i]);
				}
				res.clear();
				}
			cnt++;
		}
		cout<< "载入成功!OVO" << endl;
		in.close();
	}
	Sleep(2000);
	system("cls");
}

void tailinsert(Node *p,Node *q, string s) {
		memset(p, 0, (sizeof(Node)));
		p->next = NULL;
		p->key =s;
		q->next = p;
		q = p;
}

  

posted @ 2017-08-28 20:34  小九xD  阅读(228)  评论(0编辑  收藏  举报