为什么我的代码在任何问题上都失败了?

发布于 2025-02-12 22:35:21 字数 5963 浏览 1 评论 0原文

因此,我相信,链接列表的C实施在几个领域存在缺陷。我的问题主要是这样,尽管对其他任何帮助表示赞赏。 我知道该代码将运行到完成并正确打印输出。但是,在添加代码稍后访问的另一个函数后,该代码甚至可以在达到该点之前锁定。我想知道为什么是这样,以及如何修复它。我将包括以下所有文件中的文本,但是对于此作业,我只能编辑printandcombine.c及其headerFile。

printAndCombine.h

//Riley Branting
#ifndef PRINTANDCOMBINE_H
#define PRINTANDCOMBINE_H

//Returns a string representation of the struct term_t
char * term_to_string(term_t * term);

//Prints a linkedlist in no specific order
void print_linklist(node_t * curr);

//Returns a new linkedlist that is the result of combining the like terms
node_t * combine_like_terms(const node_t * current_node_ptr);

#endif

printandcombine.c

// Riley Branting
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"

// Returns a string representation of the struct term_t
char *term_to_string(term_t *term) {
    // printf("term_to_string 1");

    char *str = malloc((sizeof(int)) * 4);
    // printf("term_to_string 2");

    sprintf(str, "%d%c%d ", term->coefficient, term->var, term->exponent);
    printf("%s", str);

    // printf("term_to_string 3");

    return str;
}

// Prints a linkedlist in no specific order
void print_linklist(node_t *curr) {
    // printf("print_linkedlist 1");
    printf("%s", term_to_string(curr->term));
    // printf("print_linkedlist 2");
    if (!(curr->next_node == NULL)) {
        print_linklist(curr->next_node);
    }
    // printf("print_linkedlist 3");
}

// Returns a new linkedlist that is the result of combining the like terms
node_t *combine_like_terms(const node_t *current_node_ptr) {
    node_t *temp = (node_t *)current_node_ptr;

    node_t *out = malloc(sizeof(node_t));
    node_t *move = out;

    int x = 0;

    while (!(temp == NULL)) {
        if (temp->term->exponent > x) {
            x = temp->term->exponent;
            temp = temp->next_node;
        }
    }
    for (; x >= 0; x--) {
        temp = (node_t *)current_node_ptr;
        int a = 0;
        while (!(temp == NULL)) {
            if (temp->term->exponent == x) {
                a = a + temp->term->coefficient;
            }

            move->term->coefficient = a;
            move->term->var = 'x';
            move->term->exponent = x;
            if (x > 0) {
                move->next_node = (node_t *)malloc(sizeof(node_t));
                move = move->next_node;
            }

        }  // end while loop

    }  // end for loop

    return out;
}

project.c

/* This is your main file */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"common.h"
#include"buildlinklist.h"
#include"printandcombine.h"

int main() {
    node_t * node_ptr = NULL;
    node_t * new_node_ptr=NULL;

    printf("NAME: Riley Branting\n");
    
    /* Build linklist */
    read_objects(&node_ptr);

    
    /* Print the link list */
    printf("Original: ");
    print_linklist(node_ptr);
    
    /* Combine like terms in the link list and craeate a new link list */
    new_node_ptr=combine_like_terms(node_ptr);
    printf("\nCombined: : ");
    /* Print new combine linklist */
    print_linklist(new_node_ptr);

    printf("\nRiley Branting\n");
    free(node_ptr);
    free(new_node_ptr);
    return 0;


}

sideNote: SideNote:这些最后一个文件非常重要,但是我是 添加

buildlist.h

#ifndef BUILDLINKLIST_H
#define BUILDLINKLIST_H

#include"common.h"


/* Read from a text file and put in to a term_t structure. Then create a linklist of node (each node has an element of term struct.)*/
void read_objects(node_t ** pointer_to_node_ptr);

/* Assign value to term_t object elements */
term_t * term_from_string(char * buff);

/* Add a node to the link list */
int add_node(node_t ** pointer_to_node_ptr, void * term);

#endif

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



/* Assign value to term_t object elements */
term_t * term_from_string(char * buff) {
    term_t * ret = malloc(sizeof(term_t));
    ret->coefficient=atoi(strtok(buff, " "));
    ret->var=*strtok(NULL, " ");
    ret->exponent=atoi(strtok(NULL, " "));
    return ret;
}


/* Add a node to the link list */
int add_node(node_t ** pointer_to_node_ptr, void * term) {
  int ret = 0;
  node_t * newnode_ptr = (node_t *) malloc(sizeof(node_t));
  if (newnode_ptr == NULL) {
    ret = -1;
  }
  else {
    newnode_ptr->term = term;
    newnode_ptr->next_node = *pointer_to_node_ptr;
    *pointer_to_node_ptr = newnode_ptr;
  }
  return ret;
}


/* Read from a text file and put in to a term_t structure. Then create a linklist of node (each node has an element of term struct.)*/
void read_objects(node_t ** pointer_to_node_ptr) {

    FILE *fp;
    char buffer[BUFFERLEN];
    fp = fopen("terms.txt", "r");

    while (fgets(buffer, BUFFERLEN,fp)) {
        term_t * this_term;
        this_term =  term_from_string(buffer);
        add_node(pointer_to_node_ptr, this_term);
    }
    fclose(fp);
}

它们

#ifndef COMMON_H
#define COMMON_H

#define BUFFERLEN 40

typedef struct{           /* struct to hold elements like: 3x4 */
    int coefficient;
    char var;
    int exponent;
} term_t;

typedef struct node {
  term_t * term;            /* pointer to term */
  struct node * next_node;  /* pointer to next node */
} node_t;

#endif

中 :

CC=gcc 
CFLAGS=-std=c11 -Wall -pedantic 

all: project1.c buildlinklist.o printandcombine.o
    $(CC) $(CFLAGS) project1.c buildlinklist.o printandcombine.o -o project1

buildlinklist.o: buildlinklist.c
    $(CC) $(CFLAGS) -c buildlinklist.c

printandcombine.o: printandcombine.c
    $(CC) $(CFLAGS) -c printandcombine.c
clean:
    rm -f *.o *~ project1.exe

So I'm confident that this c implementation of a linked list is flawed in a few areas. My question is primarily this, though any other help is appreciated;
I know that the code will run to completion and print the output properly. However, upon adding another function accessed later in the code, the code will lock up before that point is even reached. I was wondering why this is, and how to fix it. I will include the text from all the files below, but for this assignment in particular I can only edit printandcombine.c and its headerfile.

printandcombine.h:

//Riley Branting
#ifndef PRINTANDCOMBINE_H
#define PRINTANDCOMBINE_H

//Returns a string representation of the struct term_t
char * term_to_string(term_t * term);

//Prints a linkedlist in no specific order
void print_linklist(node_t * curr);

//Returns a new linkedlist that is the result of combining the like terms
node_t * combine_like_terms(const node_t * current_node_ptr);

#endif

printandcombine.c:

// Riley Branting
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"

// Returns a string representation of the struct term_t
char *term_to_string(term_t *term) {
    // printf("term_to_string 1");

    char *str = malloc((sizeof(int)) * 4);
    // printf("term_to_string 2");

    sprintf(str, "%d%c%d ", term->coefficient, term->var, term->exponent);
    printf("%s", str);

    // printf("term_to_string 3");

    return str;
}

// Prints a linkedlist in no specific order
void print_linklist(node_t *curr) {
    // printf("print_linkedlist 1");
    printf("%s", term_to_string(curr->term));
    // printf("print_linkedlist 2");
    if (!(curr->next_node == NULL)) {
        print_linklist(curr->next_node);
    }
    // printf("print_linkedlist 3");
}

// Returns a new linkedlist that is the result of combining the like terms
node_t *combine_like_terms(const node_t *current_node_ptr) {
    node_t *temp = (node_t *)current_node_ptr;

    node_t *out = malloc(sizeof(node_t));
    node_t *move = out;

    int x = 0;

    while (!(temp == NULL)) {
        if (temp->term->exponent > x) {
            x = temp->term->exponent;
            temp = temp->next_node;
        }
    }
    for (; x >= 0; x--) {
        temp = (node_t *)current_node_ptr;
        int a = 0;
        while (!(temp == NULL)) {
            if (temp->term->exponent == x) {
                a = a + temp->term->coefficient;
            }

            move->term->coefficient = a;
            move->term->var = 'x';
            move->term->exponent = x;
            if (x > 0) {
                move->next_node = (node_t *)malloc(sizeof(node_t));
                move = move->next_node;
            }

        }  // end while loop

    }  // end for loop

    return out;
}

project.c:

/* This is your main file */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"common.h"
#include"buildlinklist.h"
#include"printandcombine.h"

int main() {
    node_t * node_ptr = NULL;
    node_t * new_node_ptr=NULL;

    printf("NAME: Riley Branting\n");
    
    /* Build linklist */
    read_objects(&node_ptr);

    
    /* Print the link list */
    printf("Original: ");
    print_linklist(node_ptr);
    
    /* Combine like terms in the link list and craeate a new link list */
    new_node_ptr=combine_like_terms(node_ptr);
    printf("\nCombined: : ");
    /* Print new combine linklist */
    print_linklist(new_node_ptr);

    printf("\nRiley Branting\n");
    free(node_ptr);
    free(new_node_ptr);
    return 0;


}

sidenote: It is very unlikely these last files are too important, but i'm adding them just in case

buildlist.h:

#ifndef BUILDLINKLIST_H
#define BUILDLINKLIST_H

#include"common.h"


/* Read from a text file and put in to a term_t structure. Then create a linklist of node (each node has an element of term struct.)*/
void read_objects(node_t ** pointer_to_node_ptr);

/* Assign value to term_t object elements */
term_t * term_from_string(char * buff);

/* Add a node to the link list */
int add_node(node_t ** pointer_to_node_ptr, void * term);

#endif

buildlist.c:

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



/* Assign value to term_t object elements */
term_t * term_from_string(char * buff) {
    term_t * ret = malloc(sizeof(term_t));
    ret->coefficient=atoi(strtok(buff, " "));
    ret->var=*strtok(NULL, " ");
    ret->exponent=atoi(strtok(NULL, " "));
    return ret;
}


/* Add a node to the link list */
int add_node(node_t ** pointer_to_node_ptr, void * term) {
  int ret = 0;
  node_t * newnode_ptr = (node_t *) malloc(sizeof(node_t));
  if (newnode_ptr == NULL) {
    ret = -1;
  }
  else {
    newnode_ptr->term = term;
    newnode_ptr->next_node = *pointer_to_node_ptr;
    *pointer_to_node_ptr = newnode_ptr;
  }
  return ret;
}


/* Read from a text file and put in to a term_t structure. Then create a linklist of node (each node has an element of term struct.)*/
void read_objects(node_t ** pointer_to_node_ptr) {

    FILE *fp;
    char buffer[BUFFERLEN];
    fp = fopen("terms.txt", "r");

    while (fgets(buffer, BUFFERLEN,fp)) {
        term_t * this_term;
        this_term =  term_from_string(buffer);
        add_node(pointer_to_node_ptr, this_term);
    }
    fclose(fp);
}

common.h:

#ifndef COMMON_H
#define COMMON_H

#define BUFFERLEN 40

typedef struct{           /* struct to hold elements like: 3x4 */
    int coefficient;
    char var;
    int exponent;
} term_t;

typedef struct node {
  term_t * term;            /* pointer to term */
  struct node * next_node;  /* pointer to next node */
} node_t;

#endif

And finally, this is the make file i'm using:

CC=gcc 
CFLAGS=-std=c11 -Wall -pedantic 

all: project1.c buildlinklist.o printandcombine.o
    $(CC) $(CFLAGS) project1.c buildlinklist.o printandcombine.o -o project1

buildlinklist.o: buildlinklist.c
    $(CC) $(CFLAGS) -c buildlinklist.c

printandcombine.o: printandcombine.c
    $(CC) $(CFLAGS) -c printandcombine.c
clean:
    rm -f *.o *~ project1.exe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2025-02-19 22:35:21

我构建了一些试图构造数据以符合您的结构的测试数据。当我添加一些调试点并运行代码时,如果后续术语记录的指数值小于先前读取的术语记录,它似乎会陷入无尽的循环。无尽的循环发生在“ bombine_like_terms”函数中。

while (!(temp == NULL)) {
    if (temp->term->exponent > x) {
        x = temp->term->exponent;
        temp = temp->next_node;
    }
}

我不知道这是否符合您正在做的事情的精神,但是我将“下一个节点”更新移到了以下代码段中所述的“ IF”语句之外。

if (temp->term->exponent > x)
{
    x = temp->term->exponent;
}
temp = temp->next_node;

至少已经超越了无尽的循环行为。您可以尝试一下。另外,如果您有一些测试数据来构建一个更有效的“ terms.txt”文件,那么如果需要进行其他故障排除,这将很有用。但是,您可能会从这个代码开始。

希望有帮助。

问候。

I built some test data attempting to structure the data to conform to your structure. When I added in some debugging points and ran the code, it seemed to get stuck in an endless loop if the subsequent terms record had an exponent value that was less than the previously read terms record. The endless looping occurred in the "combine_like_terms" function.

while (!(temp == NULL)) {
    if (temp->term->exponent > x) {
        x = temp->term->exponent;
        temp = temp->next_node;
    }
}

I don't know if this keeps with the spirit of what you are doing, but I moved the "next node" update outside of the "if" statement as noted in the following code snippet.

if (temp->term->exponent > x)
{
    x = temp->term->exponent;
}
temp = temp->next_node;

That at least got past the endless loop behavior. You might try that out. Also, if you have some test data to build a more valid "terms.txt" file, that would be useful if other troubleshooting needs to be done. But, you might start at this bit of code.

Hope that helps.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文