我有這個項目,我需要編寫一個代碼來模擬DFA接受url的。我已經發現的DFA部分是完全正確的,但我在代碼中遇到了一些問題。 第一個問題是,當我運行的代碼似乎不走過去的COUT < <「輸入y/n的一部分。 第二個可能是它的一部分,但我得到了很多逗號轉到問題和for循環問題
27左操作數操作者沒有影響[-Wunused值]
逗號操作的27右操作數沒有任何效果[-Wunused值]
27錯誤:預期「;」之前「)」令牌
31錯誤:預期主表達式之前「轉到」
31錯誤:「之前轉到」
在每個爲出於某種原因
迴路預期「)」#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main() {
char asdf='y';
string str;
int stringlength,state;
while (asdf=='y'){
start:
cout << "Do you want to enter a string?(y/n)\n";
cin >> asdf;
cout << "Enter the string:\n";
cin >> str;
stringlength=str.size();
state=0;
//transition states q0 start
//w means goto q1,increment state by 1
if(str[0]=='w'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q1;
}
//. means trap state,loop the rest of the char's and output trap state and output "rejected", then return to start
if(str[0]=='.'){
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
}
//not w and not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q7;
q1:
//w means goto q2
if (str[1]=='w'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q2;
}
//. means go to q6
if (str[1]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q6;
}
//not w and not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q7;
q2:
//w means goto q3
if (str[2]=='w'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q3;
}
//. means go to q6
if (str[2]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q6;
}
//not w and not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q7;
q3:
//w means goto q3
if (str[3]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q4;
}
//not . means goto q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q7;
q4:
//. mean trap state
if (str[4]=='.'){
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
}
//everything else goes to q5
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q5;
q5:
//. means goto q6
if (str[state]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q6;
}
//everything else goes back to q5
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q5;
q6:
//c means goto q8
if (str[state]=='c'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q8;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
q7:
//. means goto q6
if (str[state]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q6;
}
//everything else goes back to q7
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q7;
q8:
//o means goto q9
if (str[state]=='o'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q9;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
q9:
//m means goto q10
if (str[state]=='m'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q10;
}
//. means goto q11
if (str[state]=='.'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q11;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
q10:
//compare length of string to state,if it matches there are no more char in string and is accepted
if (stringlength==state){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
cout << "accepted\n";
goto start;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
q11:
//j means goto q12
if (str[state]=='j'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q12;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
q12:
//p means goto q13
if (str[state]=='p'){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
state++;
goto q13;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
q13:
//compare length of string to state,if it matches there are no more char in string and is accepted
if (stringlength==state){
cout << "character:"<<str[state]<<" "<<"state:"<<state<<"\n";
cout << "accepted\n";
goto start;
}
//everything else goes to trap state
state=stringlength-state;
for(state,state>0,state--){
cout << "character:"<<str[stringlength-state]<<" "<<"state:trap\n";
}
cout << "rejected\n";
goto start;
}
return 0;
}
如果你爲狀態使用了一個表,你的代碼將更容易調試和理解。哇,你可以使用指針來處理特殊情況下的函數。在網上搜索「C++狀態設計模式」。 – 2014-09-30 21:59:21
我喜歡用我的意大利麪條代碼提供肉丸。 :-) – 2014-09-30 22:00:26