Palindrome使用递归

时间:2014-04-08 22:01:10

标签: c

以下代码需要检查回文并使用递归向后写入用户输入,然后使用相同的方法找到最大公分母。找到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;
}

3 个答案:

答案 0 :(得分: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 :(得分:1)

看起来你用这段代码超越了缓冲区的末尾:

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

因为printindex == counter和userinput [counter]超出范围。请尝试使用for-statement:

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

答案 2 :(得分:0)

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

在if语句中调用递归条件不满足。但它会无限期地堆叠溢出重复的调用,因为情况没有变化。