GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Mar-2004, 12:15
ayoub ayoub is offline
New Member
 
Join Date: Mar 2004
Posts: 4
ayoub is on a distinguished road

Modify a C program


i have done that program which convert non deterministic finite automata (NFA) to deterministic finite automata(DFA) but it runs when the NFA is initialized inside the program and i was wondering if someone would mind modifying it to let the user enter any NFA not to initialize the NFA in the program:
CPP / C++ / C Code:
/*NFA --> DFA conversion program 
*/ 
#include <stdio.h> 
#include <string.h>
#include<conio.h>
#define STATES 25
#define SYMBOLS 20

int N_symbols; /* number of input symbols */
int NFA_states; /* number of NFA states */
char *NFAtab[STATES][SYMBOLS];

int DFA_states; /* number of DFA states */
int DFAtab[STATES][SYMBOLS];

/*
Print state-transition table.
State names: 'A', 'B', 'C', ...
*/
void put_dfa_table(
int tab[][SYMBOLS], /* DFA table */
int nstates, /* number of states */
int nsymbols) /* number of input symbols */
{
int i, j;

puts("STATE TRANSITION TABLE");

/* input symbols: '0', '1', ... */
printf(" | ");
for (i = 0; i < nsymbols; i++) printf(" %c ", '0'+i);

printf("\n-----+--");
for (i = 0; i < nsymbols; i++) printf("-----");
printf("\n");

for (i = 0; i < nstates; i++) {
printf(" %c | ", 'A'+i); /* state */
for (j = 0; j < nsymbols; j++)
printf(" %c ", 'A'+tab[i][j]);
printf("\n");
}
}

/*
Initialize NFA table.
*/
void init_NFA_table()
{
/*
NFA table for ex.21 at p.76

NFAtab[0][0] = "01";
NFAtab[0][1] = "0";
NFAtab[1][0] = "";
NFAtab[1][1] = "01";

NFA_states = 2;
DFA_states = 0;
N_symbols = 2;
*/
/*
NFA table for ex.17 at p.72
*/
NFAtab[0][0] = "12";
NFAtab[0][1] = "13";
NFAtab[1][0] = "12";
NFAtab[1][1] = "13";
NFAtab[2][0] = "4";
NFAtab[2][1] = "";
NFAtab[3][0] = "";
NFAtab[3][1] = "4";
NFAtab[4][0] = "4";
NFAtab[4][1] = "4";

NFA_states = 5;
DFA_states = 0;
N_symbols = 2;
}

/*
String 't' is merged into 's' in an alphabetical order.
*/
void string_merge(char *s, char *t)
{
char temp[STATES], *r=temp, *p=s;

while (*p && *t) {
if (*p == *t) {
*r++ = *p++; t++;
} else if (*p < *t) {
*r++ = *p++;
} else
*r++ = *t++;
}
*r = '\0';

if (*p) strcat(r, p);
else if (*t) strcat(r, t);

strcpy(s, temp);
}

void get_next_state(char *nextstates, char *cur_states,
char *nfa[STATES][SYMBOLS], int n_nfa, int symbol)
{
int i;
char temp[STATES];

temp[0] = '\0';
for (i = 0; i < strlen(cur_states); i++)
string_merge(temp, nfa[cur_states[i]-'0'][symbol]);
strcpy(nextstates, temp);
}


int state_index(char *state, char statename[][STATES], int *pn)
{
int i;

if (!*state) return -1; /* no next state */

for (i = 0; i < *pn; i++)
if (!strcmp(state, statename[i])) return i;

strcpy(statename[i], state); /* new state-name */
return (*pn)++;
}

int nfa_to_dfa(char *nfa[STATES][SYMBOLS], int n_nfa,
int n_sym, int dfa[][SYMBOLS])
{
char statename[STATES][STATES];
int i = 0; /* current index of DFA */
int n = 1; /* number of DFA states */

char nextstate[STATES];
int j;

strcpy(statename[0], "0"); /* start state */

for (i = 0; i < n; i++) { /* for each DFA state */
for (j = 0; j < n_sym; j++) { /* for each input symbol */
get_next_state(nextstate, statename[i], nfa, n_nfa, j);
dfa[i][j] = state_index(nextstate, statename, &n);
}
}

return n; /* number of DFA states */
}

void main()
{
clrscr();
init_NFA_table();
DFA_states = nfa_to_dfa(NFAtab, NFA_states, N_symbols, DFAtab);
put_dfa_table(DFAtab, DFA_states, N_symbols);
getch();
}

i'm looking forward to getting your answer.
  #2  
Old 13-Mar-2004, 15:34
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ayoub
i have done that program which convert non deterministic finite automata (NFA) to deterministic finite automata(DFA) but it runs when the NFA is initialized inside the program and i was wondering if someone would mind modifying it to let the user enter any NFA not to initialize the NFA in the program:
CPP / C++ / C Code:
the code

i'm looking forward to getting your answer.

Maybe not...

First, you need to format your code. See the formatting tutorial in the tutorial thread http://www.gidforums.com/t-2034.html about indentation

Second, we will help you change the program. I for one don't wish to read, analyze, and understand someone else's program without guidance. Please post the area of code you would like help with and explain what inputs you need. We'll guide you to your solution.
  #3  
Old 14-Mar-2004, 01:50
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Whew, that's a lot of reading I'm not prepared to do... just post the section of code you're having a problem with, and please read the code formatting tutorial! That has to be some of the worst attempted code formatting I've ever seen...
__________________
-Aaron
  #4  
Old 15-Mar-2004, 11:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Ayoub.

I have deleted your last post, because it seemed identical to this one. If not, please let me know and I will restore it. Please do not make new threads to discuss the same issue. If you have information to add, please just reply to the original thread. Also when you post c code, please enclose between [c] and [/c].

As to your problem and the responses you have gotten, you may want to look into the suggestions that they have given. There is no requirement to post properly formatted code on these forums, but you will get a better response if people can read it more easily.

Secondly, this is quite a complicated task. Don't be surprised if someone here doesn't want to give up their free time to help you code a program. We want to help people around here, but this is not a place to come if you just want someone to provide you with code for free.

Good luck with your project and we encourage you to post here for help in solving this project.

Cheers,
d
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C program to convert NFA to DFA ayoub C Programming Language 11 14-Sep-2007 14:51
Please Help, problems writing newbie c program soulfly C Programming Language 14 04-Mar-2004 15:16
compiling a program within another (C++) Siphiro C++ Forum 5 06-Feb-2004 15:35
error during program rjd72285 C++ Forum 0 11-Nov-2003 18:49
one program access another? dgoulston C++ Forum 1 07-Oct-2003 11:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.