1118实验三有限自动机的构造与识别
2015-12-11 08:44
330 查看
#include<stdio.h> #include<iostream> #include<vector> #include<string> using namespace std; class TransTile { public: char current; char next; char input; TransTile(char C,char I,char Ne){ current = C; next = Ne; input = I; } }; class DFA { public: //构造状态集各个元素 string States; char startStates; string finalStates; string Alphabets; vector <TransTile> Tile; DFA(){ init(); } void init() { cout << "输入有限状态集S:" << endl; cin >> States; cout << "输入字符集A:" << endl; cin >> Alphabets; cout << "输入状态转换式(格式为:状态-输入字符-下一状态,输入#结束):" << endl; cout << "例如:1a1 \n 1a0 \n 2a1 \n #" << endl; int h = 0; } while(true){ char input[4]; cin>>input; if(strcmp(input,"#")==0) break; TransTile transval(input[0],input[1],input[2]); Tile.push_back(transval); } cout << "输入初态:" << endl; cin >> startStates; cout << "输入终态:" << endl; cin >> finalStates; } //遍历转换表 char move(char P,char I){ for (int i = 0; i < Tile.size(); i++){ if (Tile[i].current == P&&Tile[i].input == I){ return Tile[i].next; } } return 'E'; } //识别字符串函数 void recognition(){ string str; cout << "输入需要识别的字符串:" << endl; cin >> str; int i = 0; char current = startStates; while (i < str.length()){ current = move(current, str[i]); if (current == 'E'){ break; } i++; } if (finalStates.find(current) != finalStates.npos){ cout << "该自动机识别该字符串!" << endl; } else { cout << "该自动机不能识别该字符串!" << endl; } } }; int main(){ DFA dfa; bool tag; while(1){ cout<<"你想要继续吗?是请输入1,否输入0:"<<endl; cin>>tag; if(tag){ dfa.recognition(); }else break; } return 0; }
相关文章推荐
- 12月10号指针
- 第四周 项目三 单链表应用(2)
- 第9周项目2对称矩阵压缩存储的实现与应用1
- 第十五周 项目1 验证算法
- 第十五周项目2-用哈希法组织关键字
- OPENCV不规则ROI-圆形ROI
- H264中NAL帧识别
- Xcode 7.1 - Unable to find execution service for selected run destination
- 有穷自动机
- 重载
- 第十二周项目4-利用遍历思想求解图问题(6)
- 第十二周项目三 图遍历算法实现
- 我是ptspzy
- MVC框架基础
- 第四周 项目3 单链表应用(1)
- LeetCode 221 Maximal Square
- Quartz.NET学习系列
- Xcode iOS布局autolayout和sizeclass的使用
- cs231n 问题
- 要引入java吸管工具