is this ok

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ricky

    is this ok

    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<<"\nInitia l 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<<"\nTranit ions of the NFA are: \n";
    cout<<line;
    print_NFA();
    cout<<"\n"<<lin e;
    cin.get(); cin.ignore();
    return 0;
    }






  • Thomas Matthews

    #2
    Re: is this ok

    Ricky wrote:[color=blue]
    > 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[/color]

    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.

    [color=blue]
    > struct TLink
    > {
    > int linkNode;
    > int stringMax;
    > int string[5];
    > };[/color]

    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.

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

    1. The member "isFinalSta te" 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.

    [color=blue]
    > struct DLink
    > {
    > int meCilinLidhet;
    > int stringNo;
    > int node[5];
    > };[/color]

    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.

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

    [color=blue]
    >
    > int max_states;
    > int maxNode;[/color]

    Do these variables _need_ to be global?

    [color=blue]
    > void NFA_input()
    > {
    > int i, j, k, f;
    > cout<<"\nHow MAny states does the NFA has =";
    > cin>>max_states ;[/color]

    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).
    [color=blue]
    > for (i=0; i<max_states; i++)
    > {
    > cout<<"\nIs State q["<<i<<"] final State ? (yes=1, No=0)";
    > cin>>NFA[i].isFinalState;[/color]

    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.

    [color=blue]
    > cout<<"\nHow many transitions does this State have ?";[/color]

    I suggest you flush the output buffer before requesting input:
    cout.flush();
    [color=blue]
    > cin>>NFA[i].max_links;[/color]

    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.

    [color=blue]
    > 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];[/color]

    Possible array overflow: you only have 5 allocated.
    What happens when there are 6, or zero?

    [color=blue]
    > }
    > }
    > }
    > }
    >[/color]
    [snip -- printing function]
    [color=blue]
    > int main()
    > {
    > clrscr();[/color]

    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?
    [color=blue]
    > char line[80]="-------------------------------------------------";
    > NFA_input();
    > cout<<"\nTranit ions of the NFA are: \n";
    > cout<<line;
    > print_NFA();
    > cout<<"\n"<<lin e;
    > cin.get(); cin.ignore();
    > return 0;
    > }[/color]

    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:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Jonathan Turkanis

      #3
      Re: is this ok

      "Thomas Matthews" <Thomas_Matthew sIHateSpamBots@ sbcglobal.net> wrote
      in message news:c1_Mb.2441 2$Ub6.16056@new ssvr33.news.pro digy.com...[color=blue]
      > Ricky wrote:[color=green]
      > > thanks for the replysir.
      > > i have made this code so far in declaring the NFA. In fact i do[/color][/color]
      know what am[color=blue][color=green]
      > > 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[/color][/color]
      right way,[color=blue][color=green]
      > > or can i do the covertion from this structure, and if possible[/color][/color]
      (not trying[color=blue][color=green]
      > > to bother you) one kind of solution.
      > > thanks much[/color]
      >
      > I will review your code, but I have no clue what a NFA is. Perhaps[/color]
      you[color=blue]
      > should not use acronyms or abbreviations, but spell them out.
      >
      >[/color]

      It stands for National Fire Academy. It is in constant competition
      with the DFA (Department of Foreign Affairs.)

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


      Comment

      • angelayoub

        #4
        C program that convert NFA to DFA

        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

        Comment

        • angelayoub

          #5
          C program that convert NFA to DFA

          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

          Comment

          • angelayoub

            #6
            C program that convert NFA to DFA

            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

            Comment

            • angelayoub

              #7
              C program that convert NFA to DFA

              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

              Comment

              • angelayoub

                #8
                C program that convert NFA to DFA

                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

                Comment

                • Victor Bazarov

                  #9
                  Re: C program that convert NFA to DFA

                  "angelayoub " <angelayoub@car amail.com> wrote...[color=blue]
                  > Dear sir,[/color]

                  What if I'm a madam?
                  [color=blue]
                  > I am so pleased to write you,and i want on this opportunity to thank you[/color]
                  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.[color=blue]
                  > I am looking forward to getting you answer.[/color]

                  Here is my answer. Read carefully. First, this is a C++ newsgroup.
                  If you need help with a C program, you're in a wrong place. Please
                  consider posting to comp.lang.c. Second, if you need some program
                  written, you're better off contacting your fellow students from the
                  university. We here don't write programs to strangers. If you want
                  to hire somebody, consider posting to misc.jobs.offer ed. Third, if
                  this is your first time here, consider reading the Welcome message
                  posted here weekly by Shiva, and the FAQ list. You can find the FAQ
                  here: http://www.parashift.com/c++-faq-lite/ and the Welcome message
                  is duplicated at http://www.slack.net/~shiva/welcome.txt. Fourth,
                  please don't post FIVE TIMES. Be patient, it takes a few minutes
                  for the people around the globe to see your posting and reply.

                  There is probably more that you could correct about the way you
                  posted. I'll leave it to others.

                  Good luck with your studies!


                  Comment

                  • Jonathan Turkanis

                    #10
                    Re: C program that convert NFA to DFA

                    "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                    news:GNvUb.1831 51$5V2.887680@a ttbi_s53...[color=blue]
                    > "angelayoub " <angelayoub@car amail.com> wrote...[color=green]
                    > > Dear sir,[/color]
                    >
                    > What if I'm a madam?
                    >[color=green]
                    > > I am so pleased to write you,and i want on this opportunity to[/color][/color]
                    thank you[color=blue]
                    > about your help,i was wondering if you would mind helping me to[/color]
                    write a C[color=blue]
                    > program that convert a NFA to an equivalent DFA,i found this program[/color]
                    in C++[color=blue]
                    > program but i haven't studied it yet at the university.[color=green]
                    > > I am looking forward to getting you answer.[/color]
                    >
                    > Here is my answer. Read carefully. <snip>[/color]

                    Yes. And please don't send unsolicited email.

                    Jonathan


                    Comment

                    • angelayoub

                      #11
                      Any one help me

                      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

                      Comment

                      • angelayoub

                        #12
                        C program that convert NFA to DFA

                        Dear sir or madam,
                        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



                        Comment

                        • angelayoub

                          #13
                          C program that convert NFA to DFA

                          Dear sir or madam,
                          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



                          Comment

                          • angelayoub

                            #14
                            C program that convert NFA to DFA

                            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 Non deterministic Finite Automat to an equivalent Deterministic Finite Automata,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



                            Comment

                            • angelayoub

                              #15
                              can Any one help me?

                              Dear sir or madam,
                              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



                              Comment

                              Working...