为什么我的C代码在第一次scanf之后打印一个换行符?

nhhxz33t  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(88)

我正在用C语言实现一个带有特定方法的AVL树,作为算法和数据结构I的一个小项目。
修正是由moodle完成的。出于某种原因,打印了一个额外的换行符,我不知道为什么。这在自动修正和我在终端手动运行时都会发生。
我想这是因为一些我不知道的scanf,但我不确定。
我试过改变打印字符串从开头到结尾的换行符。换行符消失了,但是自动更正没有识别它。我想换行符一定是在结尾。
无论哪种方式,都必须有一种方法将它们保留在字符串的末尾,而不会产生这种奇怪的换行符。
为了澄清,这里是输出和代码的一小部分:
start of main function
start of insert function

[Incorrect program output]
--- Input ---
I 8 10
I 16 9
I 20 5
A
I 10 5
I 15 4
B 15
P

--- Program output ---

[Ja esta balanceado]
[Ja esta balanceado]
[No desbalanceado: 8]
[Rotacao: SE]
[x=8 y=16 z=20]
A=1
[Ja esta balanceado]
[No desbalanceado: 8]
[Rotacao: SE]
[x=8 y=10 z=15]
C=5 Nota=4
[8 15 10 20 16 ]

--- Expected output (exact text)---
[Ja esta balanceado]
[Ja esta balanceado]
[No desbalanceado: 8]
[Rotacao: SE]
[x=8 y=16 z=20]
A=1
[Ja esta balanceado]
[No desbalanceado: 8]
[Rotacao: SE]
[x=8 y=10 z=15]
C=5 Nota=4
[8 15 10 20 16 ]

以下是完整的源代码:

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

typedef int item_t;
typedef int number;

typedef struct avl_node_t {
    item_t ra;
    number nota;    
    struct avl_node_t* left;
    struct avl_node_t* right;
    int height;
} avl_node_t;

avl_node_t* avl_insert(avl_node_t* root, item_t ra, number nota);
avl_node_t* priv_avl_insert(avl_node_t* root, item_t ra, number nota, bool* rotacaoAplicada);
avl_node_t* avl_create_node(item_t ra, number nota);
void update_height(avl_node_t* root);
int maximum(int a, int b);
avl_node_t* avl_rebalance
    (avl_node_t* root, bool* rotacao_aplicada);
int avl_balance_factor(avl_node_t* root);
int avl_height(avl_node_t* root);
avl_node_t* avl_rotate_left(avl_node_t* root);
avl_node_t* avl_rotate_right(avl_node_t* root);
void print_with_indent(avl_node_t* root, int indent);
void fill_spaces(int size);
avl_node_t* priv_avl_remove(avl_node_t* root, item_t ra, bool* removed, bool* rotacao_aplicada);
avl_node_t* avl_remove(avl_node_t* root, item_t ra, bool* removed);
avl_node_t* avl_find_min(avl_node_t* root);
void avl_search(avl_node_t* root, int ra);
void avl_find_height(avl_node_t* root);
void avl_destroy(avl_node_t* root);
void avl_print_and_free(avl_node_t* root);

avl_node_t* avl_insert(avl_node_t* root, item_t ra, number nota){
    // printf("\nI %d %d", ra, nota);
    bool rotacao_aplicada = false;
    bool* rotacao_aplicada_p = &rotacao_aplicada;

    root = priv_avl_insert(root, ra, nota, rotacao_aplicada_p);

    if(*rotacao_aplicada_p == false){
        printf("\n[Ja esta balanceado]");
    }

    return root;
}

avl_node_t* priv_avl_insert(avl_node_t* root, item_t ra, number nota, bool* rotacao_aplicada) {
    if (root == NULL) {
        return avl_create_node(ra, nota);
    }
    if (ra < root->ra) {
        root->left = priv_avl_insert(root->left, ra, nota, rotacao_aplicada);
    }
    if (ra > root->ra) {
        root->right = priv_avl_insert(root->right, ra, nota, rotacao_aplicada);
    }
    if(ra == root->ra){
        root->nota = nota;
    }
    update_height(root);

    root = avl_rebalance(root, rotacao_aplicada); //pendente
    return root;
}

avl_node_t* avl_create_node(item_t ra, number nota) {
    avl_node_t* node = malloc(sizeof(avl_node_t));

    node->ra = ra;
    node->left = NULL;
    node->right = NULL;
    node->height = 0;
    node->nota = nota;

    return node;
}

void update_height(avl_node_t* root) {
    if (root != NULL) {
        root->height = 1 + maximum(avl_height(root->left), avl_height(root->right));
    }
}

int maximum(int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}

avl_node_t* avl_right_right(avl_node_t* root) {
    avl_node_t* pivot = root->right;

    root->right = pivot->left;
    pivot->left = root;

    update_height(root);
    update_height(pivot);

    return pivot;
}

avl_node_t* avl_left_left(avl_node_t* root) {
    avl_node_t* pivot = root->left;

    root->left = pivot->right;
    pivot->right = root;

    update_height(root);
    update_height(pivot);

    return pivot;
}

avl_node_t * avl_left_right(avl_node_t *root) {
    root->left = avl_right_right(root->left);
    return avl_left_left(root);
}

avl_node_t * avl_right_left(avl_node_t *root) {
    root->right = avl_left_left(root->right);
    return avl_right_right(root);
}

int get_balance(avl_node_t* root) {
    if (root == NULL) return 0;
    return avl_height(root->left) - avl_height(root->right);
}

avl_node_t* avl_rebalance
    (avl_node_t* root, bool* rotacao_aplicada){
    if (root == NULL) return root;

    // printf("root: %d, bf: %d", root->ra, get_balance(root));
    if (get_balance(root) > 1) { //left
        *rotacao_aplicada = true;
        if (get_balance(root->left) >= 0){
            printf("\n[No desbalanceado: %d]", root->ra);
            printf("\n[Rotacao: SD]");
            printf("\n[x=%d y=%d z=%d]", root->left->left->ra, root->left->ra, root->ra);
            root = avl_left_left(root);
        } else {
            printf("\n[No desbalanceado: %d]", root->ra);
            printf("\n[Rotacao: DD]");
            printf("\n[x=%d y=%d z=%d]", root->left->ra, root->left->right->ra, root->ra);
            root = avl_left_right(root);
        }
    } else if (get_balance(root) < -1) { //right
        *rotacao_aplicada = true;
        if (get_balance(root->right) <= 0){
            printf("\n[No desbalanceado: %d]", root->ra);
            printf("\n[Rotacao: SE]");
            printf("\n[x=%d y=%d z=%d]", root->ra, root->right->ra, root->right->right->ra);
            root = avl_right_right(root);
        }
        else {
            printf("\n[No desbalanceado: %d]", root->ra);
            printf("\n[Rotacao: DE]");
            printf("\n[x=%d y=%d z=%d]", root->ra, root->right->left->ra, root->right->ra);
            root = avl_right_left(root);
        }
    }

    return root;
}
int avl_height(avl_node_t* root) {
    if (root == NULL) {
        return -1;
    }
    
    return root->height;
}

avl_node_t* priv_avl_remove
    (avl_node_t* root, item_t ra, bool* removed, bool* rotacao_aplicada) {
    if (root == NULL) {
        return NULL;
    }
    if (ra < root->ra) {
        root->left = priv_avl_remove(root->left, ra, removed, rotacao_aplicada);
    } else if (ra > root->ra) {
        root->right = priv_avl_remove(root->right, ra, removed, rotacao_aplicada);
    } else {
        if (root->right == NULL) {
            avl_node_t* child = root->left;

            free(root);
            *removed = true;

            return child;
        }
        if (root->left == NULL) {
            avl_node_t* child = root->right;

            free(root);
            *removed = true;

            return child;
        }
        avl_node_t* min_child = avl_find_min(root->right);

        root->ra = min_child->ra;
        root->right = priv_avl_remove(root->right, min_child->ra, removed, rotacao_aplicada);
    }

    update_height(root);

    return avl_rebalance(root, rotacao_aplicada);
}

avl_node_t* avl_remove(avl_node_t* root, item_t ra, bool* removed){
    // printf("\nR %d", ra);
    bool rotacao_aplicada = false;
    bool* rotacao_aplicada_p = &rotacao_aplicada;

    root = priv_avl_remove(root, ra, removed, rotacao_aplicada_p);

    if(*rotacao_aplicada_p == false){
        printf("\n[Ja esta balanceado]");
    }

    return root;
}

avl_node_t* avl_find_min(avl_node_t* root) {
    if (root->left == NULL) {
        return root;
    }

    return avl_find_min(root->left);
}

avl_node_t* priv_avl_binary_search(avl_node_t* root, int ra, int* comparacoes) {
    if (root == NULL || root->ra == ra) {
        *comparacoes = *comparacoes + 1;
        return root;
    }
    if (ra < root->ra) {
        *comparacoes = *comparacoes + 2;
        return priv_avl_binary_search(root->left, ra, comparacoes);
    } else {
        *comparacoes = *comparacoes + 2;
        return priv_avl_binary_search(root->right, ra, comparacoes);
    }
}

void avl_search(avl_node_t* root, int ra){
    int comparacoes = 0;
    avl_node_t* no_buscado = priv_avl_binary_search(root, ra, &comparacoes);
    // printf("\nB %d", ra);

    if(no_buscado == NULL){
        printf("\nC=%d Nota=-1", comparacoes);
    } else {
        printf("\nC=%d Nota=%d", comparacoes, no_buscado->nota);
    }
}

void avl_find_height(avl_node_t* root){
    int height = avl_height(root);
    // printf("\nA");
    printf("\nA=%d", height);
}

void avl_destroy(avl_node_t* root) {
    if (root != NULL) {
        avl_destroy(root->left);
        avl_destroy(root->right);
        free(root);
    }
}

void avl_print_post_order(avl_node_t* root){
    if(root != NULL){
        avl_print_post_order(root->left);
        avl_print_post_order(root->right);
        printf("%d ", root->ra);
    }
}

void avl_print_and_free(avl_node_t* root){
    // printf("\nP");
    printf("\n[");
    avl_print_post_order(root);
    printf("]");

    avl_destroy(root);
}

void fill_spaces(int size) {
    for (int i = 0; i < size; i++) {
        printf(" ");
    }
}

void print_with_indent(avl_node_t* root, int indent) {
    if (root != NULL) {
        printf("\n");
        fill_spaces(indent);
        printf("(");
        printf("%d,%d", root->ra, avl_height(root));
        print_with_indent(root->right, indent + 2);
        print_with_indent(root->left, indent + 2);
        printf(")");
    } else {
        printf("\n");
        fill_spaces(indent);
        printf("()");
    }
}

int main() {

    char opcao;
    avl_node_t* raiz = NULL;
    do{
        scanf(" %c", &opcao);
        if(opcao == 'I'){
            int ra;
            int nota;
            scanf("%d", &ra);
            scanf("%d", &nota);
            raiz = avl_insert(raiz, ra, nota);
        } else if(opcao == 'R'){
            bool removed = false;
            int ra;
            scanf("%d", &ra);
            raiz = avl_remove(raiz, ra, &removed);
        } else if(opcao == 'B'){
            int ra;
            scanf("%d", &ra);
            avl_search(raiz, ra);
        } else if(opcao == 'A'){
            avl_find_height(raiz);
        } else if (opcao == 'p'){
            print_with_indent(raiz,2);
        } else {
            avl_print_and_free(raiz);
        }
    } while(opcao != 'P');
    
    
    return 0;
}
bhmjp9jg

bhmjp9jg1#

因为你的输入是“I ...”,这意味着第一个print语句是在avl_insert()中。最简单的修复方法可能只是将“\n”移动到字符串的末尾:

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

typedef int item_t;
typedef int number;

typedef struct avl_node_t {
    item_t ra;
    number nota;
    struct avl_node_t* left;
    struct avl_node_t* right;
    int height;
} avl_node_t;

avl_node_t* avl_insert(avl_node_t* root, item_t ra, number nota);
avl_node_t* priv_avl_insert(avl_node_t* root, item_t ra, number nota, bool* rotacaoAplicada);
avl_node_t* avl_create_node(item_t ra, number nota);
void update_height(avl_node_t* root);
int maximum(int a, int b);
avl_node_t* avl_rebalance(avl_node_t* root, bool* rotacao_aplicada);
int avl_balance_factor(avl_node_t* root);
int avl_height(avl_node_t* root);
avl_node_t* avl_rotate_left(avl_node_t* root);
avl_node_t* avl_rotate_right(avl_node_t* root);
void print_with_indent(avl_node_t* root, int indent);
void fill_spaces(int size);
avl_node_t* priv_avl_remove(avl_node_t* root, item_t ra, bool* removed, bool* rotacao_aplicada);
avl_node_t* avl_remove(avl_node_t* root, item_t ra, bool* removed);
avl_node_t* avl_find_min(avl_node_t* root);
void avl_search(avl_node_t* root, int ra);
void avl_find_height(avl_node_t* root);
void avl_destroy(avl_node_t* root);
void avl_print_and_free(avl_node_t* root);

avl_node_t* avl_insert(avl_node_t* root, item_t ra, number nota){
    bool rotacao_aplicada = false;
    bool* rotacao_aplicada_p = &rotacao_aplicada;
    root = priv_avl_insert(root, ra, nota, rotacao_aplicada_p);
    if(!*rotacao_aplicada_p){
        printf("[Ja esta balanceado]\n");
    }
    return root;
}

avl_node_t* priv_avl_insert(avl_node_t* root, item_t ra, number nota, bool* rotacao_aplicada) {
    if (!root)
        return avl_create_node(ra, nota);
    if (ra < root->ra)
        root->left = priv_avl_insert(root->left, ra, nota, rotacao_aplicada);
    if (ra > root->ra)
        root->right = priv_avl_insert(root->right, ra, nota, rotacao_aplicada);
    if(ra == root->ra)
        root->nota = nota;
    update_height(root);
    return avl_rebalance(root, rotacao_aplicada); //pendente
}

avl_node_t* avl_create_node(item_t ra, number nota) {
    avl_node_t* node = malloc(sizeof(avl_node_t));
    node->ra = ra;
    node->left = NULL;
    node->right = NULL;
    node->height = 0;
    node->nota = nota;
    return node;
}

void update_height(avl_node_t* root) {
    if (root)
        root->height = 1 + maximum(avl_height(root->left), avl_height(root->right));
}

int maximum(int a, int b) {
    return a > b ? a : b;
}

avl_node_t* avl_right_right(avl_node_t* root) {
    avl_node_t* pivot = root->right;
    root->right = pivot->left;
    pivot->left = root;
    update_height(root);
    update_height(pivot);
    return pivot;
}

avl_node_t* avl_left_left(avl_node_t* root) {
    avl_node_t* pivot = root->left;
    root->left = pivot->right;
    pivot->right = root;
    update_height(root);
    update_height(pivot);
    return pivot;
}

avl_node_t * avl_left_right(avl_node_t *root) {
    root->left = avl_right_right(root->left);
    return avl_left_left(root);
}

avl_node_t * avl_right_left(avl_node_t *root) {
    root->right = avl_left_left(root->right);
    return avl_right_right(root);
}

int get_balance(avl_node_t* root) {
    if (!root)
        return 0;
    return avl_height(root->left) - avl_height(root->right);
}

avl_node_t* avl_rebalance
(avl_node_t* root, bool* rotacao_aplicada){
    if (!root)
        return root;
    if (get_balance(root) > 1) { //left
        *rotacao_aplicada = true;
        if (get_balance(root->left) >= 0){
            printf("[No desbalanceado: %d]\n", root->ra);
            printf("[Rotacao: SD]\n");
            printf("[x=%d y=%d z=%d]\n", root->left->left->ra, root->left->ra, root->ra);
            root = avl_left_left(root);
        } else {
            printf("[No desbalanceado: %d]\n", root->ra);
            printf("[Rotacao: DD]\n");
            printf("[x=%d y=%d z=%d]\n", root->left->ra, root->left->right->ra, root->ra);
            root = avl_left_right(root);
        }
    } else if (get_balance(root) < -1) { //right
        *rotacao_aplicada = true;
        if (get_balance(root->right) <= 0){
            printf("[No desbalanceado: %d]\n", root->ra);
            printf("[Rotacao: SE]\n");
            printf("[x=%d y=%d z=%d]\n", root->ra, root->right->ra, root->right->right->ra);
            root = avl_right_right(root);
        }
        else {
            printf("[No desbalanceado: %d]\n", root->ra);
            printf("[Rotacao: DE]\n");
            printf("[x=%d y=%d z=%d]\n", root->ra, root->right->left->ra, root->right->ra);
            root = avl_right_left(root);
        }
    }
    return root;
}

int avl_height(avl_node_t* root) {
    return root ? root->height : -1;
}

avl_node_t* priv_avl_remove
(avl_node_t* root, item_t ra, bool* removed, bool* rotacao_aplicada) {
    if (!root)
        return NULL;
    if (ra < root->ra)
        root->left = priv_avl_remove(root->left, ra, removed, rotacao_aplicada);
    else if (ra > root->ra)
        root->right = priv_avl_remove(root->right, ra, removed, rotacao_aplicada);
    else {
        if (root->right == NULL) {
            avl_node_t* child = root->left;
            free(root);
            *removed = true;
            return child;
        }
        if (root->left == NULL) {
            avl_node_t* child = root->right;
            free(root);
            *removed = true;
            return child;
        }
        avl_node_t* min_child = avl_find_min(root->right);
        root->ra = min_child->ra;
        root->right = priv_avl_remove(root->right, min_child->ra, removed, rotacao_aplicada);
    }
    update_height(root);
    return avl_rebalance(root, rotacao_aplicada);
}

avl_node_t* avl_remove(avl_node_t* root, item_t ra, bool* removed){
    bool rotacao_aplicada = false;
    bool* rotacao_aplicada_p = &rotacao_aplicada;
    root = priv_avl_remove(root, ra, removed, rotacao_aplicada_p);
    if(*rotacao_aplicada_p == false){
        printf("[Ja esta balanceado]\n");
    }
    return root;
}

avl_node_t* avl_find_min(avl_node_t* root) {
    if (!root->left)
        return root;
    return avl_find_min(root->left);
}

avl_node_t* priv_avl_binary_search(avl_node_t* root, int ra, int* comparacoes) {
    if (!root || root->ra == ra) {
        *comparacoes = *comparacoes + 1;
        return root;
    }
    if (ra < root->ra) {
        *comparacoes = *comparacoes + 2;
        return priv_avl_binary_search(root->left, ra, comparacoes);
    } else {
        *comparacoes = *comparacoes + 2;
        return priv_avl_binary_search(root->right, ra, comparacoes);
    }
}

void avl_search(avl_node_t* root, int ra){
    int comparacoes = 0;
    avl_node_t* no_buscado = priv_avl_binary_search(root, ra, &comparacoes);
    printf("C=%d Nota=%d\n", comparacoes, no_buscado ? no_buscado->nota : -1);
}

void avl_find_height(avl_node_t* root){
    int height = avl_height(root);
    printf("A=%d\n", height);
}

void avl_destroy(avl_node_t* root) {
    if (!root)
        return;
    avl_destroy(root->left);
    avl_destroy(root->right);
    free(root);
}

void avl_print_post_order(avl_node_t* root){
    if(!root)
        return;
    avl_print_post_order(root->left);
    avl_print_post_order(root->right);
    printf("%d ", root->ra);
}

void avl_print_and_free(avl_node_t* root){
    printf("[");
    avl_print_post_order(root);
    printf("]\n");
    avl_destroy(root);
}

void fill_spaces(int size) {
    for (int i = 0; i < size; i++)
        printf(" ");
}

void print_with_indent(avl_node_t* root, int indent) {
    printf("\n");
    if (!root) {
        fill_spaces(indent);
        printf("()");
        return;
    }
    fill_spaces(indent);
    printf("(");
    printf("%d,%d", root->ra, avl_height(root));
    print_with_indent(root->right, indent + 2);
    print_with_indent(root->left, indent + 2);
    printf(")\n");
}

int main() {
    char opcao;
    avl_node_t* raiz = NULL;
    do {
        scanf(" %c", &opcao);
        if(opcao == 'I'){
            int ra;
            int nota;
            scanf("%d", &ra);
            scanf("%d", &nota);
            raiz = avl_insert(raiz, ra, nota);
        } else if(opcao == 'R'){
            bool removed = false;
            int ra;
            scanf("%d", &ra);
            raiz = avl_remove(raiz, ra, &removed);
        } else if(opcao == 'B'){
            int ra;
            scanf("%d", &ra);
            avl_search(raiz, ra);
        } else if(opcao == 'A'){
            avl_find_height(raiz);
        } else if (opcao == 'p'){
            print_with_indent(raiz,2);
        } else {
            avl_print_and_free(raiz);
        }
    } while(opcao != 'P');
}

和样本输出:

[Ja esta balanceado]
[Ja esta balanceado]
[No desbalanceado: 8]
[Rotacao: SE]
[x=8 y=16 z=20]
A=1
[Ja esta balanceado]
[No desbalanceado: 8]
[Rotacao: SE]
[x=8 y=10 z=15]
C=5 Nota=4
[8 15 10 20 16 ]

相关问题