我正在尝试编写一个程序来模拟DFA。我需要做的是从用户处获取大量输入并将其保存在两个独立的数组中(将其作为行和列使用),然后创建第三个数组(第2个数组)作为第一个数值表两个阵列。在二维数组中存储字符串和字符(C)
例如:数组2 = {A,B} ARRAY1 = {Q1,Q2,Q3} 阵列[ARRAY1] [数组2] =(见下表)
a b
========
q1 | v1 v2
q2 | v3 v4
q3 | V5 V6
问题:
1)I不能保存字符串Q1,Q2,Q3 ...在阵列
2)所述第二阵列值以某种方式覆盖在第一阵列值,(也许因为我现在用的是同一个变量作为他们的柜台?如果我改变了第二循环的计数器变量,它给分段错误
这将是巨大的,如果有人能指出我做的哪一部分是错误的。
编辑:分割问题是solv编辑,感谢coolguy和jayesh的回答。我仍然有一个问题,它的array1不返回一个字符串,它只返回字符,如果我输入q1它只返回q。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
// Function declaration
void clearNewLines(void);
int main(int argc, char *argv[]){
// Number of states and number of alphabets of DFA
int numStates;
int numAlphabets;
// Array for name of alphabets, and name of states
char nameOfAlphabets[numAlphabets];
char nameOfStates[numStates];
// Saving transition table
char *transitionTable[numStates][numAlphabets];
// Read numStates
printf("Enter the number of STATES:");
scanf("%d",&numStates);
// Flush STDIN
clearNewLines();
// Read the nameOfStates
int i;
for(i=0;i<numStates;i++){
printf("Name of STATES:");
fgets(&nameOfStates[i], 100,stdin);
}// End of for-loop to read nameOfStates
// Read numAlphabets
printf("Enter the number of ALPHABETS: ");
scanf("%d", &numAlphabets);
// Flush STDIN
clearNewLines();
// Read name of alphabets
for(i=0;i<numAlphabets;i++){
printf("Name of ALPHABETS:");
nameOfAlphabets[i] = getchar();
// Flush STDIN
clearNewLines();
}// End for-loop to read alphabets
// Get the transitionTable[states][alphabets]
int row;
for(row=0;row<numStates;row++){
int col;
for(col=0;col<numAlphabets;col++){
printf("Enter Transition From %c to %c: ",nameOfStates[row],nameOfAlphabets[col]);
printf("\n");
}
}
return 0;
}// End of main function
/*
*
* clearNewLines - clear any newline character present at the STDIN
*/
void clearNewLines(void)
{
int c;
do
{
c = getchar();
} while (c != '\n' && c != EOF);
}
哦!我很惊讶,我怎么没有看到错误! – aaa 2014-10-16 11:41:01