2012-11-27 43 views
3
#include<string.h> 
#include<stdio.h> 


int firstState(char s[], int length); 
int secondState(char s[], int length); 
int thirdState(char s[], int length); 
int forthState(char s[], int length); 

int main() 
{ 
    char string[10]; 

    gets(string); 

    if(firstState(string, 0)) 
     printf("Accept\n"); 
    else 
     printf("Not accept\n"); 

    return 0; 
} 

int firstState(char s[], int length) 
{ 
    if(s[length] == 'a') 
     return (secondState(s, length++)); 
    else if(s[length] == 'b') 
     return firstState(s, length++); 
    else 
     return 0; 
} 

int secondState(char s[], int length) 
{ 
    if(s[length] == 'a') 
     return secondState(s, length++); 
    else if(s[length] == 'b') 
     return thirdState(s, length++); 
    else 
     return 0; 
} 

int thirdState(char s[], int length) 
{ 
    if(s[length] == 'a') 
     return secondState(s, length++); 
    else if(s[length] == 'b') 
     return forthState(s, length++); 
    else 
     return 0; 
} 

int forthState(char s[], int length) 
{ 
    if(s[length] == 'a') 
     return secondState(s, length++); 
    else if(s[length] == 'b') 
     return firstState(s, length++); 
    else 
     return 0; 
} 

它給了我一個分段錯誤或核心轉儲我很困惑! 有人可以解釋爲什麼它給了我這種類型的錯誤? 並告訴如何調試以使我的代碼運行得非常清晰!我的C代碼中的「分段錯誤」錯誤

我真的厭倦了這個:(

對不起我的英語不好

+0

錯誤消息是否包含行號?它指的是哪裏? –

+2

您正在測試的字符串是什麼? –

+1

您的平臺是否包含調試器? –

回答

3

段錯誤是訪問衝突,即當程序試圖訪問emmory時,它不應該爲http://en.wikipedia.org/wiki/Segmentation_fault

在你的情況下,它是由不安全的數組索引引起的。

int firstState(char s[], int length) 
{ 
... 
return firstState(s, length++); 
... 
} 

其實你不檢查,如果新的長度仍範圍內,所以如果字符串是不是空終止,它可能不是你的情況,那麼你就會有一個無限循環導致SegFault。

對於調試,使用GUI將是最合理的方法,嘗試在Windows上使用Visual Studio,在其他任何地方使用Eclipse。

7

你有一個無限遞歸,

return (secondState(s, length++)); 

length參數傳遞是之前的length值增量,所以你只有看過第一個char

通過length作爲參數length + 1,並檢查length是否小於10(char數組string的長度)。

在另一方面,

gets(string); 

是非常不安全的,如果輸入的長度超過九個字符,你寫的分配的內存之外。使用

fgets(string, sizeof string, stdin); 

改爲。


好吧,既然只需要上述的修復和一個返回值的變化,邏輯的大部分是正確的,固定的代碼:

// #include<string.h> <- We don't use that 
#include<stdio.h> 

// Match the grammar (a+b)*abb 

int firstState(char s[], int length); // nothing of the suffix matched 
int secondState(char s[], int length); // matched one character of the suffix 
int thirdState(char s[], int length); // matched two 
int forthState(char s[], int length); // matched the complete suffix 

int main() 
{ 
    char string[10]; 
    // Get a 0-terminated string into the buffer. 
    fgets(string, sizeof string, stdin); 

    if(firstState(string, 0)) 
     printf("Accept\n"); 
    else 
     printf("Not accept\n"); 

    return 0; 
} 

int firstState(char s[], int length) 
{ 
    if(s[length] == 'a') // first character of suffix matched 
     return (secondState(s, length+1)); 
    else if(s[length] == 'b') // nothing matched 
     return firstState(s, length+1); 
    else // end of string in not-accepting state 
     return 0; 
} 

int secondState(char s[], int length) 
{ 
    if(s[length] == 'a') // the old matched 'a' wasn't part of the suffix, the new may be 
     return secondState(s, length+1); 
    else if(s[length] == 'b') // now matched two characters of the suffix 
     return thirdState(s, length+1); 
    else // end of string in not-accepting state 
     return 0; 
} 

int thirdState(char s[], int length) 
{ 
    if(s[length] == 'a') // last three chars aba, the last 'a' could be part of the suffix 
     return secondState(s, length+1); 
    else if(s[length] == 'b') // full suffix matched 
     return forthState(s, length+1); 
    else // end of string in not-accepting state 
     return 0; 
} 

int forthState(char s[], int length) 
{ 
    if(s[length] == 'a') // another char, start a new candidate for the suffix 
     return secondState(s, length+1); 
    else if(s[length] == 'b') // another char, can't be part of the suffix, start over 
     return firstState(s, length+1); 
    else  // end of string in accepting state, yay! 
     return 1; 
     // return s[length] == '\0'; 
     // if characters other than 'a' and 'b' need not signal the end of the string 
} 
+0

第26行:3059總線錯誤stdbuf --error = 0 --output = 0「$ @」 –

+0

究竟產生了什麼錯誤信息?看起來不像我得到的任何錯誤消息,什麼是操作系統? –

+0

我使用這個在線編譯器http:// run。cs50.net/ –