简体   繁体   中英

Recursive inverting

i made this code for my college lesson:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100

int palindromoR(int i, int f, char *s)
{
    
    if (f - i <= 0) 
        return 1;
    
    if (s[i] != s[f]) {
        return 0;
    } else {
        return palindromoR(i+1, f-1, s);
    }
}

void palindromo(char *s)
{
    int saida = palindromoR(0, strlen(s) - 1, s);

    if (saida)
    {
        printf("eh palindromo\n");
    }
    else
    {
        printf("nao eh palindromo\n");
    }
}

void inversaR(char *str)
{
    static int i=0;
    int tam = strlen(str) - i;
    char temp;
    

    if (tam +1 == 0)
        return;
    temp = str[tam];
    printf ("%c",temp);
    i++;
    return inversaR (str);
    
}

void inversa(char *s)
{
    inversaR(s);
    printf("\n");
}

unsigned long stirlingR(unsigned long n, unsigned long k)
{
    // implemente essa função recursiva
    return 0;
}

void stirling(int n, int k)
{
    printf("%lu\n", stirlingR(n, k));
}

void padraoR(unsigned n)
{
    
}

void padrao(unsigned n)
{
    padraoR(n);
    printf("\n");
}

int main(int argc, char *argv[])
{
    char file_name[MAX], aux[MAX];
    FILE *entrada;
    int t, a, b;

    scanf("%s", file_name);
    entrada = fopen(file_name, "r");
    if (entrada == NULL)
    {
        printf("Nao encontrei o arquivo!");
        exit(EXIT_FAILURE);
    }

    fscanf(entrada, "%d", &t);

    if (t < 1 || t > 4)
    {
        printf("Parametros incorretos.\n");
        printf("Ex:\n");
        printf("tp01_recursao 1 [para testar palindromo]\n");
        printf("tp01_recursao 2 [para testar inversa]\n");
        printf("tp01_recursao 3 [para testar Stirling]\n");
        printf("tp01_recursao 4 [para testar padrao]\n");
    }

    if (t == 1)
    {
        printf("\nTestando palindromo()\n\n");
        fscanf(entrada, "%s", aux);
        while (aux[0] != '.')
        {
            palindromo(aux);
            fscanf(entrada, "%s", aux);
        }
    }
    else if (t == 2)
    {
        printf("\nTestando inversa()\n\n");
        fscanf(entrada, "%s", aux);
        while (aux[0] != '.')
        {
            inversa(aux);
            fscanf(entrada, "%s", aux);
        }
    }

    else if (t == 3)
    {
        printf("\nTestando Stirling()\n\n");
        fscanf(entrada, "%d %d", &a, &b);
        while (a != -1)
        {
            stirling(a, b);
            fscanf(entrada, "%d %d", &a, &b);
        }
    }
    else if (t == 4)
    {
        printf("\nTestando padrao()\n\n");

        fscanf(entrada, "%d", &a);
        while (a != -1)
        {
            padrao(a);
            fscanf(entrada, "%d", &a);
        }
    }

    return 0;
}

My function inversaR seems to work when i try like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void inversaR(char *str)
{
    static int i=0;
    int tam = strlen(str) - i;
    char temp;
    

    if (tam +1 == 0)
        return;
    temp = str[tam];
    printf ("%c",temp);
    i++;
    return inversaR (str);
    
}

void inversa(char *s)
{
    inversaR(s);
    printf("\n");
}

int main (){
    
    char teste[100] = "alucard";
    inversa(teste);
    
    return 0;
}

the code above gives me the answer "dracula" as expected, but when trying with the first code it cuts the strings in 2 characters. The archive it's reading contains the following strings:

2 ab gato minerva alucard.

I tried to chance it using the function strrev() and it seems to work just fine, otherwise the same problem kept blowing my mind.

I tried out your code and saw the quirky chopped output you referred to. Doing some debugging of the code led me to the issue with your static variable, "i" in the recursive "inversaR" function. After each call with a different string, the value was left with an arbitrary value from the previous recursive call set which was giving either a partial string or no string at all.

With that it was apparent that when the final character had been printed and the return back up the function call stack was occurring, this variable needed to be reset. Following is a refactored version of the function.

void inversaR(char *str)
{
    static int i=0;
    int tam = strlen(str) - i;
    char temp;

    //printf("tam: %d\n", tam);

    if (tam +1 == 0)
    {
        i = 0;                  /* This needs to be reset when returning up the recursive call stack */
        return;
    }
    temp = str[tam];
    printf ("%c",temp);
    i++;
    return inversaR (str);

}

Also, just to clarify to anyone testing this program, I added a "printf" statement prior to the prompt for a file name so that it is clear what is wanted for input.

printf("File name: ");          /* Added to alert the user to enter a file name */
scanf("%s", file_name);
entrada = fopen(file_name, "r");

With those two bits of code refactored, and having created a text file with the sample string set, the following terminal output was created.

@Vera:~/C_Programs/Console/Dracula/bin/Release$ ./Dracula 
File name: Test.txt

Testando inversa()

ba
otag
avrenim
dracula

All of the strings appear to have been properly reversed. Go ahead and try out these program tweaks and see if they meet the spirit of your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM