2014-04-08 60 views
-1

以下代碼需要檢查迴文並使用遞歸向後寫入用戶輸入,然後使用相同的方法找到最大公用分母。找到gcd的代碼部分,告訴它是否是一個迴文序列,但每當它得到它反轉代碼的部分時,它就會崩潰。爲什麼每次都會崩潰。使用遞歸的迴文

#include <stdio.h> 
#include <stdlib.h> 
/*Delare the prototypes*/ 
char palindromes(char[], int); 
char backwards(char[], int, int); 
int gcdfunc(int, int); 
int findgcd(int, int); 

int main() 
{ 
    char userinput[10];/*Declares the character array for the input*/ 
    int index= 0;/*Index for the counting loop*/ 
    int counter= 0;/*Counts number of elements entered in the array*/ 
    int printindex= 0;/*Index to print the values on the screen*/ 
    int gcd= 0;/*Sets a value for the GCD*/ 
      int palcheck = 0; 
    int value1= 0;/*User value 1*/ 
    int value2= 0;/*User value 2*/ 
    int flipindex=0;/*Sets an index for the gcd function*/ 

    printf("Please enter a series of nine or less characters to test for a palindrome.\n"); 
    scanf(" %9s%n", &userinput, &counter); 
    printf("\n"); 

    palcheck = palindromes(userinput, counter-1); 

    if(palcheck == 0) 
    { 
     printf("Your input was not a palindrome \n"); 
    }/*End of if statement*/ 
    else 
    { 
     printf("Your input was a palindrome \n"); 
    }/*End of else statement*/ 

    backwards(userinput, counter-1, flipindex); 

    printf("Your input backwards is: "); 
    for(printindex; printindex <= counter; printindex++) 
    { 
     printf("%c", userinput[printindex]); 
    }/*End of printing backwards loop*/ 
    printf("\n"); 

    printf("\nEnter two numbers: "); 
    scanf("%d %d",&value1,&value2); 

    gcd=gcdfunc(value1, value2); 

    printf("The GCD of %d and %d is: %d",value1,value2,gcd); 

    system("pause"); 

}/*End of main function*/ 

char palindromes(char userinput[], int counter) 
{ 
    int palindex= 0;/*Declares the index to check for a palindrome*/ 
    int palendinx= counter; 
    int modulus = counter%2; 
    if(modulus = 0) 
    { 
     if(userinput[palindex]==userinput[palendinx]) 
     { 
      palindex++; 
      palendinx--; 
      if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx]) 
      return 1; 
      palindromes(userinput, counter); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
    else 
    { 
     if(userinput[palindex]==userinput[palendinx]) 
     { 
      palindex++; 
      palendinx--; 
      if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx]) 
      return 1; 
      palindromes(userinput, counter); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
}/*End of palidrome function*/ 

char backwards(char userinput[], int counter, int flipindex) 
{ 
    while(flipindex<(counter/2)) 
    { 
     char temp;/*Sets a temporary value to swap the two values*/ 
     temp = userinput[flipindex]; 
     userinput[flipindex] = userinput[counter-flipindex]; 
     userinput[counter-flipindex] = temp; 
     backwards(userinput, counter, flipindex++); 
    } 

}/*End of reverse function*/ 

int gcdfunc(int value1, int value2) 
{ 
    int gcd; 
    gcd=findgcd(value1,value2); 
    return gcd; 
} 

int findgcd(int value1,int value2) 
{ 
    while(value1!=value2) 
    { 
     if(value1>value2) 
     return findgcd(value1-value2,value2); 
     else 
     return findgcd(value1,value2-value1); 
    } 
    return value1; 
} 
+1

您可以使用GDB並在不會導致崩潰的行上縮小範圍。 – brokenfoot

回答

2

在你的函數中沒有變量的傳遞,它一遍又一遍地停留在同一個循環中,並且在遞歸中,同一個函數不會共享相同的變量,它會創建一個新的變量,並且如果修改它,它不會影響其他變量。

char palindromes(char userinput[], int counter) 
{ 
    int palindex= 0;/*Declares the index to check for a palindrome*/ 
    int palendinx= counter; 
    int modulus = counter%2; 
    if(modulus = 0) 
    { 
     if(userinput[palindex]==userinput[palendinx]) 
     { 
      palindex++; 
      palendinx--; 
      if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx]) 
      return 1; 
      palindromes(userinput, counter); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
    else 
    { 
     if(userinput[palindex]==userinput[palendinx]) 
     { 
      palindex++; 
      palendinx--; 
      if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx]) 
      return 1; 
      palindromes(userinput, counter); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
}/*End of palidrome function*/ 

因此,這裏是我的代碼做

int palindromes(char userinput[], int counter,int index , int palendex) 
{ 

     if(userinput[palendex] == userinput[index]) 
     { 

      if(counter%2 == 0 && (index - palendex) == 1) 
       return 0;   
      if(index == palendex)   
       return 0;    
      else 
      return palindromes(userinput, counter , index-1 ,palendex+1 );    


     } 
     else 
      return 1; 
     return 1; 
} 

在你的後退功能是,它從來沒有離開的週期,同時,做了很多實例的可用存儲空間用完。所以我將它更改爲if語句,並且看起來像這樣(以及一些更改)

int backwards(char userinput[], int counter, int flipindex) 
{ 
    if(flipindex<(counter/2)) 
    { 
     char temp;/*Sets a temporary value to swap the two values*/ 
     temp = userinput[flipindex]; 
     userinput[flipindex] = userinput[counter-flipindex-1]; 
     userinput[counter-flipindex-1] = temp; 
     backwards(userinput, counter, flipindex+1); 
    } 
    return 1; 
} 
1

它看起來像你晃過與此代碼緩衝區的末尾:

for(printindex; printindex <= counter; printindex++) 
{ 
    printf("%c", userinput[printindex]); 
}/*End of printing backwards loop*/ 

因爲printindex ==計數器和userinput [計數器]是出界。試試這個for語句代替:

for(printindex; printindex < counter; printindex++) 
+0

沒有注意到,謝謝。雖然它從來沒有達到這一線,但一旦它碰到反向函數,Visual C立即給我一個休息繼續屏幕。 – user3259144

+0

嗯,看向後(..)函數,它遞歸調用自己,但也有一個while循環。如果你必須使用遞歸,你不應該有一個while循環 - 只是一個if語句,而不是確保'flipindex'永遠不會大於'counter/2' –

+0

什麼是'for(printindex; .....)'應該成爲? –

0
if(palindex==(counter/2) && userinput[palindex]==userinput[palendinx]) 
    return 1; 
palindromes(userinput, counter); 

調用它遞歸在if語句的條件並不滿意。但它將無限期地堆棧溢出重複呼叫,因爲情況沒有變化。