这个可以吗 [英] is this ok

查看:65
本文介绍了这个可以吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢回复。

到目前为止,我已经宣布了NFA。事实上,我确实知道我应该做什么,我只是不能做对。

请看下面的代码并告诉我,我是在右边方式,

或者我可以从这个结构中进行转换,如果可能的话(不要试图打击你)一种解决方案。

非常感谢


struct TLink

{

int linkNode;

int stringMax;

int string [5];

};


struct Node

{

int isFinalState;

int max_links;

TLink Link [5];

} NFA [5];


struct DLink

{

int meCilinLidhet;

int stringNo;

int node [5];

};


struct NodeD

{

int stringNode;

int isFinalState;

int max_links;

DLink Link [5];

} DFA [5];


int max_states;

int maxNode;


无效NFA _input()

{

int i,j,k,f;

cout<<" \\\
How MAny states是NFA有=" ;;

cin>> max_states;

for(i = 0; I< max_states; i ++)

{

cout<<" \ nIs State q ["<< i<<""] final state? (是= 1,否= 0)" ;;

cin>> NFA [i] .isFinalState;

cout<<" \ n许多过渡这个州有吗?" ;;

cin>> NFA [i] .max_links;

for(j = 0; j< NFA [i] .max_links; j ++)

{

cout<<" \\\
The"<<<< j + 1<<<""转换是将状态q ["<< i<<""]与

state =" ;;

cin>> NFA [i]相关联。链接[j] .linkNode;

cout<<" \\\
许多字符串使这个转换=" ;;

cin>> NFA [i]。链接[j] .stringMax;

for(k = 0; k< NFA [i] .Link [j] .stringMax; k ++)

{

cout<<" \ n输入那些字符串= \ n" ;;

cout<<"" delta ["<< k + 1<< "] =" ;;

cin>> NFA [i] .Link [j] .string [k];

}

}

}

}


无效print_NFA()

{

int i,j,k;

for(i = 0; i< max_states; i ++)

{

cout<< ;" \ nnq q ["<<<<<"]" ;;

for(j = 0; j< NFA [i] .max_links; j ++)

{

cout<<" \ n与州q ["<< NFA [i] .Link [j] .linkNode<< ;"]" ;;

cout& LT;<"带字符串" ;;

for(k = 0; k< NFA [i] .Link [j] .stringMax; k ++)

cout<<" " << NFA [i] .Link [j] .string [k];

}

cout<<" \\\
" ;;


}

cout<<" \\\
Initial自动机的状态为:q [0] \ n";

cout<<" \ n自动机的最终状态是:" ;;

for(i = 0; i< max_states; i ++)

{

if(NFA [i] .isFinalState == 1)

cout<<" \\\
q ["<< i<<"] \\ \\ t \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ t \\ b \\ b
}

}

int main()

{

clrscr();

char line [80] =" ----------------------------- --------------------" ;;

NFA_input();

cout<<" \\ \\ n NFA的特征是:\ n" ;;

cout<< line;

print_NFA();

cout<< " \ n"<< line;

cin.get(); cin.ignore();

返回0;

}


thanks for the replysir.
i have made this code so far in declaring the NFA. In fact i do know what am
i supposed to do , i just cant get it right.
please have a look at the following code and tell me am i in the right way,
or can i do the covertion from this structure, and if possible (not trying
to bother you) one kind of solution.
thanks much

struct TLink
{
int linkNode;
int stringMax;
int string[5];
};

struct Node
{
int isFinalState;
int max_links;
TLink Link[5];
} NFA[5];

struct DLink
{
int meCilinLidhet;
int stringNo;
int node[5];
};

struct NodeD
{
int stringNode;
int isFinalState;
int max_links;
DLink Link[5];
} DFA[5];

int max_states;
int maxNode;

void NFA_input()
{
int i, j, k, f;
cout<<"\nHow MAny states does the NFA has =";
cin>>max_states;
for (i=0; i<max_states; i++)
{
cout<<"\nIs State q["<<i<<"] final State ? (yes=1, No=0)";
cin>>NFA[i].isFinalState;
cout<<"\nHow many transitions does this State have ?";
cin>>NFA[i].max_links;
for (j=0; j<NFA[i].max_links; j++)
{
cout<<"\nThe "<<j+1<<" transition is linking state q["<<i<<"] with
state =";
cin>>NFA[i].Link[j].linkNode;
cout<<"\nHow many strings make this transition=";
cin>>NFA[i].Link[j].stringMax;
for (k = 0; k < NFA[i].Link[j].stringMax; k++)
{
cout<<"\nEnter those strings=\n";
cout<<"delta["<<k+1<<"]= ";
cin>>NFA[i].Link[j].string[k];
}
}
}
}

void print_NFA()
{
int i, j, k ;
for (i=0; i<max_states; i++)
{
cout<<"\nState q["<<i<<"]";
for (j=0; j<NFA[i].max_links; j++)
{
cout<<"\n is linked with state q["<<NFA[i].Link[j].linkNode<<"]";
cout<<" with string";
for (k = 0; k < NFA[i].Link[j].stringMax; k++)
cout<<" " <<NFA[i].Link[j].string[k];
}
cout<<"\n";

}
cout<<"\nInitial State of the automata is:q[0]\n ";
cout<<"\nFinal States of the automata are: ";
for (i=0; i<max_states; i++)
{
if (NFA[i].isFinalState == 1)
cout<<"\n q["<<i<<"] \t";
}
}
int main()
{
clrscr();
char line[80]="-------------------------------------------------";
NFA_input();
cout<<"\nTranitions of the NFA are: \n";
cout<<line;
print_NFA();
cout<<"\n"<<line;
cin.get(); cin.ignore();
return 0;
}


推荐答案

Ricky写道:
谢谢你的回复。
到目前为止,我已经宣布了NFA。事实上,我确实知道我应该做什么,我只是不能正确。
请看看下面的代码并告诉我我是以正确的方式,
或我可以从这个结构中进行转换吗?如果可能的话(不要试图打扰你)一种解决方案。
非常感谢


我会审核你的代码,但我不知道NFA是什么。也许你不需要使用首字母缩写词或缩写词,而是拼写出来。


struct TLink
{
int linkNode;
int stringMax;
int string [5];
};


为什么字符串有5个int值?

''5'是_magic_数字,应该是一个命名常量,例如

as:

const unsigned int MAX_NUMBERS_IN_STRING = 5;

另外,你想要有符号或无符号整数吗?在许多通信

场景中,数字是无符号的,以允许更多范围。如果

数字不是负数,则将其声明为无符号。


struct Node
{int /> int isFinalState;
int max_links;
TLink Link [5];
} NFA [5];


1.会员isFinalState暗示一个布尔(真/假)类型。

你应该这样命名:

bool isFinalState;

2.什么是号码''5''?魔数再次出现。转换为

一个命名常量。见上文。


struct DLink
{meCilinLidhet;
int stringNo;
int node [5];
};


1.数字'5'是什么?魔数再次出现。转换为

一个命名常量。见上文。

2. TLink和DLink有什么区别?

你的名字不是很具描述性。


struct NodeD
{int / int intNode;
int isFinalState;
int max_links;
DLink Link [5];
} DFA [5];
1.数字'5'是什么?魔数再次出现。转换为

一个命名常量。见上文。

1.成员isFinalState暗示一个布尔(true / false)类型。

你应该这样命名:

bool isFinalState;

int max_states ;
int maxNode;


这些变量_need_是全局的吗?


void NFA_input()
{
int i,j, k,f;
cout<<" \\\
如何,MAY状态NFA有=" ;;
cin>> max_states;


1.由于状态数是动态的,你可能想要b $ b b考虑从动态内存中分配它们(即使用

''new'')并使用动态容器,例如矢量

或列表。


2.您不能检查无效输入。如果

用户输入apple,会发生什么?或者3.14159?

(我相信输入操作会设置流的'

失败位或坏位)。

for(i = 0; i< max_states; i ++)
cout<<" \ nIs状态q ["<<<<<""]最终状态? (是= 1,否= 0)" ;;
cin>> NFA [i] .isFinalState;


或者:

unsigned int yes_no;

NFA [i] .isFinalState = yes_no == 1;


当max_states为10时会发生什么?你的数组中只分配了5个
元素。


cout<<" \这个州有多少次转换?;


我建议您在请求输入之前刷新输出缓冲区:

cout.flush();

cin>> NFA [I] .max_links;


如果转换次数是30,会发生什么?

您只有5次分配。

当用户进入时会发生什么负数,例如
为-2?你确实允许通过将max_links指定为

int而不是unsigned int。


for(j = 0; j< NFA [i] .max_links ; j ++)
{cout<<" \ nThe"<<<< j + 1<<<""转换是将状态q ["<<<<""]与
state =" ;;
cin>> NFA [i] .Link [j] .linkNode;
cout<<" \\\
许多字符串使这种转变=" ;;
cin>> NFA [i] .Link [j] .stringMax;
for(k = 0 ; k< NFA [i] .Link [j] .stringMax; k ++)
{
cout<<" \ n输入那些字符串= \ n" ;;
cout< <""<<< k + 1<<""""              


可能的阵列溢出:你只分配了5个。

当有6或零时会发生什么?


}
}
}
}
[剪辑 - 打印功能]

int main()
{
clrscr();


这不是标准的C ++函数。你真的需要清除

屏幕吗?该程序将更具可移植性。

例如,在某些操作系统中,您可以将程序的

输出重定向到文件中。如何通过重定向输出清除

屏幕?

char line [80] =" --------------- ----------------------------------" ;;
NFA_input();
cout< ;<" \ nTFA的NFA是:\ n" ;;
cout<< line;
print_NFA();
cout<<" \ n" << line;
cin.get(); cin.ignore();
返回0;
}
thanks for the replysir.
i have made this code so far in declaring the NFA. In fact i do know what am
i supposed to do , i just cant get it right.
please have a look at the following code and tell me am i in the right way,
or can i do the covertion from this structure, and if possible (not trying
to bother you) one kind of solution.
thanks much
I will review your code, but I have no clue what a NFA is. Perhaps you
should not use acronyms or abbreviations, but spell them out.

struct TLink
{
int linkNode;
int stringMax;
int string[5];
};
Why are there 5 int values for the string?
The ''5'' is a _magic_ number and should be a named constant, such
as:
const unsigned int MAX_NUMBERS_IN_STRING = 5;
Also, do you want signed or unsigned integers. In many communications
scenarios, numbers are unsigned to allow for more range. If the
number is not negative, declare it as unsigned.

struct Node
{
int isFinalState;
int max_links;
TLink Link[5];
} NFA[5];
1. The member "isFinalState" implies a boolean (true / false) type.
You should name it that way:
bool isFinalState;
2. What is with the number ''5''? Magic number again. Convert to
a named constant. See above.

struct DLink
{
int meCilinLidhet;
int stringNo;
int node[5];
};
1. What is with the number ''5''? Magic number again. Convert to
a named constant. See above.
2. What is the difference between a TLink and a DLink?
Your names are not very descriptive.

struct NodeD
{
int stringNode;
int isFinalState;
int max_links;
DLink Link[5];
} DFA[5]; 1. What is with the number ''5''? Magic number again. Convert to
a named constant. See above.
1. The member "isFinalState" implies a boolean (true / false) type.
You should name it that way:
bool isFinalState;


int max_states;
int maxNode;
Do these variables _need_ to be global?

void NFA_input()
{
int i, j, k, f;
cout<<"\nHow MAny states does the NFA has =";
cin>>max_states;
1. Since the number of states is dynamic, you may want to
consider allocating them from dynamic memory (i.e. using
''new'') and using a dynamic container, such as a vector
or list.

2. You don''t check for invalid input. What happens if
the user enters "apple" or "3.14159"?
(I believe the input operation will set the stream''s
fail bit or bad bit).
for (i=0; i<max_states; i++)
{
cout<<"\nIs State q["<<i<<"] final State ? (yes=1, No=0)";
cin>>NFA[i].isFinalState;
Or:
unsigned int yes_no;
NFA[i].isFinalState = yes_no == 1;

What happens when max_states is 10? You only have 5
elements allocated in your array.

cout<<"\nHow many transitions does this State have ?";
I suggest you flush the output buffer before requesting input:
cout.flush();
cin>>NFA[i].max_links;
What happens if the number of transitions is 30?
You only have 5 allocated.
What happens when the user enters a negative number, such
as -2? You did allow that by specifying max_links as an
int and not an unsigned int.

for (j=0; j<NFA[i].max_links; j++)
{
cout<<"\nThe "<<j+1<<" transition is linking state q["<<i<<"] with
state =";
cin>>NFA[i].Link[j].linkNode;
cout<<"\nHow many strings make this transition=";
cin>>NFA[i].Link[j].stringMax;
for (k = 0; k < NFA[i].Link[j].stringMax; k++)
{
cout<<"\nEnter those strings=\n";
cout<<"delta["<<k+1<<"]= ";
cin>>NFA[i].Link[j].string[k];
Possible array overflow: you only have 5 allocated.
What happens when there are 6, or zero?

}
}
}
}
[snip -- printing function]
int main()
{
clrscr();
This is not a standard C++ function. Do you really need the
screen cleared? The program will be more portable with out it.
For example, in some operating systems, you could redirect the
output of your program into a file. How does clearing the
screen work with redirecting the output?
char line[80]="-------------------------------------------------";
NFA_input();
cout<<"\nTranitions of the NFA are: \n";
cout<<line;
print_NFA();
cout<<"\n"<<line;
cin.get(); cin.ignore();
return 0;
}




摘要

-------

我相信你没有高效的数据结构。你的

暗示要求允许动态数量的状态和

转换。固定大小的数组非常不利于动态元数量的b $ b。您的I / O未检查

边界条件或流故障。你的命名

约定可以使用改进来提高可读性。


处理动态结构时需要使用指针。

如果你坚持使用数组,在你的结构中放置一个指针

并在运行时分配数组。您将需要

记住数组的大小,因为C ++语言

不为动态数组提供sizeof()操作。

std :: vector更加安全,并且还可以处理动态的
扩展。


你的节点有一组共同的成员(也许是方法

),你应​​该将共性分解为

a基类:

struct BaseState

{

bool is_final_state;

unsigned int max_states;

};


struct Node

: public BaseState

{

TLink * links;

};


struct DNode

:public BaseState

{

int stringNode;

DLink * links;

};

-

托马斯马修斯


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt
C ++常见问题: http://www.parashift。 com / c ++ - faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c -c ++ faq:
http:// www。 raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍



Summary
-------
I believe you don''t have an efficient data structure. Your
implied requirements allow a dynamic amount of states and
transitions. Fixed sized arrays are very bad for dynamic
quantities of elements. Your I/O is not checked for
boundary conditions or stream failures. Your naming
conventions could use improvement for better readability.

You need to use pointer when dealing with dynamic structures.
If you insist on arrays, place a pointer in your structure
and allocate the array at run-time. You will need to
remember the size of the array because the C++ language
does not provide a sizeof() operation for dynamic arrays.
A std::vector is much safer, and also handles dynamic
expansion.

Your nodes have a common set of members (maybe methods
too), you should factor out the commonalities into
a base class:
struct BaseState
{
bool is_final_state;
unsigned int max_states;
};

struct Node
: public BaseState
{
TLink * links;
};

struct DNode
: public BaseState
{
int stringNode;
DLink * links;
};
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


" Thomas Matthews" ; <钍************************** @ sbcglobal.net>在消息新闻中写了

:c1 ******************* @ newssvr33.news.prodigy。 com ...
"Thomas Matthews" <Th**************************@sbcglobal.net> wrote
in message news:c1*******************@newssvr33.news.prodigy. com...
Ricky写道:
谢谢你的回复。
到目前为止,我已经宣布了NFA。事实上我做
知道我应该做什么,我只是不能正确。
请看看下面的代码并告诉我,我是在
正确的方式,或者我可以从这个结构进行转换吗?如果可能的话,我可以用b b b(不要打扰你)一种解决方案。
非常感谢
我会检查你的代码,但我不知道是什么NFA是。也许
thanks for the replysir.
i have made this code so far in declaring the NFA. In fact i do know what am i supposed to do , i just cant get it right.
please have a look at the following code and tell me am i in the right way, or can i do the covertion from this structure, and if possible (not trying to bother you) one kind of solution.
thanks much
I will review your code, but I have no clue what a NFA is. Perhaps



你不应该使用首字母缩写词或缩写词,但要拼写出来。


you should not use acronyms or abbreviations, but spell them out.




它代表National消防学院。正在与DFA(外交部)进行持续竞争




http://www.google.com/search?q=NFA+DFA


尊敬的先生,

我很高兴为您写信,我希望借此机会感谢您的帮助,我想知道您是否介意帮助我写信一个将NFA转换为等效DFA的C程序,我在C ++程序中发现了这个程序,但我还没在大学里研究过它。

我期待着你的回答。 br $>
此致,

Ayoub

Dear sir,
I am so pleased to write you,and i want on this opportunity to thank you about your help,i was wondering if you would mind helping me to write a C program that convert a NFA to an equivalent DFA,i found this program in C++ program but i haven''t studied it yet at the university.
I am looking forward to getting you answer.
Sincerely,
Ayoub


这篇关于这个可以吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆