Jump to content

Preciso de ajuda para converter C para Python


matesteves
Go to solution Solved by thoga31,

Recommended Posts

Preciso de ajuda para traduzir este código de C para Python, não entendo nada de C. 

int n1, n2;
void mult(int n1, int n2, int aux);

main(){
    printf("N1: ");
    scanf("%d", &n1);
    printf("N2: ");
    scanf("%d", &n2);
    mult(n1, n2, n1);
}

void mult(int n1, int n2, int aux){
    if (n1 == 0 && n2 == 0)
        return 0;
    else
        if (n2 > 1){
            n1 += aux;
            n2--;
            printf("%d ", n1);
            mult (n1, n2, aux);
        }
        if (n2 < -1){
            if (aux > 0){
                if (n1 == aux)
                    n1 -= (aux-aux-aux);
                else
                    n1 -= aux;
                n2++;
                printf("%d ", n1);
                mult (n1, n2, aux);
            }
            else{
                n1 -= aux;
                n2++;
                printf("%d ", n1);
                mult (n1, n2, aux);
            }

        }
}

Agradeço desde já.

Link to comment
Share on other sites

  • Solution

Essencialmente tens a função main, que é onde o programa irá iniciar a sua execução, e a função mult, a qual é invocada pela main. No topo tens declarado o protótipo da função mult e em baixo tens a sua implementação.

O resto da síntaxe não é muito diferente. Há, contudo, dois detalhes particulares:

  1. Os blocos de código em C são delimitados com chavetas { }, não com espaços como no Python;
  2. Os operadores ++ e -- correspondem a um +=1 e -=1 respectivamente.

Neste sentido, algumas dicas:

  • printf("N1: ");
    scanf("%d", &n1);

    corresponde em Python a

    n1 = int(input("N1: "))
  • void mult(int n1, int n2, int aux);

    corresponde a

    def mult(n1, n2, aux):
  • Este trecho de código
    if (n1 == 0 && n2 == 0)
        return 0;
    else {
        if (n2 > 1) {
            n1 += aux;
            n2--;
            printf("%d ", n1);
            mult (n1, n2, aux);
        }
    // ...

    traduz-se em

    if n1 == 0 and n2 == 0:
        return 0
    else:
        if n2 > 1:
            n1 += aux
            n2 -= 1
            print(n1)
            mult(n1, n2, aux)
        # to be continued...

     

 

Espero que com isto consigas traduzir o código em causa. Se tiveres mais alguma dúvida, diz-nos 😉

Cumprimentos.

Edited by thoga31
Pequena gafe no código
  • Vote 2

Knowledge is free!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.